OculusHandPointerModel.js 9.2 KB

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