Skip to content

Commit f4082da

Browse files
committed
Update PlatformHandMeshVisualizer.cs
1 parent cd2cd12 commit f4082da

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

org.mixedrealitytoolkit.input/Visualizers/PlatformHandVisualizer/PlatformHandMeshVisualizer.cs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using UnityEngine;
66
using UnityEngine.XR;
7-
using UnityEngine.XR.Interaction.Toolkit;
87

98
#if MROPENXR_PRESENT && (UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_ANDROID)
109
using Microsoft.MixedReality.OpenXR;
@@ -32,6 +31,7 @@ public class PlatformHandMeshVisualizer : HandMeshVisualizer
3231
private bool initializedUVs = false;
3332

3433
private XRMeshSubsystem meshSubsystem = null;
34+
private readonly List<MeshInfo> meshInfos = new List<MeshInfo>();
3535

3636
/// <inheritdoc/>
3737
protected override void OnEnable()
@@ -74,35 +74,47 @@ protected override void OnEnable()
7474

7575
protected void Update()
7676
{
77-
if (meshSubsystem != null)
77+
if (!ShouldRenderHand())
7878
{
79-
List<MeshInfo> meshInfos = new List<MeshInfo>();
80-
if (meshSubsystem.TryGetMeshInfos(meshInfos))
81-
{
82-
int handMeshIndex = HandNode == XRNode.LeftHand ? 0 : 1;
79+
// Hide the hand and abort if we shouldn't be
80+
// showing the hand, for whatever reason.
81+
// (Missing joint data, no subsystem, additive
82+
// display, etc!)
83+
handRenderer.enabled = false;
84+
return;
85+
}
8386

84-
if (meshInfos[handMeshIndex].ChangeState == MeshChangeState.Added
85-
|| meshInfos[handMeshIndex].ChangeState == MeshChangeState.Updated)
86-
{
87-
meshSubsystem.GenerateMeshAsync(meshInfos[handMeshIndex].MeshId, meshFilter.mesh,
88-
null, MeshVertexAttributes.Normals, result => { });
87+
if (meshSubsystem != null &&
88+
meshSubsystem.TryGetMeshInfos(meshInfos))
89+
{
90+
int handMeshIndex = HandNode == XRNode.LeftHand ? 0 : 1;
8991

90-
if (!handRenderer.enabled)
91-
{
92-
handRenderer.enabled = true;
93-
}
94-
}
95-
}
96-
else if (handRenderer.enabled)
92+
if (meshInfos[handMeshIndex].ChangeState == MeshChangeState.Added
93+
|| meshInfos[handMeshIndex].ChangeState == MeshChangeState.Updated)
9794
{
98-
handRenderer.enabled = false;
95+
meshSubsystem.GenerateMeshAsync(meshInfos[handMeshIndex].MeshId, meshFilter.mesh,
96+
null, MeshVertexAttributes.Normals, result => { });
97+
98+
handRenderer.enabled = true;
9999
}
100100
}
101-
102101
#if MROPENXR_PRESENT && (UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_ANDROID)
103-
else if (!ShouldRenderHand() ||
104-
!handMeshTracker.TryGetHandMesh(FrameTime.OnUpdate, meshFilter.mesh) ||
105-
!handMeshTracker.TryLocateHandMesh(FrameTime.OnUpdate, out Pose pose))
102+
else if (handMeshTracker != null &&
103+
handMeshTracker.TryGetHandMesh(FrameTime.OnUpdate, meshFilter.mesh) &&
104+
handMeshTracker.TryLocateHandMesh(FrameTime.OnUpdate, out Pose pose))
105+
{
106+
handRenderer.enabled = true;
107+
108+
if (!initializedUVs && handMeshTracker.TryGetHandMesh(FrameTime.OnUpdate, neutralPoseMesh, HandPoseType.ReferenceOpenPalm))
109+
{
110+
meshFilter.mesh.uv = InitializeUVs(neutralPoseMesh.vertices);
111+
initializedUVs = true;
112+
}
113+
114+
transform.SetPositionAndRotation(pose.position, pose.rotation);
115+
}
116+
#endif
117+
else
106118
{
107119
// Hide the hand and abort if we shouldn't be
108120
// showing the hand, for whatever reason.
@@ -112,28 +124,13 @@ protected void Update()
112124
return;
113125
}
114126

115-
handRenderer.enabled = true;
116-
117-
if (!initializedUVs && handMeshTracker.TryGetHandMesh(FrameTime.OnUpdate, neutralPoseMesh, HandPoseType.ReferenceOpenPalm))
118-
{
119-
meshFilter.mesh.uv = InitializeUVs(neutralPoseMesh.vertices);
120-
initializedUVs = true;
121-
}
122-
123-
transform.SetPositionAndRotation(pose.position, pose.rotation);
124-
#endif
125-
126127
UpdateHandMaterial();
127128
}
128129

129130
protected override bool ShouldRenderHand()
130131
{
131132
// If we're missing anything, don't render the hand.
132-
return
133-
#if MROPENXR_PRESENT && (UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_ANDROID)
134-
handMeshTracker != null &&
135-
#endif
136-
meshFilter != null && handRenderer != null && base.ShouldRenderHand();
133+
return meshFilter != null && handRenderer != null && base.ShouldRenderHand();
137134
}
138135

139136
#if MROPENXR_PRESENT && (UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_ANDROID)

0 commit comments

Comments
 (0)