WebXRDOMOverlay.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Tools } from "../../Misc/tools.js";
  2. import { WebXRFeatureName, WebXRFeaturesManager } from "../webXRFeaturesManager.js";
  3. import { WebXRAbstractFeature } from "./WebXRAbstractFeature.js";
  4. /**
  5. * DOM Overlay Feature
  6. *
  7. * @since 5.0.0
  8. */
  9. export class WebXRDomOverlay extends WebXRAbstractFeature {
  10. /**
  11. * Creates a new instance of the dom-overlay feature
  12. * @param _xrSessionManager an instance of WebXRSessionManager
  13. * @param options options to use when constructing this feature
  14. */
  15. constructor(_xrSessionManager,
  16. /**
  17. * options to use when constructing this feature
  18. */
  19. options) {
  20. super(_xrSessionManager);
  21. this.options = options;
  22. /**
  23. * Type of overlay - non-null when available
  24. */
  25. this._domOverlayType = null;
  26. /**
  27. * Event Listener to supress "beforexrselect" events.
  28. */
  29. this._beforeXRSelectListener = null;
  30. /**
  31. * Element used for overlay
  32. */
  33. this._element = null;
  34. this.xrNativeFeatureName = "dom-overlay";
  35. // https://immersive-web.github.io/dom-overlays/
  36. Tools.Warn("dom-overlay is an experimental and unstable feature.");
  37. }
  38. /**
  39. * attach this feature
  40. * Will usually be called by the features manager
  41. *
  42. * @returns true if successful.
  43. */
  44. attach() {
  45. if (!super.attach()) {
  46. return false;
  47. }
  48. // Feature not available
  49. if (!this._xrSessionManager.session.domOverlayState || this._xrSessionManager.session.domOverlayState.type === null) {
  50. return false;
  51. }
  52. this._domOverlayType = this._xrSessionManager.session.domOverlayState.type;
  53. if (this._element !== null && this.options.supressXRSelectEvents === true) {
  54. this._beforeXRSelectListener = (ev) => {
  55. ev.preventDefault();
  56. };
  57. this._element.addEventListener("beforexrselect", this._beforeXRSelectListener);
  58. }
  59. return true;
  60. }
  61. /**
  62. * The type of DOM overlay (null when not supported). Provided by UA and remains unchanged for duration of session.
  63. */
  64. get domOverlayType() {
  65. return this._domOverlayType;
  66. }
  67. /**
  68. * Dispose this feature and all of the resources attached
  69. */
  70. dispose() {
  71. super.dispose();
  72. if (this._element !== null && this._beforeXRSelectListener) {
  73. this._element.removeEventListener("beforexrselect", this._beforeXRSelectListener);
  74. }
  75. }
  76. _onXRFrame(_xrFrame) {
  77. /* empty */
  78. }
  79. /**
  80. * Extends the session init object if needed
  81. * @returns augmentation object for the xr session init object.
  82. */
  83. async getXRSessionInitExtension() {
  84. if (this.options.element === undefined) {
  85. Tools.Warn('"element" option must be provided to attach xr-dom-overlay feature.');
  86. return {};
  87. }
  88. else if (typeof this.options.element === "string") {
  89. const selectedElement = document.querySelector(this.options.element);
  90. if (selectedElement === null) {
  91. Tools.Warn(`element not found '${this.options.element}' (not requesting xr-dom-overlay)`);
  92. return {};
  93. }
  94. this._element = selectedElement;
  95. }
  96. else {
  97. this._element = this.options.element;
  98. }
  99. return {
  100. domOverlay: {
  101. root: this._element,
  102. },
  103. };
  104. }
  105. }
  106. /**
  107. * The module's name
  108. */
  109. WebXRDomOverlay.Name = WebXRFeatureName.DOM_OVERLAY;
  110. /**
  111. * The (Babylon) version of this module.
  112. * This is an integer representing the implementation version.
  113. * This number does not correspond to the WebXR specs version
  114. */
  115. WebXRDomOverlay.Version = 1;
  116. //register the plugin
  117. WebXRFeaturesManager.AddWebXRFeature(WebXRDomOverlay.Name, (xrSessionManager, options) => {
  118. return () => new WebXRDomOverlay(xrSessionManager, options);
  119. }, WebXRDomOverlay.Version, false);
  120. //# sourceMappingURL=WebXRDOMOverlay.js.map