XRHandOculusMeshModel.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. var 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. object.getObjectByProperty( "type", "SkinnedMesh" ).frustumCulled = false;
  15. const bonesMapping = [
  16. 'b_%_wrist', // XRHand.WRIST,
  17. 'b_%_thumb1', // XRHand.THUMB_METACARPAL,
  18. 'b_%_thumb2', // XRHand.THUMB_PHALANX_PROXIMAL,
  19. 'b_%_thumb3', // XRHand.THUMB_PHALANX_DISTAL,
  20. 'b_%_thumb_null', // XRHand.THUMB_PHALANX_TIP,
  21. null, //'b_%_index1', // XRHand.INDEX_METACARPAL,
  22. 'b_%_index1', // XRHand.INDEX_PHALANX_PROXIMAL,
  23. 'b_%_index2', // XRHand.INDEX_PHALANX_INTERMEDIATE,
  24. 'b_%_index3', // XRHand.INDEX_PHALANX_DISTAL,
  25. 'b_%_index_null', // XRHand.INDEX_PHALANX_TIP,
  26. null, //'b_%_middle1', // XRHand.MIDDLE_METACARPAL,
  27. 'b_%_middle1', // XRHand.MIDDLE_PHALANX_PROXIMAL,
  28. 'b_%_middle2', // XRHand.MIDDLE_PHALANX_INTERMEDIATE,
  29. 'b_%_middle3', // XRHand.MIDDLE_PHALANX_DISTAL,
  30. 'b_%_middlenull', // XRHand.MIDDLE_PHALANX_TIP,
  31. null, //'b_%_ring1', // XRHand.RING_METACARPAL,
  32. 'b_%_ring1', // XRHand.RING_PHALANX_PROXIMAL,
  33. 'b_%_ring2', // XRHand.RING_PHALANX_INTERMEDIATE,
  34. 'b_%_ring3', // XRHand.RING_PHALANX_DISTAL,
  35. 'b_%_ring_inull', // XRHand.RING_PHALANX_TIP,
  36. 'b_%_pinky0', // XRHand.LITTLE_METACARPAL,
  37. 'b_%_pinky1', // XRHand.LITTLE_PHALANX_PROXIMAL,
  38. 'b_%_pinky2', // XRHand.LITTLE_PHALANX_INTERMEDIATE,
  39. 'b_%_pinky3', // XRHand.LITTLE_PHALANX_DISTAL,
  40. 'b_%_pinkynull', // XRHand.LITTLE_PHALANX_TIP
  41. ];
  42. bonesMapping.forEach( boneName => {
  43. if ( boneName ) {
  44. const bone = object.getObjectByName( boneName.replace( "%", handedness === "right" ? "r" : "l" ) );
  45. this.bones.push( bone );
  46. } else {
  47. this.bones.push( null );
  48. }
  49. } );
  50. } );
  51. }
  52. updateMesh() {
  53. // XR Joints
  54. const XRJoints = this.controller.joints;
  55. for ( var i = 0; i < this.bones.length; i ++ ) {
  56. const bone = this.bones[ i ];
  57. const XRJoint = XRJoints[ i ];
  58. if ( XRJoint ) {
  59. if ( XRJoint.visible ) {
  60. let position = XRJoint.position;
  61. if ( bone ) {
  62. bone.position.copy( position.clone().multiplyScalar( 100 ) );
  63. bone.quaternion.copy( XRJoint.quaternion );
  64. // bone.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. export { XRHandOculusMeshModel };