webXRDefaultExperience.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  11. * Options for the default xr helper
  12. */
  13. export class WebXRDefaultExperienceOptions {
  14. /**
  15. * Floor meshes that should be used for teleporting
  16. */
  17. public floorMeshes?: Array<AbstractMesh>;
  18. /**
  19. * Enable or disable default UI to enter XR
  20. */
  21. public disableDefaultUI?: boolean;
  22. /**
  23. * optional configuration for the output canvas
  24. */
  25. public outputCanvasOptions?: WebXRManagedOutputCanvasOptions;
  26. /**
  27. * optional UI options. This can be used among other to change session mode and reference space type
  28. */
  29. public uiOptions?: WebXREnterExitUIOptions;
  30. /**
  31. * Disable the controller mesh-loading. Can be used if you want to load your own meshes
  32. */
  33. public inputOptions?: IWebXRInputOptions;
  34. }
  35. /**
  36. * Default experience which provides a similar setup to the previous webVRExperience
  37. */
  38. export class WebXRDefaultExperience {
  39. /**
  40. * Base experience
  41. */
  42. public baseExperience: WebXRExperienceHelper;
  43. /**
  44. * Input experience extension
  45. */
  46. public input: WebXRInput;
  47. /**
  48. * Enables laser pointer and selection
  49. */
  50. public pointerSelection: WebXRControllerPointerSelection;
  51. /**
  52. * Enables teleportation
  53. */
  54. public teleportation: WebXRMotionControllerTeleportation;
  55. /**
  56. * Enables ui for entering/exiting xr
  57. */
  58. public enterExitUI: WebXREnterExitUI;
  59. /**
  60. * Default target xr should render to
  61. */
  62. public renderTarget: WebXRRenderTarget;
  63. /**
  64. * Creates the default xr experience
  65. * @param scene scene
  66. * @param options options for basic configuration
  67. * @returns resulting WebXRDefaultExperience
  68. */
  69. public static CreateAsync(scene: Scene, options: WebXRDefaultExperienceOptions = {}) {
  70. var result = new WebXRDefaultExperience();
  71. // Create base experience
  72. return WebXRExperienceHelper.CreateAsync(scene).then((xrHelper) => {
  73. result.baseExperience = xrHelper;
  74. // Add controller support
  75. result.input = new WebXRInput(xrHelper.sessionManager, xrHelper.camera, options.inputOptions);
  76. result.pointerSelection = <WebXRControllerPointerSelection>result.baseExperience.featuresManager.enableFeature(WebXRControllerPointerSelection.Name, "latest", {
  77. xrInput: result.input
  78. });
  79. if (options.floorMeshes) {
  80. result.teleportation = <WebXRMotionControllerTeleportation>result.baseExperience.featuresManager.enableFeature(WebXRMotionControllerTeleportation.Name, "latest", {
  81. floorMeshes: options.floorMeshes,
  82. xrInput: result.input
  83. });
  84. result.teleportation.setSelectionFeature(result.pointerSelection);
  85. }
  86. // Create the WebXR output target
  87. result.renderTarget = result.baseExperience.sessionManager.getWebXRRenderTarget(xrHelper.onStateChangedObservable, options.outputCanvasOptions);
  88. if (!options.disableDefaultUI) {
  89. if (options.uiOptions) {
  90. options.uiOptions.renderTarget = options.uiOptions.renderTarget || result.renderTarget;
  91. }
  92. // Create ui for entering/exiting xr
  93. return WebXREnterExitUI.CreateAsync(scene, result.baseExperience, options.uiOptions || { renderTarget: result.renderTarget }).then((ui) => {
  94. result.enterExitUI = ui;
  95. });
  96. } else {
  97. return;
  98. }
  99. }).then(() => {
  100. return result;
  101. });
  102. }
  103. private constructor() {
  104. }
  105. /**
  106. * DIsposes of the experience helper
  107. */
  108. public dispose() {
  109. if (this.baseExperience) {
  110. this.baseExperience.dispose();
  111. }
  112. if (this.input) {
  113. this.input.dispose();
  114. }
  115. if (this.enterExitUI) {
  116. this.enterExitUI.dispose();
  117. }
  118. if (this.renderTarget) {
  119. this.renderTarget.dispose();
  120. }
  121. }
  122. }