WebXRManager.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. import { ArrayCamera } from '../../cameras/ArrayCamera.js';
  2. import { EventDispatcher } from '../../core/EventDispatcher.js';
  3. import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
  4. import { Vector3 } from '../../math/Vector3.js';
  5. import { Vector4 } from '../../math/Vector4.js';
  6. import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
  7. import { WebXRController } from './WebXRController.js';
  8. class WebXRManager extends EventDispatcher {
  9. constructor( renderer, gl ) {
  10. super();
  11. const scope = this;
  12. const state = renderer.state;
  13. let session = null;
  14. let framebufferScaleFactor = 1.0;
  15. let referenceSpace = null;
  16. let referenceSpaceType = 'local-floor';
  17. let pose = null;
  18. let glBinding = null;
  19. let glFramebuffer = null;
  20. let glProjLayer = null;
  21. let glBaseLayer = null;
  22. let isMultisample = false;
  23. let glMultisampledFramebuffer = null;
  24. let glColorRenderbuffer = null;
  25. let glDepthRenderbuffer = null;
  26. let xrFrame = null;
  27. let depthStyle = null;
  28. let clearStyle = null;
  29. const controllers = [];
  30. const inputSourcesMap = new Map();
  31. //
  32. const cameraL = new PerspectiveCamera();
  33. cameraL.layers.enable( 1 );
  34. cameraL.viewport = new Vector4();
  35. const cameraR = new PerspectiveCamera();
  36. cameraR.layers.enable( 2 );
  37. cameraR.viewport = new Vector4();
  38. const cameras = [ cameraL, cameraR ];
  39. const cameraVR = new ArrayCamera();
  40. cameraVR.layers.enable( 1 );
  41. cameraVR.layers.enable( 2 );
  42. let _currentDepthNear = null;
  43. let _currentDepthFar = null;
  44. //
  45. this.cameraAutoUpdate = true;
  46. this.enabled = false;
  47. this.isPresenting = false;
  48. this.getController = function ( index ) {
  49. let controller = controllers[ index ];
  50. if ( controller === undefined ) {
  51. controller = new WebXRController();
  52. controllers[ index ] = controller;
  53. }
  54. return controller.getTargetRaySpace();
  55. };
  56. this.getControllerGrip = function ( index ) {
  57. let controller = controllers[ index ];
  58. if ( controller === undefined ) {
  59. controller = new WebXRController();
  60. controllers[ index ] = controller;
  61. }
  62. return controller.getGripSpace();
  63. };
  64. this.getHand = function ( index ) {
  65. let controller = controllers[ index ];
  66. if ( controller === undefined ) {
  67. controller = new WebXRController();
  68. controllers[ index ] = controller;
  69. }
  70. return controller.getHandSpace();
  71. };
  72. //
  73. function onSessionEvent( event ) {
  74. const controller = inputSourcesMap.get( event.inputSource );
  75. if ( controller ) {
  76. controller.dispatchEvent( { type: event.type, data: event.inputSource } );
  77. }
  78. }
  79. function onSessionEnd() {
  80. inputSourcesMap.forEach( function ( controller, inputSource ) {
  81. controller.disconnect( inputSource );
  82. } );
  83. inputSourcesMap.clear();
  84. _currentDepthNear = null;
  85. _currentDepthFar = null;
  86. // restore framebuffer/rendering state
  87. state.bindXRFramebuffer( null );
  88. renderer.setRenderTarget( renderer.getRenderTarget() );
  89. if ( glFramebuffer ) gl.deleteFramebuffer( glFramebuffer );
  90. if ( glMultisampledFramebuffer ) gl.deleteFramebuffer( glMultisampledFramebuffer );
  91. if ( glColorRenderbuffer ) gl.deleteRenderbuffer( glColorRenderbuffer );
  92. if ( glDepthRenderbuffer ) gl.deleteRenderbuffer( glDepthRenderbuffer );
  93. glFramebuffer = null;
  94. glMultisampledFramebuffer = null;
  95. glColorRenderbuffer = null;
  96. glDepthRenderbuffer = null;
  97. glBaseLayer = null;
  98. glProjLayer = null;
  99. glBinding = null;
  100. session = null;
  101. //
  102. animation.stop();
  103. scope.isPresenting = false;
  104. scope.dispatchEvent( { type: 'sessionend' } );
  105. }
  106. this.setFramebufferScaleFactor = function ( value ) {
  107. framebufferScaleFactor = value;
  108. if ( scope.isPresenting === true ) {
  109. console.warn( 'THREE.WebXRManager: Cannot change framebuffer scale while presenting.' );
  110. }
  111. };
  112. this.setReferenceSpaceType = function ( value ) {
  113. referenceSpaceType = value;
  114. if ( scope.isPresenting === true ) {
  115. console.warn( 'THREE.WebXRManager: Cannot change reference space type while presenting.' );
  116. }
  117. };
  118. this.getReferenceSpace = function () {
  119. return referenceSpace;
  120. };
  121. this.getBaseLayer = function () {
  122. return glProjLayer !== null ? glProjLayer : glBaseLayer;
  123. };
  124. this.getBinding = function () {
  125. return glBinding;
  126. };
  127. this.getFrame = function () {
  128. return xrFrame;
  129. };
  130. this.getSession = function () {
  131. return session;
  132. };
  133. this.setSession = async function ( value ) {
  134. session = value;
  135. if ( session !== null ) {
  136. session.addEventListener( 'select', onSessionEvent );
  137. session.addEventListener( 'selectstart', onSessionEvent );
  138. session.addEventListener( 'selectend', onSessionEvent );
  139. session.addEventListener( 'squeeze', onSessionEvent );
  140. session.addEventListener( 'squeezestart', onSessionEvent );
  141. session.addEventListener( 'squeezeend', onSessionEvent );
  142. session.addEventListener( 'end', onSessionEnd );
  143. session.addEventListener( 'inputsourceschange', onInputSourcesChange );
  144. const attributes = gl.getContextAttributes();
  145. if ( attributes.xrCompatible !== true ) {
  146. await gl.makeXRCompatible();
  147. }
  148. if ( session.renderState.layers === undefined ) {
  149. const layerInit = {
  150. antialias: attributes.antialias,
  151. alpha: attributes.alpha,
  152. depth: attributes.depth,
  153. stencil: attributes.stencil,
  154. framebufferScaleFactor: framebufferScaleFactor
  155. };
  156. glBaseLayer = new XRWebGLLayer( session, gl, layerInit );
  157. session.updateRenderState( { baseLayer: glBaseLayer } );
  158. } else if ( gl instanceof WebGLRenderingContext ) {
  159. // Use old style webgl layer because we can't use MSAA
  160. // WebGL2 support.
  161. const layerInit = {
  162. antialias: true,
  163. alpha: attributes.alpha,
  164. depth: attributes.depth,
  165. stencil: attributes.stencil,
  166. framebufferScaleFactor: framebufferScaleFactor
  167. };
  168. glBaseLayer = new XRWebGLLayer( session, gl, layerInit );
  169. session.updateRenderState( { layers: [ glBaseLayer ] } );
  170. } else {
  171. isMultisample = attributes.antialias;
  172. let depthFormat = null;
  173. if ( attributes.depth ) {
  174. clearStyle = gl.DEPTH_BUFFER_BIT;
  175. if ( attributes.stencil ) clearStyle |= gl.STENCIL_BUFFER_BIT;
  176. depthStyle = attributes.stencil ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
  177. depthFormat = attributes.stencil ? gl.DEPTH24_STENCIL8 : gl.DEPTH_COMPONENT24;
  178. }
  179. const projectionlayerInit = {
  180. colorFormat: attributes.alpha ? gl.RGBA8 : gl.RGB8,
  181. depthFormat: depthFormat,
  182. scaleFactor: framebufferScaleFactor
  183. };
  184. glBinding = new XRWebGLBinding( session, gl );
  185. glProjLayer = glBinding.createProjectionLayer( projectionlayerInit );
  186. glFramebuffer = gl.createFramebuffer();
  187. session.updateRenderState( { layers: [ glProjLayer ] } );
  188. if ( isMultisample ) {
  189. glMultisampledFramebuffer = gl.createFramebuffer();
  190. glColorRenderbuffer = gl.createRenderbuffer();
  191. gl.bindRenderbuffer( gl.RENDERBUFFER, glColorRenderbuffer );
  192. gl.renderbufferStorageMultisample(
  193. gl.RENDERBUFFER,
  194. 4,
  195. gl.RGBA8,
  196. glProjLayer.textureWidth,
  197. glProjLayer.textureHeight );
  198. state.bindFramebuffer( gl.FRAMEBUFFER, glMultisampledFramebuffer );
  199. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, glColorRenderbuffer );
  200. gl.bindRenderbuffer( gl.RENDERBUFFER, null );
  201. if ( depthFormat !== null ) {
  202. glDepthRenderbuffer = gl.createRenderbuffer();
  203. gl.bindRenderbuffer( gl.RENDERBUFFER, glDepthRenderbuffer );
  204. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, 4, depthFormat, glProjLayer.textureWidth, glProjLayer.textureHeight );
  205. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, depthStyle, gl.RENDERBUFFER, glDepthRenderbuffer );
  206. gl.bindRenderbuffer( gl.RENDERBUFFER, null );
  207. }
  208. state.bindFramebuffer( gl.FRAMEBUFFER, null );
  209. }
  210. }
  211. referenceSpace = await session.requestReferenceSpace( referenceSpaceType );
  212. animation.setContext( session );
  213. animation.start();
  214. scope.isPresenting = true;
  215. scope.dispatchEvent( { type: 'sessionstart' } );
  216. }
  217. };
  218. function onInputSourcesChange( event ) {
  219. const inputSources = session.inputSources;
  220. // Assign inputSources to available controllers
  221. for ( let i = 0; i < controllers.length; i ++ ) {
  222. inputSourcesMap.set( inputSources[ i ], controllers[ i ] );
  223. }
  224. // Notify disconnected
  225. for ( let i = 0; i < event.removed.length; i ++ ) {
  226. const inputSource = event.removed[ i ];
  227. const controller = inputSourcesMap.get( inputSource );
  228. if ( controller ) {
  229. controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
  230. inputSourcesMap.delete( inputSource );
  231. }
  232. }
  233. // Notify connected
  234. for ( let i = 0; i < event.added.length; i ++ ) {
  235. const inputSource = event.added[ i ];
  236. const controller = inputSourcesMap.get( inputSource );
  237. if ( controller ) {
  238. controller.dispatchEvent( { type: 'connected', data: inputSource } );
  239. }
  240. }
  241. }
  242. //
  243. const cameraLPos = new Vector3();
  244. const cameraRPos = new Vector3();
  245. /**
  246. * Assumes 2 cameras that are parallel and share an X-axis, and that
  247. * the cameras' projection and world matrices have already been set.
  248. * And that near and far planes are identical for both cameras.
  249. * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
  250. */
  251. function setProjectionFromUnion( camera, cameraL, cameraR ) {
  252. cameraLPos.setFromMatrixPosition( cameraL.matrixWorld );
  253. cameraRPos.setFromMatrixPosition( cameraR.matrixWorld );
  254. const ipd = cameraLPos.distanceTo( cameraRPos );
  255. const projL = cameraL.projectionMatrix.elements;
  256. const projR = cameraR.projectionMatrix.elements;
  257. // VR systems will have identical far and near planes, and
  258. // most likely identical top and bottom frustum extents.
  259. // Use the left camera for these values.
  260. const near = projL[ 14 ] / ( projL[ 10 ] - 1 );
  261. const far = projL[ 14 ] / ( projL[ 10 ] + 1 );
  262. const topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
  263. const bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
  264. const leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
  265. const rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
  266. const left = near * leftFov;
  267. const right = near * rightFov;
  268. // Calculate the new camera's position offset from the
  269. // left camera. xOffset should be roughly half `ipd`.
  270. const zOffset = ipd / ( - leftFov + rightFov );
  271. const xOffset = zOffset * - leftFov;
  272. // TODO: Better way to apply this offset?
  273. cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
  274. camera.translateX( xOffset );
  275. camera.translateZ( zOffset );
  276. camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale );
  277. camera.matrixWorldInverse.copy( camera.matrixWorld ).invert();
  278. // Find the union of the frustum values of the cameras and scale
  279. // the values so that the near plane's position does not change in world space,
  280. // although must now be relative to the new union camera.
  281. const near2 = near + zOffset;
  282. const far2 = far + zOffset;
  283. const left2 = left - xOffset;
  284. const right2 = right + ( ipd - xOffset );
  285. const top2 = topFov * far / far2 * near2;
  286. const bottom2 = bottomFov * far / far2 * near2;
  287. camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 );
  288. }
  289. function updateCamera( camera, parent ) {
  290. if ( parent === null ) {
  291. camera.matrixWorld.copy( camera.matrix );
  292. } else {
  293. camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
  294. }
  295. camera.matrixWorldInverse.copy( camera.matrixWorld ).invert();
  296. }
  297. this.updateCamera = function ( camera ) {
  298. if ( session === null ) return;
  299. cameraVR.near = cameraR.near = cameraL.near = camera.near;
  300. cameraVR.far = cameraR.far = cameraL.far = camera.far;
  301. if ( _currentDepthNear !== cameraVR.near || _currentDepthFar !== cameraVR.far ) {
  302. // Note that the new renderState won't apply until the next frame. See #18320
  303. session.updateRenderState( {
  304. depthNear: cameraVR.near,
  305. depthFar: cameraVR.far
  306. } );
  307. _currentDepthNear = cameraVR.near;
  308. _currentDepthFar = cameraVR.far;
  309. }
  310. const parent = camera.parent;
  311. const cameras = cameraVR.cameras;
  312. updateCamera( cameraVR, parent );
  313. for ( let i = 0; i < cameras.length; i ++ ) {
  314. updateCamera( cameras[ i ], parent );
  315. }
  316. cameraVR.matrixWorld.decompose( cameraVR.position, cameraVR.quaternion, cameraVR.scale );
  317. // update user camera and its children
  318. camera.position.copy( cameraVR.position );
  319. camera.quaternion.copy( cameraVR.quaternion );
  320. camera.scale.copy( cameraVR.scale );
  321. camera.matrix.copy( cameraVR.matrix );
  322. camera.matrixWorld.copy( cameraVR.matrixWorld );
  323. const children = camera.children;
  324. for ( let i = 0, l = children.length; i < l; i ++ ) {
  325. children[ i ].updateMatrixWorld( true );
  326. }
  327. // update projection matrix for proper view frustum culling
  328. if ( cameras.length === 2 ) {
  329. setProjectionFromUnion( cameraVR, cameraL, cameraR );
  330. } else {
  331. // assume single camera setup (AR)
  332. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  333. }
  334. };
  335. this.getCamera = function () {
  336. return cameraVR;
  337. };
  338. this.getFoveation = function () {
  339. if ( glProjLayer !== null ) {
  340. return glProjLayer.fixedFoveation;
  341. }
  342. if ( glBaseLayer !== null ) {
  343. return glBaseLayer.fixedFoveation;
  344. }
  345. return undefined;
  346. };
  347. this.setFoveation = function ( foveation ) {
  348. // 0 = no foveation = full resolution
  349. // 1 = maximum foveation = the edges render at lower resolution
  350. if ( glProjLayer !== null ) {
  351. glProjLayer.fixedFoveation = foveation;
  352. }
  353. if ( glBaseLayer !== null && glBaseLayer.fixedFoveation !== undefined ) {
  354. glBaseLayer.fixedFoveation = foveation;
  355. }
  356. };
  357. // Animation Loop
  358. let onAnimationFrameCallback = null;
  359. function onAnimationFrame( time, frame ) {
  360. pose = frame.getViewerPose( referenceSpace );
  361. xrFrame = frame;
  362. if ( pose !== null ) {
  363. const views = pose.views;
  364. if ( glBaseLayer !== null ) {
  365. state.bindXRFramebuffer( glBaseLayer.framebuffer );
  366. }
  367. let cameraVRNeedsUpdate = false;
  368. // check if it's necessary to rebuild cameraVR's camera list
  369. if ( views.length !== cameraVR.cameras.length ) {
  370. cameraVR.cameras.length = 0;
  371. cameraVRNeedsUpdate = true;
  372. }
  373. for ( let i = 0; i < views.length; i ++ ) {
  374. const view = views[ i ];
  375. let viewport = null;
  376. if ( glBaseLayer !== null ) {
  377. viewport = glBaseLayer.getViewport( view );
  378. } else {
  379. const glSubImage = glBinding.getViewSubImage( glProjLayer, view );
  380. state.bindXRFramebuffer( glFramebuffer );
  381. if ( glSubImage.depthStencilTexture !== undefined ) {
  382. gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, glSubImage.depthStencilTexture, 0 );
  383. }
  384. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glSubImage.colorTexture, 0 );
  385. viewport = glSubImage.viewport;
  386. }
  387. const camera = cameras[ i ];
  388. camera.matrix.fromArray( view.transform.matrix );
  389. camera.projectionMatrix.fromArray( view.projectionMatrix );
  390. camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
  391. if ( i === 0 ) {
  392. cameraVR.matrix.copy( camera.matrix );
  393. }
  394. if ( cameraVRNeedsUpdate === true ) {
  395. cameraVR.cameras.push( camera );
  396. }
  397. }
  398. if ( isMultisample ) {
  399. state.bindXRFramebuffer( glMultisampledFramebuffer );
  400. if ( clearStyle !== null ) gl.clear( clearStyle );
  401. }
  402. }
  403. //
  404. const inputSources = session.inputSources;
  405. for ( let i = 0; i < controllers.length; i ++ ) {
  406. const controller = controllers[ i ];
  407. const inputSource = inputSources[ i ];
  408. controller.update( inputSource, frame, referenceSpace );
  409. }
  410. if ( onAnimationFrameCallback ) onAnimationFrameCallback( time, frame );
  411. if ( isMultisample ) {
  412. const width = glProjLayer.textureWidth;
  413. const height = glProjLayer.textureHeight;
  414. state.bindFramebuffer( gl.READ_FRAMEBUFFER, glMultisampledFramebuffer );
  415. state.bindFramebuffer( gl.DRAW_FRAMEBUFFER, glFramebuffer );
  416. // Invalidate the depth here to avoid flush of the depth data to main memory.
  417. gl.invalidateFramebuffer( gl.READ_FRAMEBUFFER, [ depthStyle ] );
  418. gl.invalidateFramebuffer( gl.DRAW_FRAMEBUFFER, [ depthStyle ] );
  419. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, gl.COLOR_BUFFER_BIT, gl.NEAREST );
  420. // Invalidate the MSAA buffer because it's not needed anymore.
  421. gl.invalidateFramebuffer( gl.READ_FRAMEBUFFER, [ gl.COLOR_ATTACHMENT0 ] );
  422. state.bindFramebuffer( gl.READ_FRAMEBUFFER, null );
  423. state.bindFramebuffer( gl.DRAW_FRAMEBUFFER, null );
  424. state.bindFramebuffer( gl.FRAMEBUFFER, glMultisampledFramebuffer );
  425. }
  426. xrFrame = null;
  427. }
  428. const animation = new WebGLAnimation();
  429. animation.setAnimationLoop( onAnimationFrame );
  430. this.setAnimationLoop = function ( callback ) {
  431. onAnimationFrameCallback = callback;
  432. };
  433. this.dispose = function () {};
  434. }
  435. }
  436. export { WebXRManager };