OculusHandPointerModel.js 9.2 KB

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