WebXRManager.js 18 KB

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