XRHandMeshModel.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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. const joints = [
  18. 'wrist',
  19. 'thumb-metacarpal',
  20. 'thumb-phalanx-proximal',
  21. 'thumb-phalanx-distal',
  22. 'thumb-tip',
  23. 'index-finger-metacarpal',
  24. 'index-finger-phalanx-proximal',
  25. 'index-finger-phalanx-intermediate',
  26. 'index-finger-phalanx-distal',
  27. 'index-finger-tip',
  28. 'middle-finger-metacarpal',
  29. 'middle-finger-phalanx-proximal',
  30. 'middle-finger-phalanx-intermediate',
  31. 'middle-finger-phalanx-distal',
  32. 'middle-finger-tip',
  33. 'ring-finger-metacarpal',
  34. 'ring-finger-phalanx-proximal',
  35. 'ring-finger-phalanx-intermediate',
  36. 'ring-finger-phalanx-distal',
  37. 'ring-finger-tip',
  38. 'pinky-finger-metacarpal',
  39. 'pinky-finger-phalanx-proximal',
  40. 'pinky-finger-phalanx-intermediate',
  41. 'pinky-finger-phalanx-distal',
  42. 'pinky-finger-tip',
  43. ];
  44. joints.forEach( jointName => {
  45. const bone = object.getObjectByName( jointName );
  46. if ( bone !== undefined ) {
  47. bone.jointName = jointName;
  48. } else {
  49. console.warn( `Couldn't find ${jointName} in ${handedness} hand mesh` );
  50. }
  51. this.bones.push( bone );
  52. } );
  53. } );
  54. }
  55. updateMesh() {
  56. // XR Joints
  57. const XRJoints = this.controller.joints;
  58. for ( let i = 0; i < this.bones.length; i ++ ) {
  59. const bone = this.bones[ i ];
  60. if ( bone ) {
  61. const XRJoint = XRJoints[ bone.jointName ];
  62. if ( XRJoint.visible ) {
  63. const position = XRJoint.position;
  64. bone.position.copy( position );
  65. bone.quaternion.copy( XRJoint.quaternion );
  66. // bone.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  67. }
  68. }
  69. }
  70. }
  71. }
  72. export { XRHandMeshModel };