Browse Source

Merge pull request #6021 from TrevorDev/assetContainerFix

Asset container support environment texture, do not dispose all sounds in scene when AC is disposed
David Catuhe 6 years ago
parent
commit
234599997a

+ 1 - 0
dist/preview release/what's new.md

@@ -204,6 +204,7 @@
 - Inspector showing duplicate nodes when attached to gizmo ([TrevorDev](https://github.com/TrevorDev))
 - Add missing dependencies for files to support including them from a direct path (eg. import "@babylonjs/core/Helpers/sceneHelpers";) ([TrevorDev](https://github.com/TrevorDev))
 - Fix a bug with mesh visibility in PBR materials ([bghgary](https://github.com/bghgary))
+- AssetContainer should not dispose objects it doesn't contain, Support for environmentTexture ([TrevorDev](https://github.com/TrevorDev))
 
 ### Core Engine
 - Fixed a bug with `mesh.alwaysSelectAsActiveMesh` preventing layerMask to be taken in account ([Deltakosh](https://github.com/deltakosh))

+ 5 - 1
src/Audio/audioSceneComponent.ts

@@ -275,8 +275,9 @@ export class AudioSceneComponent implements ISceneSerializableComponent {
     /**
      * Removes all the elements in the container from the scene
      * @param container contains the elements to remove
+     * @param dispose if the removed element should be disposed (default: false)
      */
-    public removeFromContainer(container: AbstractScene): void {
+    public removeFromContainer(container: AbstractScene, dispose = false): void {
         if (!container.sounds) {
             return;
         }
@@ -284,6 +285,9 @@ export class AudioSceneComponent implements ISceneSerializableComponent {
             sound.stop();
             sound.autoplay = false;
             this.scene.mainSoundTrack.RemoveSound(sound);
+            if (dispose) {
+                sound.dispose();
+            }
         });
     }
 

+ 5 - 1
src/Layers/effectLayerSceneComponent.ts

@@ -149,13 +149,17 @@ export class EffectLayerSceneComponent implements ISceneSerializableComponent {
     /**
      * Removes all the elements in the container from the scene
      * @param container contains the elements to remove
+     * @param dispose if the removed element should be disposed (default: false)
      */
-    public removeFromContainer(container: AbstractScene): void {
+    public removeFromContainer(container: AbstractScene, dispose?: boolean): void {
         if (!container.effectLayers) {
             return;
         }
         container.effectLayers.forEach((o) => {
             this.scene.removeEffectLayer(o);
+            if (dispose) {
+                o.dispose();
+            }
         });
     }
 

+ 5 - 1
src/LensFlares/lensFlareSystemSceneComponent.ts

@@ -149,13 +149,17 @@ export class LensFlareSystemSceneComponent implements ISceneSerializableComponen
     /**
      * Removes all the elements in the container from the scene
      * @param container contains the elements to remove
+     * @param dispose if the removed element should be disposed (default: false)
      */
-    public removeFromContainer(container: AbstractScene): void {
+    public removeFromContainer(container: AbstractScene, dispose?: boolean): void {
         if (!container.lensFlareSystems) {
             return;
         }
         container.lensFlareSystems.forEach((o) => {
             this.scene.removeLensFlareSystem(o);
+            if (dispose) {
+                o.dispose();
+            }
         });
     }
 

+ 2 - 1
src/Lights/Shadows/shadowGeneratorSceneComponent.ts

@@ -83,8 +83,9 @@ export class ShadowGeneratorSceneComponent implements ISceneSerializableComponen
     /**
      * Removes all the elements in the container from the scene
      * @param container contains the elements to remove
+     * @param dispose if the removed element should be disposed (default: false)
      */
-    public removeFromContainer(container: AbstractScene): void {
+    public removeFromContainer(container: AbstractScene, dispose?: boolean): void {
         // Nothing To Do Here. (directly attached to a light)
     }
 

+ 27 - 0
src/Loading/Plugins/babylonFileLoader.ts

@@ -74,6 +74,33 @@ var loadAssetContainer = (scene: Scene, data: string, rootUrl: string, onError?:
 
         var index: number;
         var cache: number;
+
+        // Environment texture
+        if (parsedData.environmentTexture !== undefined && parsedData.environmentTexture !== null) {
+            // PBR needed for both HDR texture (gamma space) & a sky box
+            var isPBR = parsedData.isPBR !== undefined ? parsedData.isPBR : true;
+            if (parsedData.environmentTextureType && parsedData.environmentTextureType === "BABYLON.HDRCubeTexture") {
+                var hdrSize: number = (parsedData.environmentTextureSize) ? parsedData.environmentTextureSize : 128;
+                var hdrTexture = new HDRCubeTexture((parsedData.environmentTexture.match(/https?:\/\//g) ? "" : rootUrl) + parsedData.environmentTexture, scene, hdrSize, true, !isPBR);
+                if (parsedData.environmentTextureRotationY) {
+                    hdrTexture.rotationY = parsedData.environmentTextureRotationY;
+                }
+                scene.environmentTexture = hdrTexture;
+            } else {
+                var cubeTexture = CubeTexture.CreateFromPrefilteredData((parsedData.environmentTexture.match(/https?:\/\//g) ? "" : rootUrl) + parsedData.environmentTexture, scene);
+                if (parsedData.environmentTextureRotationY) {
+                    cubeTexture.rotationY = parsedData.environmentTextureRotationY;
+                }
+                scene.environmentTexture = cubeTexture;
+            }
+            if (parsedData.createDefaultSkybox === true) {
+                var skyboxScale = (scene.activeCamera !== undefined && scene.activeCamera !== null) ? (scene.activeCamera.maxZ - scene.activeCamera.minZ) / 2 : 1000;
+                var skyboxBlurLevel = parsedData.skyboxBlurLevel || 0;
+                scene.createDefaultSkybox(scene.environmentTexture, isPBR, skyboxScale, skyboxBlurLevel);
+            }
+            container.environmentTexture = scene.environmentTexture;
+        }
+
         // Lights
         if (parsedData.lights !== undefined && parsedData.lights !== null) {
             for (index = 0, cache = parsedData.lights.length; index < cache; index++) {

+ 5 - 0
src/abstractScene.ts

@@ -193,4 +193,9 @@ export abstract class AbstractScene {
      * Textures to keep.
      */
     public textures = new Array<BaseTexture>();
+
+    /**
+     * Environment texture for the scene
+     */
+    public environmentTexture: Nullable<BaseTexture> = null;
 }

+ 11 - 1
src/assetContainer.ts

@@ -78,6 +78,8 @@ export class AssetContainer extends AbstractScene {
             this.scene.addReflectionProbe(o);
         });
 
+        this.scene.environmentTexture = this.environmentTexture;
+
         for (let component of this.scene._serializableComponents) {
             component.addFromContainer(this);
         }
@@ -130,6 +132,10 @@ export class AssetContainer extends AbstractScene {
             this.scene.removeReflectionProbe(o);
         });
 
+        if (this.environmentTexture == this.scene.environmentTexture) {
+            this.scene.environmentTexture = null;
+        }
+
         for (let component of this.scene._serializableComponents) {
             component.removeFromContainer(this);
         }
@@ -176,8 +182,12 @@ export class AssetContainer extends AbstractScene {
             o.dispose();
         });
 
+        if (this.scene.environmentTexture) {
+            this.scene.environmentTexture.dispose();
+        }
+
         for (let component of this.scene._serializableComponents) {
-            component.dispose();
+            component.removeFromContainer(this, true);
         }
     }
 

+ 2 - 1
src/sceneComponent.ts

@@ -129,8 +129,9 @@ export interface ISceneSerializableComponent extends ISceneComponent {
     /**
      * Removes all the elements in the container from the scene
      * @param container contains the elements to remove
+     * @param dispose if the removed element should be disposed (default: false)
      */
-    removeFromContainer(container: AbstractScene): void;
+    removeFromContainer(container: AbstractScene, dispose?: boolean): void;
 
     /**
      * Serializes the component data to the specified json object