OculusHandPointerModel.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import * as THREE 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 THREE.Vector3( 0, 1, 0 );
  16. const ZAXIS = /* @__PURE__ */ new THREE.Vector3( 0, 0, 1 );
  17. const CURSOR_RADIUS = 0.02;
  18. const CURSOR_MAX_DISTANCE = 1.5;
  19. class OculusHandPointerModel extends THREE.Object3D {
  20. constructor( hand, controller ) {
  21. super();
  22. this.hand = hand;
  23. this.controller = controller;
  24. // Unused
  25. this.motionController = null;
  26. this.envMap = null;
  27. this.mesh = null;
  28. this.pointerGeometry = null;
  29. this.pointerMesh = null;
  30. this.pointerObject = null;
  31. this.pinched = false;
  32. this.attached = false;
  33. this.cursorObject = null;
  34. this.raycaster = null;
  35. this._onConnected = this._onConnected.bind( this );
  36. this._onDisconnected = this._onDisconnected.bind( this );
  37. this.hand.addEventListener( 'connected', this._onConnected );
  38. this.hand.addEventListener( 'disconnected', this._onDisconnected );
  39. }
  40. _onConnected( event ) {
  41. const xrInputSource = event.data;
  42. if ( xrInputSource.hand ) {
  43. this.visible = true;
  44. this.xrInputSource = xrInputSource;
  45. this.createPointer();
  46. }
  47. }
  48. _onDisconnected() {
  49. this.visible = false;
  50. this.xrInputSource = null;
  51. if ( this.pointerGeometry ) this.pointerGeometry.dispose();
  52. if ( this.pointerMesh && this.pointerMesh.material ) this.pointerMesh.material.dispose();
  53. this.clear();
  54. }
  55. _drawVerticesRing( vertices, baseVector, ringIndex ) {
  56. const segmentVector = baseVector.clone();
  57. for ( let i = 0; i < POINTER_SEGMENTS; i ++ ) {
  58. segmentVector.applyAxisAngle( ZAXIS, ( Math.PI * 2 ) / POINTER_SEGMENTS );
  59. const vid = ringIndex * POINTER_SEGMENTS + i;
  60. vertices[ 3 * vid ] = segmentVector.x;
  61. vertices[ 3 * vid + 1 ] = segmentVector.y;
  62. vertices[ 3 * vid + 2 ] = segmentVector.z;
  63. }
  64. }
  65. _updatePointerVertices( rearRadius ) {
  66. const vertices = this.pointerGeometry.attributes.position.array;
  67. // first ring for front face
  68. const frontFaceBase = new THREE.Vector3(
  69. POINTER_FRONT_RADIUS,
  70. 0,
  71. - 1 * ( POINTER_LENGTH - rearRadius )
  72. );
  73. this._drawVerticesRing( vertices, frontFaceBase, 0 );
  74. // rings for rear hemisphere
  75. const rearBase = new THREE.Vector3(
  76. Math.sin( ( Math.PI * POINTER_HEMISPHERE_ANGLE ) / 180 ) * rearRadius,
  77. Math.cos( ( Math.PI * POINTER_HEMISPHERE_ANGLE ) / 180 ) * rearRadius,
  78. 0
  79. );
  80. for ( let i = 0; i < POINTER_RINGS; i ++ ) {
  81. this._drawVerticesRing( vertices, rearBase, i + 1 );
  82. rearBase.applyAxisAngle(
  83. YAXIS,
  84. ( Math.PI * POINTER_HEMISPHERE_ANGLE ) / 180 / ( POINTER_RINGS * - 2 )
  85. );
  86. }
  87. // front and rear face center vertices
  88. const frontCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS );
  89. const rearCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS ) + 1;
  90. const frontCenter = new THREE.Vector3(
  91. 0,
  92. 0,
  93. - 1 * ( POINTER_LENGTH - rearRadius )
  94. );
  95. vertices[ frontCenterIndex * 3 ] = frontCenter.x;
  96. vertices[ frontCenterIndex * 3 + 1 ] = frontCenter.y;
  97. vertices[ frontCenterIndex * 3 + 2 ] = frontCenter.z;
  98. const rearCenter = new THREE.Vector3( 0, 0, rearRadius );
  99. vertices[ rearCenterIndex * 3 ] = rearCenter.x;
  100. vertices[ rearCenterIndex * 3 + 1 ] = rearCenter.y;
  101. vertices[ rearCenterIndex * 3 + 2 ] = rearCenter.z;
  102. this.pointerGeometry.setAttribute(
  103. 'position',
  104. new THREE.Float32BufferAttribute( vertices, 3 )
  105. );
  106. // verticesNeedUpdate = true;
  107. }
  108. createPointer() {
  109. let i, j;
  110. const vertices = new Array(
  111. ( ( POINTER_RINGS + 1 ) * POINTER_SEGMENTS + 2 ) * 3
  112. ).fill( 0 );
  113. // const vertices = [];
  114. const indices = [];
  115. this.pointerGeometry = new THREE.BufferGeometry();
  116. this.pointerGeometry.setAttribute(
  117. 'position',
  118. new THREE.Float32BufferAttribute( vertices, 3 )
  119. );
  120. this._updatePointerVertices( POINTER_REAR_RADIUS );
  121. // construct faces to connect rings
  122. for ( i = 0; i < POINTER_RINGS; i ++ ) {
  123. for ( j = 0; j < POINTER_SEGMENTS - 1; j ++ ) {
  124. indices.push(
  125. i * POINTER_SEGMENTS + j,
  126. i * POINTER_SEGMENTS + j + 1,
  127. ( i + 1 ) * POINTER_SEGMENTS + j
  128. );
  129. indices.push(
  130. i * POINTER_SEGMENTS + j + 1,
  131. ( i + 1 ) * POINTER_SEGMENTS + j + 1,
  132. ( i + 1 ) * POINTER_SEGMENTS + j
  133. );
  134. }
  135. indices.push(
  136. ( i + 1 ) * POINTER_SEGMENTS - 1,
  137. i * POINTER_SEGMENTS,
  138. ( i + 2 ) * POINTER_SEGMENTS - 1
  139. );
  140. indices.push(
  141. i * POINTER_SEGMENTS,
  142. ( i + 1 ) * POINTER_SEGMENTS,
  143. ( i + 2 ) * POINTER_SEGMENTS - 1
  144. );
  145. }
  146. // construct front and rear face
  147. const frontCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS );
  148. const rearCenterIndex = POINTER_SEGMENTS * ( 1 + POINTER_RINGS ) + 1;
  149. for ( i = 0; i < POINTER_SEGMENTS - 1; i ++ ) {
  150. indices.push( frontCenterIndex, i + 1, i );
  151. indices.push(
  152. rearCenterIndex,
  153. i + POINTER_SEGMENTS * POINTER_RINGS,
  154. i + POINTER_SEGMENTS * POINTER_RINGS + 1
  155. );
  156. }
  157. indices.push( frontCenterIndex, 0, POINTER_SEGMENTS - 1 );
  158. indices.push(
  159. rearCenterIndex,
  160. POINTER_SEGMENTS * ( POINTER_RINGS + 1 ) - 1,
  161. POINTER_SEGMENTS * POINTER_RINGS
  162. );
  163. const material = new THREE.MeshBasicMaterial();
  164. material.transparent = true;
  165. material.opacity = POINTER_OPACITY_MIN;
  166. this.pointerGeometry.setIndex( indices );
  167. this.pointerMesh = new THREE.Mesh( this.pointerGeometry, material );
  168. this.pointerMesh.position.set( 0, 0, - 1 * POINTER_REAR_RADIUS );
  169. this.pointerObject = new THREE.Object3D();
  170. this.pointerObject.add( this.pointerMesh );
  171. this.raycaster = new THREE.Raycaster();
  172. // create cursor
  173. const cursorGeometry = new THREE.SphereGeometry( CURSOR_RADIUS, 10, 10 );
  174. const cursorMaterial = new THREE.MeshBasicMaterial();
  175. cursorMaterial.transparent = true;
  176. cursorMaterial.opacity = POINTER_OPACITY_MIN;
  177. this.cursorObject = new THREE.Mesh( cursorGeometry, cursorMaterial );
  178. this.pointerObject.add( this.cursorObject );
  179. this.add( this.pointerObject );
  180. }
  181. _updateRaycaster() {
  182. if ( this.raycaster ) {
  183. const pointerMatrix = this.pointerObject.matrixWorld;
  184. const tempMatrix = new THREE.Matrix4();
  185. tempMatrix.identity().extractRotation( pointerMatrix );
  186. this.raycaster.ray.origin.setFromMatrixPosition( pointerMatrix );
  187. this.raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  188. }
  189. }
  190. _updatePointer() {
  191. this.pointerObject.visible = this.controller.visible;
  192. const indexTip = this.hand.joints[ 'index-finger-tip' ];
  193. const thumbTip = this.hand.joints[ 'thumb-tip' ];
  194. const distance = indexTip.position.distanceTo( thumbTip.position );
  195. const position = indexTip.position
  196. .clone()
  197. .add( thumbTip.position )
  198. .multiplyScalar( 0.5 );
  199. this.pointerObject.position.copy( position );
  200. this.pointerObject.quaternion.copy( this.controller.quaternion );
  201. this.pinched = distance <= PINCH_THRESHOLD;
  202. const pinchScale = ( distance - PINCH_MIN ) / ( PINCH_MAX - PINCH_MIN );
  203. const focusScale = ( distance - PINCH_MIN ) / ( PINCH_THRESHOLD - PINCH_MIN );
  204. if ( pinchScale > 1 ) {
  205. this._updatePointerVertices( POINTER_REAR_RADIUS );
  206. this.pointerMesh.position.set( 0, 0, - 1 * POINTER_REAR_RADIUS );
  207. this.pointerMesh.material.opacity = POINTER_OPACITY_MIN;
  208. } else if ( pinchScale > 0 ) {
  209. const rearRadius =
  210. ( POINTER_REAR_RADIUS - POINTER_REAR_RADIUS_MIN ) * pinchScale +
  211. POINTER_REAR_RADIUS_MIN;
  212. this._updatePointerVertices( rearRadius );
  213. if ( focusScale < 1 ) {
  214. this.pointerMesh.position.set(
  215. 0,
  216. 0,
  217. - 1 * rearRadius - ( 1 - focusScale ) * POINTER_ADVANCE_MAX
  218. );
  219. this.pointerMesh.material.opacity =
  220. POINTER_OPACITY_MIN +
  221. ( 1 - focusScale ) * ( POINTER_OPACITY_MAX - POINTER_OPACITY_MIN );
  222. } else {
  223. this.pointerMesh.position.set( 0, 0, - 1 * rearRadius );
  224. this.pointerMesh.material.opacity = POINTER_OPACITY_MIN;
  225. }
  226. } else {
  227. this._updatePointerVertices( POINTER_REAR_RADIUS_MIN );
  228. this.pointerMesh.position.set(
  229. 0,
  230. 0,
  231. - 1 * POINTER_REAR_RADIUS_MIN - POINTER_ADVANCE_MAX
  232. );
  233. this.pointerMesh.material.opacity = POINTER_OPACITY_MAX;
  234. }
  235. this.cursorObject.material.opacity = this.pointerMesh.material.opacity;
  236. }
  237. updateMatrixWorld( force ) {
  238. super.updateMatrixWorld( force );
  239. if ( this.pointerGeometry ) {
  240. this._updatePointer();
  241. this._updateRaycaster();
  242. }
  243. }
  244. isPinched() {
  245. return this.pinched;
  246. }
  247. setAttached( attached ) {
  248. this.attached = attached;
  249. }
  250. isAttached() {
  251. return this.attached;
  252. }
  253. intersectObject( object, recursive = true ) {
  254. if ( this.raycaster ) {
  255. return this.raycaster.intersectObject( object, recursive );
  256. }
  257. }
  258. intersectObjects( objects, recursive = true ) {
  259. if ( this.raycaster ) {
  260. return this.raycaster.intersectObjects( objects, recursive );
  261. }
  262. }
  263. checkIntersections( objects, recursive = false ) {
  264. if ( this.raycaster && ! this.attached ) {
  265. const intersections = this.raycaster.intersectObjects( objects, recursive );
  266. const direction = new THREE.Vector3( 0, 0, - 1 );
  267. if ( intersections.length > 0 ) {
  268. const intersection = intersections[ 0 ];
  269. const distance = intersection.distance;
  270. this.cursorObject.position.copy( direction.multiplyScalar( distance ) );
  271. } else {
  272. this.cursorObject.position.copy( direction.multiplyScalar( CURSOR_MAX_DISTANCE ) );
  273. }
  274. }
  275. }
  276. setCursor( distance ) {
  277. const direction = new THREE.Vector3( 0, 0, - 1 );
  278. if ( this.raycaster && ! this.attached ) {
  279. this.cursorObject.position.copy( direction.multiplyScalar( distance ) );
  280. }
  281. }
  282. dispose() {
  283. this._onDisconnected();
  284. this.hand.removeEventListener( 'connected', this._onConnected );
  285. this.hand.removeEventListener( 'disconnected', this._onDisconnected );
  286. }
  287. }
  288. export { OculusHandPointerModel };