@@ -31,14 +31,21 @@ public ArticulatedHandDefinition(IMixedRealityInputSource source, Handedness han
31
31
private readonly float cursorBeamBackwardTolerance = 0.5f ;
32
32
private readonly float cursorBeamUpTolerance = 0.8f ;
33
33
34
- private Dictionary < TrackedHandJoint , MixedRealityPose > unityJointPoses = new Dictionary < TrackedHandJoint , MixedRealityPose > ( ) ;
34
+ private IDictionary < TrackedHandJoint , MixedRealityPose > unityJointPoseDictionary = new Dictionary < TrackedHandJoint , MixedRealityPose > ( ) ;
35
+ private MixedRealityPose [ ] unityJointPoses = null ;
35
36
private MixedRealityPose currentIndexPose = MixedRealityPose . ZeroIdentity ;
37
+ private Vector3 currentPalmNormal = Vector3 . zero ;
38
+
39
+ private const int PalmIndex = ( int ) TrackedHandJoint . Palm ;
40
+ private const int ThumbTipIndex = ( int ) TrackedHandJoint . ThumbTip ;
41
+ private const int IndexKnuckleIndex = ( int ) TrackedHandJoint . IndexKnuckle ;
42
+ private const int IndexTipIndex = ( int ) TrackedHandJoint . IndexTip ;
36
43
37
44
// Minimum distance between the index and the thumb tip required to enter a pinch
38
- private readonly float minimumPinchDistance = 0.015f ;
45
+ private const float MinimumPinchDistance = 0.015f ;
39
46
40
47
// Maximum distance between the index and thumb tip required to exit the pinch gesture
41
- private readonly float maximumPinchDistance = 0.1f ;
48
+ private const float MaximumPinchDistance = 0.1f ;
42
49
43
50
// Default enterPinchDistance value
44
51
private float enterPinchDistance = 0.02f ;
@@ -52,13 +59,13 @@ public float EnterPinchDistance
52
59
get => enterPinchDistance ;
53
60
set
54
61
{
55
- if ( value >= minimumPinchDistance && value <= maximumPinchDistance )
62
+ if ( value >= MinimumPinchDistance && value <= MaximumPinchDistance )
56
63
{
57
64
enterPinchDistance = value ;
58
65
}
59
66
else
60
67
{
61
- Debug . LogError ( "EnterPinchDistance must be between 0.015 and 0.1, please change Enter Pinch Distance in the Leap Motion Device Manager Profile " ) ;
68
+ Debug . LogError ( $ "EnterPinchDistance must be between { MinimumPinchDistance } and { MaximumPinchDistance } . ") ;
62
69
}
63
70
}
64
71
}
@@ -75,13 +82,13 @@ public float ExitPinchDistance
75
82
get => exitPinchDistance ;
76
83
set
77
84
{
78
- if ( value >= minimumPinchDistance && value <= maximumPinchDistance )
85
+ if ( value >= MinimumPinchDistance && value <= MaximumPinchDistance )
79
86
{
80
87
exitPinchDistance = value ;
81
88
}
82
89
else
83
90
{
84
- Debug . LogError ( "ExitPinchDistance must be between 0.015 and 0.1, please change Exit Pinch Distance in the Leap Motion Device Manager Profile " ) ;
91
+ Debug . LogError ( $ "ExitPinchDistance must be between { MinimumPinchDistance } and { MaximumPinchDistance } . ") ;
85
92
}
86
93
}
87
94
}
@@ -131,23 +138,19 @@ public bool IsInPointingPose
131
138
{
132
139
get
133
140
{
134
- MixedRealityPose palmJoint ;
135
- if ( unityJointPoses . TryGetValue ( TrackedHandJoint . Palm , out palmJoint ) )
141
+ if ( unityJointPoses != null )
136
142
{
137
- Vector3 palmNormal = palmJoint . Rotation * ( - 1 * Vector3 . up ) ;
138
- if ( cursorBeamBackwardTolerance >= 0 && CameraCache . Main != null )
143
+ if ( cursorBeamBackwardTolerance >= 0
144
+ && CameraCache . Main != null
145
+ && Vector3 . Dot ( currentPalmNormal . normalized , - CameraCache . Main . transform . forward ) > cursorBeamBackwardTolerance )
139
146
{
140
- if ( Vector3 . Dot ( palmNormal . normalized , - CameraCache . Main . transform . forward ) > cursorBeamBackwardTolerance )
141
- {
142
- return false ;
143
- }
147
+ return false ;
144
148
}
145
- if ( cursorBeamUpTolerance >= 0 )
149
+
150
+ if ( cursorBeamUpTolerance >= 0
151
+ && Vector3 . Dot ( currentPalmNormal , Vector3 . up ) > cursorBeamUpTolerance )
146
152
{
147
- if ( Vector3 . Dot ( palmNormal , Vector3 . up ) > cursorBeamUpTolerance )
148
- {
149
- return false ;
150
- }
153
+ return false ;
151
154
}
152
155
}
153
156
return ! IsInTeleportPose ;
@@ -161,7 +164,10 @@ protected bool IsInTeleportPose
161
164
{
162
165
get
163
166
{
164
- if ( ! unityJointPoses . TryGetValue ( TrackedHandJoint . Palm , out var palmPose ) ) return false ;
167
+ if ( unityJointPoses == null )
168
+ {
169
+ return false ;
170
+ }
165
171
166
172
Camera mainCamera = CameraCache . Main ;
167
173
@@ -172,9 +178,8 @@ protected bool IsInTeleportPose
172
178
173
179
Transform cameraTransform = mainCamera . transform ;
174
180
175
- Vector3 palmNormal = - palmPose . Up ;
176
181
// We check if the palm up is roughly in line with the camera up
177
- return Vector3 . Dot ( palmNormal , cameraTransform . up ) > 0.6f
182
+ return Vector3 . Dot ( currentPalmNormal , cameraTransform . up ) > 0.6f
178
183
// Thumb must be extended, and middle must be grabbing
179
184
&& ! isThumbGrabbing && isMiddleGrabbing ;
180
185
}
@@ -192,11 +197,9 @@ public bool IsPinching
192
197
{
193
198
get
194
199
{
195
- MixedRealityPose thumbTip ;
196
- MixedRealityPose indexTip ;
197
- if ( unityJointPoses . TryGetValue ( TrackedHandJoint . ThumbTip , out thumbTip ) && unityJointPoses . TryGetValue ( TrackedHandJoint . IndexTip , out indexTip ) )
200
+ if ( unityJointPoses != null )
198
201
{
199
- float distance = Vector3 . Distance ( thumbTip . Position , indexTip . Position ) ;
202
+ float distance = Vector3 . Distance ( unityJointPoses [ ThumbTipIndex ] . Position , currentIndexPose . Position ) ;
200
203
201
204
if ( isPinching && distance > ExitPinchDistance )
202
205
{
@@ -224,19 +227,18 @@ public bool IsPinching
224
227
225
228
// Velocity internal states
226
229
private float deltaTimeStart ;
227
- private const int velocityUpdateInterval = 6 ;
230
+ private const int VelocityUpdateInterval = 6 ;
228
231
private int frameOn = 0 ;
229
232
230
- private readonly Vector3 [ ] velocityPositionsCache = new Vector3 [ velocityUpdateInterval ] ;
231
- private readonly Vector3 [ ] velocityNormalsCache = new Vector3 [ velocityUpdateInterval ] ;
233
+ private readonly Vector3 [ ] velocityPositionsCache = new Vector3 [ VelocityUpdateInterval ] ;
234
+ private readonly Vector3 [ ] velocityNormalsCache = new Vector3 [ VelocityUpdateInterval ] ;
232
235
private Vector3 velocityPositionsSum = Vector3 . zero ;
233
236
private Vector3 velocityNormalsSum = Vector3 . zero ;
234
237
235
238
public Vector3 AngularVelocity { get ; protected set ; }
236
239
237
240
public Vector3 Velocity { get ; protected set ; }
238
241
239
-
240
242
private static readonly ProfilerMarker UpdateHandJointsPerfMarker = new ProfilerMarker ( "[MRTK] ArticulatedHandDefinition.UpdateHandJoints" ) ;
241
243
242
244
#region Hand Definition Update functions
@@ -246,11 +248,54 @@ public bool IsPinching
246
248
/// </summary>
247
249
/// <param name="jointPoses">The new joint poses.</param>
248
250
public void UpdateHandJoints ( Dictionary < TrackedHandJoint , MixedRealityPose > jointPoses )
251
+ {
252
+ using ( UpdateHandJointsPerfMarker . Auto ( ) )
253
+ {
254
+ unityJointPoseDictionary = jointPoses ;
255
+ _ = unityJointPoseDictionary . TryGetValue ( TrackedHandJoint . IndexTip , out currentIndexPose ) ;
256
+ if ( unityJointPoseDictionary . TryGetValue ( TrackedHandJoint . Palm , out MixedRealityPose palmPose ) )
257
+ {
258
+ currentPalmNormal = palmPose . Rotation * Vector3 . down ;
259
+ }
260
+
261
+ if ( unityJointPoses == null )
262
+ {
263
+ unityJointPoses = new MixedRealityPose [ ArticulatedHandPose . JointCount ] ;
264
+ }
265
+
266
+ for ( int i = 1 ; i < ArticulatedHandPose . JointCount ; i ++ )
267
+ {
268
+ unityJointPoseDictionary . TryGetValue ( ( TrackedHandJoint ) i , out unityJointPoses [ i ] ) ;
269
+ }
270
+
271
+ CoreServices . InputSystem ? . RaiseHandJointsUpdated ( InputSource , Handedness , unityJointPoseDictionary ) ;
272
+ }
273
+ }
274
+
275
+ /// <summary>
276
+ /// Updates the current hand joints with new data.
277
+ /// </summary>
278
+ /// <param name="jointPoses">The new joint poses.</param>
279
+ public void UpdateHandJoints ( MixedRealityPose [ ] jointPoses )
249
280
{
250
281
using ( UpdateHandJointsPerfMarker . Auto ( ) )
251
282
{
252
283
unityJointPoses = jointPoses ;
253
- CoreServices . InputSystem ? . RaiseHandJointsUpdated ( InputSource , Handedness , unityJointPoses ) ;
284
+
285
+ if ( unityJointPoses == null )
286
+ {
287
+ return ;
288
+ }
289
+
290
+ currentIndexPose = unityJointPoses [ IndexTipIndex ] ;
291
+ currentPalmNormal = unityJointPoses [ PalmIndex ] . Rotation * Vector3 . down ;
292
+
293
+ for ( int i = 1 ; i < ArticulatedHandPose . JointCount ; i ++ )
294
+ {
295
+ unityJointPoseDictionary [ ( TrackedHandJoint ) i ] = unityJointPoses [ i ] ;
296
+ }
297
+
298
+ CoreServices . InputSystem ? . RaiseHandJointsUpdated ( InputSource , Handedness , unityJointPoseDictionary ) ;
254
299
}
255
300
}
256
301
@@ -264,7 +309,7 @@ public void UpdateCurrentIndexPose(MixedRealityInteractionMapping interactionMap
264
309
{
265
310
using ( UpdateCurrentIndexPosePerfMarker . Auto ( ) )
266
311
{
267
- if ( unityJointPoses . TryGetValue ( TrackedHandJoint . IndexTip , out currentIndexPose ) )
312
+ if ( unityJointPoses != null )
268
313
{
269
314
// Update the interaction data source
270
315
interactionMapping . PoseData = currentIndexPose ;
@@ -287,7 +332,7 @@ public void UpdateCurrentIndexPose(MixedRealityInteractionMapping interactionMap
287
332
private static readonly ProfilerMarker UpdateCurrentTeleportPosePerfMarker = new ProfilerMarker ( "[MRTK] ArticulatedHandDefinition.UpdateCurrentTeleportPose" ) ;
288
333
289
334
/// <summary>
290
- /// Updates the MixedRealityInteractionMapping with the lastest teleport pose status and fires an event when appropriate
335
+ /// Updates the MixedRealityInteractionMapping with the latest teleport pose status and fires an event when appropriate
291
336
/// </summary>
292
337
/// <param name="interactionMapping">The teleport action's interaction mapping.</param>
293
338
public void UpdateCurrentTeleportPose ( MixedRealityInteractionMapping interactionMapping )
@@ -346,32 +391,26 @@ public void UpdateCurrentTeleportPose(MixedRealityInteractionMapping interaction
346
391
}
347
392
348
393
/// <summary>
349
- /// Updates the MixedRealityInteractionMapping with the lastest pointer pose status and fires a corresponding pose event.
394
+ /// Updates the MixedRealityInteractionMapping with the latest pointer pose status and fires a corresponding pose event.
350
395
/// </summary>
351
396
/// <param name="interactionMapping">The pointer pose's interaction mapping.</param>
352
397
public void UpdatePointerPose ( MixedRealityInteractionMapping interactionMapping )
353
398
{
354
- if ( ! unityJointPoses . TryGetValue ( TrackedHandJoint . IndexKnuckle , out var knucklePose ) ) return ;
355
- if ( ! unityJointPoses . TryGetValue ( TrackedHandJoint . Palm , out var palmPose ) ) return ;
399
+ if ( unityJointPoses == null ) return ;
356
400
357
- Vector3 rayPosition = knucklePose . Position ;
358
- Vector3 palmNormal = - palmPose . Up ;
401
+ Vector3 rayPosition = unityJointPoses [ IndexKnuckleIndex ] . Position ;
359
402
360
- HandRay . Update ( rayPosition , palmNormal , CameraCache . Main . transform , Handedness ) ;
403
+ HandRay . Update ( rayPosition , currentPalmNormal , CameraCache . Main . transform , Handedness ) ;
361
404
Ray ray = HandRay . Ray ;
362
405
363
- MixedRealityPose pointerPose = new MixedRealityPose ( ) ;
364
- pointerPose . Position = ray . origin ;
365
- pointerPose . Rotation = Quaternion . LookRotation ( ray . direction ) ;
366
-
367
406
// Update the interaction data source
368
- interactionMapping . PoseData = pointerPose ;
407
+ interactionMapping . PoseData = new MixedRealityPose ( ray . origin , Quaternion . LookRotation ( ray . direction ) ) ;
369
408
370
409
// If our value changed raise it
371
410
if ( interactionMapping . Changed )
372
411
{
373
412
// Raise input system event if it's enabled
374
- CoreServices . InputSystem ? . RaisePoseInputChanged ( InputSource , Handedness , interactionMapping . MixedRealityInputAction , pointerPose ) ;
413
+ CoreServices . InputSystem ? . RaisePoseInputChanged ( InputSource , Handedness , interactionMapping . MixedRealityInputAction , interactionMapping . PoseData ) ;
375
414
}
376
415
}
377
416
@@ -380,35 +419,34 @@ public void UpdatePointerPose(MixedRealityInteractionMapping interactionMapping)
380
419
/// </summary>
381
420
public void UpdateVelocity ( )
382
421
{
383
- if ( unityJointPoses . TryGetValue ( TrackedHandJoint . Palm , out var currentPalmPose ) )
422
+ if ( unityJointPoses != null )
384
423
{
385
- Vector3 palmPosition = currentPalmPose . Position ;
386
- Vector3 palmNormal = - currentPalmPose . Up ;
424
+ Vector3 palmPosition = unityJointPoses [ PalmIndex ] . Position ;
387
425
388
- if ( frameOn < velocityUpdateInterval )
426
+ if ( frameOn < VelocityUpdateInterval )
389
427
{
390
428
velocityPositionsCache [ frameOn ] = palmPosition ;
391
429
velocityPositionsSum += velocityPositionsCache [ frameOn ] ;
392
- velocityNormalsCache [ frameOn ] = palmNormal ;
430
+ velocityNormalsCache [ frameOn ] = currentPalmNormal ;
393
431
velocityNormalsSum += velocityNormalsCache [ frameOn ] ;
394
432
}
395
433
else
396
434
{
397
- int frameIndex = frameOn % velocityUpdateInterval ;
435
+ int frameIndex = frameOn % VelocityUpdateInterval ;
398
436
399
437
float deltaTime = Time . unscaledTime - deltaTimeStart ;
400
438
401
439
Vector3 newPositionsSum = velocityPositionsSum - velocityPositionsCache [ frameIndex ] + palmPosition ;
402
- Vector3 newNormalsSum = velocityNormalsSum - velocityNormalsCache [ frameIndex ] + palmNormal ;
440
+ Vector3 newNormalsSum = velocityNormalsSum - velocityNormalsCache [ frameIndex ] + currentPalmNormal ;
403
441
404
- Velocity = ( newPositionsSum - velocityPositionsSum ) / deltaTime / velocityUpdateInterval ;
442
+ Velocity = ( newPositionsSum - velocityPositionsSum ) / deltaTime / VelocityUpdateInterval ;
405
443
406
- Quaternion rotation = Quaternion . FromToRotation ( velocityNormalsSum / velocityUpdateInterval , newNormalsSum / velocityUpdateInterval ) ;
444
+ Quaternion rotation = Quaternion . FromToRotation ( velocityNormalsSum / VelocityUpdateInterval , newNormalsSum / VelocityUpdateInterval ) ;
407
445
Vector3 rotationRate = rotation . eulerAngles * Mathf . Deg2Rad ;
408
446
AngularVelocity = rotationRate / deltaTime ;
409
447
410
448
velocityPositionsCache [ frameIndex ] = palmPosition ;
411
- velocityNormalsCache [ frameIndex ] = palmNormal ;
449
+ velocityNormalsCache [ frameIndex ] = currentPalmNormal ;
412
450
velocityPositionsSum = newPositionsSum ;
413
451
velocityNormalsSum = newNormalsSum ;
414
452
}
0 commit comments