Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SkinnedMeshes of the local player head cast shadows: #78

Open
wants to merge 2 commits into
base: lts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Basis/Packages/Basis Framework/Avatar/BasisAvatarFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Basis.Scripts.BasisSdk.Helpers;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace Basis.Scripts.Avatar
{
public static class BasisAvatarFactory
{
public static BasisLoadableBundle LoadingAvatar = new BasisLoadableBundle()
public static BasisLoadableBundle LoadingAvatar = new BasisLoadableBundle()
{
BasisBundleInformation = new BasisBundleInformation()
{
Expand Down Expand Up @@ -180,7 +181,7 @@ private static void InitializePlayerAvatar(BasisPlayer Player, GameObject Output
localPlayer.InitalizeIKCalibration(localPlayer.AvatarDriver);
for (int Index = 0; Index < Avatar.Renders.Length; Index++)
{
Avatar.Renders[Index].gameObject.layer = 6;
Avatar.Renders[Index].gameObject.layer = BasisLayer.LocalPlayerAvatar;
}
Avatar.OnAvatarReady?.Invoke(true);
}
Expand All @@ -191,7 +192,7 @@ private static void InitializePlayerAvatar(BasisPlayer Player, GameObject Output
remotePlayer.InitalizeIKCalibration(remotePlayer.RemoteAvatarDriver);
for (int Index = 0; Index < Avatar.Renders.Length; Index++)
{
Avatar.Renders[Index].gameObject.layer = 7;
Avatar.Renders[Index].gameObject.layer = BasisLayer.RemotePlayerAvatar;
}
Avatar.OnAvatarReady?.Invoke(false);
}
Expand Down Expand Up @@ -251,7 +252,7 @@ public static void LoadLoadingAvatar(BasisPlayer Player, string LoadingAvatarToU
Player.InitalizeIKCalibration(BasisLocalPlayer.AvatarDriver);
for (int Index = 0; Index < RenderCount; Index++)
{
Avatar.Renders[Index].gameObject.layer = 6;
Avatar.Renders[Index].gameObject.layer = BasisLayer.LocalPlayerAvatar;
}
}
else
Expand All @@ -262,7 +263,7 @@ public static void LoadLoadingAvatar(BasisPlayer Player, string LoadingAvatarToU
Player.InitalizeIKCalibration(BasisRemotePlayer.RemoteAvatarDriver);
for (int Index = 0; Index < RenderCount; Index++)
{
Avatar.Renders[Index].gameObject.layer = 7;
Avatar.Renders[Index].gameObject.layer = BasisLayer.RemotePlayerAvatar;
}
}
}
Expand Down Expand Up @@ -305,6 +306,9 @@ public static void CreateLocal(BasisLocalPlayer Player)
Debug.LogError("Missing LocalPlayer or Avatar");
return;
}
if (!Player.HeadShadowDriver) Player.HeadShadowDriver = BasisHelpers.GetOrAddComponent<BasisHeadShadowDriver>(Player.gameObject);

Player.HeadShadowDriver.Initialize(Player.Avatar);

Player.AvatarDriver.InitialLocalCalibration(Player);
}
Expand Down
9 changes: 9 additions & 0 deletions Basis/Packages/Basis Framework/Avatar/BasisLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Basis.Scripts.Avatar
{
public class BasisLayer
{
// TODO: These eventually need to be configurable, with values to be chosen by the framework consumer.
public const int LocalPlayerAvatar = 6;
public const int RemotePlayerAvatar = 7;
}
}
3 changes: 3 additions & 0 deletions Basis/Packages/Basis Framework/Avatar/BasisLayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private void OnBeforeRender()
{
BasisLocalPlayer.Instance.LocalBoneDriver.SimulateAndApply(Time.timeAsDouble,Time.deltaTime);
AfterAvatarChanges?.Invoke();

// Defensive check, this only seems to be null when the first avatar is not loaded yet.
// May be fixable by updating the prefab so that it's always defined by default.
if (BasisLocalPlayer.Instance.HeadShadowDriver)
BasisLocalPlayer.Instance.HeadShadowDriver.PrepareThisFrame();
}

public void Update()
Expand Down Expand Up @@ -304,4 +309,4 @@ public void RunCancelled()
}
}
}
}
}
42 changes: 40 additions & 2 deletions Basis/Packages/Basis Framework/Drivers/BasisLocalCameraDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class BasisLocalCameraDriver : MonoBehaviour
public Vector3 largerScale;
public static Vector3 LeftEye;
public static Vector3 RightEye;

