webXREnterExitUI.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { Nullable } from "../types";
  2. import { Observable } from "../Misc/observable";
  3. import { IDisposable, Scene } from "../scene";
  4. import { WebXRExperienceHelper } from "./webXRExperienceHelper";
  5. import { WebXRState, WebXRRenderTarget } from './webXRTypes';
  6. /**
  7. * Button which can be used to enter a different mode of XR
  8. */
  9. export class WebXREnterExitUIButton {
  10. /**
  11. * Creates a WebXREnterExitUIButton
  12. * @param element button element
  13. * @param sessionMode XR initialization session mode
  14. * @param referenceSpaceType the type of reference space to be used
  15. */
  16. constructor(
  17. /** button element */
  18. public element: HTMLElement,
  19. /** XR initialization options for the button */
  20. public sessionMode: XRSessionMode,
  21. /** Reference space type */
  22. public referenceSpaceType: XRReferenceSpaceType
  23. ) { }
  24. /**
  25. * Extendable function which can be used to update the button's visuals when the state changes
  26. * @param activeButton the current active button in the UI
  27. */
  28. public update(activeButton: Nullable<WebXREnterExitUIButton>) {
  29. }
  30. }
  31. /**
  32. * Options to create the webXR UI
  33. */
  34. export class WebXREnterExitUIOptions {
  35. /**
  36. * User provided buttons to enable/disable WebXR. The system will provide default if not set
  37. */
  38. customButtons?: Array<WebXREnterExitUIButton>;
  39. /**
  40. * A reference space type to use when creating the default button.
  41. * Default is local-floor
  42. */
  43. referenceSpaceType?: XRReferenceSpaceType;
  44. /**
  45. * Context to enter xr with
  46. */
  47. renderTarget?: Nullable<WebXRRenderTarget>;
  48. /**
  49. * A session mode to use when creating the default button.
  50. * Default is immersive-vr
  51. */
  52. sessionMode?: XRSessionMode;
  53. }
  54. /**
  55. * UI to allow the user to enter/exit XR mode
  56. */
  57. export class WebXREnterExitUI implements IDisposable {
  58. private _activeButton: Nullable<WebXREnterExitUIButton> = null;
  59. private _buttons: Array<WebXREnterExitUIButton> = [];
  60. private _overlay: HTMLDivElement;
  61. /**
  62. * Fired every time the active button is changed.
  63. *
  64. * When xr is entered via a button that launches xr that button will be the callback parameter
  65. *
  66. * When exiting xr the callback parameter will be null)
  67. */
  68. public activeButtonChangedObservable = new Observable<Nullable<WebXREnterExitUIButton>>();
  69. /**
  70. *
  71. * @param scene babylon scene object to use
  72. * @param options (read-only) version of the options passed to this UI
  73. */
  74. private constructor(
  75. private scene: Scene,
  76. /** version of the options passed to this UI */
  77. public options: WebXREnterExitUIOptions
  78. ) {
  79. this._overlay = document.createElement("div");
  80. this._overlay.style.cssText = "z-index:11;position: absolute; right: 20px;bottom: 50px;";
  81. if (options.customButtons) {
  82. this._buttons = options.customButtons;
  83. } else {
  84. const sessionMode = options.sessionMode || "immersive-vr";
  85. const referenceSpaceType = options.referenceSpaceType || "local-floor";
  86. const url = !window.SVGSVGElement ? "https://cdn.babylonjs.com/Assets/vrButton.png" : "data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%222048%22%20height%3D%221152%22%20viewBox%3D%220%200%202048%201152%22%20version%3D%221.1%22%3E%3Cpath%20transform%3D%22rotate%28180%201024%2C576.0000000000001%29%22%20d%3D%22m1109%2C896q17%2C0%2030%2C-12t13%2C-30t-12.5%2C-30.5t-30.5%2C-12.5l-170%2C0q-18%2C0%20-30.5%2C12.5t-12.5%2C30.5t13%2C30t30%2C12l170%2C0zm-85%2C256q59%2C0%20132.5%2C-1.5t154.5%2C-5.5t164.5%2C-11.5t163%2C-20t150%2C-30t124.5%2C-41.5q23%2C-11%2042%2C-24t38%2C-30q27%2C-25%2041%2C-61.5t14%2C-72.5l0%2C-257q0%2C-123%20-47%2C-232t-128%2C-190t-190%2C-128t-232%2C-47l-81%2C0q-37%2C0%20-68.5%2C14t-60.5%2C34.5t-55.5%2C45t-53%2C45t-53%2C34.5t-55.5%2C14t-55.5%2C-14t-53%2C-34.5t-53%2C-45t-55.5%2C-45t-60.5%2C-34.5t-68.5%2C-14l-81%2C0q-123%2C0%20-232%2C47t-190%2C128t-128%2C190t-47%2C232l0%2C257q0%2C68%2038%2C115t97%2C73q54%2C24%20124.5%2C41.5t150%2C30t163%2C20t164.5%2C11.5t154.5%2C5.5t132.5%2C1.5zm939%2C-298q0%2C39%20-24.5%2C67t-58.5%2C42q-54%2C23%20-122%2C39.5t-143.5%2C28t-155.5%2C19t-157%2C11t-148.5%2C5t-129.5%2C1.5q-59%2C0%20-130%2C-1.5t-148%2C-5t-157%2C-11t-155.5%2C-19t-143.5%2C-28t-122%2C-39.5q-34%2C-14%20-58.5%2C-42t-24.5%2C-67l0%2C-257q0%2C-106%2040.5%2C-199t110%2C-162.5t162.5%2C-109.5t199%2C-40l81%2C0q27%2C0%2052%2C14t50%2C34.5t51%2C44.5t55.5%2C44.5t63.5%2C34.5t74%2C14t74%2C-14t63.5%2C-34.5t55.5%2C-44.5t51%2C-44.5t50%2C-34.5t52%2C-14l14%2C0q37%2C0%2070%2C0.5t64.5%2C4.5t63.5%2C12t68%2C23q71%2C30%20128.5%2C78.5t98.5%2C110t63.5%2C133.5t22.5%2C149l0%2C257z%22%20fill%3D%22white%22%20/%3E%3C/svg%3E%0A";
  87. var css = ".babylonVRicon { color: #868686; border-color: #868686; border-style: solid; margin-left: 10px; height: 50px; width: 80px; background-color: rgba(51,51,51,0.7); background-image: url(" + url + "); background-size: 80%; background-repeat:no-repeat; background-position: center; border: none; outline: none; transition: transform 0.125s ease-out } .babylonVRicon:hover { transform: scale(1.05) } .babylonVRicon:active {background-color: rgba(51,51,51,1) } .babylonVRicon:focus {background-color: rgba(51,51,51,1) }";
  88. css += ".babylonVRicon.vrdisplaypresenting { background-image: none;} .vrdisplaypresenting::after { content: \"EXIT\"} .xr-error::after { content: \"ERROR\"}";
  89. var style = document.createElement('style');
  90. style.appendChild(document.createTextNode(css));
  91. document.getElementsByTagName('head')[0].appendChild(style);
  92. var hmdBtn = document.createElement("button");
  93. hmdBtn.className = "babylonVRicon";
  94. hmdBtn.title = `${sessionMode} - ${referenceSpaceType}`;
  95. this._buttons.push(new WebXREnterExitUIButton(hmdBtn, sessionMode, referenceSpaceType));
  96. this._buttons[this._buttons.length - 1].update = function(activeButton: WebXREnterExitUIButton) {
  97. this.element.style.display = (activeButton === null || activeButton === this) ? "" : "none";
  98. hmdBtn.className = "babylonVRicon" + (activeButton === this ? " vrdisplaypresenting" : "");
  99. };
  100. this._updateButtons(null);
  101. }
  102. var renderCanvas = scene.getEngine().getInputElement();
  103. if (renderCanvas && renderCanvas.parentNode) {
  104. renderCanvas.parentNode.appendChild(this._overlay);
  105. scene.onDisposeObservable.addOnce(() => {
  106. this.dispose();
  107. });
  108. }
  109. }
  110. /**
  111. * Creates UI to allow the user to enter/exit XR mode
  112. * @param scene the scene to add the ui to
  113. * @param helper the xr experience helper to enter/exit xr with
  114. * @param options options to configure the UI
  115. * @returns the created ui
  116. */
  117. public static CreateAsync(scene: Scene, helper: WebXRExperienceHelper, options: WebXREnterExitUIOptions): Promise<WebXREnterExitUI> {
  118. var ui = new WebXREnterExitUI(scene, options);
  119. var supportedPromises = ui._buttons.map((btn) => {
  120. return helper.sessionManager.isSessionSupportedAsync(btn.sessionMode);
  121. });
  122. helper.onStateChangedObservable.add((state) => {
  123. if (state == WebXRState.NOT_IN_XR) {
  124. ui._updateButtons(null);
  125. }
  126. });
  127. return Promise.all(supportedPromises).then((results) => {
  128. results.forEach((supported, i) => {
  129. if (supported) {
  130. ui._overlay.appendChild(ui._buttons[i].element);
  131. ui._buttons[i].element.onclick = async() => {
  132. if (helper.state == WebXRState.IN_XR) {
  133. await helper.exitXRAsync();
  134. ui._updateButtons(null);
  135. } else if (helper.state == WebXRState.NOT_IN_XR) {
  136. if (options.renderTarget) {
  137. try {
  138. await helper.enterXRAsync(ui._buttons[i].sessionMode, ui._buttons[i].referenceSpaceType, options.renderTarget);
  139. ui._updateButtons(ui._buttons[i]);
  140. } catch (e) {
  141. // make sure button is visible
  142. ui._updateButtons(null);
  143. const element = ui._buttons[i].element;
  144. const prevTitle = element.title;
  145. element.title = "Error entering XR session : " + prevTitle;
  146. element.classList.add("xr-error");
  147. }
  148. }
  149. }
  150. };
  151. }
  152. });
  153. return ui;
  154. });
  155. }
  156. /**
  157. * Disposes of the XR UI component
  158. */
  159. public dispose() {
  160. var renderCanvas = this.scene.getEngine().getInputElement();
  161. if (renderCanvas && renderCanvas.parentNode && renderCanvas.parentNode.contains(this._overlay)) {
  162. renderCanvas.parentNode.removeChild(this._overlay);
  163. }
  164. this.activeButtonChangedObservable.clear();
  165. }
  166. private _updateButtons(activeButton: Nullable<WebXREnterExitUIButton>) {
  167. this._activeButton = activeButton;
  168. this._buttons.forEach((b) => {
  169. b.update(this._activeButton);
  170. });
  171. this.activeButtonChangedObservable.notifyObservers(this._activeButton);
  172. }
  173. }