WebXRBackgroundRemover.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { WebXRFeaturesManager, WebXRFeatureName } from "../webXRFeaturesManager";
  2. import { WebXRSessionManager } from '../webXRSessionManager';
  3. import { AbstractMesh } from '../../Meshes/abstractMesh';
  4. import { Observable } from '../../Misc/observable';
  5. import { WebXRAbstractFeature } from './WebXRAbstractFeature';
  6. /**
  7. * Options interface for the background remover plugin
  8. */
  9. export interface IWebXRBackgroundRemoverOptions {
  10. /**
  11. * Further background meshes to disable when entering AR
  12. */
  13. backgroundMeshes?: AbstractMesh[];
  14. /**
  15. * flags to configure the removal of the environment helper.
  16. * If not set, the entire background will be removed. If set, flags should be set as well.
  17. */
  18. environmentHelperRemovalFlags?: {
  19. /**
  20. * Should the skybox be removed (default false)
  21. */
  22. skyBox?: boolean;
  23. /**
  24. * Should the ground be removed (default false)
  25. */
  26. ground?: boolean;
  27. };
  28. /**
  29. * don't disable the environment helper
  30. */
  31. ignoreEnvironmentHelper?: boolean;
  32. }
  33. /**
  34. * A module that will automatically disable background meshes when entering AR and will enable them when leaving AR.
  35. */
  36. export class WebXRBackgroundRemover extends WebXRAbstractFeature {
  37. /**
  38. * The module's name
  39. */
  40. public static readonly Name = WebXRFeatureName.BACKGROUND_REMOVER;
  41. /**
  42. * The (Babylon) version of this module.
  43. * This is an integer representing the implementation version.
  44. * This number does not correspond to the WebXR specs version
  45. */
  46. public static readonly Version = 1;
  47. /**
  48. * registered observers will be triggered when the background state changes
  49. */
  50. public onBackgroundStateChangedObservable: Observable<boolean> = new Observable();
  51. /**
  52. * constructs a new background remover module
  53. * @param _xrSessionManager the session manager for this module
  54. * @param options read-only options to be used in this module
  55. */
  56. constructor(_xrSessionManager: WebXRSessionManager,
  57. /**
  58. * read-only options to be used in this module
  59. */
  60. public readonly options: IWebXRBackgroundRemoverOptions = {}) {
  61. super(_xrSessionManager);
  62. }
  63. /**
  64. * attach this feature
  65. * Will usually be called by the features manager
  66. *
  67. * @returns true if successful.
  68. */
  69. public attach(): boolean {
  70. this._setBackgroundState(false);
  71. return super.attach();
  72. }
  73. /**
  74. * detach this feature.
  75. * Will usually be called by the features manager
  76. *
  77. * @returns true if successful.
  78. */
  79. public detach(): boolean {
  80. this._setBackgroundState(true);
  81. return super.detach();
  82. }
  83. /**
  84. * Dispose this feature and all of the resources attached
  85. */
  86. public dispose(): void {
  87. super.dispose();
  88. this.onBackgroundStateChangedObservable.clear();
  89. }
  90. protected _onXRFrame(_xrFrame: XRFrame) {
  91. // no-op
  92. }
  93. private _setBackgroundState(newState: boolean) {
  94. const scene = this._xrSessionManager.scene;
  95. if (!this.options.ignoreEnvironmentHelper) {
  96. if (this.options.environmentHelperRemovalFlags) {
  97. if (this.options.environmentHelperRemovalFlags.skyBox) {
  98. const backgroundSkybox = scene.getMeshByName("BackgroundSkybox");
  99. if (backgroundSkybox) {
  100. backgroundSkybox.setEnabled(newState);
  101. }
  102. }
  103. if (this.options.environmentHelperRemovalFlags.ground) {
  104. const backgroundPlane = scene.getMeshByName("BackgroundPlane");
  105. if (backgroundPlane) {
  106. backgroundPlane.setEnabled(newState);
  107. }
  108. }
  109. } else {
  110. const backgroundHelper = scene.getMeshByName("BackgroundHelper");
  111. if (backgroundHelper) {
  112. backgroundHelper.setEnabled(newState);
  113. }
  114. }
  115. }
  116. if (this.options.backgroundMeshes) {
  117. this.options.backgroundMeshes.forEach((mesh) => mesh.setEnabled(newState));
  118. }
  119. this.onBackgroundStateChangedObservable.notifyObservers(newState);
  120. }
  121. }
  122. //register the plugin
  123. WebXRFeaturesManager.AddWebXRFeature(WebXRBackgroundRemover.Name, (xrSessionManager, options) => {
  124. return () => new WebXRBackgroundRemover(xrSessionManager, options);
  125. }, WebXRBackgroundRemover.Version, true);