XRHandPrimitiveModel.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. SphereBufferGeometry,
  3. BoxBufferGeometry,
  4. MeshStandardMaterial,
  5. Mesh,
  6. Group
  7. } from "../../../build/three.module.js";
  8. class XRHandPrimitiveModel {
  9. constructor( controller, handedness, options ) {
  10. this.controller = controller;
  11. this.envMap = null;
  12. this.handMesh = new Group();
  13. this.controller.add( this.handMesh );
  14. if ( window.XRHand ) {
  15. var geometry;
  16. if ( ! options || ! options.primitive || options.primitive === "sphere" ) {
  17. geometry = new SphereBufferGeometry( 1, 10, 10 );
  18. } else if ( options.primitive === "box" ) {
  19. geometry = new BoxBufferGeometry( 1, 1, 1 );
  20. }
  21. var jointMaterial = new MeshStandardMaterial( { color: 0x000000, roughness: 0.2, metalness: 0.8 } );
  22. var tipMaterial = new MeshStandardMaterial( { color: 0x333333, roughness: 0.2, metalness: 0.8 } );
  23. const tipIndexes = [
  24. XRHand.THUMB_PHALANX_TIP,
  25. XRHand.INDEX_PHALANX_TIP,
  26. XRHand.MIDDLE_PHALANX_TIP,
  27. XRHand.RING_PHALANX_TIP,
  28. XRHand.LITTLE_PHALANX_TIP
  29. ];
  30. for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
  31. var cube = new Mesh( geometry, tipIndexes.indexOf( i ) !== - 1 ? tipMaterial : jointMaterial );
  32. cube.castShadow = true;
  33. this.handMesh.add( cube );
  34. }
  35. }
  36. }
  37. updateMesh() {
  38. const defaultRadius = 0.008;
  39. const objects = this.handMesh.children;
  40. // XR Joints
  41. const XRJoints = this.controller.joints;
  42. for ( var i = 0; i < objects.length; i ++ ) {
  43. const jointMesh = objects[ i ];
  44. const XRJoint = XRJoints[ i ];
  45. if ( XRJoint.visible ) {
  46. jointMesh.position.copy( XRJoint.position );
  47. jointMesh.quaternion.copy( XRJoint.quaternion );
  48. jointMesh.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  49. }
  50. jointMesh.visible = XRJoint.visible;
  51. }
  52. }
  53. }
  54. export { XRHandPrimitiveModel };