XRHandMeshModel.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { GLTFLoader } from '../loaders/GLTFLoader.js';
  2. const DEFAULT_HAND_PROFILE_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/assets@1.0/dist/profiles/generic-hand/';
  3. /**
  4. * Represents one of the hand model types {@link XRHandModelFactory} might produce
  5. * depending on the selected profile. `XRHandMeshModel` represents a hand with a
  6. * custom asset.
  7. *
  8. * @three_import import { XRHandMeshModel } from 'three/addons/webxr/XRHandMeshModel.js';
  9. */
  10. class XRHandMeshModel {
  11. /**
  12. * Constructs a new XR hand mesh model.
  13. *
  14. * @param {XRHandModel} handModel - The hand model.
  15. * @param {Group} controller - The WebXR controller.
  16. * @param {?string} path - The model path.
  17. * @param {XRHandedness} handedness - The handedness of the XR input source.
  18. * @param {?Loader} [loader=null] - The loader. If not provided, an instance of `GLTFLoader` will be used to load models.
  19. * @param {?Function} [onLoad=null] - A callback that is executed when a controller model has been loaded.
  20. */
  21. constructor( handModel, controller, path, handedness, loader = null, onLoad = null ) {
  22. /**
  23. * The WebXR controller.
  24. *
  25. * @type {Group}
  26. */
  27. this.controller = controller;
  28. /**
  29. * The hand model.
  30. *
  31. * @type {XRHandModel}
  32. */
  33. this.handModel = handModel;
  34. /**
  35. * An array of bones representing the bones
  36. * of the hand skeleton.
  37. *
  38. * @type {Array<Bone>}
  39. */
  40. this.bones = [];
  41. if ( loader === null ) {
  42. loader = new GLTFLoader();
  43. loader.setPath( path || DEFAULT_HAND_PROFILE_PATH );
  44. }
  45. loader.load( `${handedness}.glb`, gltf => {
  46. const object = gltf.scene.children[ 0 ];
  47. this.handModel.add( object );
  48. const mesh = object.getObjectByProperty( 'type', 'SkinnedMesh' );
  49. mesh.frustumCulled = false;
  50. mesh.castShadow = true;
  51. mesh.receiveShadow = true;
  52. const joints = [
  53. 'wrist',
  54. 'thumb-metacarpal',
  55. 'thumb-phalanx-proximal',
  56. 'thumb-phalanx-distal',
  57. 'thumb-tip',
  58. 'index-finger-metacarpal',
  59. 'index-finger-phalanx-proximal',
  60. 'index-finger-phalanx-intermediate',
  61. 'index-finger-phalanx-distal',
  62. 'index-finger-tip',
  63. 'middle-finger-metacarpal',
  64. 'middle-finger-phalanx-proximal',
  65. 'middle-finger-phalanx-intermediate',
  66. 'middle-finger-phalanx-distal',
  67. 'middle-finger-tip',
  68. 'ring-finger-metacarpal',
  69. 'ring-finger-phalanx-proximal',
  70. 'ring-finger-phalanx-intermediate',
  71. 'ring-finger-phalanx-distal',
  72. 'ring-finger-tip',
  73. 'pinky-finger-metacarpal',
  74. 'pinky-finger-phalanx-proximal',
  75. 'pinky-finger-phalanx-intermediate',
  76. 'pinky-finger-phalanx-distal',
  77. 'pinky-finger-tip',
  78. ];
  79. joints.forEach( jointName => {
  80. const bone = object.getObjectByName( jointName );
  81. if ( bone !== undefined ) {
  82. bone.jointName = jointName;
  83. } else {
  84. console.warn( `Couldn't find ${jointName} in ${handedness} hand mesh` );
  85. }
  86. this.bones.push( bone );
  87. } );
  88. if ( onLoad ) onLoad( object );
  89. } );
  90. }
  91. /**
  92. * Updates the mesh based on the tracked XR joints data.
  93. */
  94. updateMesh() {
  95. // XR Joints
  96. const XRJoints = this.controller.joints;
  97. for ( let i = 0; i < this.bones.length; i ++ ) {
  98. const bone = this.bones[ i ];
  99. if ( bone ) {
  100. const XRJoint = XRJoints[ bone.jointName ];
  101. if ( XRJoint.visible ) {
  102. const position = XRJoint.position;
  103. bone.position.copy( position );
  104. bone.quaternion.copy( XRJoint.quaternion );
  105. // bone.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  106. }
  107. }
  108. }
  109. }
  110. }
  111. export { XRHandMeshModel };