OculusHandModel.js 1.7 KB

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