-
Notifications
You must be signed in to change notification settings - Fork 71
/
FeatureUsageHandJointsManager.cs
229 lines (203 loc) · 8.99 KB
/
FeatureUsageHandJointsManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
namespace Microsoft.MixedReality.OpenXR.BasicSample
{
public class FeatureUsageHandJointsManager : MonoBehaviour
{
[SerializeField, Tooltip("The prefab to use for rendering hand joints in the scene. (optional)")]
private GameObject handJointPrefab = null;
private Hand leftHand = null;
private Hand rightHand = null;
private void Start()
{
leftHand = new Hand(handJointPrefab);
rightHand = new Hand(handJointPrefab);
}
private void Update()
{
UpdateHandJoints(InputDeviceCharacteristics.Left, leftHand);
UpdateHandJoints(InputDeviceCharacteristics.Right, rightHand);
}
private void OnDisable()
{
leftHand?.DisableHandJoints();
rightHand?.DisableHandJoints();
}
private void OnDestroy()
{
leftHand?.DestroyHandJoints();
rightHand?.DestroyHandJoints();
}
private static void UpdateHandJoints(InputDeviceCharacteristics flag, Hand hand)
{
List<InputDevice> inputDeviceList = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HandTracking | flag, inputDeviceList);
UnityEngine.XR.Hand xrHand = default;
foreach (InputDevice device in inputDeviceList)
{
if (device.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked)
&& isTracked
&& device.TryGetFeatureValue(CommonUsages.handData, out xrHand))
{
break;
}
}
if (xrHand != default)
{
hand?.UpdateHandJoints(xrHand);
}
else
{
// If we get here, we didn't successfully update hand joints for any tracked input device
hand?.DisableHandJoints();
}
}
private class Hand
{
public Hand(GameObject handJointPrefab)
{
this.handJointPrefab = handJointPrefab;
handRoot = new GameObject("HandParent");
}
private readonly GameObject handJointPrefab = null;
private readonly GameObject handRoot = null;
// Visualize hand joints when using FeatureUsages, HandFinger / TryGetFeatureValue hand joints
private static readonly HandFinger[] HandFingers = System.Enum.GetValues(typeof(HandFinger)) as HandFinger[];
private readonly Dictionary<HandFinger, GameObject[]> handFingerGameObjects = new Dictionary<HandFinger, GameObject[]>();
private readonly List<Bone> fingerBones = new List<Bone>();
private GameObject palmGameObject = null;
/// <summary>
/// Instantiates either a predefined prefab or a new cube primitive if no prefab is provided.
/// </summary>
/// <returns>True if the returned new object is a cube primitive and needs coloring.</returns>
private bool InstantiateJointPrefab(out GameObject gameObject)
{
if (handJointPrefab != null)
{
gameObject = Instantiate(handJointPrefab);
gameObject.transform.localScale = new Vector3(0.015f, 0.015f, 0.015f);
gameObject.transform.parent = handRoot.transform;
return false;
}
else
{
gameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
Destroy(gameObject.GetComponent<Collider>());
gameObject.transform.localScale = new Vector3(0.015f, 0.015f, 0.015f);
gameObject.transform.parent = handRoot.transform;
return true;
}
}
/// <summary>
/// Update this hand's internal state with the Unity XR InputDevice's hand data.
/// </summary>
/// <param name="device">The InputDevice to get the CommonUsages.handData feature value from.</param>
public void UpdateHandJoints(UnityEngine.XR.Hand hand)
{
// If the hand was previously disabled, this is the first new update and it should be re-enabled
if (!handRoot.activeSelf)
{
handRoot.SetActive(true);
}
if (hand.TryGetRootBone(out Bone palm))
{
if (palmGameObject == null)
{
if (InstantiateJointPrefab(out palmGameObject))
{
ColorJointObject(palmGameObject, null, null);
}
}
bool positionAvailable = palm.TryGetPosition(out Vector3 position);
bool rotationAvailable = palm.TryGetRotation(out Quaternion rotation);
if (positionAvailable || rotationAvailable)
{
palmGameObject.transform.SetPositionAndRotation(position, rotation);
}
}
foreach (HandFinger finger in HandFingers)
{
if (hand.TryGetFingerBones(finger, fingerBones))
{
if (!handFingerGameObjects.ContainsKey(finger))
{
GameObject[] jointArray = new GameObject[fingerBones.Count];
for (int i = 0; i < fingerBones.Count; i++)
{
if (InstantiateJointPrefab(out GameObject jointObject))
{
ColorJointObject(jointObject, finger, i);
}
jointArray[i] = jointObject;
}
handFingerGameObjects[finger] = jointArray;
}
GameObject[] fingerJointGameObjects = handFingerGameObjects[finger];
for (int i = 0; i < fingerBones.Count; i++)
{
Bone bone = fingerBones[i];
bool positionAvailable = bone.TryGetPosition(out Vector3 position);
bool rotationAvailable = bone.TryGetRotation(out Quaternion rotation);
if (positionAvailable || rotationAvailable)
{
fingerJointGameObjects[i].transform.SetPositionAndRotation(position, rotation);
}
}
}
}
}
/// <summary>
/// When this hand becomes inactive, it's best practice to hide the in-scene representation.
/// </summary>
public void DisableHandJoints()
{
if (handRoot != null)
{
handRoot.SetActive(false);
}
}
/// <summary>
/// Destroys the hand representation when the hand needs to be cleaned up.
/// </summary>
public void DestroyHandJoints()
{
if (handRoot != null)
{
Destroy(handRoot);
}
}
private static void ColorJointObject(GameObject jointObject, HandFinger? finger, int? index)
{
Color jointColor = Color.magenta;
switch (finger)
{
case HandFinger.Thumb:
// The wrist is the 0th entry on the thumb, so we want to keep that the default color
jointColor = (index == 0) ? Color.magenta : Color.red;
break;
case HandFinger.Index:
// Orange
jointColor = new Color(1.0f, 0.647f, 0.0f);
break;
case HandFinger.Middle:
jointColor = Color.yellow;
break;
case HandFinger.Ring:
jointColor = Color.green;
break;
case HandFinger.Pinky:
jointColor = Color.blue;
break;
}
// Lighten the colors as we go up the finger.
int i = index ?? 0;
float saturation = 1.0f - (i * 0.15f);
Color.RGBToHSV(jointColor, out float h, out float _, out float v);
jointObject.GetComponent<Renderer>().material.color = Color.HSVToRGB(h, saturation, v);
}
}
}
}