WebXRManager.js 10 KB

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