public Color UnMutedMutedIconColorActive = Color.white;
public Color UnMutedMutedIconColorInactive = Color.grey;

Expand All @@ -69,6 +69,7 @@ public void OnEnable()
MicrophoneRecorder.MainThreadOnHasAudio += MicrophoneTransmitting;
MicrophoneRecorder.MainThreadOnHasSilence += MicrophoneNotTransmitting;
RenderPipelineManager.beginCameraRendering += BeginCameraRendering;
RenderPipelineManager.endCameraRendering += EndCameraRendering;
BasisDeviceManagement.Instance.OnBootModeChanged += OnModeSwitch;
BasisLocalPlayer.Instance.OnPlayersHeightChanged += OnHeightChanged;
InstanceExists?.Invoke();
Expand Down Expand Up @@ -285,6 +286,7 @@ public void OnDisable()
if (HasEvents)
{
RenderPipelineManager.beginCameraRendering -= BeginCameraRendering;
RenderPipelineManager.endCameraRendering -= BeginCameraRendering;
BasisDeviceManagement.Instance.OnBootModeChanged -= OnModeSwitch;
MicrophoneRecorder.MainThreadOnHasAudio -= MicrophoneTransmitting;
MicrophoneRecorder.MainThreadOnHasSilence -= MicrophoneNotTransmitting;
Expand All @@ -297,6 +299,11 @@ public void BeginCameraRendering(ScriptableRenderContext context, Camera Camera)
{
if (Camera.GetInstanceID() == CameraInstanceID)
{
// Defensive check, this only seems to be null when the first avatar is not loaded yet.
// May be fixable by updating the prefab so that it's always defined by default.
if (LocalPlayer.HeadShadowDriver)
LocalPlayer.HeadShadowDriver.BeforeRenderFirstPerson();

ScaleheadToZero();
if (CameraData.allowXRRendering)
{
Expand All @@ -310,10 +317,41 @@ public void BeginCameraRendering(ScriptableRenderContext context, Camera Camera)
}
else
{
// Defensive check, this only seems to be null when the first avatar is not loaded yet.
// May be fixable by updating the prefab so that it's always defined by default.
if (LocalPlayer.HeadShadowDriver)
LocalPlayer.HeadShadowDriver.BeforeRenderThirdPerson();

ScaleHeadToNormal();
}
}
}

private void EndCameraRendering(ScriptableRenderContext context, Camera Camera)
{
if (LocalPlayer.HasAvatarDriver && LocalPlayer.AvatarDriver.References.Hashead)
{
if (Camera.GetInstanceID() == CameraInstanceID)
{
// Rescale head to avoid making the end state of the avatar
// dependent on the last rendered camera.
ScaleHeadToNormal();

// Defensive check, this only seems to be null when the first avatar is not loaded yet.
// May be fixable by updating the prefab so that it's always defined by default.
if (LocalPlayer.HeadShadowDriver)
LocalPlayer.HeadShadowDriver.AfterRenderFirstPerson();
}
else
{
// Defensive check, this only seems to be null when the first avatar is not loaded yet.
// May be fixable by updating the prefab so that it's always defined by default.
if (LocalPlayer.HeadShadowDriver)
LocalPlayer.HeadShadowDriver.AfterRenderThirdPerson();
}
}
}

public void ScaleHeadToNormal()
{
if (LocalPlayer.AvatarDriver.References.head.localScale != LocalPlayer.AvatarDriver.HeadScale)
Expand Down Expand Up @@ -341,4 +379,4 @@ Vector3 CalculatePosition(Vector2 size, Vector3 percentage)
return offset + center;
}
}
}
}
Loading
Loading