OculusHandModel.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { Object3D, Sphere, Box3 } from '../../../build/three.module.js';
  2. import { fetchProfile } from '../libs/motion-controllers.module.js';
  3. import { XRHandMeshModel } from './XRHandMeshModel.js';
  4. const DEFAULT_PROFILES_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/[email protected]/dist/profiles';
  5. const DEFAULT_PROFILE = 'generic-hand';
  6. const TOUCH_RADIUS = 0.01;
  7. const POINTING_JOINT = 'index-finger-tip';
  8. class OculusHandModel extends Object3D {
  9. constructor( controller ) {
  10. super();
  11. this.controller = controller;
  12. this.motionController = null;
  13. this.envMap = null;
  14. this.mesh = null;
  15. controller.addEventListener( 'connected', ( event ) => {
  16. const xrInputSource = event.data;
  17. if ( xrInputSource.hand && ! this.motionController ) {
  18. this.visible = true;
  19. this.xrInputSource = xrInputSource;
  20. fetchProfile( xrInputSource, DEFAULT_PROFILES_PATH, DEFAULT_PROFILE ).then( ( { profile, assetPath } ) => {
  21. this.motionController = new XRHandMeshModel(
  22. this,
  23. controller,
  24. assetPath
  25. );
  26. } ).catch( ( err ) => {
  27. console.warn( err );
  28. } );
  29. }
  30. } );
  31. controller.addEventListener( 'disconnected', () => {
  32. this.clear();
  33. this.motionController = null;
  34. } );
  35. }
  36. updateMatrixWorld( force ) {
  37. super.updateMatrixWorld( force );
  38. if ( this.motionController ) {
  39. this.motionController.updateMesh();
  40. }
  41. }
  42. getPointerPosition() {
  43. const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
  44. if ( indexFingerTip ) {
  45. return indexFingerTip.position;
  46. } else {
  47. return null;
  48. }
  49. }
  50. intersectBoxObject( boxObject ) {
  51. const pointerPosition = this.getPointerPosition();
  52. if ( pointerPosition ) {
  53. const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
  54. const box = new Box3().setFromObject( boxObject );
  55. return indexSphere.intersectsBox( box );
  56. } else {
  57. return false;
  58. }
  59. }
  60. checkButton( button ) {
  61. if ( this.intersectBoxObject( button ) ) {
  62. button.onPress();
  63. } else {
  64. button.onClear();
  65. }
  66. if ( button.isPressed() ) {
  67. button.whilePressed();
  68. }
  69. }
  70. }
  71. export { OculusHandModel };