XRHandPrimitiveModel.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {
  2. SphereGeometry,
  3. BoxGeometry,
  4. MeshStandardMaterial,
  5. Mesh,
  6. Group
  7. } from '../../../build/three.module.js';
  8. class XRHandPrimitiveModel {
  9. constructor( handModel, controller, path, handedness, options ) {
  10. this.controller = controller;
  11. this.handModel = handModel;
  12. this.envMap = null;
  13. this.handMesh = new Group();
  14. this.handModel.add( this.handMesh );
  15. if ( window.XRHand ) {
  16. let geometry;
  17. if ( ! options || ! options.primitive || options.primitive === 'sphere' ) {
  18. geometry = new SphereGeometry( 1, 10, 10 );
  19. } else if ( options.primitive === 'box' ) {
  20. geometry = new BoxGeometry( 1, 1, 1 );
  21. }
  22. const jointMaterial = new MeshStandardMaterial( { color: 0xffffff, roughness: 1, metalness: 0 } );
  23. const tipMaterial = new MeshStandardMaterial( { color: 0x999999, roughness: 1, metalness: 0 } );
  24. const joints = [
  25. 'wrist',
  26. 'thumb-metacarpal',
  27. 'thumb-phalanx-proximal',
  28. 'thumb-phalanx-distal',
  29. 'thumb-tip',
  30. 'index-finger-metacarpal',
  31. 'index-finger-phalanx-proximal',
  32. 'index-finger-phalanx-intermediate',
  33. 'index-finger-phalanx-distal',
  34. 'index-finger-tip',
  35. 'middle-finger-metacarpal',
  36. 'middle-finger-phalanx-proximal',
  37. 'middle-finger-phalanx-intermediate',
  38. 'middle-finger-phalanx-distal',
  39. 'middle-finger-tip',
  40. 'ring-finger-metacarpal',
  41. 'ring-finger-phalanx-proximal',
  42. 'ring-finger-phalanx-intermediate',
  43. 'ring-finger-phalanx-distal',
  44. 'ring-finger-tip',
  45. 'pinky-finger-metacarpal',
  46. 'pinky-finger-phalanx-proximal',
  47. 'pinky-finger-phalanx-intermediate',
  48. 'pinky-finger-phalanx-distal',
  49. 'pinky-finger-tip'
  50. ];
  51. for ( const jointName of joints ) {
  52. var cube = new Mesh( geometry, jointName.indexOf( 'tip' ) !== - 1 ? tipMaterial : jointMaterial );
  53. cube.castShadow = true;
  54. cube.receiveShadow = true;
  55. cube.jointName = jointName;
  56. this.handMesh.add( cube );
  57. }
  58. }
  59. }
  60. updateMesh() {
  61. const defaultRadius = 0.008;
  62. const objects = this.handMesh.children;
  63. // XR Joints
  64. const XRJoints = this.controller.joints;
  65. for ( let i = 0; i < objects.length; i ++ ) {
  66. const jointMesh = objects[ i ];
  67. const XRJoint = XRJoints[ jointMesh.jointName ];
  68. if ( XRJoint.visible ) {
  69. jointMesh.position.copy( XRJoint.position );
  70. jointMesh.quaternion.copy( XRJoint.quaternion );
  71. jointMesh.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  72. }
  73. jointMesh.visible = XRJoint.visible;
  74. }
  75. }
  76. }
  77. export { XRHandPrimitiveModel };