From ac299772fdb43163ededcacc789948baf588cbae Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Mon, 11 Dec 2023 13:52:14 -0800 Subject: [PATCH 1/7] Add support for offerSession --- src/components/scene/xr-mode-ui.js | 2 ++ src/core/scene/a-scene.js | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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..6de11c102c8 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -270,13 +270,15 @@ class AScene extends AEntity { * @param {bool?} useAR - if true, try immersive-ar mode * @returns {Promise} */ - enterVR (useAR) { + enterVR (useAR, useOfferSession = false) { var self = this; var vrDisplay; var vrManager = self.renderer.xr; var xrInit; // Don't enter VR if already in VR. + if (navigator.xr.offerSession === undefined) { return Promise.resolve('OfferSession is not supported.'); } + if (this.is('has-offer-session')) { return Promise.resolve('Already has offerSession.'); } if (this.is('vr-mode')) { return Promise.resolve('Already in VR.'); } // Has VR. @@ -294,9 +296,15 @@ 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 requestFunction = useOfferSession ? navigator.xr.offerSession.bind(navigator.xr) : navigator.xr.requestSession.bind(navigator.xr); + if (useOfferSession) { + self.addState('has-offer-session'); + } + requestFunction(xrMode, xrInit).then( function requestSuccess (xrSession) { self.xrSession = xrSession; + self.removeState('has-offer-session'); + vrManager.layersEnabled = xrInit.requiredFeatures.indexOf('layers') !== -1; vrManager.setSession(xrSession).then(function () { vrManager.setFoveation(rendererSystem.foveationLevel); @@ -306,6 +314,7 @@ class AScene extends AEntity { enterVRSuccess(resolve); }, function requestFail (error) { + self.removeState('has-offer-session'); var useAR = xrMode === 'immersive-ar'; var mode = useAR ? 'AR' : 'VR'; reject(new Error('Failed to enter ' + mode + ' mode (`requestSession`)', { cause: error })); From e173bb9fc6dc090212c319b296f4d1d549359db5 Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Mon, 11 Dec 2023 15:47:15 -0800 Subject: [PATCH 2/7] simplify offerSession implementation --- src/components/scene/xr-mode-ui.js | 2 -- src/core/scene/a-scene.js | 6 ------ 2 files changed, 8 deletions(-) diff --git a/src/components/scene/xr-mode-ui.js b/src/components/scene/xr-mode-ui.js index d515467a230..2a274fa955c 100644 --- a/src/components/scene/xr-mode-ui.js +++ b/src/components/scene/xr-mode-ui.js @@ -120,8 +120,6 @@ module.exports.Component = registerComponent('xr-mode-ui', { this.orientationModalEl = createOrientationModal(this.onModalClick); sceneEl.appendChild(this.orientationModalEl); - - this.updateEnterInterfaces(); }, remove: function () { diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index 6de11c102c8..154a89d7153 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -278,7 +278,6 @@ class AScene extends AEntity { // Don't enter VR if already in VR. if (navigator.xr.offerSession === undefined) { return Promise.resolve('OfferSession is not supported.'); } - if (this.is('has-offer-session')) { return Promise.resolve('Already has offerSession.'); } if (this.is('vr-mode')) { return Promise.resolve('Already in VR.'); } // Has VR. @@ -297,13 +296,9 @@ class AScene extends AEntity { xrInit = this.sceneEl.systems.webxr.sessionConfiguration; return new Promise(function (resolve, reject) { var requestFunction = useOfferSession ? navigator.xr.offerSession.bind(navigator.xr) : navigator.xr.requestSession.bind(navigator.xr); - if (useOfferSession) { - self.addState('has-offer-session'); - } requestFunction(xrMode, xrInit).then( function requestSuccess (xrSession) { self.xrSession = xrSession; - self.removeState('has-offer-session'); vrManager.layersEnabled = xrInit.requiredFeatures.indexOf('layers') !== -1; vrManager.setSession(xrSession).then(function () { @@ -314,7 +309,6 @@ class AScene extends AEntity { enterVRSuccess(resolve); }, function requestFail (error) { - self.removeState('has-offer-session'); var useAR = xrMode === 'immersive-ar'; var mode = useAR ? 'AR' : 'VR'; reject(new Error('Failed to enter ' + mode + ' mode (`requestSession`)', { cause: error })); From a03d112296355a2b60e03c6159bd32398cd912a5 Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Mon, 11 Dec 2023 16:30:04 -0800 Subject: [PATCH 3/7] Address review comments --- src/core/scene/a-scene.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index 154a89d7153..41a467708ef 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,14 +271,15 @@ class AScene extends AEntity { * @param {bool?} useAR - if true, try immersive-ar mode * @returns {Promise} */ - enterVR (useAR, useOfferSession = false) { + enterVR (useAR, useOfferSession) { var self = this; var vrDisplay; var vrManager = self.renderer.xr; var xrInit; // Don't enter VR if already in VR. - if (navigator.xr.offerSession === undefined) { return Promise.resolve('OfferSession is not supported.'); } + if (useOfferSession && navigator.xr.offerSession === undefined) { return Promise.resolve('OfferSession is not supported.'); } + if (self.usedOfferSession) { return Promise.resolve('OfferSession was already called.'); } if (this.is('vr-mode')) { return Promise.resolve('Already in VR.'); } // Has VR. @@ -296,9 +298,11 @@ class AScene extends AEntity { xrInit = this.sceneEl.systems.webxr.sessionConfiguration; return new Promise(function (resolve, reject) { var requestFunction = useOfferSession ? navigator.xr.offerSession.bind(navigator.xr) : navigator.xr.requestSession.bind(navigator.xr); + self.usedOfferSession |= useOfferSession; requestFunction(xrMode, xrInit).then( function requestSuccess (xrSession) { self.xrSession = xrSession; + self.usedOfferSession = false; vrManager.layersEnabled = xrInit.requiredFeatures.indexOf('layers') !== -1; vrManager.setSession(xrSession).then(function () { From 94488f7ceb1613c3a4f805c865c5f8842b73116c Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Tue, 12 Dec 2023 00:42:30 +0000 Subject: [PATCH 4/7] Address review comments --- src/components/scene/xr-mode-ui.js | 2 ++ src/core/scene/a-scene.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/scene/xr-mode-ui.js b/src/components/scene/xr-mode-ui.js index 2a274fa955c..d515467a230 100644 --- a/src/components/scene/xr-mode-ui.js +++ b/src/components/scene/xr-mode-ui.js @@ -120,6 +120,8 @@ module.exports.Component = registerComponent('xr-mode-ui', { this.orientationModalEl = createOrientationModal(this.onModalClick); sceneEl.appendChild(this.orientationModalEl); + + this.updateEnterInterfaces(); }, remove: function () { diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index 41a467708ef..af45cb19109 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -297,9 +297,9 @@ class AScene extends AEntity { var xrMode = useAR ? 'immersive-ar' : 'immersive-vr'; xrInit = this.sceneEl.systems.webxr.sessionConfiguration; return new Promise(function (resolve, reject) { - var requestFunction = useOfferSession ? navigator.xr.offerSession.bind(navigator.xr) : navigator.xr.requestSession.bind(navigator.xr); + var requestSession = useOfferSession ? navigator.xr.offerSession.bind(navigator.xr) : navigator.xr.requestSession.bind(navigator.xr); self.usedOfferSession |= useOfferSession; - requestFunction(xrMode, xrInit).then( + requestSession(xrMode, xrInit).then( function requestSuccess (xrSession) { self.xrSession = xrSession; self.usedOfferSession = false; From d1a057fe55b211050161304f5db03dab0f951528 Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Tue, 12 Dec 2023 00:50:15 +0000 Subject: [PATCH 5/7] handle self.usedOfferSession state properly --- src/core/scene/a-scene.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index af45cb19109..6494926a36e 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -279,7 +279,7 @@ class AScene extends AEntity { // Don't enter VR if already in VR. if (useOfferSession && navigator.xr.offerSession === undefined) { return Promise.resolve('OfferSession is not supported.'); } - if (self.usedOfferSession) { return Promise.resolve('OfferSession was already called.'); } + if (self.usedOfferSession && useOfferSession) { return Promise.resolve('OfferSession was already called.'); } if (this.is('vr-mode')) { return Promise.resolve('Already in VR.'); } // Has VR. @@ -302,7 +302,10 @@ class AScene extends AEntity { requestSession(xrMode, xrInit).then( function requestSuccess (xrSession) { self.xrSession = xrSession; - self.usedOfferSession = false; + + if (useOfferSession) { + self.usedOfferSession = false; + } vrManager.layersEnabled = xrInit.requiredFeatures.indexOf('layers') !== -1; vrManager.setSession(xrSession).then(function () { From e44bd2b016b2e258bb76d684f9bd22b3067481cf Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Tue, 12 Dec 2023 01:15:29 +0000 Subject: [PATCH 6/7] fix build problem --- src/core/scene/a-scene.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index 6494926a36e..186ad493bb7 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -278,7 +278,7 @@ class AScene extends AEntity { var xrInit; // Don't enter VR if already in VR. - if (useOfferSession && navigator.xr.offerSession === undefined) { return Promise.resolve('OfferSession is not supported.'); } + if (useOfferSession && (navigator.xr === undefined || navigator.xr.offerSession === undefined)) { 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.'); } From 4fd2c88b4a1433b77d483a27fab4b216bb8cfa41 Mon Sep 17 00:00:00 2001 From: Rik Cabanier Date: Tue, 12 Dec 2023 01:28:23 +0000 Subject: [PATCH 7/7] Address review comment --- src/core/scene/a-scene.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/scene/a-scene.js b/src/core/scene/a-scene.js index 186ad493bb7..00d90a800e2 100644 --- a/src/core/scene/a-scene.js +++ b/src/core/scene/a-scene.js @@ -278,7 +278,7 @@ class AScene extends AEntity { var xrInit; // Don't enter VR if already in VR. - if (useOfferSession && (navigator.xr === undefined || navigator.xr.offerSession === undefined)) { return Promise.resolve('OfferSession is not supported.'); } + 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.'); }