XRHandPrimitiveModel.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. DynamicDrawUsage,
  3. SphereGeometry,
  4. BoxGeometry,
  5. MeshStandardMaterial,
  6. InstancedMesh,
  7. Matrix4,
  8. Vector3
  9. } from '../../../build/three.module.js';
  10. class XRHandPrimitiveModel {
  11. constructor( handModel, controller, path, handedness, options ) {
  12. this.controller = controller;
  13. this.handModel = handModel;
  14. this.envMap = null;
  15. let geometry;
  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. const material = new MeshStandardMaterial();
  22. this.handMesh = new InstancedMesh( geometry, material, 30 );
  23. this.handMesh.instanceMatrix.setUsage( DynamicDrawUsage ); // will be updated every frame
  24. this.handMesh.castShadow = true;
  25. this.handMesh.receiveShadow = true;
  26. this.handModel.add( this.handMesh );
  27. this.joints = [
  28. 'wrist',
  29. 'thumb-metacarpal',
  30. 'thumb-phalanx-proximal',
  31. 'thumb-phalanx-distal',
  32. 'thumb-tip',
  33. 'index-finger-metacarpal',
  34. 'index-finger-phalanx-proximal',
  35. 'index-finger-phalanx-intermediate',
  36. 'index-finger-phalanx-distal',
  37. 'index-finger-tip',
  38. 'middle-finger-metacarpal',
  39. 'middle-finger-phalanx-proximal',
  40. 'middle-finger-phalanx-intermediate',
  41. 'middle-finger-phalanx-distal',
  42. 'middle-finger-tip',
  43. 'ring-finger-metacarpal',
  44. 'ring-finger-phalanx-proximal',
  45. 'ring-finger-phalanx-intermediate',
  46. 'ring-finger-phalanx-distal',
  47. 'ring-finger-tip',
  48. 'pinky-finger-metacarpal',
  49. 'pinky-finger-phalanx-proximal',
  50. 'pinky-finger-phalanx-intermediate',
  51. 'pinky-finger-phalanx-distal',
  52. 'pinky-finger-tip'
  53. ];
  54. this.tempMat = new Matrix4();
  55. this.tempVec = new Vector3();
  56. }
  57. updateMesh() {
  58. const defaultRadius = 0.008;
  59. const joints = this.controller.joints;
  60. let count = 0;
  61. for ( let i = 0; i < this.joints.length; i ++ ) {
  62. const joint = joints[ this.joints[ i ] ];
  63. if ( joint.visible ) {
  64. this.tempVec.setScalar( joint.jointRadius || defaultRadius );
  65. this.tempMat.compose( joint.position, joint.quaternion, this.tempVec );
  66. this.handMesh.setMatrixAt( i, this.tempMat );
  67. count ++;
  68. }
  69. }
  70. this.handMesh.count = count;
  71. this.handMesh.instanceMatrix.needsUpdate = true;
  72. }
  73. }
  74. export { XRHandPrimitiveModel };