Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebXR XRSession.enabledFeatures missing workaround #6181

Merged
merged 4 commits into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/src/examples/xr/ar-anchors-persistence/example.mjs
Expand Up @@ -127,8 +127,8 @@ if (app.xr.supported) {
}
});

app.xr.on('start', function () {
message('Immersive AR session has started');
app.xr.anchors.on('available', () => {
message('Anchors became available');

// restore all persistent anchors
if (app.xr.anchors.persistence) {
Expand All @@ -138,6 +138,10 @@ if (app.xr.supported) {
}
}
});

app.xr.on('start', function () {
message('Immersive AR session has started');
});
app.xr.on('end', function () {
message('Immersive AR session has ended');
});
Expand Down
8 changes: 5 additions & 3 deletions examples/src/examples/xr/ar-hit-test-anchors/example.mjs
Expand Up @@ -133,9 +133,7 @@ if (app.xr.supported) {
}
});

app.xr.on('start', function () {
message('Immersive AR session has started');

app.xr.hitTest.on('available', () => {
if (!app.xr.hitTest.supported || !app.xr.anchors.supported) return;

// provide gaze-like way to create anchors
Expand Down Expand Up @@ -166,6 +164,10 @@ if (app.xr.supported) {
createAnchor(lastHitTestResult);
});
});

app.xr.on('start', function () {
message('Immersive AR session has started');
});
app.xr.on('end', function () {
message('Immersive AR session has ended');
});
Expand Down
10 changes: 5 additions & 5 deletions examples/src/examples/xr/ar-hit-test/example.mjs
Expand Up @@ -107,11 +107,7 @@ if (app.xr.supported) {
}
});

