XRHandPrimitiveModel.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  13. * Represents one of the hand model types {@link XRHandModelFactory} might produce
  14. * depending on the selected profile. `XRHandPrimitiveModel` represents a hand
  15. * with sphere or box primitives according to the selected `primitive` option.
  16. */
  17. class XRHandPrimitiveModel {
  18. /**
  19. * Constructs a new XR hand primitive model.
  20. *
  21. * @param {XRHandModel} handModel - The hand model.
  22. * @param {Group} controller - The WebXR controller.
  23. * @param {string} path - The model path.
  24. * @param {XRHandedness} handedness - The handedness of the XR input source.
  25. * @param {XRHandPrimitiveModel~Options} options - The model options.
  26. */
  27. constructor( handModel, controller, path, handedness, options ) {
  28. /**
  29. * The WebXR controller.
  30. *
  31. * @type {Group}
  32. */
  33. this.controller = controller;
  34. /**
  35. * The hand model.
  36. *
  37. * @type {XRHandModel}
  38. */
  39. this.handModel = handModel;
  40. /**
  41. * The model's environment map.
  42. *
  43. * @type {?Texture}
  44. * @default null
  45. */
  46. this.envMap = null;
  47. let geometry;
  48. if ( ! options || ! options.primitive || options.primitive === 'sphere' ) {
  49. geometry = new SphereGeometry( 1, 10, 10 );
  50. } else if ( options.primitive === 'box' ) {
  51. geometry = new BoxGeometry( 1, 1, 1 );
  52. }
  53. const material = new MeshStandardMaterial();
  54. this.handMesh = new InstancedMesh( geometry, material, 30 );
  55. this.handMesh.frustumCulled = false;
  56. this.handMesh.instanceMatrix.setUsage( DynamicDrawUsage ); // will be updated every frame
  57. this.handMesh.castShadow = true;
  58. this.handMesh.receiveShadow = true;
  59. this.handModel.add( this.handMesh );
  60. this.joints = [
  61. 'wrist',
  62. 'thumb-metacarpal',
  63. 'thumb-phalanx-proximal',
  64. 'thumb-phalanx-distal',
  65. 'thumb-tip',
  66. 'index-finger-metacarpal',
  67. 'index-finger-phalanx-proximal',
  68. 'index-finger-phalanx-intermediate',
  69. 'index-finger-phalanx-distal',
  70. 'index-finger-tip',
  71. 'middle-finger-metacarpal',
  72. 'middle-finger-phalanx-proximal',
  73. 'middle-finger-phalanx-intermediate',
  74. 'middle-finger-phalanx-distal',
  75. 'middle-finger-tip',
  76. 'ring-finger-metacarpal',
  77. 'ring-finger-phalanx-proximal',
  78. 'ring-finger-phalanx-intermediate',
  79. 'ring-finger-phalanx-distal',
  80. 'ring-finger-tip',
  81. 'pinky-finger-metacarpal',
  82. 'pinky-finger-phalanx-proximal',
  83. 'pinky-finger-phalanx-intermediate',
  84. 'pinky-finger-phalanx-distal',
  85. 'pinky-finger-tip'
  86. ];
  87. }
  88. /**
  89. * Updates the mesh based on the tracked XR joints data.
  90. */
  91. updateMesh() {
  92. const defaultRadius = 0.008;
  93. const joints = this.controller.joints;
  94. let count = 0;
  95. for ( let i = 0; i < this.joints.length; i ++ ) {
  96. const joint = joints[ this.joints[ i ] ];
  97. if ( joint.visible ) {
  98. _vector.setScalar( joint.jointRadius || defaultRadius );
  99. _matrix.compose( joint.position, joint.quaternion, _vector );
  100. this.handMesh.setMatrixAt( i, _matrix );
  101. count ++;
  102. }
  103. }
  104. this.handMesh.count = count;
  105. this.handMesh.instanceMatrix.needsUpdate = true;
  106. }
  107. }
  108. /**
  109. * Constructor options of `XRHandPrimitiveModel`.
  110. *
  111. * @typedef {Object} XRHandPrimitiveModel~Options
  112. * @property {('box'|'sphere')} [primitive] - The primitive type.
  113. **/
  114. export { XRHandPrimitiveModel };
粤ICP备19079148号