WebXRBackgroundRemover.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { WebXRFeaturesManager, WebXRFeatureName } from "../webXRFeaturesManager.js";
  2. import { Observable } from "../../Misc/observable.js";
  3. import { WebXRAbstractFeature } from "./WebXRAbstractFeature.js";
  4. /**
  5. * A module that will automatically disable background meshes when entering AR and will enable them when leaving AR.
  6. */
  7. export class WebXRBackgroundRemover extends WebXRAbstractFeature {
  8. /**
  9. * constructs a new background remover module
  10. * @param _xrSessionManager the session manager for this module
  11. * @param options read-only options to be used in this module
  12. */
  13. constructor(_xrSessionManager,
  14. /**
  15. * read-only options to be used in this module
  16. */
  17. options = {}) {
  18. super(_xrSessionManager);
  19. this.options = options;
  20. /**
  21. * registered observers will be triggered when the background state changes
  22. */
  23. this.onBackgroundStateChangedObservable = new Observable();
  24. }
  25. /**
  26. * attach this feature
  27. * Will usually be called by the features manager
  28. *
  29. * @returns true if successful.
  30. */
  31. attach() {
  32. this._setBackgroundState(false);
  33. return super.attach();
  34. }
  35. /**
  36. * detach this feature.
  37. * Will usually be called by the features manager
  38. *
  39. * @returns true if successful.
  40. */
  41. detach() {
  42. this._setBackgroundState(true);
  43. return super.detach();
  44. }
  45. /**
  46. * Dispose this feature and all of the resources attached
  47. */
  48. dispose() {
  49. super.dispose();
  50. this.onBackgroundStateChangedObservable.clear();
  51. }
  52. _onXRFrame(_xrFrame) {
  53. // no-op
  54. }
  55. _setBackgroundState(newState) {
  56. const scene = this._xrSessionManager.scene;
  57. if (!this.options.ignoreEnvironmentHelper) {
  58. if (this.options.environmentHelperRemovalFlags) {
  59. if (this.options.environmentHelperRemovalFlags.skyBox) {
  60. const backgroundSkybox = scene.getMeshByName("BackgroundSkybox");
  61. if (backgroundSkybox) {
  62. backgroundSkybox.setEnabled(newState);
  63. }
  64. }
  65. if (this.options.environmentHelperRemovalFlags.ground) {
  66. const backgroundPlane = scene.getMeshByName("BackgroundPlane");
  67. if (backgroundPlane) {
  68. backgroundPlane.setEnabled(newState);
  69. }
  70. }
  71. }
  72. else {
  73. const backgroundHelper = scene.getMeshByName("BackgroundHelper");
  74. if (backgroundHelper) {
  75. backgroundHelper.setEnabled(newState);
  76. }
  77. }
  78. }
  79. if (this.options.backgroundMeshes) {
  80. this.options.backgroundMeshes.forEach((mesh) => mesh.setEnabled(newState));
  81. }
  82. this.onBackgroundStateChangedObservable.notifyObservers(newState);
  83. }
  84. }
  85. /**
  86. * The module's name
  87. */
  88. WebXRBackgroundRemover.Name = WebXRFeatureName.BACKGROUND_REMOVER;
  89. /**
  90. * The (Babylon) version of this module.
  91. * This is an integer representing the implementation version.
  92. * This number does not correspond to the WebXR specs version
  93. */
  94. WebXRBackgroundRemover.Version = 1;
  95. //register the plugin
  96. WebXRFeaturesManager.AddWebXRFeature(WebXRBackgroundRemover.Name, (xrSessionManager, options) => {
  97. return () => new WebXRBackgroundRemover(xrSessionManager, options);
  98. }, WebXRBackgroundRemover.Version, true);
  99. //# sourceMappingURL=WebXRBackgroundRemover.js.map