WebXRManager.js 16 KB

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