Skip to content

Commit

Permalink
Add support for offerSession (#5410)
Browse files Browse the repository at this point in the history
* Add support for offerSession

* simplify offerSession implementation

* Address review comments

* Address review comments

* handle self.usedOfferSession state properly

* fix build problem

* Address review comment
  • Loading branch information
cabanier authored Dec 12, 2023
1 parent fbc1139 commit fc54fe9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/scene/xr-mode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},

Expand All @@ -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);
}
},

Expand Down
14 changes: 12 additions & 2 deletions src/core/scene/a-scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down

0 comments on commit fc54fe9

Please sign in to comment.