Skip to content

Commit

Permalink
Make SkinnedMeshes of the local player cast shadows:
Browse files Browse the repository at this point in the history
- The SkinnedMeshes of the local player that reference any bone under the Head bone should now cast shadows onto the world and other players.
- SkinnedMeshes placed directly under the Head bone without any bone reference are not supported in this version.
- MeshRenderers placed directly under the Head bone are not supported in this version.
- This implementation:
  - Evaluates at runtime.
  - Creates a copy of all the transforms under the Head hierarchy.
  - Creates copies of SkinnedMeshes that reference any bone under the Head.
    - Bones under Head will reference the aforementionned copy.
    - Bones not under Head will reference a zero-scale bone under the neck.
    - Those SkinnedMeshes are set to Shadow Only.
  - This implementation currently uses "Update When Offscreen" OFF and an off-world "Bounds" value to prevent the shadow from displaying when rendering the local player avatar in third person.
    - This implementation was chosen because it's currently unclear how to manage the camera layers, and should be discussed further.
  - Just before any camera is rendered, once per frame, we copy the transforms, SkinnedMesh component enabled-ness, and blendshapes, so that the shadow reflects the visual state of the original SkinnedMesh.
  • Loading branch information
hai-vr committed Dec 21, 2024
1 parent e311df1 commit 4a70f63
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 10 deletions.
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,7 @@ private void OnBeforeRender()
{
BasisLocalPlayer.Instance.LocalBoneDriver.SimulateAndApply(Time.timeAsDouble,Time.deltaTime);
AfterAvatarChanges?.Invoke();
BasisLocalPlayer.Instance.HeadShadowDriver.PrepareThisFrame();
}

public void Update()
Expand Down Expand Up @@ -304,4 +305,4 @@ public void RunCancelled()
}
}
}
}
}
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 Down Expand Up @@ -297,6 +297,7 @@ public void BeginCameraRendering(ScriptableRenderContext context, Camera Camera)
{
if (Camera.GetInstanceID() == CameraInstanceID)
{
LocalPlayer.HeadShadowDriver.BeforeRenderFirstPerson();
ScaleheadToZero();
if (CameraData.allowXRRendering)
{
Expand All @@ -310,6 +311,7 @@ public void BeginCameraRendering(ScriptableRenderContext context, Camera Camera)
}
else
{
LocalPlayer.HeadShadowDriver.BeforeRenderThirdPerson();
ScaleHeadToNormal();
}
}
Expand Down Expand Up @@ -341,4 +343,4 @@ Vector3 CalculatePosition(Vector2 size, Vector3 percentage)
return offset + center;
}
}
}
}
Loading

0 comments on commit 4a70f63

Please sign in to comment.