소스 검색

support environment texture in assetcontainer

Trevor Baron 6 년 전
부모
커밋
a521f821c8

+ 1 - 1
src/Audio/audioSceneComponent.ts

@@ -285,7 +285,7 @@ export class AudioSceneComponent implements ISceneSerializableComponent {
             sound.stop();
             sound.autoplay = false;
             this.scene.mainSoundTrack.RemoveSound(sound);
-            if(dispose){
+            if (dispose) {
                 sound.dispose();
             }
         });

+ 1 - 1
src/Layers/effectLayerSceneComponent.ts

@@ -157,7 +157,7 @@ export class EffectLayerSceneComponent implements ISceneSerializableComponent {
         }
         container.effectLayers.forEach((o) => {
             this.scene.removeEffectLayer(o);
-            if(dispose){
+            if (dispose) {
                 o.dispose();
             }
         });

+ 1 - 1
src/LensFlares/lensFlareSystemSceneComponent.ts

@@ -157,7 +157,7 @@ export class LensFlareSystemSceneComponent implements ISceneSerializableComponen
         }
         container.lensFlareSystems.forEach((o) => {
             this.scene.removeLensFlareSystem(o);
-            if(dispose){
+            if (dispose) {
                 o.dispose();
             }
         });

+ 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;
 }

+ 10 - 0
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,6 +182,10 @@ export class AssetContainer extends AbstractScene {
             o.dispose();
         });
 
+        if (this.scene.environmentTexture) {
+            this.scene.environmentTexture.dispose();
+        }
+
         for (let component of this.scene._serializableComponents) {
             component.removeFromContainer(this, true);
         }