WebXRManager.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { ArrayCamera } from '../../cameras/ArrayCamera.js';
  5. import { EventDispatcher } from '../../core/EventDispatcher.js';
  6. import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
  7. import { Vector3 } from '../../math/Vector3.js';
  8. import { Vector4 } from '../../math/Vector4.js';
  9. import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
  10. import { WebXRController } from './WebXRController.js';
  11. function WebXRManager( renderer, gl ) {
  12. var scope = this;
  13. var session = null;
  14. var framebufferScaleFactor = 1.0;
  15. var referenceSpace = null;
  16. var referenceSpaceType = 'local-floor';
  17. var pose = null;
  18. var controllers = [];
  19. var inputSourcesMap = new Map();
  20. //
  21. var cameraL = new PerspectiveCamera();
  22. cameraL.layers.enable( 1 );
  23. cameraL.viewport = new Vector4();
  24. var cameraR = new PerspectiveCamera();
  25. cameraR.layers.enable( 2 );
  26. cameraR.viewport = new Vector4();
  27. var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
  28. cameraVR.layers.enable( 1 );
  29. cameraVR.layers.enable( 2 );
  30. var _currentDepthNear = null;
  31. var _currentDepthFar = null;
  32. //
  33. this.enabled = false;
  34. this.isPresenting = false;
  35. this.getController = function ( index ) {
  36. var controller = controllers[ index ];
  37. if ( controller === undefined ) {
  38. controller = new WebXRController();
  39. controllers[ index ] = controller;
  40. }
  41. return controller.getTargetRaySpace();
  42. };
  43. this.getControllerGrip = function ( index ) {
  44. var controller = controllers[ index ];
  45. if ( controller === undefined ) {
  46. controller = new WebXRController();
  47. controllers[ index ] = controller;
  48. }
  49. return controller.getGripSpace();
  50. };
  51. //
  52. function onSessionEvent( event ) {
  53. var controller = inputSourcesMap.get( event.inputSource );
  54. if ( controller ) {
  55. controller.dispatchEvent( { type: event.type } );
  56. }
  57. }
  58. function onSessionEnd() {
  59. inputSourcesMap.forEach( function ( controller, inputSource ) {
  60. controller.disconnect( inputSource );
  61. } );
  62. inputSourcesMap.clear();
  63. //
  64. renderer.setFramebuffer( null );
  65. renderer.setRenderTarget( renderer.getRenderTarget() ); // Hack #15830
  66. animation.stop();
  67. scope.isPresenting = false;
  68. scope.dispatchEvent( { type: 'sessionend' } );
  69. }
  70. function onRequestReferenceSpace( value ) {
  71. referenceSpace = value;
  72. animation.setContext( session );
  73. animation.start();
  74. scope.isPresenting = true;
  75. scope.dispatchEvent( { type: 'sessionstart' } );
  76. }
  77. this.setFramebufferScaleFactor = function ( value ) {
  78. framebufferScaleFactor = value;
  79. if ( scope.isPresenting === true ) {
  80. console.warn( 'THREE.WebXRManager: Cannot change framebuffer scale while presenting XR content.' );
  81. }
  82. };
  83. this.setReferenceSpaceType = function ( value ) {
  84. referenceSpaceType = value;
  85. if ( scope.isPresenting === true ) {
  86. console.warn( 'THREE.WebXRManager: Cannot change reference space type while presenting XR content.' );
  87. }
  88. };
  89. this.getReferenceSpace = function () {
  90. return referenceSpace;
  91. };
  92. this.getSession = function () {
  93. return session;
  94. };
  95. this.setSession = function ( value ) {
  96. session = value;
  97. if ( session !== null ) {
  98. session.addEventListener( 'select', onSessionEvent );
  99. session.addEventListener( 'selectstart', onSessionEvent );
  100. session.addEventListener( 'selectend', onSessionEvent );
  101. session.addEventListener( 'squeeze', onSessionEvent );
  102. session.addEventListener( 'squeezestart', onSessionEvent );
  103. session.addEventListener( 'squeezeend', onSessionEvent );
  104. session.addEventListener( 'end', onSessionEnd );
  105. var attributes = gl.getContextAttributes();
  106. var layerInit = {
  107. antialias: attributes.antialias,
  108. alpha: attributes.alpha,
  109. depth: attributes.depth,
  110. stencil: attributes.stencil,
  111. framebufferScaleFactor: framebufferScaleFactor
  112. };
  113. // eslint-disable-next-line no-undef
  114. var baseLayer = new XRWebGLLayer( session, gl, layerInit );
  115. session.updateRenderState( { baseLayer: baseLayer } );
  116. session.requestReferenceSpace( referenceSpaceType ).then( onRequestReferenceSpace );
  117. //
  118. session.addEventListener( 'inputsourceschange', updateInputSources );
  119. }
  120. };
  121. function updateInputSources( event ) {
  122. var inputSources = session.inputSources;
  123. // Assign inputSources to available controllers
  124. for ( var i = 0; i < controllers.length; i ++ ) {
  125. inputSourcesMap.set( inputSources[ i ], controllers[ i ] );
  126. }
  127. // Notify disconnected
  128. for ( var i = 0; i < event.removed.length; i ++ ) {
  129. var inputSource = event.removed[ i ];
  130. var controller = inputSourcesMap.get( inputSource );
  131. if ( controller ) {
  132. controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
  133. inputSourcesMap.delete( inputSource );
  134. }
  135. }
  136. // Notify connected
  137. for ( var i = 0; i < event.added.length; i ++ ) {
  138. var inputSource = event.added[ i ];
  139. var controller = inputSourcesMap.get( inputSource );
  140. if ( controller ) {
  141. controller.dispatchEvent( { type: 'connected', data: inputSource } );
  142. }
  143. }
  144. }
  145. //
  146. var cameraLPos = new Vector3();
  147. var cameraRPos = new Vector3();
  148. /**
  149. * @author jsantell / https://www.jsantell.com/
  150. *
  151. * Assumes 2 cameras that are parallel and share an X-axis, and that
  152. * the cameras' projection and world matrices have already been set.
  153. * And that near and far planes are identical for both cameras.
  154. * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
  155. */
  156. function setProjectionFromUnion( camera, cameraL, cameraR ) {
  157. cameraLPos.setFromMatrixPosition( cameraL.matrixWorld );
  158. cameraRPos.setFromMatrixPosition( cameraR.matrixWorld );
  159. var ipd = cameraLPos.distanceTo( cameraRPos );
  160. var projL = cameraL.projectionMatrix.elements;
  161. var projR = cameraR.projectionMatrix.elements;
  162. // VR systems will have identical far and near planes, and
  163. // most likely identical top and bottom frustum extents.
  164. // Use the left camera for these values.
  165. var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
  166. var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
  167. var topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
  168. var bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
  169. var leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
  170. var rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
  171. var left = near * leftFov;
  172. var right = near * rightFov;
  173. // Calculate the new camera's position offset from the
  174. // left camera. xOffset should be roughly half `ipd`.
  175. var zOffset = ipd / ( - leftFov + rightFov );
  176. var xOffset = zOffset * - leftFov;
  177. // TODO: Better way to apply this offset?
  178. cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
  179. camera.translateX( xOffset );
  180. camera.translateZ( zOffset );
  181. camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale );
  182. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  183. // Find the union of the frustum values of the cameras and scale
  184. // the values so that the near plane's position does not change in world space,
  185. // although must now be relative to the new union camera.
  186. var near2 = near + zOffset;
  187. var far2 = far + zOffset;
  188. var left2 = left - xOffset;
  189. var right2 = right + ( ipd - xOffset );
  190. var top2 = topFov * far / far2 * near2;
  191. var bottom2 = bottomFov * far / far2 * near2;
  192. camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 );
  193. }
  194. function updateCamera( camera, parent ) {
  195. if ( parent === null ) {
  196. camera.matrixWorld.copy( camera.matrix );
  197. } else {
  198. camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
  199. }
  200. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  201. }
  202. this.getCamera = function ( camera ) {
  203. cameraVR.near = cameraR.near = cameraL.near = camera.near;
  204. cameraVR.far = cameraR.far = cameraL.far = camera.far;
  205. if ( _currentDepthNear !== cameraVR.near || _currentDepthFar !== cameraVR.far ) {
  206. // Note that the new renderState won't apply until the next frame. See #18320
  207. session.updateRenderState( {
  208. depthNear: cameraVR.near,
  209. depthFar: cameraVR.far
  210. } );
  211. _currentDepthNear = cameraVR.near;
  212. _currentDepthFar = cameraVR.far;
  213. }
  214. var parent = camera.parent;
  215. var cameras = cameraVR.cameras;
  216. updateCamera( cameraVR, parent );
  217. for ( var i = 0; i < cameras.length; i ++ ) {
  218. updateCamera( cameras[ i ], parent );
  219. }
  220. // update camera and its children
  221. camera.matrixWorld.copy( cameraVR.matrixWorld );
  222. var children = camera.children;
  223. for ( var i = 0, l = children.length; i < l; i ++ ) {
  224. children[ i ].updateMatrixWorld( true );
  225. }
  226. setProjectionFromUnion( cameraVR, cameraL, cameraR );
  227. return cameraVR;
  228. };
  229. // Animation Loop
  230. var onAnimationFrameCallback = null;
  231. function onAnimationFrame( time, frame ) {
  232. pose = frame.getViewerPose( referenceSpace );
  233. if ( pose !== null ) {
  234. var views = pose.views;
  235. var baseLayer = session.renderState.baseLayer;
  236. renderer.setFramebuffer( baseLayer.framebuffer );
  237. for ( var i = 0; i < views.length; i ++ ) {
  238. var view = views[ i ];
  239. var viewport = baseLayer.getViewport( view );
  240. var camera = cameraVR.cameras[ i ];
  241. camera.matrix.fromArray( view.transform.matrix );
  242. camera.projectionMatrix.fromArray( view.projectionMatrix );
  243. camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
  244. if ( i === 0 ) {
  245. cameraVR.matrix.copy( camera.matrix );
  246. }
  247. }
  248. }
  249. //
  250. var inputSources = session.inputSources;
  251. for ( var i = 0; i < controllers.length; i ++ ) {
  252. var controller = controllers[ i ];
  253. var inputSource = inputSources[ i ];
  254. controller.update( inputSource, frame, referenceSpace );
  255. }
  256. if ( onAnimationFrameCallback ) onAnimationFrameCallback( time, frame );
  257. }
  258. var animation = new WebGLAnimation();
  259. animation.setAnimationLoop( onAnimationFrame );
  260. this.setAnimationLoop = function ( callback ) {
  261. onAnimationFrameCallback = callback;
  262. };
  263. this.dispose = function () {};
  264. }
  265. Object.assign( WebXRManager.prototype, EventDispatcher.prototype );
  266. export { WebXRManager };