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

XrAnchors - Availability flag #5879

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/framework/xr/xr-anchors.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ import { XrAnchor } from './xr-anchor.js';
* @category XR
*/
class XrAnchors extends EventHandler {
/**
* @type {import('./xr-manager.js').XrManager}
* @ignore
*/
manager;

/**
* @type {boolean}
* @private
*/
_supported = platform.browser && !!window.XRAnchor;

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

/**
* @type {boolean}
* @private
Expand Down Expand Up @@ -87,10 +99,23 @@ class XrAnchors extends EventHandler {
this.manager = manager;

if (this._supported) {
this.manager.on('start', this._onSessionStart, this);
this.manager.on('end', this._onSessionEnd, this);
}
}

/**
* Fired when anchors becomes available.
*
* @event XrAnchors#available
*/

/**
* Fired when anchors becomes unavailable.
*
* @event XrAnchors#unavailable
*/

/**
* Fired when anchor failed to be created.
*
Expand Down Expand Up @@ -120,8 +145,19 @@ class XrAnchors extends EventHandler {
* });
*/

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

/** @private */
_onSessionEnd() {
if (!this._available) return;
this._available = false;

// clear anchor creation queue
for (let i = 0; i < this._creationQueue.length; i++) {
if (!this._creationQueue[i].callback)
Expand All @@ -140,6 +176,8 @@ class XrAnchors extends EventHandler {
this._list[i].destroy();
}
this._list.length = 0;

this.fire('unavailable');
}

/**
Expand Down Expand Up @@ -194,6 +232,11 @@ class XrAnchors extends EventHandler {
* });
*/
create(position, rotation, callback) {
if (!this._available) {
callback?.(new Error('Anchors API is not available'), null);
return;
}

// eslint-disable-next-line no-undef
if (window.XRHitTestResult && position instanceof XRHitTestResult) {
const hitResult = position;
Expand Down Expand Up @@ -247,6 +290,11 @@ class XrAnchors extends EventHandler {
* }
*/
restore(uuid, callback) {
if (!this._available) {
callback?.(new Error('Anchors API is not available'), null);
return;
}

if (!this._persistence) {
callback?.(new Error('Anchor Persistence is not supported'), null);
return;
Expand Down Expand Up @@ -283,6 +331,11 @@ class XrAnchors extends EventHandler {
* }
*/
forget(uuid, callback) {
if (!this._available) {
callback?.(new Error('Anchors API is not available'));
return;
}

if (!this._persistence) {
callback?.(new Error('Anchor Persistence is not supported'));
return;
Expand All @@ -308,6 +361,9 @@ class XrAnchors extends EventHandler {
* @ignore
*/
update(frame) {
if (!this._available)
return;

// check if need to create anchors
if (this._creationQueue.length) {
for (let i = 0; i < this._creationQueue.length; i++) {
Expand Down Expand Up @@ -378,6 +434,15 @@ class XrAnchors extends EventHandler {
return this._supported;
}

/**
* True if Anchors are available. This information is available only when session has started.
*
* @type {boolean}
*/
get available() {
return this._available;
}

/**
* True if Anchors support persistence.
*
Expand All @@ -393,6 +458,9 @@ class XrAnchors extends EventHandler {
* @type {null|string[]}
*/
get uuids() {
if (!this._available)
return null;

if (!this._persistence)
return null;

Expand Down