WebXRHandController.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {
  2. Object3D
  3. } from "../../../build/three.module.js";
  4. import {
  5. XRHandPrimitiveModel
  6. } from "./XRHandPrimitiveModel.js";
  7. import {
  8. XRHandOculusMeshModel
  9. } from "./XRHandOculusMeshModel.js";
  10. function XRHandModel( controller ) {
  11. Object3D.call( this );
  12. this.controller = controller;
  13. this.motionController = null;
  14. this.envMap = null;
  15. this.mesh = null;
  16. }
  17. XRHandModel.prototype = Object.assign( Object.create( Object3D.prototype ), {
  18. constructor: XRHandModel,
  19. updateMatrixWorld: function ( force ) {
  20. Object3D.prototype.updateMatrixWorld.call( this, force );
  21. if ( this.motionController ) {
  22. this.motionController.updateMesh();
  23. }
  24. },
  25. } );
  26. var XRHandModelFactory = ( function () {
  27. function XRHandModelFactory() {}
  28. XRHandModelFactory.prototype = {
  29. constructor: XRHandModelFactory,
  30. createHandModel: function ( controller, profile, options ) {
  31. const handModel = new XRHandModel( controller );
  32. let scene = null;
  33. controller.addEventListener( 'connected', ( event ) => {
  34. const xrInputSource = event.data;
  35. if ( xrInputSource.hand && ! handModel.motionController ) {
  36. handModel.visible = true;
  37. handModel.xrInputSource = xrInputSource;
  38. // @todo Detect profile if not provided
  39. if ( profile === undefined || profile === "spheres" ) {
  40. handModel.motionController = new XRHandPrimitiveModel( controller, xrInputSource.handedness, { primitive: "sphere" } );
  41. } else if ( profile === "boxes" ) {
  42. handModel.motionController = new XRHandPrimitiveModel( controller, xrInputSource.handedness, { primitive: "box" } );
  43. } else if ( profile === "oculus" ) {
  44. handModel.motionController = new XRHandOculusMeshModel( controller, xrInputSource.handedness, options );
  45. }
  46. }
  47. } );
  48. controller.addEventListener( 'disconnected', () => {
  49. // handModel.motionController = null;
  50. // handModel.remove( scene );
  51. // scene = null;
  52. } );
  53. return handModel;
  54. }
  55. };
  56. return XRHandModelFactory;
  57. } )();
  58. export { XRHandModelFactory };