XRHandPrimitiveModel.js 2.2 KB

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