API

Class WebXRSessionManager

Manages an XRSession to work with Babylon's engine

Implements

Index
baseReferenceSpace: XRReferenceSpace

The base reference space from which the session started. good if you want to reset your reference space

currentFrame: any

Current XR frame

currentTimestamp: number = -1

WebXR timestamp updated every frame

defaultHeightCompensation: number = 1.7

Used just in case of a failure to initialize an immersive session. The viewer reference space is compensated using this height, creating a kind of "viewer-floor" reference space

inXRFrameLoop: boolean = false

Are we currently in the XR loop?

inXRSession: boolean = false

Are we in an XR session?

onWorldScaleFactorChangedObservable: Observable<
    { newScaleFactor: number; previousScaleFactor: number },
> = ...

Observable raised when the world scale has changed

onXRFrameObservable: Observable<XRFrame> = ...

Fires every time a new xrFrame arrives which can be used to update the camera

Fires when the session manager is rendering the first frame

onXRReferenceSpaceChanged: Observable<XRReferenceSpace> = ...

Fires when the reference space changed

onXRReferenceSpaceInitialized: Observable<XRReferenceSpace> = ...

Fires when the xr reference space has been initialized

onXRSessionEnded: Observable<any> = ...

Fires when the xr session is ended either by the device or manually done

onXRSessionInit: Observable<XRSession> = ...

Fires when the xr session is initialized: right after requestSession was called and returned with a successful result

scene: Scene

The scene which the session should be created for

session: XRSession

Underlying xr session

viewerReferenceSpace: XRReferenceSpace

The viewer (head position) reference space. This can be used to get the XR world coordinates or get the offset the player is currently at.

  • get referenceSpace(): XRReferenceSpace

    The current reference space used in this session. This reference space can constantly change! It is mainly used to offset the camera's position.

    Returns XRReferenceSpace

  • set referenceSpace(newReferenceSpace: XRReferenceSpace): void

    Set a new reference space and triggers the observable

    Parameters

    • newReferenceSpace: XRReferenceSpace

    Returns void

  • get IsWebGPUXRSupported(): boolean
    Experimental

    Returns whether the runtime exposes the WebGPU-XR binding APIs required by Babylon.js.

    This is an advisory check only. XR session negotiation can still fail for the active device, permissions, or adapter. A WebGPU engine intended for XR must also be created with xrCompatible: true.

    Returns boolean

    whether the required XRGPUBinding projection APIs are exposed WebGPU-XR support is experimental and may change.

  • Gets the runtime-recommended viewport scale for the current XR view. A number is returned when the runtime has a recommendation, null when the API is supported but the runtime has no recommendation, and undefined when the API is not supported. This method must be called during an active XR frame.

    Parameters

    • viewIndex: number

      the index of the view in the current viewer pose

    Returns Nullable<number> | undefined

    the recommended viewport scale, null when no recommendation is available, or undefined when unsupported

  • Initializes an xr session

    Parameters

    • xrSessionMode: XRSessionMode = "immersive-vr"

      mode to initialize

    • xrSessionInit: XRSessionInit = {}

      defines optional and required values to pass to the session builder

    Returns Promise<XRSession>

    a promise which will resolve once the session has been initialized

  • Checks if a session would be supported for the creation options specified

    Parameters

    • sessionMode: XRSessionMode

      session mode to check if supported eg. immersive-vr

    Returns Promise<boolean>

    A Promise that resolves to true if supported and false if not

  • Checks whether the current XR view exposes the dynamic viewport scaling API. API availability does not guarantee that the active XR device will change the viewport dimensions. This method must be called during an active XR frame.

    Parameters

    • viewIndex: number

      the index of the view in the current viewer pose

    Returns boolean

    whether dynamic viewport scaling is exposed for the view

  • Requests a viewport scale for the current XR view. The request is a hint to the runtime. Babylon uses the native viewport returned on subsequent frames and does not derive viewport dimensions from this value. Pass 1 to restore the full viewport scale; null follows the native no-op behavior. Native ignored-value, clamping, and exception behavior is preserved. Requests made from an application observer of onXRFrameObservable apply to a future frame because Babylon's camera acquires the current frame's viewport before notifying application observers. This method must be called during an active XR frame.

    Parameters

    • viewIndex: number

      the index of the view in the current viewer pose

    • scale: Nullable<number>

      the viewport scale requested from the runtime

    Returns void

  • Run a callback in the xr render loop

    Parameters

    • callback: () => void

      the callback to call when in XR Frame

    • ignoreIfNotInSession: boolean = true

      if no session is currently running, run it first thing on the next session

    Returns void

  • Sets the reference space on the xr session

    Parameters

    • referenceSpaceType: XRReferenceSpaceType = "local-floor"

      space to set

    Returns Promise<XRReferenceSpace>

    a promise that will resolve once the reference space has been set

  • Updates the render state of the session. Note that this is deprecated in favor of WebXRSessionManager.updateRenderState().

    Parameters

    • state: XRRenderState

      state to set

    Returns Promise<void>

    a promise that resolves once the render state has been updated

    Use updateRenderState() instead.

  • Set the framerate of the session.

    Parameters

    • rate: number

      the new framerate. This value needs to be in the supportedFrameRates array

    Returns Promise<void>

    a promise that resolves once the framerate has been set

  • Returns a promise that resolves with a boolean indicating if the provided session mode is supported by this browser

    Parameters

    • sessionMode: XRSessionMode

      defines the session to test

    Returns Promise<boolean>

    a promise with boolean as final value