XRHandModelFactory.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {
  2. Object3D
  3. } from '../../../build/three.module.js';
  4. import {
  5. XRHandPrimitiveModel
  6. } from './XRHandPrimitiveModel.js';
  7. import {
  8. XRHandMeshModel
  9. } from './XRHandMeshModel.js';
  10. import {
  11. fetchProfile
  12. } from '../libs/motion-controllers.module.js';
  13. const DEFAULT_PROFILES_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/[email protected]/dist/profiles';
  14. const DEFAULT_PROFILE = 'generic-hand';
  15. class XRHandModel extends Object3D {
  16. constructor( controller ) {
  17. super();
  18. this.controller = controller;
  19. this.motionController = null;
  20. this.envMap = null;
  21. this.mesh = null;
  22. }
  23. updateMatrixWorld( force ) {
  24. super.updateMatrixWorld( force );
  25. if ( this.motionController ) {
  26. this.motionController.updateMesh();
  27. }
  28. }
  29. }
  30. class XRHandModelFactory {
  31. constructor() {
  32. this.path = '';
  33. }
  34. setPath( path ) {
  35. this.path = path;
  36. return this;
  37. }
  38. createHandModel( controller, profile, options ) {
  39. const handModel = new XRHandModel( controller );
  40. controller.addEventListener( 'connected', ( event ) => {
  41. const xrInputSource = event.data;
  42. if ( xrInputSource.hand && ! handModel.motionController ) {
  43. handModel.visible = true;
  44. handModel.xrInputSource = xrInputSource;
  45. // @todo Detect profile if not provided
  46. if ( profile === undefined || profile === 'spheres' ) {
  47. handModel.motionController = new XRHandPrimitiveModel( handModel, controller, this.path, xrInputSource.handedness, { primitive: 'sphere' } );
  48. } else if ( profile === 'boxes' ) {
  49. handModel.motionController = new XRHandPrimitiveModel( handModel, controller, this.path, xrInputSource.handedness, { primitive: 'box' } );
  50. } else if ( profile === 'oculus' ) {
  51. fetchProfile( xrInputSource, DEFAULT_PROFILES_PATH, DEFAULT_PROFILE ).then( ( { profile, assetPath } ) => {
  52. handModel.motionController = new XRHandMeshModel( handModel, controller, assetPath );
  53. } ).catch( ( err ) => {
  54. console.warn( err );
  55. } );
  56. }
  57. }
  58. } );
  59. controller.addEventListener( 'disconnected', () => {
  60. // handModel.motionController = null;
  61. // handModel.remove( scene );
  62. // scene = null;
  63. } );
  64. return handModel;
  65. }
  66. }
  67. export { XRHandModelFactory };