navigator.xr
.requestSession("immersive-vr")
.then((xrSession) => {
xrSession.addEventListener("end", onXRSessionEnded);
// Do necessary session setup here.
// Begin the session's animation loop.
xrSession.requestAnimationFrame(onXRAnimationFrame);
})
.catch((error) => {
// "immersive-vr" sessions are not supported
console.error(
"'immersive-vr' isn't supported, or an error occurred activating VR!",
);
});XRSystem: requestSession() method / notebook
XRSystem: requestSession() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
if (navigator.xr) {
navigator.xr.isSessionSupported("immersive-vr").then((isSupported) => {
if (isSupported) {
immersiveButton.addEventListener("click", onButtonClicked);
immersiveButton.textContent = "Enter XR";
immersiveButton.disabled = false;
} else {
console.error("WebXR doesn't support immersive-vr mode!");
}
});
} else {
console.error("WebXR is not available!");
}
function onButtonClicked() {
if (!xrSession) {
navigator.xr.requestSession("immersive-vr").then((session) => {
xrSession = session;
// onSessionStarted() not shown for reasons of brevity and clarity.
onSessionStarted(xrSession);
});
} else {
// Button is a toggle button.
xrSession.end().then(() => (xrSession = null));
}
}navigator.xr.requestSession("immersive-vr", {
requiredFeatures: ["unbounded"],
});navigator.xr.requestSession("immersive-ar", {
optionalFeatures: ["dom-overlay"],
domOverlay: {
root: document.getElementById("xr-overlay"),
},
});navigator.xr.requestSession("immersive-ar", {
requiredFeatures: ["depth-sensing"],
depthSensing: {
usagePreference: ["cpu-optimized", "gpu-optimized"],
dataFormatPreference: ["luminance-alpha", "float32"],
},
});