WebXRBackgroundRemover.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { WebXRFeaturesManager, WebXRFeature } 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. //register the plugin
  7. WebXRFeaturesManager.AddWebXRFeature(Name, (xrSessionManager, options) => {
  8. return () => new WebXRBackgroundRemover(xrSessionManager, options);
  9. });
  10. export interface WebXRBackgroundRemoverOptions {
  11. ignoreEnvironmentHelper?: boolean;
  12. environmentHelperRemovalFlags?: {
  13. skyBox?: boolean;
  14. ground?: boolean;
  15. };
  16. backgroundMeshes?: AbstractMesh[];
  17. }
  18. export class WebXRBackgroundRemover implements WebXRFeature {
  19. public onBackgroundStateChanged: Observable<boolean> = new Observable();
  20. constructor(private xrSessionManager: WebXRSessionManager, public readonly options: WebXRBackgroundRemoverOptions = {}) {
  21. }
  22. attach(): boolean {
  23. this.setBackgroundState(false);
  24. return true;
  25. }
  26. detach(): boolean {
  27. this.setBackgroundState(true);
  28. return true;
  29. }
  30. private setBackgroundState(newState: boolean) {
  31. const scene = this.xrSessionManager.scene;
  32. if (!this.options.ignoreEnvironmentHelper) {
  33. if (this.options.environmentHelperRemovalFlags) {
  34. if (this.options.environmentHelperRemovalFlags.skyBox) {
  35. const backgroundSkybox = scene.getMeshByName("BackgroundSkybox");
  36. if (backgroundSkybox) {
  37. backgroundSkybox.setEnabled(newState);
  38. }
  39. }
  40. if (this.options.environmentHelperRemovalFlags.ground) {
  41. const backgroundPlane = scene.getMeshByName("BackgroundPlane");
  42. if (backgroundPlane) {
  43. backgroundPlane.setEnabled(newState);
  44. }
  45. }
  46. } else {
  47. const backgroundHelper = scene.getMeshByName("BackgroundHelper");
  48. if (backgroundHelper) {
  49. backgroundHelper.setEnabled(newState);
  50. }
  51. }
  52. }
  53. if (this.options.backgroundMeshes) {
  54. this.options.backgroundMeshes.forEach((mesh) => mesh.setEnabled(newState));
  55. }
  56. this.onBackgroundStateChanged.notifyObservers(newState);
  57. }
  58. dispose(): void {
  59. this.onBackgroundStateChanged.clear();
  60. }
  61. }