Skip to content

Commit

Permalink
WebXR FrameRate (#6077)
Browse files Browse the repository at this point in the history
* xr framerate

* float32array > array

* lint

* Update src/framework/xr/xr-manager.js

---------

Co-authored-by: Martin Valigursky <59932779+mvaligursky@users.noreply.github.com>
  • Loading branch information
Maksims and mvaligursky authored Feb 21, 2024
1 parent cac7efc commit 6a81133
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/framework/xr/xr-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ class XrManager extends EventHandler {
*/
_depthFar = 1000;

/**
* @type {number[]|null}
* @private
*/
_supportedFrameRates = null;

/**
* @type {number}
* @private
Expand Down Expand Up @@ -633,6 +639,30 @@ class XrManager extends EventHandler {
});
}

/**
* Update target frame rate of an XR session to one of supported value provided by
* supportedFrameRates list.
*
* @param {number} frameRate - Target frame rate. It should be any value from the list
* of supportedFrameRates.
* @param {Function} [callback] - Callback that will be called when frameRate has been
* updated or failed to update with error provided.
*/
updateTargetFrameRate(frameRate, callback) {
if (!this._session?.updateTargetFrameRate) {
callback?.(new Error('unable to update frameRate'));
return;
}

this._session.updateTargetFrameRate(frameRate)
.then(() => {
callback?.();
})
.catch((err) => {
callback?.(err);
});
}

/**
* @param {string} type - Session type.
* @private
Expand Down Expand Up @@ -709,6 +739,16 @@ class XrManager extends EventHandler {

this._createBaseLayer();

if (this.session.supportedFrameRates) {
this._supportedFrameRates = Array.from(this.session.supportedFrameRates);
} else {
this._supportedFrameRates = null;
}

this._session.addEventListener('frameratechange', () => {
this.fire('frameratechange', this._session?.frameRate);
});

// request reference space
session.requestReferenceSpace(spaceType).then((referenceSpace) => {
this._referenceSpace = referenceSpace;
Expand Down Expand Up @@ -949,6 +989,25 @@ class XrManager extends EventHandler {
return this._session;
}

/**
* XR session frameRate or null if this information is not available. This value can change
* during an active XR session.
*
* @type {number|null}
*/
get frameRate() {
return this._session?.frameRate ?? null;
}

/**
* List of supported frame rates, or null if this data is not available.
*
* @type {number[]|null}
*/
get supportedFrameRates() {
return this._supportedFrameRates;
}

/**
* Framebuffer scale factor. This value is read-only and can only be set when starting a new
* XR session.
Expand Down

0 comments on commit 6a81133

Please sign in to comment.