XRHandModelFactory.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. class XRHandModel extends Object3D {
  11. constructor( controller ) {
  12. super();
  13. this.controller = controller;
  14. this.motionController = null;
  15. this.envMap = null;
  16. this.mesh = null;
  17. }
  18. updateMatrixWorld( force ) {
  19. super.updateMatrixWorld( force );
  20. if ( this.motionController ) {
  21. this.motionController.updateMesh();
  22. }
  23. }
  24. }
  25. class XRHandModelFactory {
  26. constructor() {
  27. this.path = '';
  28. }
  29. setPath( path ) {
  30. this.path = path;
  31. return this;
  32. }
  33. createHandModel( controller, profile, options ) {
  34. const handModel = new XRHandModel( controller );
  35. controller.addEventListener( 'connected', ( event ) => {
  36. const xrInputSource = event.data;
  37. if ( xrInputSource.hand && ! handModel.motionController ) {
  38. handModel.visible = true;
  39. handModel.xrInputSource = xrInputSource;
  40. // @todo Detect profile if not provided
  41. if ( profile === undefined || profile === 'spheres' ) {
  42. handModel.motionController = new XRHandPrimitiveModel( handModel, controller, this.path, xrInputSource.handedness, { primitive: 'sphere' } );
  43. } else if ( profile === 'boxes' ) {
  44. handModel.motionController = new XRHandPrimitiveModel( handModel, controller, this.path, xrInputSource.handedness, { primitive: 'box' } );
  45. } else if ( profile === 'oculus' ) {
  46. handModel.motionController = new XRHandOculusMeshModel( handModel, controller, this.path, xrInputSource.handedness, options );
  47. }
  48. }
  49. } );
  50. controller.addEventListener( 'disconnected', () => {
  51. // handModel.motionController = null;
  52. // handModel.remove( scene );
  53. // scene = null;
  54. } );
  55. return handModel;
  56. }
  57. }
  58. export { XRHandModelFactory };