XRHandModelFactory.js 1.9 KB

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