XRHandPrimitiveModel.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. var 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. var jointMaterial = new MeshStandardMaterial( { color: 0xffffff, roughness: 1, metalness: 0 } );
  23. var tipMaterial = new MeshStandardMaterial( { color: 0x999999, roughness: 1, metalness: 0 } );
  24. const tipIndexes = [
  25. XRHand.THUMB_PHALANX_TIP,
  26. XRHand.INDEX_PHALANX_TIP,
  27. XRHand.MIDDLE_PHALANX_TIP,
  28. XRHand.RING_PHALANX_TIP,
  29. XRHand.LITTLE_PHALANX_TIP
  30. ];
  31. for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
  32. var cube = new Mesh( geometry, tipIndexes.indexOf( i ) !== - 1 ? tipMaterial : jointMaterial );
  33. cube.castShadow = true;
  34. this.handMesh.add( cube );
  35. }
  36. }
  37. }
  38. updateMesh() {
  39. const defaultRadius = 0.008;
  40. const objects = this.handMesh.children;
  41. // XR Joints
  42. const XRJoints = this.controller.joints;
  43. for ( var i = 0; i < objects.length; i ++ ) {
  44. const jointMesh = objects[ i ];
  45. const XRJoint = XRJoints[ i ];
  46. if ( XRJoint.visible ) {
  47. jointMesh.position.copy( XRJoint.position );
  48. jointMesh.quaternion.copy( XRJoint.quaternion );
  49. jointMesh.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  50. }
  51. jointMesh.visible = XRJoint.visible;
  52. }
  53. }
  54. }
  55. export { XRHandPrimitiveModel };