XRHandPrimitiveModel.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {
  2. SphereBufferGeometry,
  3. BoxBufferGeometry,
  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 SphereBufferGeometry( 1, 10, 10 );
  19. } else if ( options.primitive === 'box' ) {
  20. geometry = new BoxBufferGeometry( 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 tipIndexes = [
  25. 4,
  26. 9,
  27. 14,
  28. 19,
  29. 24,
  30. ];
  31. for ( let i = 0; i <= 24; i ++ ) {
  32. var cube = new Mesh( geometry, tipIndexes.indexOf( i ) !== - 1 ? tipMaterial : jointMaterial );
  33. cube.castShadow = true;
  34. cube.receiveShadow = true;
  35. this.handMesh.add( cube );
  36. }
  37. }
  38. }
  39. updateMesh() {
  40. const defaultRadius = 0.008;
  41. const objects = this.handMesh.children;
  42. // XR Joints
  43. const XRJoints = this.controller.joints;
  44. for ( let i = 0; i < objects.length; i ++ ) {
  45. const jointMesh = objects[ i ];
  46. const XRJoint = XRJoints[ i ];
  47. if ( XRJoint.visible ) {
  48. jointMesh.position.copy( XRJoint.position );
  49. jointMesh.quaternion.copy( XRJoint.quaternion );
  50. jointMesh.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  51. }
  52. jointMesh.visible = XRJoint.visible;
  53. }
  54. }
  55. }
  56. export { XRHandPrimitiveModel };