WebXR Device API Specification

repository·main·Indexed 25 days ago

https://github.com/immersive-web/webxr

The standard JavaScript interface for accessing VR and AR hardware, including sensors and head-mounted displays, through web browsers. This documentation covers the core specification, input profiles, and explainers for spatial tracking, privacy, and security. It also provides detailed guidance on accessibility considerations—such as mobility, visual impairments, and photosensitivity—as well as requirements for requesting immersive sessions and managing secure origins.

Tokens
27K
Snippets
50
Records
113
Agent score
84%

What's inside WebXR Device API

  1. Overview of the WebXR Device API

    main

    The WebXR Device API provides web developers with access to the input and output capabilities of Virtual Reality (VR), Augmented Reality (AR), and Mixed Reality (MR) devices. It enables the development and hosting of immersive experiences directly in the browser.

    Core Goals

    • Capability Detection: Determine if XR capabilities are available on the user's device.
    • Device Querying: Query specific XR device capabilities.
    • Input Polling: Poll the state of the XR device and its associated input devices.
    • High-Performance Rendering: Display imagery on the XR device at the appropriate frame rate.

    Target Hardware

    The API is designed to work across a wide range of hardware, including:

    • ARCore-compatible devices and Android XR-compatible devices.
    • Meta Quest (1, 2, and Pro).
    • Microsoft HoloLens and Windows Mixed Reality headsets.
    • HTC Vive and Magic Leap One.
    • Samsung Galaxy XR.
  2. Overview of WebXR Device API

    main

    The WebXR Device API provides a JavaScript interface for accessing virtual reality (VR) and augmented reality (AR) devices. This includes interacting with sensors and head-mounted displays (HMDs) via the web.

    Supported modes include:

    • VR (Virtual Reality): Headset devices (previously handled by WebVR) and handheld devices using 'Magic Window' behavior.
    • AR (Augmented Reality): Mixed Reality headsets and handheld devices (e.g., Phone AR).
  3. Understand WebXR Privacy and Security Concepts

    main

    WebXR involves sensors (cameras, accelerometers, etc.) that can generate sensitive real-world data. Developers should be aware of the following privacy risks:

    • Sensitive Information: Includes user-configurable data like interpupillary distance (IPD) and sensor-based data like poses. While immersive sessions always expose some sensitive data for rendering, inline sessions may also expose it.
    • Device Fingerprinting: Data unique to a device (e.g., raw IMU data or specific IPD values) can be used to identify users. User agents may anonymize data to mitigate this.
    • User Profiling: Data that could signal demographic characteristics (race, gender, age) requires explicit consent. For example, IPD data can potentially be used to infer these characteristics.
    • Private Browsing: WebXR does not generate persistent data or unique user identifiers, so it does not impose additional requirements for private browsing modes.
  4. Privacy requirements across WebXR modes

    main

    Privacy requirements for exposing data types are consistent across all WebXR modes (inline, immersive, etc.). However, the specific requirements for a session depend on the data types and reference spaces requested:

    • Low-risk sessions: An inline session that only supports the viewer reference space, does not support 6DOF controller data, and does not support multiple views may meet all privacy requirements without needing user consent.
    • High-risk sessions: An immersive session supporting the unbounded reference space type with a native origin tracked via sensors will have different, more stringent requirements for user consent and mitigations.

    If a user agent wishes to avoid requesting user consent for a specific session mode, it must meet all mitigation requirements for every data type available in that session.

  5. Understand XRReferenceSpace types

    main

    The WebXR Device API uses XRReferenceSpace to establish guarantees about motion and provide a coordinate system for retrieving XRViewerPose. Developers should select the least capable reference space that satisfies their experience's mobility needs to ensure maximum device compatibility (progressive enhancement).

    Available XRReferenceSpaceType values:

    • bounded-floor: For experiences where users move within a fixed, pre-defined boundary (e.g., VR painting, dance games).
    • unbounded: For experiences where users move freely over large distances (e.g., campus tours). The origin may drift to maintain stability.
    • local: An eye-level/seated experience. The origin is near the viewer's position at creation. Must be resilient to position changes.
    • local-floor: A floor-level/standing experience. The origin is at floor level (y=0). Must be resilient to position changes.
    • viewer: An origin always at the viewer's position/orientation. Used for non-tracking experiences (e.g., click-and-drag) or to track head-locked objects.
  6. Understand Trusted UI in WebXR

    main

    A Trusted UI is a system-level interface used by User Agents to display sensitive information (like URL bars, consent prompts, or navigation controls) in a way that a website cannot spoof or snoop on.

    In immersive experiences, a Trusted Immersive UI must be:

    • Non-spoofable: The user can verify the source.
    • Secure: If using shared secrets, they must be unobservable by Mixed Reality (MR) capture.
    • Consistent: Behaves predictably across the same User Agent.
    • User-Centric: Easy to discover and use for granting consent, but resistant to clickjacking or unintentional granting. It must provide clear methods to revoke consent.

    Note: A Trusted Immersive UI does not exit immersive mode. If a User Agent cannot provide an immersive version, it may temporarily pause/exit immersive mode to show a non-immersive Trusted UI.

  7. Understand the distinction between session requirements and configuration options

    main

    The proposed design for session creation separates session requirements from session configuration options:

    1. Session Requirements: Coarse capabilities that the application must have to function. A request for these capabilities will fail if the client cannot provide them.
    2. Session Configuration Options: Finer-grained preferences or hints provided by the application. These are treated as optional requests; the platform may choose to ignore them or provide a different configuration if the exact request cannot be met.

    This separation helps minimize fingerprinting, allows for appropriate UI button display, and enables applications to express preferences without causing session failures if those preferences are unavailable.

  8. Understand WebXR Input Targeting Categories

    main

    WebXR input sources are categorized into three types based on how they target objects in 3D space. This determines how you should interpret the input's origin and direction:

    • gaze: Uses the viewer's head position for targeting. Examples include headset buttons or voice commands. All gaze-based sources share a common targetRaySpace representing the viewer's orientation.
    • tracked-pointer: Uses a device tracked separately from the viewer, such as motion controllers or hand tracking. The target ray is typically based on the physical device's location and orientation.
    • screen: Driven by 2D mouse or touch interactions on a screen, which are then projected into 3D space as a targeting ray originating from the interaction point.
  9. Understand WebXR Spatial Tracking

    main
    WebXR uses spatial tracking to allow users to control the view of an experience via their body motion. Unlike device-specific SDKs, WebXR provides a standardized way to track motion in 3D space across a wide range of XR hardware (VR and AR), ensuring developers can reach more users without optimizing for every individual hardware form factor.
  10. Handle tracking loss using XRPose.emulatedPosition

    main

    When positional tracking is lost, WebXR continues to provide pose matrices, but the position may be estimated via inertial dead reckoning or neck modeling.

    To detect this, check the emulatedPosition attribute of the XRPose:

    • If emulatedPosition is true: The position is not actively tracked and is being emulated.
    • If emulatedPosition is false: The position is actively tracked.

    Common Strategies:

    • VR Experiences: Continue rendering even when emulatedPosition is true to prevent the world from freezing, allowing the user to move via inertial tracking.
    • Precision Tasks (e.g., Painting): Stop the specific action (like painting) when emulatedPosition becomes true to ensure high-quality, accurate results.
    • Anchoring: If real-world alignment is critical, stop rendering the main scene and show a warning when emulatedPosition is true.
  11. Mandatory conditions for XRSession creation

    main

    To successfully call XRRequestSession and create an XRSession, the following conditions must be met:

    • Secure Context: The document must be a responsible document of a secure context.
    • Same Origin: The currently focused area must belong to a document whose origin is the same origin-domain as the active document.
    • Feature Policy: The document must be allowed to use all policy-controlled features associated with the sensor types used by the session (e.g., sensors for XRViewerPose, XRPose, or XRBoundedReferenceSpace).
    • User Consent Requirements:
      • Required for bounded-floor or unbounded reference space types.
      • Required for local-floor reference space types if the floor level reflects the real-world location.
      • Required on devices with configurable interpupillary distance (IPD) if XRFrame.getViewerPose() returns XRView transforms that can compute the configured IPD.
      • Required if the user agent does not otherwise mitigate sensor fingerprinting threats via XRViewerPose or XRPose data.

    If required consent is not obtained, the promise will be rejected with a SecurityError DOMException.

  12. Set up XRWebGLLayer and WebGL compatibility

    main

    To render content to an XR device, you must create an XRWebGLLayer and set it as the session's baseLayer via xrSession.updateRenderState().

    Depending on your app type, you must ensure WebGL context compatibility:

    1. XR Enhanced (Progressive Enhancement): Call gl.makeXRCompatible() on your existing context. This may cause the context to be lost and recreated on a compatible graphics adapter. You must handle webglcontextlost and webglcontextrestored events.
    2. XR Centric (Core XR App): Create the context with the { xrCompatible: true } flag to avoid the overhead of makeXRCompatible().
    // Option 1: XR Enhanced
    function setupWebGLLayer() {
      return gl.makeXRCompatible().then(() => {
        xrSession.updateRenderState({ baseLayer: new XRWebGLLayer(xrSession, gl) });
      });
    }
    
    // Handle context loss for XR Enhanced apps
    glCanvas.addEventListener("webglcontextlost", (event) => {
      event.preventDefault();
    });
    
    glCanvas.addEventListener("webglcontextrestored", () => {
      loadSceneGraphics(gl);
    });
    
    // Option 2: XR Centric
    let gl = glCanvas.getContext("webgl", { xrCompatible: true });