XRHandPrimitiveModel.js 3.4 KB

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