XRHandPrimitiveModel.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.frustumCulled = false;
  26. this.handMesh.instanceMatrix.setUsage( DynamicDrawUsage ); // will be updated every frame
  27. this.handMesh.castShadow = true;
  28. this.handMesh.receiveShadow = true;
  29. this.handModel.add( this.handMesh );
  30. this.joints = [
  31. 'wrist',
  32. 'thumb-metacarpal',
  33. 'thumb-phalanx-proximal',
  34. 'thumb-phalanx-distal',
  35. 'thumb-tip',
  36. 'index-finger-metacarpal',
  37. 'index-finger-phalanx-proximal',
  38. 'index-finger-phalanx-intermediate',
  39. 'index-finger-phalanx-distal',
  40. 'index-finger-tip',
  41. 'middle-finger-metacarpal',
  42. 'middle-finger-phalanx-proximal',
  43. 'middle-finger-phalanx-intermediate',
  44. 'middle-finger-phalanx-distal',
  45. 'middle-finger-tip',
  46. 'ring-finger-metacarpal',
  47. 'ring-finger-phalanx-proximal',
  48. 'ring-finger-phalanx-intermediate',
  49. 'ring-finger-phalanx-distal',
  50. 'ring-finger-tip',
  51. 'pinky-finger-metacarpal',
  52. 'pinky-finger-phalanx-proximal',
  53. 'pinky-finger-phalanx-intermediate',
  54. 'pinky-finger-phalanx-distal',
  55. 'pinky-finger-tip'
  56. ];
  57. }
  58. updateMesh() {
  59. const defaultRadius = 0.008;
  60. const joints = this.controller.joints;
  61. let count = 0;
  62. for ( let i = 0; i < this.joints.length; i ++ ) {
  63. const joint = joints[ this.joints[ i ] ];
  64. if ( joint.visible ) {
  65. _vector.setScalar( joint.jointRadius || defaultRadius );
  66. _matrix.compose( joint.position, joint.quaternion, _vector );
  67. this.handMesh.setMatrixAt( i, _matrix );
  68. count ++;
  69. }
  70. }
  71. this.handMesh.count = count;
  72. this.handMesh.instanceMatrix.needsUpdate = true;
  73. }
  74. }
  75. export { XRHandPrimitiveModel };