app.xr.on('start', function () {
message('Immersive AR session has started');

if (!app.xr.hitTest.supported) return;

app.xr.hitTest.on('available', () => {
app.xr.hitTest.start({
entityTypes: [pc.XRTRACKABLE_POINT, pc.XRTRACKABLE_PLANE],
callback: function (err, hitTestSource) {
Expand All @@ -127,6 +123,10 @@ if (app.xr.supported) {
}
});
});

app.xr.on('start', function () {
message('Immersive AR session has started');
});
app.xr.on('end', function () {
message('Immersive AR session has ended');
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"twgsl": "readonly",
"webkitAudioContext": "readonly",
"XRRay": "readonly",
"XRRigidTransform": "readonly",
"XRWebGLLayer": "readonly"
},
"parser": "@babel/eslint-parser",
Expand Down
26 changes: 24 additions & 2 deletions src/framework/xr/xr-anchors.js
Expand Up @@ -101,6 +101,12 @@ class XrAnchors extends EventHandler {
*/
_available = false;

/**
* @type {boolean}
* @private
*/
_checkingAvailability = false;

/**
* @type {boolean}
* @private
Expand Down Expand Up @@ -283,7 +289,7 @@ class XrAnchors extends EventHandler {
});
} else {
this._creationQueue.push({
transform: new XRRigidTransform(position, rotation), // eslint-disable-line no-undef
transform: new XRRigidTransform(position, rotation),
callback: callback
});
}
Expand Down Expand Up @@ -380,8 +386,24 @@ class XrAnchors extends EventHandler {
* @ignore
*/
update(frame) {
if (!this._available)
if (!this._available) {
// enabledFeatures - is not available, requires alternative way to check feature availability
if (!this.manager.session.enabledFeatures && !this._checkingAvailability) {
this._checkingAvailability = true;

frame.createAnchor(new XRRigidTransform(), this.manager._referenceSpace)
.then((xrAnchor) => {
// successfully created an anchor - feature is available
xrAnchor.delete();
if (this.manager.active) {
this._available = true;
this.fire('available');
}
})
.catch(() => { }); // stay unavailable
}
return;
}

// check if need to create anchors
if (this._creationQueue.length) {
Expand Down
36 changes: 32 additions & 4 deletions src/framework/xr/xr-hit-test.js
Expand Up @@ -111,6 +111,12 @@ class XrHitTest extends EventHandler {
*/
_available = false;

/**
* @type {boolean}
* @private
*/
_checkingAvailability = false;

/**
* List of active {@link XrHitTestSource}.
*
Expand All @@ -137,10 +143,29 @@ class XrHitTest extends EventHandler {

/** @private */
_onSessionStart() {
const available = this.manager.session.enabledFeatures.indexOf('hit-test') !== -1;
if (!available) return;
this._available = available;
this.fire('available');
if (this.manager.session.enabledFeatures) {
const available = this.manager.session.enabledFeatures.indexOf('hit-test') !== -1;
if (!available) return;
this._available = available;
this.fire('available');
} else if (!this._checkingAvailability) {
this._checkingAvailability = true;

// enabledFeatures - is not available, requires alternative way to check feature availability

this.manager.session.requestReferenceSpace(XRSPACE_VIEWER).then((referenceSpace) => {
this.manager.session.requestHitTestSource({
space: referenceSpace
}).then((hitTestSource) => {
hitTestSource.cancel();

if (this.manager.active) {
this._available = true;
this.fire('available');
}
}).catch(() => { });
}).catch(() => {});
}
}

/** @private */
Expand Down Expand Up @@ -316,6 +341,9 @@ class XrHitTest extends EventHandler {
* @ignore
*/
update(frame) {
if (!this._available)
return;

for (let i = 0; i < this.sources.length; i++) {
this.sources[i].update(frame);
}
Expand Down
20 changes: 14 additions & 6 deletions src/framework/xr/xr-mesh-detection.js
Expand Up @@ -120,8 +120,14 @@ class XrMeshDetection extends EventHandler {
* @ignore
*/
update(frame) {
if (!this._supported || !this._available)
return;
if (!this._available) {
if (!this._manager.session.enabledFeatures && frame.detectedMeshes.size) {
this._available = true;
this.fire('available');
} else {
return;
}
}

// add meshes
for (const xrMesh of frame.detectedMeshes) {
Expand Down Expand Up @@ -159,10 +165,12 @@ class XrMeshDetection extends EventHandler {

/** @private */
_onSessionStart() {
const available = this._manager.session.enabledFeatures.indexOf('mesh-detection') !== -1;
if (!available) return;
this._available = available;
this.fire('available');
if (this._manager.session.enabledFeatures) {
const available = this._manager.session.enabledFeatures.indexOf('mesh-detection') !== -1;
if (!available) return;
this._available = available;
this.fire('available');
}
}

/** @private */
Expand Down
20 changes: 14 additions & 6 deletions src/framework/xr/xr-plane-detection.js
Expand Up @@ -117,10 +117,12 @@ class XrPlaneDetection extends EventHandler {

/** @private */
_onSessionStart() {
const available = this._supported && this._manager.session.enabledFeatures.indexOf('plane-detection') !== -1;
if (available) {
this._available = true;
this.fire('available');
if (this._manager.session.enabledFeatures) {
const available = this._manager.session.enabledFeatures.indexOf('plane-detection') !== -1;
if (available) {
this._available = true;
this.fire('available');
}
}
}

Expand All @@ -145,8 +147,14 @@ class XrPlaneDetection extends EventHandler {
* @ignore
*/
update(frame) {
if (!this._supported || !this._available)
return;
if (!this._available) {
if (!this._manager.session.enabledFeatures && frame.detectedPlanes.size) {
this._available = true;
this.fire('available');
} else {
return;
}
}

const detectedPlanes = frame.detectedPlanes;

Expand Down
3 changes: 3 additions & 0 deletions src/framework/xr/xr-views.js
Expand Up @@ -261,6 +261,9 @@ class XrViews extends EventHandler {
if (this._manager.type !== XRTYPE_AR)
return;

if (!this._manager.session.enabledFeatures)
return;

this._availableColor = this._manager.session.enabledFeatures.indexOf('camera-access') !== -1;
this._availableDepth = this._manager.session.enabledFeatures.indexOf('depth-sensing') !== -1;

Expand Down