XRHandSpheresModel.js 1.5 KB

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