OculusHandModel.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /**
  6. * Represents an Oculus hand model.
  7. *
  8. * @augments Object3D
  9. * @three_import import { OculusHandModel } from 'three/addons/webxr/OculusHandModel.js';
  10. */
  11. class OculusHandModel extends Object3D {
  12. /**
  13. * Constructs a new Oculus hand model.
  14. *
  15. * @param {Group} controller - The hand controller.
  16. * @param {?Loader} [loader=null] - A loader that is used to load hand models.
  17. * @param {?Function} [onLoad=null] - A callback that is executed when a hand model has been loaded.
  18. */
  19. constructor( controller, loader = null, onLoad = null ) {
  20. super();
  21. /**
  22. * The hand controller.
  23. *
  24. * @type {Group}
  25. */
  26. this.controller = controller;
  27. /**
  28. * The motion controller.
  29. *
  30. * @type {?MotionController}
  31. * @default null
  32. */
  33. this.motionController = null;
  34. /**
  35. * The model's environment map.
  36. *
  37. * @type {?Texture}
  38. * @default null
  39. */
  40. this.envMap = null;
  41. /**
  42. * A loader that is used to load hand models.
  43. *
  44. * @type {?Loader}
  45. * @default null
  46. */
  47. this.loader = loader;
  48. /**
  49. * A callback that is executed when a hand model has been loaded.
  50. *
  51. * @type {?Function}
  52. * @default null
  53. */
  54. this.onLoad = onLoad;
  55. /**
  56. * The path to the model repository.
  57. *
  58. * @type {?string}
  59. * @default null
  60. */
  61. this.path = null;
  62. /**
  63. * The model mesh.
  64. *
  65. * @type {Mesh}
  66. * @default null
  67. */
  68. this.mesh = null;
  69. controller.addEventListener( 'connected', ( event ) => {
  70. const xrInputSource = event.data;
  71. if ( xrInputSource.hand && ! this.motionController ) {
  72. this.xrInputSource = xrInputSource;
  73. this.motionController = new XRHandMeshModel( this, controller, this.path, xrInputSource.handedness, this.loader, this.onLoad );
  74. }
  75. } );
  76. controller.addEventListener( 'disconnected', () => {
  77. this.clear();
  78. this.motionController = null;
  79. } );
  80. }
  81. /**
  82. * Overwritten with a custom implementation. Makes sure the motion controller updates the mesh.
  83. *
  84. * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
  85. * when {@link Object3D#matrixWorldAutoUpdate} is set to `false`.
  86. */
  87. updateMatrixWorld( force ) {
  88. super.updateMatrixWorld( force );
  89. if ( this.motionController ) {
  90. this.motionController.updateMesh();
  91. }
  92. }
  93. /**
  94. * Returns the pointer position which is the position of the index finger tip.
  95. *
  96. * @return {Vector3|null} The pointer position. Returns `null` if not index finger tip joint was found.
  97. */
  98. getPointerPosition() {
  99. const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
  100. if ( indexFingerTip ) {
  101. return indexFingerTip.position;
  102. } else {
  103. return null;
  104. }
  105. }
  106. /**
  107. * Returns `true` if the current pointer position (the index finger tip) intersections
  108. * with the given box object.
  109. *
  110. * @param {Mesh} boxObject - The box object.
  111. * @return {boolean} Whether an intersection was found or not.
  112. */
  113. intersectBoxObject( boxObject ) {
  114. const pointerPosition = this.getPointerPosition();
  115. if ( pointerPosition ) {
  116. const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
  117. const box = new Box3().setFromObject( boxObject );
  118. return indexSphere.intersectsBox( box );
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Executed actions depending on the interaction state with
  125. * the given button.
  126. *
  127. * @param {Object} button - The button.
  128. */
  129. checkButton( button ) {
  130. if ( this.intersectBoxObject( button ) ) {
  131. button.onPress();
  132. } else {
  133. button.onClear();
  134. }
  135. if ( button.isPressed() ) {
  136. button.whilePressed();
  137. }
  138. }
  139. }
  140. export { OculusHandModel };