XRHandOculusMeshModel.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { FBXLoader } from '../loaders/FBXLoader.js';
  2. class XRHandOculusMeshModel {
  3. constructor( handModel, controller, path, handedness, options ) {
  4. this.controller = controller;
  5. this.handModel = handModel;
  6. this.bones = [];
  7. const loader = new FBXLoader();
  8. const low = options && options.model === 'lowpoly' ? '_low' : '';
  9. loader.setPath( path );
  10. loader.load( `OculusHand_${handedness === 'right' ? 'R' : 'L'}${low}.fbx`, object => {
  11. this.handModel.add( object );
  12. // Hack because of the scale of the skinnedmesh
  13. object.scale.setScalar( 0.01 );
  14. const mesh = object.getObjectByProperty( 'type', 'SkinnedMesh' );
  15. mesh.frustumCulled = false;
  16. mesh.castShadow = true;
  17. mesh.receiveShadow = true;
  18. const bonesMapping = [
  19. 'b_%_wrist', // XRHand.WRIST,
  20. 'b_%_thumb1', // XRHand.THUMB_METACARPAL,
  21. 'b_%_thumb2', // XRHand.THUMB_PHALANX_PROXIMAL,
  22. 'b_%_thumb3', // XRHand.THUMB_PHALANX_DISTAL,
  23. 'b_%_thumb_null', // XRHand.THUMB_PHALANX_TIP,
  24. null, //'b_%_index1', // XRHand.INDEX_METACARPAL,
  25. 'b_%_index1', // XRHand.INDEX_PHALANX_PROXIMAL,
  26. 'b_%_index2', // XRHand.INDEX_PHALANX_INTERMEDIATE,
  27. 'b_%_index3', // XRHand.INDEX_PHALANX_DISTAL,
  28. 'b_%_index_null', // XRHand.INDEX_PHALANX_TIP,
  29. null, //'b_%_middle1', // XRHand.MIDDLE_METACARPAL,
  30. 'b_%_middle1', // XRHand.MIDDLE_PHALANX_PROXIMAL,
  31. 'b_%_middle2', // XRHand.MIDDLE_PHALANX_INTERMEDIATE,
  32. 'b_%_middle3', // XRHand.MIDDLE_PHALANX_DISTAL,
  33. 'b_%_middlenull', // XRHand.MIDDLE_PHALANX_TIP,
  34. null, //'b_%_ring1', // XRHand.RING_METACARPAL,
  35. 'b_%_ring1', // XRHand.RING_PHALANX_PROXIMAL,
  36. 'b_%_ring2', // XRHand.RING_PHALANX_INTERMEDIATE,
  37. 'b_%_ring3', // XRHand.RING_PHALANX_DISTAL,
  38. 'b_%_ring_inull', // XRHand.RING_PHALANX_TIP,
  39. 'b_%_pinky0', // XRHand.LITTLE_METACARPAL,
  40. 'b_%_pinky1', // XRHand.LITTLE_PHALANX_PROXIMAL,
  41. 'b_%_pinky2', // XRHand.LITTLE_PHALANX_INTERMEDIATE,
  42. 'b_%_pinky3', // XRHand.LITTLE_PHALANX_DISTAL,
  43. 'b_%_pinkynull', // XRHand.LITTLE_PHALANX_TIP
  44. ];
  45. bonesMapping.forEach( boneName => {
  46. if ( boneName ) {
  47. const bone = object.getObjectByName( boneName.replace( /%/g, handedness === 'right' ? 'r' : 'l' ) );
  48. this.bones.push( bone );
  49. } else {
  50. this.bones.push( null );
  51. }
  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. const XRJoint = XRJoints[ i ];
  61. if ( XRJoint ) {
  62. if ( XRJoint.visible ) {
  63. const position = XRJoint.position;
  64. if ( bone ) {
  65. bone.position.copy( position.clone().multiplyScalar( 100 ) );
  66. bone.quaternion.copy( XRJoint.quaternion );
  67. // bone.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. export { XRHandOculusMeshModel };