diff --git a/src/components/scene/xr-mode-ui.js b/src/components/scene/xr-mode-ui.js index fe8d168eeb4..d515467a230 100644 --- a/src/components/scene/xr-mode-ui.js +++ b/src/components/scene/xr-mode-ui.js @@ -150,6 +150,7 @@ module.exports.Component = registerComponent('xr-mode-ui', { } else { if (!utils.device.checkVRSupport()) { this.enterVREl.classList.add('fullscreen'); } this.enterVREl.classList.remove(HIDDEN_CLASS); + sceneEl.enterVR(false, true); } }, @@ -161,6 +162,7 @@ module.exports.Component = registerComponent('xr-mode-ui', { this.enterAREl.classList.add(HIDDEN_CLASS); } else { this.enterAREl.classList.remove(HIDDEN_CLASS); + sceneEl.enterVR(true, true); } }, diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index b5e2c116704..00d90a800e2 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -56,6 +56,7 @@ class AScene extends AEntity { self.systems = {}; self.systemNames = []; self.time = self.delta = 0; + self.usedOfferSession = false; self.behaviors = {tick: [], tock: []}; self.hasLoaded = false; @@ -270,13 +271,15 @@ class AScene extends AEntity { * @param {bool?} useAR - if true, try immersive-ar mode * @returns {Promise} */ - enterVR (useAR) { + enterVR (useAR, useOfferSession) { var self = this; var vrDisplay; var vrManager = self.renderer.xr; var xrInit; // Don't enter VR if already in VR. + if (useOfferSession && (!navigator.xr || !navigator.xr.offerSession)) { return Promise.resolve('OfferSession is not supported.'); } + if (self.usedOfferSession && useOfferSession) { return Promise.resolve('OfferSession was already called.'); } if (this.is('vr-mode')) { return Promise.resolve('Already in VR.'); } // Has VR. @@ -294,9 +297,16 @@ class AScene extends AEntity { var xrMode = useAR ? 'immersive-ar' : 'immersive-vr'; xrInit = this.sceneEl.systems.webxr.sessionConfiguration; return new Promise(function (resolve, reject) { - navigator.xr.requestSession(xrMode, xrInit).then( + var requestSession = useOfferSession ? navigator.xr.offerSession.bind(navigator.xr) : navigator.xr.requestSession.bind(navigator.xr); + self.usedOfferSession |= useOfferSession; + requestSession(xrMode, xrInit).then( function requestSuccess (xrSession) { self.xrSession = xrSession; + + if (useOfferSession) { + self.usedOfferSession = false; + } + vrManager.layersEnabled = xrInit.requiredFeatures.indexOf('layers') !== -1; vrManager.setSession(xrSession).then(function () { vrManager.setFoveation(rendererSystem.foveationLevel);