to port code from WebVR to WebXR:
Deprecated APIs: Replace all deprecated WebVR APIs with their WebXR equivalents. WebXR uses a different set of APIs that are more flexible and comprehensive than those in WebVR.
Session Handling: In WebXR, instead of directly requesting a VR display and starting a VR session, you would need to ask the user agent for an XR device, then start a session.
Reference Spaces: WebXR introduces the concept of reference spaces, which replace the eye-level and stage-level frames of reference in WebVR. You'll have to update your code to use these new reference spaces.
Input Sources: WebXR has a more flexible and comprehensive system for handling input from a variety of devices. If your WebVR code is designed to handle input from specific devices, you'll need to update it to use the new WebXR input source APIs.
Viewer and Frame Data: In WebVR, you use
getFrameData()
to get the data needed to render a frame. In WebXR, you use theXRFrame.getViewerPose()
method.Animation Loop: In WebVR, you use
VRDisplay.requestAnimationFrame()
. In WebXR, you useXRSession.requestAnimationFrame()
.Rendering: In WebXR, you don't render to the canvas directly. Instead, you render to
XRWebGLLayer
s that are part of anXRSession
.
Sure, let's take a simple "Hello, World!" example of a rotating cube in WebVR and port it to WebXR. Note that the code snippets below are simplified for illustration purposes and might require additional setup and error handling for a full production application.
WebVR Version
javascriptlet vrDisplay, frameData;
initWebGL();
initWebVR();
function initWebGL() {
// WebGL setup goes here
}
function initWebVR() {
navigator.getVRDisplays().then(function(displays) {
if(displays.length > 0) {
vrDisplay = displays[0];
frameData = new VRFrameData();
vrDisplay.requestAnimationFrame(loop);
}
});
}
function loop() {
vrDisplay.getFrameData(frameData);
render(frameData); // your rendering function
vrDisplay.requestAnimationFrame(loop);
}
WebXR Version
javascriptlet xrSession, xrReferenceSpace;
initWebGL();
initWebXR();
function initWebGL() {
// WebGL setup goes here
}
async function initWebXR() {
if (navigator.xr) {
xrSession = await navigator.xr.requestSession("immersive-vr", { optionalFeatures: ['local-floor', 'bounded-floor'] });
xrSession.updateRenderState({ baseLayer: new XRWebGLLayer(xrSession, gl) });
xrReferenceSpace = await xrSession.requestReferenceSpace('local-floor');
xrSession.requestAnimationFrame(loop);
}
}
function loop(time, xrFrame) {
let xrViewerPose = xrFrame.getViewerPose(xrReferenceSpace);
if (xrViewerPose) {
let glLayer = xrSession.renderState.baseLayer;
gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
for (let xrView of xrViewerPose.views) {
// Render from the point of view of xrView
render(xrView); // your rendering function
}
}
xrSession.requestAnimationFrame(loop);
}
This code creates a VR session, sets up the WebGL context for rendering in VR, requests a 'local-floor' reference space (which provides an origin at the user's floor level), and enters the render loop.
Remember, these are simplified examples. In a real application, you would need to handle errors, manage WebGL resources, and account for different types of VR devices and input methods. You would also need a way to start and stop the VR session based on user input.
You may also want to consider using a library such as Three.js, which can handle much of this complexity for you and provides a higher-level API for 3D rendering. As of my knowledge cutoff in September 2021, Three.js has good support for both WebVR and WebXR.
The transition from WebVR to WebXR primarily involved a change in the APIs used to interact with VR/AR hardware. Here are some of the most important changes:
navigator.getVRDisplays()
: This was a WebVR method used to get a list of all connected VR displays. In WebXR, the equivalent function isnavigator.xr.requestSession()
, which requests a session for XR content. UnlikegetVRDisplays()
,requestSession()
returns a promise for a singleXRSession
object instead of a list of displays.VRDisplay
: This interface represented a VR display in WebVR. It has been replaced by theXRSystem
interface in WebXR.XRSystem
provides methods to get information about the XR device, check if a certain session mode is supported, and request anXRSession
.VRDisplay.requestAnimationFrame()
: This method requested an animation frame for the VR display in WebVR. In WebXR, the equivalent method isXRSession.requestAnimationFrame()
. It behaves in a similar way, but is called on anXRSession
object instead of aVRDisplay
.VRDisplay.requestPresent()
: This WebVR method was used to start presenting to the VR display. It has been replaced by theXRSession.updateRenderState()
method in WebXR. Instead of directly presenting to the display, you now update the render state of the XR session.VRFrameData
: This interface represented all the data needed to render a single frame in WebVR. In WebXR, the equivalent is theXRFrame
interface. You use thegetViewerPose()
andgetPose()
methods ofXRFrame
to get the data you need for rendering.VREyeParameters
: In WebVR, this interface was used to get information about the eyes of the VR display. In WebXR, the concept of eyes has been replaced withXRView
s. You get a list ofXRView
s from anXRViewerPose
object, which you can get from anXRFrame
.VRDisplayCapabilities
: This interface represented the capabilities of a VR display in WebVR. In WebXR, capabilities are represented by theXRSessionMode
andXRSessionInit
dictionaries. You check if a certain session mode is supported by using theXRSystem.isSessionSupported()
method.VRStageParameters
: This interface represented the stage parameters in WebVR. In WebXR, stages are represented by bounded reference spaces. You request a bounded reference space by callingXRSession.requestReferenceSpace()
with the'bounded-floor'
option.
These changes reflect a shift in how the APIs conceptualize VR/AR hardware and sessions. The goal of these changes was to make the APIs more flexible, able to handle a wider range of hardware, and better aligned with how modern VR/AR hardware works.
No comments:
Post a Comment