OculusHandModel.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Object3D, Sphere, Box3 } from 'three';
  2. import { XRHandMeshModel } from './XRHandMeshModel.js';
  3. const TOUCH_RADIUS = 0.01;
  4. const POINTING_JOINT = 'index-finger-tip';
  5. class OculusHandModel extends Object3D {
  6. constructor( controller ) {
  7. super();
  8. this.controller = controller;
  9. this.motionController = null;
  10. this.envMap = null;
  11. this.mesh = null;
  12. controller.addEventListener( 'connected', ( event ) => {
  13. const xrInputSource = event.data;
  14. if ( xrInputSource.hand && ! this.motionController ) {
  15. this.xrInputSource = xrInputSource;
  16. this.motionController = new XRHandMeshModel( this, controller, this.path, xrInputSource.handedness );
  17. }
  18. } );
  19. controller.addEventListener( 'disconnected', () => {
  20. this.clear();
  21. this.motionController = null;
  22. } );
  23. }
  24. updateMatrixWorld( force ) {
  25. super.updateMatrixWorld( force );
  26. if ( this.motionController ) {
  27. this.motionController.updateMesh();
  28. }
  29. }
  30. getPointerPosition() {
  31. const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
  32. if ( indexFingerTip ) {
  33. return indexFingerTip.position;
  34. } else {
  35. return null;
  36. }
  37. }
  38. intersectBoxObject( boxObject ) {
  39. const pointerPosition = this.getPointerPosition();
  40. if ( pointerPosition ) {
  41. const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
  42. const box = new Box3().setFromObject( boxObject );
  43. return indexSphere.intersectsBox( box );
  44. } else {
  45. return false;
  46. }
  47. }
  48. checkButton( button ) {
  49. if ( this.intersectBoxObject( button ) ) {
  50. button.onPress();
  51. } else {
  52. button.onClear();
  53. }
  54. if ( button.isPressed() ) {
  55. button.whilePressed();
  56. }
  57. }
  58. }
  59. export { OculusHandModel };