XRHandMeshModel.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { GLTFLoader } from '../loaders/GLTFLoader.js';
  2. const DEFAULT_HAND_PROFILE_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/[email protected]/dist/profiles/generic-hand/';
  3. class XRHandMeshModel {
  4. constructor( handModel, controller, path, handedness ) {
  5. this.controller = controller;
  6. this.handModel = handModel;
  7. this.bones = [];
  8. const loader = new GLTFLoader();
  9. loader.setPath( path || DEFAULT_HAND_PROFILE_PATH );
  10. loader.load( `${handedness}.glb`, gltf => {
  11. const object = gltf.scene.children[ 0 ];
  12. this.handModel.add( object );
  13. const mesh = object.getObjectByProperty( 'type', 'SkinnedMesh' );
  14. mesh.frustumCulled = false;
  15. mesh.castShadow = true;
  16. mesh.receiveShadow = true;
  17. mesh.material.side = 0; // Workaround: force FrontSide
  18. const joints = [
  19. 'wrist',
  20. 'thumb-metacarpal',
  21. 'thumb-phalanx-proximal',
  22. 'thumb-phalanx-distal',
  23. 'thumb-tip',
  24. 'index-finger-metacarpal',
  25. 'index-finger-phalanx-proximal',
  26. 'index-finger-phalanx-intermediate',
  27. 'index-finger-phalanx-distal',
  28. 'index-finger-tip',
  29. 'middle-finger-metacarpal',
  30. 'middle-finger-phalanx-proximal',
  31. 'middle-finger-phalanx-intermediate',
  32. 'middle-finger-phalanx-distal',
  33. 'middle-finger-tip',
  34. 'ring-finger-metacarpal',
  35. 'ring-finger-phalanx-proximal',
  36. 'ring-finger-phalanx-intermediate',
  37. 'ring-finger-phalanx-distal',
  38. 'ring-finger-tip',
  39. 'pinky-finger-metacarpal',
  40. 'pinky-finger-phalanx-proximal',
  41. 'pinky-finger-phalanx-intermediate',
  42. 'pinky-finger-phalanx-distal',
  43. 'pinky-finger-tip',
  44. ];
  45. joints.forEach( jointName => {
  46. const bone = object.getObjectByName( jointName );
  47. if ( bone !== undefined ) {
  48. bone.jointName = jointName;
  49. } else {
  50. console.warn( `Couldn't find ${jointName} in ${handedness} hand mesh` );
  51. }
  52. this.bones.push( bone );
  53. } );
  54. } );
  55. }
  56. updateMesh() {
  57. // XR Joints
  58. const XRJoints = this.controller.joints;
  59. for ( let i = 0; i < this.bones.length; i ++ ) {
  60. const bone = this.bones[ i ];
  61. if ( bone ) {
  62. const XRJoint = XRJoints[ bone.jointName ];
  63. if ( XRJoint.visible ) {
  64. const position = XRJoint.position;
  65. if ( bone ) {
  66. bone.position.copy( position );
  67. bone.quaternion.copy( XRJoint.quaternion );
  68. // bone.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. export { XRHandMeshModel };