WebXRHandController.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. Object3D,
  3. SphereBufferGeometry,
  4. MeshStandardMaterial,
  5. Mesh
  6. } from "../../../build/three.module.js";
  7. function XRHandModel( controller ) {
  8. Object3D.call( this );
  9. this.controller = controller;
  10. this.envMap = null;
  11. if ( window.XRHand ) {
  12. var geometry = new SphereBufferGeometry( 1, 10, 10 );
  13. var jointMaterial = new MeshStandardMaterial( { color: 0x000000, roughness: 0.2, metalness: 0.8 } );
  14. var tipMaterial = new MeshStandardMaterial( { color: 0x333333, roughness: 0.2, metalness: 0.8 } );
  15. const tipIndexes = [
  16. XRHand.THUMB_PHALANX_TIP,
  17. XRHand.INDEX_PHALANX_TIP,
  18. XRHand.MIDDLE_PHALANX_TIP,
  19. XRHand.RING_PHALANX_TIP,
  20. XRHand.LITTLE_PHALANX_TIP
  21. ];
  22. for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
  23. var cube = new Mesh( geometry, tipIndexes.indexOf( i ) !== - 1 ? tipMaterial : jointMaterial );
  24. cube.castShadow = true;
  25. this.add( cube );
  26. }
  27. }
  28. }
  29. XRHandModel.prototype = Object.assign( Object.create( Object3D.prototype ), {
  30. constructor: XRHandModel,
  31. updateMatrixWorld: function ( force ) {
  32. Object3D.prototype.updateMatrixWorld.call( this, force );
  33. this.updateMesh();
  34. },
  35. updateMesh: function () {
  36. const defaultRadius = 0.008;
  37. // XR Joints
  38. const XRJoints = this.controller.joints;
  39. for ( var i = 0; i < this.children.length; i ++ ) {
  40. const jointMesh = this.children[ i ];
  41. const XRJoint = XRJoints[ i ];
  42. if ( XRJoint.visible ) {
  43. jointMesh.position.copy( XRJoint.position );
  44. jointMesh.quaternion.copy( XRJoint.quaternion );
  45. jointMesh.scale.setScalar( XRJoint.jointRadius || defaultRadius );
  46. }
  47. jointMesh.visible = XRJoint.visible;
  48. }
  49. }
  50. } );
  51. var XRHandModelFactory = ( function () {
  52. function XRHandModelFactory() {}
  53. XRHandModelFactory.prototype = {
  54. constructor: XRHandModelFactory,
  55. createHandModel: function ( controller ) {
  56. const handModel = new XRHandModel( controller );
  57. let scene = null;
  58. controller.addEventListener( 'connected', ( event ) => {
  59. const xrInputSource = event.data;
  60. console.log( "Connected!", xrInputSource );
  61. if ( xrInputSource.hand ) {
  62. handModel.xrInputSource = xrInputSource;
  63. }
  64. } );
  65. controller.addEventListener( 'disconnected', () => {
  66. handModel.motionController = null;
  67. handModel.remove( scene );
  68. scene = null;
  69. } );
  70. return handModel;
  71. }
  72. };
  73. return XRHandModelFactory;
  74. } )();
  75. export { XRHandModelFactory };