XRHandPrimitiveModel.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. SphereGeometry,
  3. BoxGeometry,
  4. MeshStandardMaterial,
  5. InstancedMesh,
  6. Matrix4,
  7. Vector3
  8. } from '../../../build/three.module.js';
  9. class XRHandPrimitiveModel {
  10. constructor( handModel, controller, path, handedness, options ) {
  11. this.controller = controller;
  12. this.handModel = handModel;
  13. this.envMap = null;
  14. let geometry;
  15. const jointMaterial = new MeshStandardMaterial( { color: 0xffffff, roughness: 1, metalness: 0 } );
  16. if ( ! options || ! options.primitive || options.primitive === 'sphere' ) {
  17. geometry = new SphereGeometry( 1, 10, 10 );
  18. } else if ( options.primitive === 'box' ) {
  19. geometry = new BoxGeometry( 1, 1, 1 );
  20. }
  21. this.handMesh = new InstancedMesh( geometry, jointMaterial, 30 );
  22. this.handMesh.castShadow = true;
  23. this.handMesh.receiveShadow = true;
  24. this.handModel.add( this.handMesh );
  25. this.joints = [
  26. 'wrist',
  27. 'thumb-metacarpal',
  28. 'thumb-phalanx-proximal',
  29. 'thumb-phalanx-distal',
  30. 'thumb-tip',
  31. 'index-finger-metacarpal',
  32. 'index-finger-phalanx-proximal',
  33. 'index-finger-phalanx-intermediate',
  34. 'index-finger-phalanx-distal',
  35. 'index-finger-tip',
  36. 'middle-finger-metacarpal',
  37. 'middle-finger-phalanx-proximal',
  38. 'middle-finger-phalanx-intermediate',
  39. 'middle-finger-phalanx-distal',
  40. 'middle-finger-tip',
  41. 'ring-finger-metacarpal',
  42. 'ring-finger-phalanx-proximal',
  43. 'ring-finger-phalanx-intermediate',
  44. 'ring-finger-phalanx-distal',
  45. 'ring-finger-tip',
  46. 'pinky-finger-metacarpal',
  47. 'pinky-finger-phalanx-proximal',
  48. 'pinky-finger-phalanx-intermediate',
  49. 'pinky-finger-phalanx-distal',
  50. 'pinky-finger-tip'
  51. ];
  52. this.tempMat = new Matrix4(); this.tempVec = new Vector3( 1, 1, 1 );
  53. }
  54. updateMesh() {
  55. const defaultRadius = 0.008;
  56. // XR Joints
  57. const XRJoints = this.controller.joints;
  58. let count = 0;
  59. for ( let i = 0; i < this.joints.length; i ++ ) {
  60. const XRJoint = XRJoints[ this.joints[ i ] ];
  61. if ( XRJoint.visible ) {
  62. this.handMesh.setMatrixAt( i, this.tempMat.compose( XRJoint.position, XRJoint.quaternion,
  63. this.tempVec.set( 1, 1, 1 ).multiplyScalar( XRJoint.jointRadius || defaultRadius ) ) );
  64. count ++;
  65. }
  66. }
  67. this.handMesh.count = count;
  68. if ( this.handMesh.instanceMatrix ) {
  69. this.handMesh.instanceMatrix.needsUpdate = true;
  70. }
  71. }
  72. }
  73. export { XRHandPrimitiveModel };