webXRDefaultExperience.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { WebXRExperienceHelper } from "./webXRExperienceHelper";
  2. import { Scene } from '../scene';
  3. import { WebXRInput, IWebXRInputOptions } from './webXRInput';
  4. import { WebXRControllerPointerSelection } from './features/WebXRControllerPointerSelection';
  5. import { WebXRRenderTarget } from './webXRTypes';
  6. import { WebXREnterExitUI, WebXREnterExitUIOptions } from './webXREnterExitUI';
  7. import { AbstractMesh } from '../Meshes/abstractMesh';
  8. import { WebXRManagedOutputCanvasOptions } from './webXRManagedOutputCanvas';
  9. import { WebXRMotionControllerTeleportation } from './features/WebXRControllerTeleportation';
  10. import { Logger } from '../Misc/logger';
  11. /**
  12. * Options for the default xr helper
  13. */
  14. export class WebXRDefaultExperienceOptions {
  15. /**
  16. * Floor meshes that will be used for teleporting
  17. */
  18. public floorMeshes?: Array<AbstractMesh>;
  19. /**
  20. * Enable or disable default UI to enter XR
  21. */
  22. public disableDefaultUI?: boolean;
  23. /**
  24. * optional configuration for the output canvas
  25. */
  26. public outputCanvasOptions?: WebXRManagedOutputCanvasOptions;
  27. /**
  28. * optional UI options. This can be used among other to change session mode and reference space type
  29. */
  30. public uiOptions?: WebXREnterExitUIOptions;
  31. /**
  32. * Disable the controller mesh-loading. Can be used if you want to load your own meshes
  33. */
  34. public inputOptions?: IWebXRInputOptions;
  35. /**
  36. * Should teleportation not initialize. defaults to false.
  37. */
  38. public disableTeleportation?: boolean;
  39. /**
  40. * If set to true, the first frame will not be used to reset position
  41. * The first frame is mainly used when copying transformation from the old camera
  42. * Mainly used in AR
  43. */
  44. public ignoreNativeCameraTransformation?: boolean;
  45. /**
  46. * When loading teleportation and pointer select, use stable versions instead of latest.
  47. */
  48. public useStablePlugins?: boolean;
  49. }
  50. /**
  51. * Default experience which provides a similar setup to the previous webVRExperience
  52. */
  53. export class WebXRDefaultExperience {
  54. /**
  55. * Base experience
  56. */
  57. public baseExperience: WebXRExperienceHelper;
  58. /**
  59. * Input experience extension
  60. */
  61. public input: WebXRInput;
  62. /**
  63. * Enables laser pointer and selection
  64. */
  65. public pointerSelection: WebXRControllerPointerSelection;
  66. /**
  67. * Enables teleportation
  68. */
  69. public teleportation: WebXRMotionControllerTeleportation;
  70. /**
  71. * Enables ui for entering/exiting xr
  72. */
  73. public enterExitUI: WebXREnterExitUI;
  74. /**
  75. * Default target xr should render to
  76. */
  77. public renderTarget: WebXRRenderTarget;
  78. /**
  79. * Creates the default xr experience
  80. * @param scene scene
  81. * @param options options for basic configuration
  82. * @returns resulting WebXRDefaultExperience
  83. */
  84. public static CreateAsync(scene: Scene, options: WebXRDefaultExperienceOptions = {}) {
  85. var result = new WebXRDefaultExperience();
  86. // Create base experience
  87. return WebXRExperienceHelper.CreateAsync(scene).then((xrHelper) => {
  88. result.baseExperience = xrHelper;
  89. if (options.ignoreNativeCameraTransformation) {
  90. result.baseExperience.camera.compensateOnFirstFrame = false;
  91. }
  92. // Add controller support
  93. result.input = new WebXRInput(xrHelper.sessionManager, xrHelper.camera, options.inputOptions);
  94. result.pointerSelection = <WebXRControllerPointerSelection>result.baseExperience.featuresManager.enableFeature(WebXRControllerPointerSelection.Name, options.useStablePlugins ? "stable" : "latest", {
  95. xrInput: result.input
  96. });
  97. // Add default teleportation, including rotation
  98. if (!options.disableTeleportation) {
  99. result.teleportation = <WebXRMotionControllerTeleportation>result.baseExperience.featuresManager.enableFeature(WebXRMotionControllerTeleportation.Name, options.useStablePlugins ? "stable" : "latest", {
  100. floorMeshes: options.floorMeshes,
  101. xrInput: result.input
  102. });
  103. result.teleportation.setSelectionFeature(result.pointerSelection);
  104. }
  105. // Create the WebXR output target
  106. result.renderTarget = result.baseExperience.sessionManager.getWebXRRenderTarget(options.outputCanvasOptions);
  107. if (!options.disableDefaultUI) {
  108. if (options.uiOptions) {
  109. options.uiOptions.renderTarget = options.uiOptions.renderTarget || result.renderTarget;
  110. }
  111. // Create ui for entering/exiting xr
  112. return WebXREnterExitUI.CreateAsync(scene, result.baseExperience, options.uiOptions || { renderTarget: result.renderTarget }).then((ui) => {
  113. result.enterExitUI = ui;
  114. });
  115. } else {
  116. return;
  117. }
  118. }).then(() => {
  119. return result;
  120. }).catch((error) => {
  121. Logger.Error("Error initializing XR");
  122. Logger.Error(error);
  123. return result;
  124. });
  125. }
  126. private constructor() {
  127. }
  128. /**
  129. * DIsposes of the experience helper
  130. */
  131. public dispose() {
  132. if (this.baseExperience) {
  133. this.baseExperience.dispose();
  134. }
  135. if (this.input) {
  136. this.input.dispose();
  137. }
  138. if (this.enterExitUI) {
  139. this.enterExitUI.dispose();
  140. }
  141. if (this.renderTarget) {
  142. this.renderTarget.dispose();
  143. }
  144. }
  145. }