webXRSessionManager.d.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { Observable } from "../Misc/observable";
  2. import type { Nullable } from "../types";
  3. import type { IDisposable, Scene } from "../scene";
  4. import type { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  5. import type { WebXRRenderTarget } from "./webXRTypes";
  6. import { WebXRManagedOutputCanvasOptions } from "./webXRManagedOutputCanvas";
  7. import type { IWebXRRenderTargetTextureProvider } from "./webXRRenderTargetTextureProvider";
  8. import type { Viewport } from "../Maths/math.viewport";
  9. import type { WebXRLayerWrapper } from "./webXRLayerWrapper";
  10. /**
  11. * Manages an XRSession to work with Babylon's engine
  12. * @see https://doc.babylonjs.com/features/featuresDeepDive/webXR/webXRSessionManagers
  13. */
  14. export declare class WebXRSessionManager implements IDisposable, IWebXRRenderTargetTextureProvider {
  15. /** The scene which the session should be created for */
  16. scene: Scene;
  17. private _engine;
  18. private _referenceSpace;
  19. private _baseLayerWrapper;
  20. private _baseLayerRTTProvider;
  21. private _xrNavigator;
  22. private _sessionMode;
  23. private _onEngineDisposedObserver;
  24. /**
  25. * The base reference space from which the session started. good if you want to reset your
  26. * reference space
  27. */
  28. baseReferenceSpace: XRReferenceSpace;
  29. /**
  30. * Current XR frame
  31. */
  32. currentFrame: Nullable<XRFrame>;
  33. /** WebXR timestamp updated every frame */
  34. currentTimestamp: number;
  35. /**
  36. * Used just in case of a failure to initialize an immersive session.
  37. * The viewer reference space is compensated using this height, creating a kind of "viewer-floor" reference space
  38. */
  39. defaultHeightCompensation: number;
  40. /**
  41. * Fires every time a new xrFrame arrives which can be used to update the camera
  42. */
  43. onXRFrameObservable: Observable<XRFrame>;
  44. /**
  45. * Fires when the reference space changed
  46. */
  47. onXRReferenceSpaceChanged: Observable<XRReferenceSpace>;
  48. /**
  49. * Fires when the xr session is ended either by the device or manually done
  50. */
  51. onXRSessionEnded: Observable<any>;
  52. /**
  53. * Fires when the xr session is initialized: right after requestSession was called and returned with a successful result
  54. */
  55. onXRSessionInit: Observable<XRSession>;
  56. /**
  57. * Fires when the xr reference space has been initialized
  58. */
  59. onXRReferenceSpaceInitialized: Observable<XRReferenceSpace>;
  60. /**
  61. * Fires when the session manager is rendering the first frame
  62. */
  63. onXRReady: Observable<WebXRSessionManager>;
  64. /**
  65. * Underlying xr session
  66. */
  67. session: XRSession;
  68. /**
  69. * The viewer (head position) reference space. This can be used to get the XR world coordinates
  70. * or get the offset the player is currently at.
  71. */
  72. viewerReferenceSpace: XRReferenceSpace;
  73. /**
  74. * Are we currently in the XR loop?
  75. */
  76. inXRFrameLoop: boolean;
  77. /**
  78. * Are we in an XR session?
  79. */
  80. inXRSession: boolean;
  81. private _worldScalingFactor;
  82. /**
  83. * Observable raised when the world scale has changed
  84. */
  85. onWorldScaleFactorChangedObservable: Observable<{
  86. previousScaleFactor: number;
  87. newScaleFactor: number;
  88. }>;
  89. /**
  90. * Scale factor to apply to all XR-related elements (camera, controllers)
  91. */
  92. get worldScalingFactor(): number;
  93. set worldScalingFactor(value: number);
  94. /**
  95. * Constructs a WebXRSessionManager, this must be initialized within a user action before usage
  96. * @param scene The scene which the session should be created for
  97. */
  98. constructor(
  99. /** The scene which the session should be created for */
  100. scene: Scene);
  101. /**
  102. * The current reference space used in this session. This reference space can constantly change!
  103. * It is mainly used to offset the camera's position.
  104. */
  105. get referenceSpace(): XRReferenceSpace;
  106. /**
  107. * Set a new reference space and triggers the observable
  108. */
  109. set referenceSpace(newReferenceSpace: XRReferenceSpace);
  110. /**
  111. * The mode for the managed XR session
  112. */
  113. get sessionMode(): XRSessionMode;
  114. /**
  115. * Disposes of the session manager
  116. * This should be called explicitly by the dev, if required.
  117. */
  118. dispose(): void;
  119. /**
  120. * Stops the xrSession and restores the render loop
  121. * @returns Promise which resolves after it exits XR
  122. */
  123. exitXRAsync(): Promise<void>;
  124. /**
  125. * Attempts to set the framebuffer-size-normalized viewport to be rendered this frame for this view.
  126. * In the event of a failure, the supplied viewport is not updated.
  127. * @param viewport the viewport to which the view will be rendered
  128. * @param view the view for which to set the viewport
  129. * @returns whether the operation was successful
  130. */
  131. trySetViewportForView(viewport: Viewport, view: XRView): boolean;
  132. /**
  133. * Gets the correct render target texture to be rendered this frame for this eye
  134. * @param eye the eye for which to get the render target
  135. * @returns the render target for the specified eye or null if not available
  136. */
  137. getRenderTargetTextureForEye(eye: XREye): Nullable<RenderTargetTexture>;
  138. /**
  139. * Gets the correct render target texture to be rendered this frame for this view
  140. * @param view the view for which to get the render target
  141. * @returns the render target for the specified view or null if not available
  142. */
  143. getRenderTargetTextureForView(view: XRView): Nullable<RenderTargetTexture>;
  144. /**
  145. * Creates a WebXRRenderTarget object for the XR session
  146. * @param options optional options to provide when creating a new render target
  147. * @returns a WebXR render target to which the session can render
  148. */
  149. getWebXRRenderTarget(options?: WebXRManagedOutputCanvasOptions): WebXRRenderTarget;
  150. /**
  151. * Initializes the manager
  152. * After initialization enterXR can be called to start an XR session
  153. * @returns Promise which resolves after it is initialized
  154. */
  155. initializeAsync(): Promise<void>;
  156. /**
  157. * Initializes an xr session
  158. * @param xrSessionMode mode to initialize
  159. * @param xrSessionInit defines optional and required values to pass to the session builder
  160. * @returns a promise which will resolve once the session has been initialized
  161. */
  162. initializeSessionAsync(xrSessionMode?: XRSessionMode, xrSessionInit?: XRSessionInit): Promise<XRSession>;
  163. /**
  164. * Checks if a session would be supported for the creation options specified
  165. * @param sessionMode session mode to check if supported eg. immersive-vr
  166. * @returns A Promise that resolves to true if supported and false if not
  167. */
  168. isSessionSupportedAsync(sessionMode: XRSessionMode): Promise<boolean>;
  169. /**
  170. * Resets the reference space to the one started the session
  171. */
  172. resetReferenceSpace(): void;
  173. /**
  174. * Starts rendering to the xr layer
  175. */
  176. runXRRenderLoop(): void;
  177. /**
  178. * Sets the reference space on the xr session
  179. * @param referenceSpaceType space to set
  180. * @returns a promise that will resolve once the reference space has been set
  181. */
  182. setReferenceSpaceTypeAsync(referenceSpaceType?: XRReferenceSpaceType): Promise<XRReferenceSpace>;
  183. /**
  184. * Updates the render state of the session.
  185. * Note that this is deprecated in favor of WebXRSessionManager.updateRenderState().
  186. * @param state state to set
  187. * @returns a promise that resolves once the render state has been updated
  188. * @deprecated Use updateRenderState() instead.
  189. */
  190. updateRenderStateAsync(state: XRRenderState): Promise<void>;
  191. /**
  192. * @internal
  193. */
  194. _setBaseLayerWrapper(baseLayerWrapper: Nullable<WebXRLayerWrapper>): void;
  195. /**
  196. * @internal
  197. */
  198. _getBaseLayerWrapper(): Nullable<WebXRLayerWrapper>;
  199. /**
  200. * Updates the render state of the session
  201. * @param state state to set
  202. */
  203. updateRenderState(state: XRRenderStateInit): void;
  204. /**
  205. * Returns a promise that resolves with a boolean indicating if the provided session mode is supported by this browser
  206. * @param sessionMode defines the session to test
  207. * @returns a promise with boolean as final value
  208. */
  209. static IsSessionSupportedAsync(sessionMode: XRSessionMode): Promise<boolean>;
  210. /**
  211. * Returns true if Babylon.js is using the BabylonNative backend, otherwise false
  212. */
  213. get isNative(): boolean;
  214. /**
  215. * The current frame rate as reported by the device
  216. */
  217. get currentFrameRate(): number | undefined;
  218. /**
  219. * A list of supported frame rates (only available in-session!
  220. */
  221. get supportedFrameRates(): Float32Array | undefined;
  222. /**
  223. * Set the framerate of the session.
  224. * @param rate the new framerate. This value needs to be in the supportedFrameRates array
  225. * @returns a promise that resolves once the framerate has been set
  226. */
  227. updateTargetFrameRate(rate: number): Promise<void>;
  228. /**
  229. * Run a callback in the xr render loop
  230. * @param callback the callback to call when in XR Frame
  231. * @param ignoreIfNotInSession if no session is currently running, run it first thing on the next session
  232. */
  233. runInXRFrame(callback: () => void, ignoreIfNotInSession?: boolean): void;
  234. /**
  235. * Check if fixed foveation is supported on this device
  236. */
  237. get isFixedFoveationSupported(): boolean;
  238. /**
  239. * Get the fixed foveation currently set, as specified by the webxr specs
  240. * If this returns null, then fixed foveation is not supported
  241. */
  242. get fixedFoveation(): Nullable<number>;
  243. /**
  244. * Set the fixed foveation to the specified value, as specified by the webxr specs
  245. * This value will be normalized to be between 0 and 1, 1 being max foveation, 0 being no foveation
  246. */
  247. set fixedFoveation(value: Nullable<number>);
  248. /**
  249. * Get the features enabled on the current session
  250. * This is only available in-session!
  251. * @see https://www.w3.org/TR/webxr/#dom-xrsession-enabledfeatures
  252. */
  253. get enabledFeatures(): Nullable<string[]>;
  254. }