WebXRBackgroundRemover.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { WebXRFeaturesManager, IWebXRFeature } from "../webXRFeaturesManager";
  2. import { WebXRSessionManager } from '../webXRSessionManager';
  3. import { AbstractMesh } from '../../../Meshes/abstractMesh';
  4. import { Observable } from '../../../Misc/observable';
  5. const Name = "xr-background-remover";
  6. /**
  7. * Options interface for the background remover plugin
  8. */
  9. export interface IWebXRBackgroundRemoverOptions {
  10. /**
  11. * don't disable the environment helper
  12. */
  13. ignoreEnvironmentHelper?: boolean;
  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. * Further background meshes to disable when entering AR
  30. */
  31. backgroundMeshes?: AbstractMesh[];
  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 implements IWebXRFeature {
  37. /**
  38. * The module's name
  39. */
  40. public static readonly Name = Name;
  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(private _xrSessionManager: WebXRSessionManager,
  57. /**
  58. * read-only options to be used in this module
  59. */
  60. public readonly options: IWebXRBackgroundRemoverOptions = {}) {
  61. }
  62. /**
  63. * attach this feature
  64. * Will usually be called by the features manager
  65. *
  66. * @returns true if successful.
  67. */
  68. attach(): boolean {
  69. this._setBackgroundState(false);
  70. return true;
  71. }
  72. /**
  73. * detach this feature.
  74. * Will usually be called by the features manager
  75. *
  76. * @returns true if successful.
  77. */
  78. detach(): boolean {
  79. this._setBackgroundState(true);
  80. return true;
  81. }
  82. private _setBackgroundState(newState: boolean) {
  83. const scene = this._xrSessionManager.scene;
  84. if (!this.options.ignoreEnvironmentHelper) {
  85. if (this.options.environmentHelperRemovalFlags) {
  86. if (this.options.environmentHelperRemovalFlags.skyBox) {
  87. const backgroundSkybox = scene.getMeshByName("BackgroundSkybox");
  88. if (backgroundSkybox) {
  89. backgroundSkybox.setEnabled(newState);
  90. }
  91. }
  92. if (this.options.environmentHelperRemovalFlags.ground) {
  93. const backgroundPlane = scene.getMeshByName("BackgroundPlane");
  94. if (backgroundPlane) {
  95. backgroundPlane.setEnabled(newState);
  96. }
  97. }
  98. } else {
  99. const backgroundHelper = scene.getMeshByName("BackgroundHelper");
  100. if (backgroundHelper) {
  101. backgroundHelper.setEnabled(newState);
  102. }
  103. }
  104. }
  105. if (this.options.backgroundMeshes) {
  106. this.options.backgroundMeshes.forEach((mesh) => mesh.setEnabled(newState));
  107. }
  108. this.onBackgroundStateChangedObservable.notifyObservers(newState);
  109. }
  110. /**
  111. * Dispose this feature and all of the resources attached
  112. */
  113. dispose(): void {
  114. this.onBackgroundStateChangedObservable.clear();
  115. }
  116. }
  117. //register the plugin
  118. WebXRFeaturesManager.AddWebXRFeature(WebXRBackgroundRemover.Name, (xrSessionManager, options) => {
  119. return () => new WebXRBackgroundRemover(xrSessionManager, options);
  120. }, WebXRBackgroundRemover.Version, true);