XRSession: updateRenderState() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The updateRenderState() method of the XRSession interface of the WebXR API schedules changes to be applied to the active render state (XRRenderState) prior to rendering of the next frame.
Syntax
updateRenderState()
updateRenderState(state)
Parameters
stateOptional-
An optional object to configure the
XRRenderState. If none is provided, a default configuration will be used.baseLayerOptional: AnXRWebGLLayerobject from which the WebXR compositor will obtain imagery. This isnullby default. To specify other (or multiple) layers, see thelayersoption.depthFarOptional: A floating-point value specifying the distance in meters from the viewer to the far clip plane, which is a plane parallel to the display surface beyond which no further rendering will occur. All rendering will take place between the distances specified bydepthNearanddepthFar. This is 1000 meters (1 kilometer) by default.depthNearOptional: A floating-point value indicating the distance in meters from the viewer to a plane parallel to the display surface to be the near clip plane. No part of the scene on the viewer's side of this plane will be rendered. This is 0.1 meters (10 centimeters) by default.inlineVerticalFieldOfViewOptional: A floating-point value indicating the default field of view, in radians, to be used when computing the projection matrix for aninlineXRSession. The projection matrix calculation also takes into account the output canvas's aspect ratio. This property must not be specified for immersive sessions, so the value isnullby default for immersive sessions. The default value is otherwise π * 0.5 (half of the value of pi).layersOptional: An ordered array ofXRLayerobjects specifying the layers that should be presented to the XR device. Settinglayerswill override thebaseLayerif one is present, withbaseLayerreportingnull. The order of the layers given is "back-to-front". For alpha blending of layers, see theXRCompositionLayer.blendTextureSourceAlphaproperty.
Return value
None (undefined).
Exceptions
InvalidStateErrorDOMException-
Thrown in any of the following situations:
- The
XRSessionhas already ended, so you cannot change its render state. - The
baseLayerwas created by anXRSessionother than the one on whichupdateRenderState()was called. - The
inlineVerticalFieldOfViewoption was set, but the session is immersive, and therefore, does not allow this property to be used.
- The
NotSupportedErrorDOMException-
Thrown in any of the following situations:
- The
layersoption is used in a session that has been created without thelayersfeature. - Both the
baseLayerandlayersoptions are specified.
- The
TypeError-
Thrown if the
layersoption contains duplicate instances.
Examples
>Adding a baseLayer
This example creates a WebGL context that is compatible with an immersive XR device and then uses it to create an XRWebGLLayer. The method updateRenderState() is then called to associate the new XRWebGLLayer.
function onXRSessionStarted(xrSession) {
let glCanvas = document.createElement("canvas");
let gl = glCanvas.getContext("webgl", { xrCompatible: true });
loadWebGLResources();
xrSession.updateRenderState({
baseLayer: new XRWebGLLayer(xrSession, gl),
});
}
Setting the layers array
To use WebXR layers, the XR session needs to be created with the layers feature descriptor (see XRSystem.requestSession()). You can then create various WebXR layers such as
Other layers, such as XRProjectionLayer or XRWebGLLayer are also available.
Layers will be presented in the order they are given in the layers array, with layers being given in "back-to-front" order.
const xrSession = navigator.xr.requestSession("immersive-ar", {
optionalFeatures: ["layers"],
});
function onXRSessionStarted(xrSession) {
const glCanvas = document.createElement("canvas");
const gl = glCanvas.getContext("webgl", { xrCompatible: true });
const xrGlBinding = new XRWebGLBinding(xrSession, gl);
const projectionLayer = new XRWebGLLayer(xrSession, gl);
const quadLayer = xrGlBinding.createQuadLayer({
pixelWidth: 1024,
pixelHeight: 1024,
});
xrSession.updateRenderState({
layers: [projectionLayer, quadLayer],
});
}
Specifications
| Specification |
|---|
| WebXR Device API> # dom-xrsession-updaterenderstate> |
Browser compatibility
Loading…