XRHandMeshModel.js 3.4 KB

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