XRHandModelFactory.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. Object3D
  3. } from 'three';
  4. import {
  5. XRHandPrimitiveModel
  6. } from './XRHandPrimitiveModel.js';
  7. import {
  8. XRHandMeshModel
  9. } from './XRHandMeshModel.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 = null;
  28. }
  29. setPath( path ) {
  30. this.path = path;
  31. return this;
  32. }
  33. createHandModel( controller, profile ) {
  34. const handModel = new XRHandModel( controller );
  35. controller.addEventListener( 'connected', ( event ) => {
  36. const xrInputSource = event.data;
  37. if ( xrInputSource.hand && ! handModel.motionController ) {
  38. handModel.xrInputSource = xrInputSource;
  39. // @todo Detect profile if not provided
  40. if ( profile === undefined || profile === 'spheres' ) {
  41. handModel.motionController = new XRHandPrimitiveModel( handModel, controller, this.path, xrInputSource.handedness, { primitive: 'sphere' } );
  42. } else if ( profile === 'boxes' ) {
  43. handModel.motionController = new XRHandPrimitiveModel( handModel, controller, this.path, xrInputSource.handedness, { primitive: 'box' } );
  44. } else if ( profile === 'mesh' ) {
  45. handModel.motionController = new XRHandMeshModel( handModel, controller, this.path, xrInputSource.handedness );
  46. }
  47. }
  48. controller.visible = true;
  49. } );
  50. controller.addEventListener( 'disconnected', () => {
  51. controller.visible = false;
  52. // handModel.motionController = null;
  53. // handModel.remove( scene );
  54. // scene = null;
  55. } );
  56. return handModel;
  57. }
  58. }
  59. export { XRHandModelFactory };