OculusHandPointerModel.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import { BufferGeometry, Float32BufferAttribute, Matrix4, Mesh, MeshBasicMaterial, Object3D, Raycaster, SphereGeometry, Vector3 } from 'three';
  2. const PINCH_MAX = 0.05;
  3. const PINCH_THRESHOLD = 0.02;
  4. const PINCH_MIN = 0.01;
  5. const POINTER_ADVANCE_MAX = 0.02;
  6. const POINTER_OPACITY_MAX = 1;
  7. const POINTER_OPACITY_MIN = 0.4;
  8. const POINTER_FRONT_RADIUS = 0.002;
  9. const POINTER_REAR_RADIUS = 0.01;
  10. const POINTER_REAR_RADIUS_MIN = 0.003;
  11. const POINTER_LENGTH = 0.035;
  12. const POINTER_SEGMENTS = 16;
  13. const POINTER_RINGS = 12;
  14. const POINTER_HEMISPHERE_ANGLE = 110;
  15. const YAXIS = /* @__PURE__ */ new Vector3( 0, 1, 0 );
  16. const ZAXIS = /* @__PURE__ */ new Vector3( 0, 0, 1 );
  17. const CURSOR_RADIUS = 0.02;
  18. const CURSOR_MAX_DISTANCE = 1.5;
  19. /**
  20. * Represents an Oculus hand pointer model.
  21. *
  22. * @augments Object3D
  23. * @three_import import { OculusHandPointerModel } from 'three/addons/webxr/OculusHandPointerModel.js';
  24. */
  25. class OculusHandPointerModel extends Object3D {
  26. /**
  27. * Constructs a new Oculus hand model.
  28. *
  29. * @param {Group} hand - The hand controller.
  30. * @param {Group} controller - The WebXR controller in target ray space.
  31. */
  32. constructor( hand, controller ) {
  33. super();
  34. /**
  35. * The hand controller.
  36. *
  37. * @type {Group}
  38. */
  39. this.hand = hand;
  40. /**
  41. * The WebXR controller in target ray space.
  42. *
  43. * @type {Group}
  44. */
  45. this.controller = controller;
  46. // Unused
  47. this.motionController = null;
  48. this.envMap = null;
  49. this.mesh = null;
  50. /**
  51. * The pointer geometry.
  52. *
  53. * @type {?BufferGeometry}
  54. * @default null
  55. */
  56. this.pointerGeometry = null;
  57. /**
  58. * The pointer mesh.
  59. *
  60. * @type {?Mesh}
  61. * @default null
  62. */
  63. this.pointerMesh = null;
  64. /**
  65. * The pointer object that holds the pointer mesh.
  66. *
  67. * @type {?Object3D}
  68. * @default null
  69. */
  70. this.pointerObject = null;
  71. /**
  72. * Whether the model is pinched or not.
  73. *
  74. * @type {?boolean}
  75. * @default false
  76. */
  77. this.pinched = false;
  78. /**
  79. * Whether the model is attached or not.
  80. *
  81. * @type {boolean}
  82. * @default false
  83. */
  84. this.attached = false;
  85. /**
  86. * The cursor object.
  87. *
  88. * @type {?Mesh}
  89. * @default null
  90. */
  91. this.cursorObject = null;
  92. /**
  93. * The internal raycaster used for detecting
  94. * intersections.
  95. *
  96. * @type {?Raycaster}
  97. * @default null
  98. */
  99. this.raycaster = null;
  100. this._onConnected = this._onConnected.bind( this );
  101. this._onDisconnected = this._onDisconnected.bind( this );
  102. this.hand.addEventListener( 'connected', this._onConnected );
  103. this.hand.addEventListener( 'disconnected', this._onDisconnected );
  104. }
  105. _onConnected( event ) {
  106. const xrInputSource = event.data;
  107. if ( xrInputSource.hand ) {
  108. this.visible = true;
  109. this.xrInputSource = xrInputSource;
  110. this.createPointer();
  111. }
  112. }
  113. _onDisconnected() {
  114. this.visible = false;
  115. this.xrInputSource = null;
  116. if ( this.pointerGeometry ) this.pointerGeometry.dispose();
  117. if ( this.pointerMesh && this.pointerMesh.material ) this.pointerMesh.material.dispose();
  118. this.clear();
  119. }
  120. _drawVerticesRing( vertices, baseVector, ringIndex ) {
  121. const segmentVector = baseVector.clone();
  122. for ( let i = 0; i < POINTER_SEGMENTS; i ++ ) {
  123. segmentVector.applyAxisAngle( ZAXIS, ( Math.PI * 2 ) / POINTER_SEGMENTS );
  124. const vid = ringIndex * POINTER_SEGMENTS + i;
  125. vertices[ 3 * vid ] = segmentVector.x;
  126. vertices[ 3 * vid + 1 ] = segmentVector.y;
  127. vertices[ 3 * vid + 2 ] = segmentVector.z;
  128. }
  129. }
  130. _updatePointerVertices( rearRadius ) {
  131. const vertices = this.pointerGeometry.attributes.position.array;
  132. // first ring for front face
  133. const frontFaceBase = new Vector3(
  134. POINTER_FRONT_RADIUS,
  135. 0,
  136. - 1 * ( POINTER_LENGTH - rearRadius )
  137. );
  138. this._drawVerticesRing( vertices, frontFaceBase, 0 );
  139. // rings for rear hemisphere
  140. const rearBase = new Vector3(
  141. Math.sin( ( Math.PI * POINTER_HEMISPHERE_ANGLE ) / 180 ) * rearRadius,
  142. Math.cos( ( Math.PI * POINTER_HEMISPHERE_ANGLE ) / 180 ) * rearRadius,
  143. 0
  144. );
  145. for ( let i = 0; i < POINTER_RINGS; i ++ ) {
  146. this._drawVerticesRing( vertices, rearBase, i + 1 );
  147. rearBase.applyAxisAngle(
  148. YAXIS,
  149. ( Math.PI * POINTER_HEMISPHERE_ANGLE ) / 180 / ( POINTER_RINGS * - 2 )
  150. );
  151. }
  152. // front and rear face center vertices
  153. const frontCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS );
  154. const rearCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS ) + 1;
  155. const frontCenter = new Vector3(
  156. 0,
  157. 0,
  158. - 1 * ( POINTER_LENGTH - rearRadius )
  159. );
  160. vertices[ frontCenterIndex * 3 ] = frontCenter.x;
  161. vertices[ frontCenterIndex * 3 + 1 ] = frontCenter.y;
  162. vertices[ frontCenterIndex * 3 + 2 ] = frontCenter.z;
  163. const rearCenter = new Vector3( 0, 0, rearRadius );
  164. vertices[ rearCenterIndex * 3 ] = rearCenter.x;
  165. vertices[ rearCenterIndex * 3 + 1 ] = rearCenter.y;
  166. vertices[ rearCenterIndex * 3 + 2 ] = rearCenter.z;
  167. this.pointerGeometry.setAttribute(
  168. 'position',
  169. new Float32BufferAttribute( vertices, 3 )
  170. );
  171. // verticesNeedUpdate = true;
  172. }
  173. /**
  174. * Creates a pointer mesh and adds it to this model.
  175. */
  176. createPointer() {
  177. let i, j;
  178. const vertices = new Array(
  179. ( ( POINTER_RINGS + 1 ) * POINTER_SEGMENTS + 2 ) * 3
  180. ).fill( 0 );
  181. // const vertices = [];
  182. const indices = [];
  183. this.pointerGeometry = new BufferGeometry();
  184. this.pointerGeometry.setAttribute(
  185. 'position',
  186. new Float32BufferAttribute( vertices, 3 )
  187. );
  188. this._updatePointerVertices( POINTER_REAR_RADIUS );
  189. // construct faces to connect rings
  190. for ( i = 0; i < POINTER_RINGS; i ++ ) {
  191. for ( j = 0; j < POINTER_SEGMENTS - 1; j ++ ) {
  192. indices.push(
  193. i * POINTER_SEGMENTS + j,
  194. i * POINTER_SEGMENTS + j + 1,
  195. ( i + 1 ) * POINTER_SEGMENTS + j
  196. );
  197. indices.push(
  198. i * POINTER_SEGMENTS + j + 1,
  199. ( i + 1 ) * POINTER_SEGMENTS + j + 1,
  200. ( i + 1 ) * POINTER_SEGMENTS + j
  201. );
  202. }
  203. indices.push(
  204. ( i + 1 ) * POINTER_SEGMENTS - 1,
  205. i * POINTER_SEGMENTS,
  206. ( i + 2 ) * POINTER_SEGMENTS - 1
  207. );
  208. indices.push(
  209. i * POINTER_SEGMENTS,
  210. ( i + 1 ) * POINTER_SEGMENTS,
  211. ( i + 2 ) * POINTER_SEGMENTS - 1
  212. );
  213. }
  214. // construct front and rear face
  215. const frontCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS );
  216. const rearCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS ) + 1;
  217. for ( i = 0; i < POINTER_SEGMENTS - 1; i ++ ) {
  218. indices.push( frontCenterIndex, i + 1, i );
  219. indices.push(
  220. rearCenterIndex,
  221. i + POINTER_SEGMENTS * POINTER_RINGS,
  222. i + POINTER_SEGMENTS * POINTER_RINGS + 1
  223. );
  224. }
  225. indices.push( frontCenterIndex, 0, POINTER_SEGMENTS - 1 );
  226. indices.push(
  227. rearCenterIndex,
  228. POINTER_SEGMENTS * ( POINTER_RINGS + 1 ) - 1,
  229. POINTER_SEGMENTS * POINTER_RINGS
  230. );
  231. const material = new MeshBasicMaterial();
  232. material.transparent = true;
  233. material.opacity = POINTER_OPACITY_MIN;
  234. this.pointerGeometry.setIndex( indices );
  235. this.pointerMesh = new Mesh( this.pointerGeometry, material );
  236. this.pointerMesh.position.set( 0, 0, - 1 * POINTER_REAR_RADIUS );
  237. this.pointerObject = new Object3D();
  238. this.pointerObject.add( this.pointerMesh );
  239. this.raycaster = new Raycaster();
  240. // create cursor
  241. const cursorGeometry = new SphereGeometry( CURSOR_RADIUS, 10, 10 );
  242. const cursorMaterial = new MeshBasicMaterial();
  243. cursorMaterial.transparent = true;
  244. cursorMaterial.opacity = POINTER_OPACITY_MIN;
  245. this.cursorObject = new Mesh( cursorGeometry, cursorMaterial );
  246. this.pointerObject.add( this.cursorObject );
  247. this.add( this.pointerObject );
  248. }
  249. _updateRaycaster() {
  250. if ( this.raycaster ) {
  251. const pointerMatrix = this.pointerObject.matrixWorld;
  252. const tempMatrix = new Matrix4();
  253. tempMatrix.identity().extractRotation( pointerMatrix );
  254. this.raycaster.ray.origin.setFromMatrixPosition( pointerMatrix );
  255. this.raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  256. }
  257. }
  258. _updatePointer() {
  259. this.pointerObject.visible = this.controller.visible;
  260. const indexTip = this.hand.joints[ 'index-finger-tip' ];
  261. const thumbTip = this.hand.joints[ 'thumb-tip' ];
  262. const distance = indexTip.position.distanceTo( thumbTip.position );
  263. const position = indexTip.position
  264. .clone()
  265. .add( thumbTip.position )
  266. .multiplyScalar( 0.5 );
  267. this.pointerObject.position.copy( position );
  268. this.pointerObject.quaternion.copy( this.controller.quaternion );
  269. this.pinched = distance <= PINCH_THRESHOLD;
  270. const pinchScale = ( distance - PINCH_MIN ) / ( PINCH_MAX - PINCH_MIN );
  271. const focusScale = ( distance - PINCH_MIN ) / ( PINCH_THRESHOLD - PINCH_MIN );
  272. if ( pinchScale > 1 ) {
  273. this._updatePointerVertices( POINTER_REAR_RADIUS );
  274. this.pointerMesh.position.set( 0, 0, - 1 * POINTER_REAR_RADIUS );
  275. this.pointerMesh.material.opacity = POINTER_OPACITY_MIN;
  276. } else if ( pinchScale > 0 ) {
  277. const rearRadius =
  278. ( POINTER_REAR_RADIUS - POINTER_REAR_RADIUS_MIN ) * pinchScale +
  279. POINTER_REAR_RADIUS_MIN;
  280. this._updatePointerVertices( rearRadius );
  281. if ( focusScale < 1 ) {
  282. this.pointerMesh.position.set(
  283. 0,
  284. 0,
  285. - 1 * rearRadius - ( 1 - focusScale ) * POINTER_ADVANCE_MAX
  286. );
  287. this.pointerMesh.material.opacity =
  288. POINTER_OPACITY_MIN +
  289. ( 1 - focusScale ) * ( POINTER_OPACITY_MAX - POINTER_OPACITY_MIN );
  290. } else {
  291. this.pointerMesh.position.set( 0, 0, - 1 * rearRadius );
  292. this.pointerMesh.material.opacity = POINTER_OPACITY_MIN;
  293. }
  294. } else {
  295. this._updatePointerVertices( POINTER_REAR_RADIUS_MIN );
  296. this.pointerMesh.position.set(
  297. 0,
  298. 0,
  299. - 1 * POINTER_REAR_RADIUS_MIN - POINTER_ADVANCE_MAX
  300. );
  301. this.pointerMesh.material.opacity = POINTER_OPACITY_MAX;
  302. }
  303. this.cursorObject.material.opacity = this.pointerMesh.material.opacity;
  304. }
  305. /**
  306. * Overwritten with a custom implementation. Makes sure the internal pointer and raycaster are updated.
  307. *
  308. * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
  309. * when {@link Object3D#matrixWorldAutoUpdate} is set to `false`.
  310. */
  311. updateMatrixWorld( force ) {
  312. super.updateMatrixWorld( force );
  313. if ( this.pointerGeometry ) {
  314. this._updatePointer();
  315. this._updateRaycaster();
  316. }
  317. }
  318. /**
  319. * Returns `true` is the model is pinched.
  320. *
  321. * @return {boolean} Whether the model is pinched or not.
  322. */
  323. isPinched() {
  324. return this.pinched;
  325. }
  326. /**
  327. * Sets the attached state.
  328. *
  329. * @param {boolean} attached - Whether the model is attached or not.
  330. */
  331. setAttached( attached ) {
  332. this.attached = attached;
  333. }
  334. /**
  335. * Returns `true` is the model is attached.
  336. *
  337. * @return {boolean} Whether the model is attached or not.
  338. */
  339. isAttached() {
  340. return this.attached;
  341. }
  342. /**
  343. * Performs an intersection test with the model's raycaster and the given object.
  344. *
  345. * @param {Object3D} object - The 3D object to check for intersection with the ray.
  346. * @param {boolean} [recursive=true] - If set to `true`, it also checks all descendants.
  347. * Otherwise it only checks intersection with the object.
  348. * @return {Array<Raycaster~Intersection>} An array holding the intersection points.
  349. */
  350. intersectObject( object, recursive = true ) {
  351. if ( this.raycaster ) {
  352. return this.raycaster.intersectObject( object, recursive );
  353. }
  354. }
  355. /**
  356. * Performs an intersection test with the model's raycaster and the given objects.
  357. *
  358. * @param {Array<Object3D>} objects - The 3D objects to check for intersection with the ray.
  359. * @param {boolean} [recursive=true] - If set to `true`, it also checks all descendants.
  360. * Otherwise it only checks intersection with the object.
  361. * @return {Array<Raycaster~Intersection>} An array holding the intersection points.
  362. */
  363. intersectObjects( objects, recursive = true ) {
  364. if ( this.raycaster ) {
  365. return this.raycaster.intersectObjects( objects, recursive );
  366. }
  367. }
  368. /**
  369. * Checks for intersections between the model's raycaster and the given objects. The method
  370. * updates the cursor object to the intersection point.
  371. *
  372. * @param {Array<Object3D>} objects - The 3D objects to check for intersection with the ray.
  373. * @param {boolean} [recursive=false] - If set to `true`, it also checks all descendants.
  374. * Otherwise it only checks intersection with the object.
  375. */
  376. checkIntersections( objects, recursive = false ) {
  377. if ( this.raycaster && ! this.attached ) {
  378. const intersections = this.raycaster.intersectObjects( objects, recursive );
  379. const direction = new Vector3( 0, 0, - 1 );
  380. if ( intersections.length > 0 ) {
  381. const intersection = intersections[ 0 ];
  382. const distance = intersection.distance;
  383. this.cursorObject.position.copy( direction.multiplyScalar( distance ) );
  384. } else {
  385. this.cursorObject.position.copy( direction.multiplyScalar( CURSOR_MAX_DISTANCE ) );
  386. }
  387. }
  388. }
  389. /**
  390. * Sets the cursor to the given distance.
  391. *
  392. * @param {number} distance - The distance to set the cursor to.
  393. */
  394. setCursor( distance ) {
  395. const direction = new Vector3( 0, 0, - 1 );
  396. if ( this.raycaster && ! this.attached ) {
  397. this.cursorObject.position.copy( direction.multiplyScalar( distance ) );
  398. }
  399. }
  400. /**
  401. * Frees the GPU-related resources allocated by this instance. Call this
  402. * method whenever this instance is no longer used in your app.
  403. */
  404. dispose() {
  405. this._onDisconnected();
  406. this.hand.removeEventListener( 'connected', this._onConnected );
  407. this.hand.removeEventListener( 'disconnected', this._onDisconnected );
  408. }
  409. }
  410. export { OculusHandPointerModel };