webvr.d.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Type definitions for WebVR API
  2. // Project: https://w3c.github.io/webvr/
  3. // Definitions by: six a <https://github.com/lostfictions>
  4. // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
  5. interface VRDisplay extends EventTarget {
  6. /**
  7. * Dictionary of capabilities describing the VRDisplay.
  8. */
  9. readonly capabilities: VRDisplayCapabilities;
  10. /**
  11. * z-depth defining the far plane of the eye view frustum
  12. * enables mapping of values in the render target depth
  13. * attachment to scene coordinates. Initially set to 10000.0.
  14. */
  15. depthFar: number;
  16. /**
  17. * z-depth defining the near plane of the eye view frustum
  18. * enables mapping of values in the render target depth
  19. * attachment to scene coordinates. Initially set to 0.01.
  20. */
  21. depthNear: number;
  22. /**
  23. * An identifier for this distinct VRDisplay. Used as an
  24. * association point in the Gamepad API.
  25. */
  26. readonly displayId: number;
  27. /**
  28. * A display name, a user-readable name identifying it.
  29. */
  30. readonly displayName: string;
  31. readonly isConnected: boolean;
  32. readonly isPresenting: boolean;
  33. /**
  34. * If this VRDisplay supports room-scale experiences, the optional
  35. * stage attribute contains details on the room-scale parameters.
  36. */
  37. readonly stageParameters: VRStageParameters | null;
  38. /**
  39. * Passing the value returned by `requestAnimationFrame` to
  40. * `cancelAnimationFrame` will unregister the callback.
  41. * @param handle Define the hanle of the request to cancel
  42. */
  43. cancelAnimationFrame(handle: number): void;
  44. /**
  45. * Stops presenting to the VRDisplay.
  46. * @returns a promise to know when it stopped
  47. */
  48. exitPresent(): Promise<void>;
  49. /**
  50. * Return the current VREyeParameters for the given eye.
  51. * @param whichEye Define the eye we want the parameter for
  52. * @returns the eye parameters
  53. */
  54. getEyeParameters(whichEye: string): VREyeParameters;
  55. /**
  56. * Populates the passed VRFrameData with the information required to render
  57. * the current frame.
  58. * @param frameData Define the data structure to populate
  59. * @returns true if ok otherwise false
  60. */
  61. getFrameData(frameData: VRFrameData): boolean;
  62. /**
  63. * Get the layers currently being presented.
  64. * @returns the list of VR layers
  65. */
  66. getLayers(): VRLayer[];
  67. /**
  68. * Return a VRPose containing the future predicted pose of the VRDisplay
  69. * when the current frame will be presented. The value returned will not
  70. * change until JavaScript has returned control to the browser.
  71. *
  72. * The VRPose will contain the position, orientation, velocity,
  73. * and acceleration of each of these properties.
  74. * @returns the pose object
  75. */
  76. getPose(): VRPose;
  77. /**
  78. * Return the current instantaneous pose of the VRDisplay, with no
  79. * prediction applied.
  80. * @returns the current instantaneous pose
  81. */
  82. getImmediatePose(): VRPose;
  83. /**
  84. * The callback passed to `requestAnimationFrame` will be called
  85. * any time a new frame should be rendered. When the VRDisplay is
  86. * presenting the callback will be called at the native refresh
  87. * rate of the HMD. When not presenting this function acts
  88. * identically to how window.requestAnimationFrame acts. Content should
  89. * make no assumptions of frame rate or vsync behavior as the HMD runs
  90. * asynchronously from other displays and at differing refresh rates.
  91. * @param callback Define the eaction to run next frame
  92. * @returns the request handle it
  93. */
  94. requestAnimationFrame(callback: FrameRequestCallback): number;
  95. /**
  96. * Begin presenting to the VRDisplay. Must be called in response to a user gesture.
  97. * Repeat calls while already presenting will update the VRLayers being displayed.
  98. * @param layers Define the list of layer to present
  99. * @returns a promise to know when the request has been fulfilled
  100. */
  101. requestPresent(layers: VRLayer[]): Promise<void>;
  102. /**
  103. * Reset the pose for this display, treating its current position and
  104. * orientation as the "origin/zero" values. VRPose.position,
  105. * VRPose.orientation, and VRStageParameters.sittingToStandingTransform may be
  106. * updated when calling resetPose(). This should be called in only
  107. * sitting-space experiences.
  108. */
  109. resetPose(): void;
  110. /**
  111. * The VRLayer provided to the VRDisplay will be captured and presented
  112. * in the HMD. Calling this function has the same effect on the source
  113. * canvas as any other operation that uses its source image, and canvases
  114. * created without preserveDrawingBuffer set to true will be cleared.
  115. * @param pose Define the pose to submit
  116. */
  117. submitFrame(pose?: VRPose): void;
  118. }
  119. declare var VRDisplay: {
  120. prototype: VRDisplay;
  121. new(): VRDisplay;
  122. };
  123. interface VRLayer {
  124. leftBounds?: number[] | Float32Array | null;
  125. rightBounds?: number[] | Float32Array | null;
  126. source?: HTMLCanvasElement | null;
  127. }
  128. interface VRDisplayCapabilities {
  129. readonly canPresent: boolean;
  130. readonly hasExternalDisplay: boolean;
  131. readonly hasOrientation: boolean;
  132. readonly hasPosition: boolean;
  133. readonly maxLayers: number;
  134. }
  135. interface VREyeParameters {
  136. /** @deprecated */
  137. readonly fieldOfView: VRFieldOfView;
  138. readonly offset: Float32Array;
  139. readonly renderHeight: number;
  140. readonly renderWidth: number;
  141. }
  142. interface VRFieldOfView {
  143. readonly downDegrees: number;
  144. readonly leftDegrees: number;
  145. readonly rightDegrees: number;
  146. readonly upDegrees: number;
  147. }
  148. interface VRFrameData {
  149. readonly leftProjectionMatrix: Float32Array;
  150. readonly leftViewMatrix: Float32Array;
  151. readonly pose: VRPose;
  152. readonly rightProjectionMatrix: Float32Array;
  153. readonly rightViewMatrix: Float32Array;
  154. readonly timestamp: number;
  155. }
  156. interface VRPose {
  157. readonly angularAcceleration: Float32Array | null;
  158. readonly angularVelocity: Float32Array | null;
  159. readonly linearAcceleration: Float32Array | null;
  160. readonly linearVelocity: Float32Array | null;
  161. readonly orientation: Float32Array | null;
  162. readonly position: Float32Array | null;
  163. readonly timestamp: number;
  164. }
  165. interface VRStageParameters {
  166. sittingToStandingTransform?: Float32Array;
  167. sizeX?: number;
  168. sizeY?: number;
  169. }
  170. interface Navigator {
  171. getVRDisplays(): Promise<VRDisplay[]>;
  172. readonly activeVRDisplays: ReadonlyArray<VRDisplay>;
  173. }
  174. interface Window {
  175. onvrdisplayconnected: ((this: Window, ev: Event) => any) | null;
  176. onvrdisplaydisconnected: ((this: Window, ev: Event) => any) | null;
  177. onvrdisplaypresentchange: ((this: Window, ev: Event) => any) | null;
  178. addEventListener(type: "vrdisplayconnected", listener: (ev: Event) => any, useCapture?: boolean): void;
  179. addEventListener(type: "vrdisplaydisconnected", listener: (ev: Event) => any, useCapture?: boolean): void;
  180. addEventListener(type: "vrdisplaypresentchange", listener: (ev: Event) => any, useCapture?: boolean): void;
  181. }
  182. interface Gamepad {
  183. readonly displayId: number;
  184. }