XRHandModelFactory.js 2.1 KB

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