Skip to content

Commit 836f871

Browse files
committed
Some optimizations and improvements
1 parent 75124ea commit 836f871

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class PlatformHandMeshVisualizer : HandMeshVisualizer
4545
{
4646
allocator = Unity.Collections.Allocator.Temp,
4747
};
48+
private XRHandSubsystem.UpdateSuccessFlags updateSuccessFlags;
4849

4950
// The property block used to modify the wrist position property on the material
5051
private MaterialPropertyBlock propertyBlock = null;
@@ -69,6 +70,15 @@ protected override void OnEnable()
6970
{
7071
Debug.Log($"Using {provider.handMeshDataSupplier.GetType()} for hand visualization.");
7172
handSubsystem = subsystem;
73+
74+
updateSuccessFlags = HandNode == XRNode.LeftHand ?
75+
XRHandSubsystem.UpdateSuccessFlags.LeftHandJoints | XRHandSubsystem.UpdateSuccessFlags.LeftHandRootPose :
76+
XRHandSubsystem.UpdateSuccessFlags.RightHandJoints | XRHandSubsystem.UpdateSuccessFlags.RightHandRootPose;
77+
78+
// Since the hand mesh is likely to change every frame, we
79+
// "optimize mesh for frequent updates" by marking it dynamic
80+
meshFilter.mesh.MarkDynamic();
81+
7282
return;
7383
}
7484
}
@@ -79,6 +89,10 @@ protected override void OnEnable()
7989
Debug.Log($"Using XR_MSFT_hand_tracking_mesh for {HandNode} visualization.");
8090
handMeshTracker = HandNode == XRNode.LeftHand ? HandMeshTracker.Left : HandMeshTracker.Right;
8191

92+
// Since the hand mesh is likely to change every frame, we
93+
// "optimize mesh for frequent updates" by marking it dynamic
94+
meshFilter.mesh.MarkDynamic();
95+
8296
if (neutralPoseMesh == null)
8397
{
8498
neutralPoseMesh = new Mesh();
@@ -106,15 +120,22 @@ protected void Update()
106120

107121
if (handSubsystem != null
108122
&& handSubsystem.running
123+
&& (handSubsystem.updateSuccessFlags & updateSuccessFlags) != 0
109124
&& (lastUpdatedFrame == Time.frameCount || handSubsystem.TryGetMeshData(out result, ref queryParams)))
110125
{
111126
lastUpdatedFrame = Time.frameCount;
112127
XRHandMeshData handMeshData = HandNode == XRNode.LeftHand ? result.leftHand : result.rightHand;
113128

114-
meshFilter.mesh.Clear();
115-
meshFilter.mesh.SetVertices(handMeshData.positions);
116-
meshFilter.mesh.SetUVs(0, handMeshData.uvs);
117-
meshFilter.mesh.SetIndices(handMeshData.indices, MeshTopology.Triangles, 0);
129+
if (handMeshData.positions.Length > 0 && handMeshData.indices.Length > 0)
130+
{
131+
meshFilter.mesh.SetVertices(handMeshData.positions);
132+
meshFilter.mesh.SetIndices(handMeshData.indices, MeshTopology.Triangles, 0);
133+
}
134+
135+
if (handMeshData.uvs.IsCreated && handMeshData.uvs.Length == meshFilter.mesh.vertexCount)
136+
{
137+
meshFilter.mesh.SetUVs(0, handMeshData.uvs);
138+
}
118139

119140
handRenderer.enabled = true;
120141

0 commit comments

Comments
 (0)