OculusHandPointerModel.js 13 KB

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