@@ -30,13 +30,19 @@ public class ControllerInteraction : MonoBehaviour
30
30
31
31
private GameObject [ ] handJointsVisuals = new GameObject [ 25 ] ;
32
32
private Dictionary < int , Transform > handJoints = new Dictionary < int , Transform > ( ) ;
33
+ public GameObject inputProfileHandModelParent ;
33
34
34
35
#if WEBXR_INPUT_PROFILES
35
36
private InputProfileLoader inputProfileLoader ;
36
37
private InputProfileModel inputProfileModel ;
37
38
private bool hasProfileList = false ;
38
39
private bool loadedModel = false ;
39
40
private string loadedProfile = null ;
41
+
42
+ private InputProfileModel inputProfileHandModel ;
43
+ private bool loadedHandModel = false ;
44
+ private string loadedHandProfile = null ;
45
+ private Dictionary < int , Transform > handModelJoints = new Dictionary < int , Transform > ( ) ;
40
46
#endif
41
47
42
48
private void Awake ( )
@@ -206,6 +212,27 @@ private void SetHandJointsVisible(bool visible)
206
212
{
207
213
handJointsVisible = visible ;
208
214
Drop ( ) ;
215
+ #if WEBXR_INPUT_PROFILES
216
+ // We want to use WebXR Input Profiles
217
+ if ( visible && useInputProfile )
218
+ {
219
+ if ( inputProfileHandModel != null && loadedHandModel )
220
+ {
221
+ // There's a loaded Input Profile Model
222
+ inputProfileHandModelParent . SetActive ( true ) ;
223
+ return ;
224
+ }
225
+ else if ( inputProfileHandModel == null )
226
+ {
227
+ // There's no loaded Input Profile Model and it's not in loading process
228
+ LoadHandInputProfile ( ) ;
229
+ }
230
+ }
231
+ else
232
+ {
233
+ inputProfileHandModelParent . SetActive ( false ) ;
234
+ }
235
+ #endif
209
236
foreach ( var visual in handJointsVisuals )
210
237
{
211
238
visual ? . SetActive ( visible ) ;
@@ -224,36 +251,48 @@ private void OnHandUpdate(WebXRHandData handData)
224
251
}
225
252
Quaternion rotationOffset = Quaternion . Inverse ( handData . joints [ 0 ] . rotation ) ;
226
253
227
- for ( int i = 0 ; i <= WebXRHandData . LITTLE_PHALANX_TIP ; i ++ )
254
+ #if WEBXR_INPUT_PROFILES
255
+ if ( useInputProfile && loadedHandModel )
228
256
{
229
- if ( handData . joints [ i ] . enabled )
257
+ for ( int i = 0 ; i <= ( int ) WebXRHandJoint . pinky_finger_tip ; i ++ )
230
258
{
231
- if ( handJoints . ContainsKey ( i ) )
259
+ if ( handModelJoints . ContainsKey ( i ) )
232
260
{
233
- handJoints [ i ] . localPosition = rotationOffset * ( handData . joints [ i ] . position - handData . joints [ 0 ] . position ) ;
234
- handJoints [ i ] . localRotation = rotationOffset * handData . joints [ i ] . rotation ;
235
- if ( handData . joints [ i ] . radius != handJoints [ i ] . localScale . x && handData . joints [ i ] . radius > 0 )
236
- {
237
- handJoints [ i ] . localScale = new Vector3 ( handData . joints [ i ] . radius , handData . joints [ i ] . radius , handData . joints [ i ] . radius ) ;
238
- }
261
+ handModelJoints [ i ] . localPosition = rotationOffset * ( handData . joints [ i ] . position - handData . joints [ 0 ] . position ) ;
262
+ handModelJoints [ i ] . localRotation = rotationOffset * handData . joints [ i ] . rotation ;
263
+ }
264
+ }
265
+ return ;
266
+ }
267
+ #endif
268
+
269
+ for ( int i = 0 ; i <= ( int ) WebXRHandJoint . pinky_finger_tip ; i ++ )
270
+ {
271
+ if ( handJoints . ContainsKey ( i ) )
272
+ {
273
+ handJoints [ i ] . localPosition = rotationOffset * ( handData . joints [ i ] . position - handData . joints [ 0 ] . position ) ;
274
+ handJoints [ i ] . localRotation = rotationOffset * handData . joints [ i ] . rotation ;
275
+ if ( handData . joints [ i ] . radius != handJoints [ i ] . localScale . x && handData . joints [ i ] . radius > 0 )
276
+ {
277
+ handJoints [ i ] . localScale = new Vector3 ( handData . joints [ i ] . radius , handData . joints [ i ] . radius , handData . joints [ i ] . radius ) ;
278
+ }
279
+ }
280
+ else
281
+ {
282
+ var clone = Instantiate ( handJointPrefab ,
283
+ rotationOffset * ( handData . joints [ i ] . position - handData . joints [ 0 ] . position ) ,
284
+ rotationOffset * handData . joints [ i ] . rotation ,
285
+ transform ) ;
286
+ if ( handData . joints [ i ] . radius > 0f )
287
+ {
288
+ clone . localScale = new Vector3 ( handData . joints [ i ] . radius , handData . joints [ i ] . radius , handData . joints [ i ] . radius ) ;
239
289
}
240
290
else
241
291
{
242
- var clone = Instantiate ( handJointPrefab ,
243
- rotationOffset * ( handData . joints [ i ] . position - handData . joints [ 0 ] . position ) ,
244
- rotationOffset * handData . joints [ i ] . rotation ,
245
- transform ) ;
246
- if ( handData . joints [ i ] . radius > 0f )
247
- {
248
- clone . localScale = new Vector3 ( handData . joints [ i ] . radius , handData . joints [ i ] . radius , handData . joints [ i ] . radius ) ;
249
- }
250
- else
251
- {
252
- clone . localScale = new Vector3 ( 0.005f , 0.005f , 0.005f ) ;
253
- }
254
- handJoints . Add ( i , clone ) ;
255
- handJointsVisuals [ i ] = clone . gameObject ;
292
+ clone . localScale = new Vector3 ( 0.005f , 0.005f , 0.005f ) ;
256
293
}
294
+ handJoints . Add ( i , clone ) ;
295
+ handJointsVisuals [ i ] = clone . gameObject ;
257
296
}
258
297
}
259
298
}
@@ -284,6 +323,13 @@ private void LoadInputProfile()
284
323
}
285
324
}
286
325
326
+ private void LoadHandInputProfile ( )
327
+ {
328
+ // Start loading the generic hand profile
329
+ loadedHandProfile = "generic-hand" ;
330
+ inputProfileLoader . LoadProfile ( new string [ ] { loadedHandProfile } , OnHandProfileLoaded ) ;
331
+ }
332
+
287
333
private void OnProfileLoaded ( bool success )
288
334
{
289
335
if ( success )
@@ -293,6 +339,15 @@ private void OnProfileLoaded(bool success)
293
339
// Nothing to do if profile didn't load
294
340
}
295
341
342
+ private void OnHandProfileLoaded ( bool success )
343
+ {
344
+ if ( success )
345
+ {
346
+ LoadHandInputModel ( ) ;
347
+ }
348
+ // Nothing to do if profile didn't load
349
+ }
350
+
296
351
private void LoadInputModel ( )
297
352
{
298
353
inputProfileModel = inputProfileLoader . LoadModelForHand (
@@ -306,6 +361,14 @@ private void LoadInputModel()
306
361
}
307
362
}
308
363
364
+ private void LoadHandInputModel ( )
365
+ {
366
+ inputProfileHandModel = inputProfileLoader . LoadModelForHand (
367
+ loadedHandProfile ,
368
+ ( InputProfileLoader . Handedness ) controller . hand ,
369
+ HandleHandModelLoaded ) ;
370
+ }
371
+
309
372
private void HandleModelLoaded ( bool success )
310
373
{
311
374
loadedModel = success ;
@@ -333,6 +396,44 @@ private void HandleModelLoaded(bool success)
333
396
}
334
397
}
335
398
399
+ private void HandleHandModelLoaded ( bool success )
400
+ {
401
+ loadedHandModel = success ;
402
+ if ( loadedHandModel )
403
+ {
404
+ // Set parent only after successful loading, to not interupt loading in case of disabled object
405
+ var inputProfileModelTransform = inputProfileHandModel . transform ;
406
+ inputProfileModelTransform . SetParent ( inputProfileHandModelParent . transform ) ;
407
+ inputProfileModelTransform . localPosition = Vector3 . zero ;
408
+ inputProfileModelTransform . localRotation = Quaternion . identity ;
409
+ inputProfileModelTransform . localScale = Vector3 . one ;
410
+ for ( int i = 0 ; i <= ( int ) WebXRHandJoint . pinky_finger_tip ; i ++ )
411
+ {
412
+ handModelJoints . Add ( i , inputProfileHandModel . GetChildTransform ( ( ( WebXRHandJoint ) i ) . ToString ( ) . Replace ( '_' , '-' ) ) ) ;
413
+ // It took at least one frame with hand data, there should be hand joint transform
414
+ if ( handJoints . ContainsKey ( i ) )
415
+ {
416
+ handModelJoints [ i ] . SetPositionAndRotation ( handJoints [ i ] . position , handJoints [ i ] . rotation ) ;
417
+ var collider = handModelJoints [ i ] . gameObject . AddComponent < SphereCollider > ( ) ;
418
+ collider . radius = handJoints [ i ] . localScale . x ;
419
+ collider . isTrigger = true ;
420
+ }
421
+ }
422
+ if ( handJointsVisible )
423
+ {
424
+ inputProfileHandModelParent . SetActive ( true ) ;
425
+ foreach ( var visual in handJointsVisuals )
426
+ {
427
+ visual ? . SetActive ( false ) ;
428
+ }
429
+ }
430
+ }
431
+ else
432
+ {
433
+ Destroy ( inputProfileHandModel . gameObject ) ;
434
+ }
435
+ }
436
+
336
437
private void UpdateModelInput ( )
337
438
{
338
439
for ( int i = 0 ; i < 6 ; i ++ )
0 commit comments