vrExperienceHelper.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import { Observable } from "../../Misc/observable";
  2. import type { Nullable } from "../../types";
  3. import type { Camera } from "../../Cameras/camera";
  4. import { DeviceOrientationCamera } from "../../Cameras/deviceOrientationCamera";
  5. import { VRDeviceOrientationFreeCamera } from "../../Cameras/VR/vrDeviceOrientationFreeCamera";
  6. import type { Scene } from "../../scene";
  7. import { Vector3 } from "../../Maths/math.vector";
  8. import { Color3 } from "../../Maths/math.color";
  9. import type { AbstractMesh } from "../../Meshes/abstractMesh";
  10. import type { PickingInfo } from "../../Collisions/pickingInfo";
  11. import { EasingFunction } from "../../Animations/easing";
  12. import type { VRCameraMetrics } from "../../Cameras/VR/vrCameraMetrics";
  13. import "../../Gamepads/gamepadSceneComponent";
  14. import "../../Animations/animatable";
  15. import type { WebXRDefaultExperience } from "../../XR/webXRDefaultExperience";
  16. import type { Mesh } from "../../Meshes/mesh.js";
  17. /**
  18. * Options to modify the vr teleportation behavior.
  19. */
  20. export interface VRTeleportationOptions {
  21. /**
  22. * The name of the mesh which should be used as the teleportation floor. (default: null)
  23. */
  24. floorMeshName?: string;
  25. /**
  26. * A list of meshes to be used as the teleportation floor. (default: empty)
  27. */
  28. floorMeshes?: Mesh[];
  29. /**
  30. * The teleportation mode. (default: TELEPORTATIONMODE_CONSTANTTIME)
  31. */
  32. teleportationMode?: number;
  33. /**
  34. * The duration of the animation in ms, apply when animationMode is TELEPORTATIONMODE_CONSTANTTIME. (default 122ms)
  35. */
  36. teleportationTime?: number;
  37. /**
  38. * The speed of the animation in distance/sec, apply when animationMode is TELEPORTATIONMODE_CONSTANTSPEED. (default 20 units / sec)
  39. */
  40. teleportationSpeed?: number;
  41. /**
  42. * The easing function used in the animation or null for Linear. (default CircleEase)
  43. */
  44. easingFunction?: EasingFunction;
  45. }
  46. /**
  47. * Options to modify the vr experience helper's behavior.
  48. */
  49. export interface VRExperienceHelperOptions {
  50. /**
  51. * Create a DeviceOrientationCamera to be used as your out of vr camera. (default: true)
  52. */
  53. createDeviceOrientationCamera?: boolean;
  54. /**
  55. * Create a VRDeviceOrientationFreeCamera to be used for VR when no external HMD is found. (default: true)
  56. */
  57. createFallbackVRDeviceOrientationFreeCamera?: boolean;
  58. /**
  59. * Uses the main button on the controller to toggle the laser casted. (default: true)
  60. */
  61. laserToggle?: boolean;
  62. /**
  63. * A list of meshes to be used as the teleportation floor. If specified, teleportation will be enabled (default: undefined)
  64. */
  65. floorMeshes?: Mesh[];
  66. /**
  67. * Distortion metrics for the fallback vrDeviceOrientationCamera (default: VRCameraMetrics.Default)
  68. */
  69. vrDeviceOrientationCameraMetrics?: VRCameraMetrics;
  70. /**
  71. * Defines if WebXR should be used (if available)
  72. */
  73. useXR?: boolean;
  74. }
  75. /**
  76. * Event containing information after VR has been entered
  77. */
  78. export declare class OnAfterEnteringVRObservableEvent {
  79. /**
  80. * If entering vr was successful
  81. */
  82. success: boolean;
  83. }
  84. /**
  85. * Helps to quickly add VR support to an existing scene.
  86. * See https://doc.babylonjs.com/features/featuresDeepDive/cameras/webVRHelper
  87. * @deprecated Use WebXR instead!
  88. */
  89. export declare class VRExperienceHelper {
  90. /** Options to modify the vr experience helper's behavior. */
  91. webVROptions: VRExperienceHelperOptions;
  92. private _scene;
  93. private _position;
  94. private _btnVR;
  95. private _btnVRDisplayed;
  96. private _hasEnteredVR;
  97. private _fullscreenVRpresenting;
  98. private _inputElement;
  99. private _vrDeviceOrientationCamera;
  100. private _deviceOrientationCamera;
  101. private _existingCamera;
  102. private _onKeyDown;
  103. private _onVrDisplayPresentChangeBind;
  104. /**
  105. * Gets or sets a boolean indicating that gaze can be enabled even if pointer lock is not engage (useful on iOS where fullscreen mode and pointer lock are not supported)
  106. */
  107. enableGazeEvenWhenNoPointerLock: boolean;
  108. /**
  109. * Gets or sets a boolean indicating that the VREXperienceHelper will exit VR if double tap is detected
  110. */
  111. exitVROnDoubleTap: boolean;
  112. /**
  113. * Observable raised right before entering VR.
  114. */
  115. onEnteringVRObservable: Observable<VRExperienceHelper>;
  116. /**
  117. * Observable raised when entering VR has completed.
  118. */
  119. onAfterEnteringVRObservable: Observable<OnAfterEnteringVRObservableEvent>;
  120. /**
  121. * Observable raised when exiting VR.
  122. */
  123. onExitingVRObservable: Observable<VRExperienceHelper>;
  124. /** Return this.onEnteringVRObservable
  125. * Note: This one is for backward compatibility. Please use onEnteringVRObservable directly
  126. */
  127. get onEnteringVR(): Observable<VRExperienceHelper>;
  128. /** Return this.onExitingVRObservable
  129. * Note: This one is for backward compatibility. Please use onExitingVRObservable directly
  130. */
  131. get onExitingVR(): Observable<VRExperienceHelper>;
  132. private _useCustomVRButton;
  133. private _teleportActive;
  134. private _floorMeshName;
  135. private _floorMeshesCollection;
  136. private _teleportationMode;
  137. private _teleportationTime;
  138. private _teleportationSpeed;
  139. private _teleportationEasing;
  140. private _rotationAllowed;
  141. private _teleportBackwardsVector;
  142. private _teleportationTarget;
  143. private _isDefaultTeleportationTarget;
  144. private _postProcessMove;
  145. private _teleportationFillColor;
  146. private _teleportationBorderColor;
  147. private _rotationAngle;
  148. private _haloCenter;
  149. private _cameraGazer;
  150. private _padSensibilityUp;
  151. private _padSensibilityDown;
  152. private _pickedLaserColor;
  153. private _pickedGazeColor;
  154. /**
  155. * Observable raised when a new mesh is selected based on meshSelectionPredicate
  156. */
  157. onNewMeshSelected: Observable<AbstractMesh>;
  158. /**
  159. * Observable raised when a new mesh is picked based on meshSelectionPredicate
  160. */
  161. onNewMeshPicked: Observable<PickingInfo>;
  162. private _circleEase;
  163. /**
  164. * Observable raised before camera teleportation
  165. */
  166. onBeforeCameraTeleport: Observable<Vector3>;
  167. /**
  168. * Observable raised after camera teleportation
  169. */
  170. onAfterCameraTeleport: Observable<Vector3>;
  171. /**
  172. * Observable raised when current selected mesh gets unselected
  173. */
  174. onSelectedMeshUnselected: Observable<AbstractMesh>;
  175. private _raySelectionPredicate;
  176. /**
  177. * To be optionally changed by user to define custom ray selection
  178. */
  179. raySelectionPredicate: (mesh: AbstractMesh) => boolean;
  180. /**
  181. * To be optionally changed by user to define custom selection logic (after ray selection)
  182. */
  183. meshSelectionPredicate: (mesh: AbstractMesh) => boolean;
  184. /**
  185. * Set teleportation enabled. If set to false camera teleportation will be disabled but camera rotation will be kept.
  186. */
  187. teleportationEnabled: boolean;
  188. private _defaultHeight;
  189. private _teleportationInitialized;
  190. private _interactionsEnabled;
  191. private _displayGaze;
  192. private _displayLaserPointer;
  193. /**
  194. * The mesh used to display where the user is going to teleport.
  195. */
  196. get teleportationTarget(): Mesh;
  197. /**
  198. * Sets the mesh to be used to display where the user is going to teleport.
  199. */
  200. set teleportationTarget(value: Mesh);
  201. /**
  202. * The mesh used to display where the user is selecting, this mesh will be cloned and set as the gazeTracker for the left and right controller
  203. * when set bakeCurrentTransformIntoVertices will be called on the mesh.
  204. * See https://doc.babylonjs.com/features/featuresDeepDive/mesh/transforms/center_origin/bakingTransforms
  205. */
  206. get gazeTrackerMesh(): Mesh;
  207. set gazeTrackerMesh(value: Mesh);
  208. /**
  209. * If the gaze trackers scale should be updated to be constant size when pointing at near/far meshes
  210. */
  211. updateGazeTrackerScale: boolean;
  212. /**
  213. * If the gaze trackers color should be updated when selecting meshes
  214. */
  215. updateGazeTrackerColor: boolean;
  216. /**
  217. * If the controller laser color should be updated when selecting meshes
  218. */
  219. updateControllerLaserColor: boolean;
  220. /**
  221. * If the ray of the gaze should be displayed.
  222. */
  223. get displayGaze(): boolean;
  224. /**
  225. * Sets if the ray of the gaze should be displayed.
  226. */
  227. set displayGaze(value: boolean);
  228. /**
  229. * If the ray of the LaserPointer should be displayed.
  230. */
  231. get displayLaserPointer(): boolean;
  232. /**
  233. * Sets if the ray of the LaserPointer should be displayed.
  234. */
  235. set displayLaserPointer(value: boolean);
  236. /**
  237. * The deviceOrientationCamera used as the camera when not in VR.
  238. */
  239. get deviceOrientationCamera(): Nullable<DeviceOrientationCamera>;
  240. /**
  241. * Based on the current WebVR support, returns the current VR camera used.
  242. */
  243. get currentVRCamera(): Nullable<Camera>;
  244. /**
  245. * The deviceOrientationCamera that is used as a fallback when vr device is not connected.
  246. */
  247. get vrDeviceOrientationCamera(): Nullable<VRDeviceOrientationFreeCamera>;
  248. /**
  249. * The html button that is used to trigger entering into VR.
  250. */
  251. get vrButton(): Nullable<HTMLButtonElement>;
  252. private get _teleportationRequestInitiated();
  253. /**
  254. * Defines whether or not Pointer lock should be requested when switching to
  255. * full screen.
  256. */
  257. requestPointerLockOnFullScreen: boolean;
  258. /**
  259. * If asking to force XR, this will be populated with the default xr experience
  260. */
  261. xr: WebXRDefaultExperience;
  262. /**
  263. * Was the XR test done already. If this is true AND this.xr exists, xr is initialized.
  264. * If this is true and no this.xr, xr exists but is not supported, using WebVR.
  265. */
  266. xrTestDone: boolean;
  267. /**
  268. * Instantiates a VRExperienceHelper.
  269. * Helps to quickly add VR support to an existing scene.
  270. * @param scene The scene the VRExperienceHelper belongs to.
  271. * @param webVROptions Options to modify the vr experience helper's behavior.
  272. */
  273. constructor(scene: Scene,
  274. /** Options to modify the vr experience helper's behavior. */
  275. webVROptions?: VRExperienceHelperOptions);
  276. private _completeVRInit;
  277. private _onResize;
  278. private _onFullscreenChange;
  279. /**
  280. * Gets a value indicating if we are currently in VR mode.
  281. */
  282. get isInVRMode(): boolean;
  283. private _moveButtonToBottomRight;
  284. private _displayVRButton;
  285. private _updateButtonVisibility;
  286. private _cachedAngularSensibility;
  287. /**
  288. * Attempt to enter VR. If a headset is connected and ready, will request present on that.
  289. * Otherwise, will use the fullscreen API.
  290. */
  291. enterVR(): void;
  292. /**
  293. * Attempt to exit VR, or fullscreen.
  294. */
  295. exitVR(): void;
  296. /**
  297. * The position of the vr experience helper.
  298. */
  299. get position(): Vector3;
  300. /**
  301. * Sets the position of the vr experience helper.
  302. */
  303. set position(value: Vector3);
  304. /**
  305. * Enables controllers and user interactions such as selecting and object or clicking on an object.
  306. */
  307. enableInteractions(): void;
  308. private _beforeRender;
  309. private _isTeleportationFloor;
  310. /**
  311. * Adds a floor mesh to be used for teleportation.
  312. * @param floorMesh the mesh to be used for teleportation.
  313. */
  314. addFloorMesh(floorMesh: Mesh): void;
  315. /**
  316. * Removes a floor mesh from being used for teleportation.
  317. * @param floorMesh the mesh to be removed.
  318. */
  319. removeFloorMesh(floorMesh: Mesh): void;
  320. /**
  321. * Enables interactions and teleportation using the VR controllers and gaze.
  322. * @param vrTeleportationOptions options to modify teleportation behavior.
  323. */
  324. enableTeleportation(vrTeleportationOptions?: VRTeleportationOptions): void;
  325. private _onNewGamepadConnected;
  326. private _checkTeleportWithRay;
  327. private _checkRotate;
  328. private _checkTeleportBackwards;
  329. private _createTeleportationCircles;
  330. private _hideTeleportationTarget;
  331. private _rotateCamera;
  332. private _workingVector;
  333. private _workingQuaternion;
  334. private _workingMatrix;
  335. /**
  336. * Time Constant Teleportation Mode
  337. */
  338. static readonly TELEPORTATIONMODE_CONSTANTTIME = 0;
  339. /**
  340. * Speed Constant Teleportation Mode
  341. */
  342. static readonly TELEPORTATIONMODE_CONSTANTSPEED = 1;
  343. /**
  344. * Teleports the users feet to the desired location
  345. * @param location The location where the user's feet should be placed
  346. */
  347. teleportCamera(location: Vector3): void;
  348. /**
  349. * Permanently set new colors for the laser pointer
  350. * @param color the new laser color
  351. * @param pickedColor the new laser color when picked mesh detected
  352. */
  353. setLaserColor(color: Color3, pickedColor?: Color3): void;
  354. /**
  355. * Set lighting enabled / disabled on the laser pointer of both controllers
  356. * @param _enabled should the lighting be enabled on the laser pointer
  357. */
  358. setLaserLightingState(_enabled?: boolean): void;
  359. /**
  360. * Permanently set new colors for the gaze pointer
  361. * @param color the new gaze color
  362. * @param pickedColor the new gaze color when picked mesh detected
  363. */
  364. setGazeColor(color: Color3, pickedColor?: Color3): void;
  365. /**
  366. * Sets the color of the laser ray from the vr controllers.
  367. * @param _color new color for the ray.
  368. */
  369. changeLaserColor(_color: Color3): void;
  370. /**
  371. * Sets the color of the ray from the vr headsets gaze.
  372. * @param color new color for the ray.
  373. */
  374. changeGazeColor(color: Color3): void;
  375. /**
  376. * Exits VR and disposes of the vr experience helper
  377. */
  378. dispose(): void;
  379. /**
  380. * Gets the name of the VRExperienceHelper class
  381. * @returns "VRExperienceHelper"
  382. */
  383. getClassName(): string;
  384. }