Browse Source

Added textures dirty mode

David Catuhe 8 years ago
parent
commit
cdc93518be

File diff suppressed because it is too large
+ 30 - 29
dist/preview release/babylon.core.js


File diff suppressed because it is too large
+ 879 - 836
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 10 - 10
dist/preview release/babylon.js


+ 44 - 8
dist/preview release/babylon.max.js

@@ -15162,6 +15162,7 @@ var BABYLON;
             serializationObject.useBlurExponentialShadowMap = this.useBlurExponentialShadowMap;
             serializationObject.usePoissonSampling = this.usePoissonSampling;
             serializationObject.forceBackFacesOnly = this.forceBackFacesOnly;
+            serializationObject.depthScale = this.depthScale;
             serializationObject.darkness = this.getDarkness();
             serializationObject.renderList = [];
             for (var meshIndex = 0; meshIndex < this.getShadowMap().renderList.length; meshIndex++) {
@@ -15198,6 +15199,9 @@ var BABYLON;
             else if (parsedShadowGenerator.useBlurVarianceShadowMap) {
                 shadowGenerator.useBlurExponentialShadowMap = true;
             }
+            if (parsedShadowGenerator.depthScale) {
+                shadowGenerator.depthScale = parsedShadowGenerator.depthScale;
+            }
             if (parsedShadowGenerator.blurScale) {
                 shadowGenerator.blurScale = parsedShadowGenerator.blurScale;
             }
@@ -29524,6 +29528,9 @@ var BABYLON;
             for (var index = 0; index < scene.lights.length; index++) {
                 var light = scene.lights[index];
                 if (!light.isEnabled()) {
+                    if (defines["LIGHT" + lightIndex] !== undefined) {
+                        defines["LIGHT" + lightIndex] = false;
+                    }
                     continue;
                 }
                 // Excluded check
@@ -29547,6 +29554,9 @@ var BABYLON;
                     light._includedOnlyMeshesIds = [];
                 }
                 if (!light.canAffectMesh(mesh)) {
+                    if (defines["LIGHT" + lightIndex] !== undefined) {
+                        defines["LIGHT" + lightIndex] = false;
+                    }
                     continue;
                 }
                 needNormals = true;
@@ -29616,6 +29626,12 @@ var BABYLON;
                 if (lightIndex === maxSimultaneousLights)
                     break;
             }
+            // Resetting all other lights if any
+            for (var index = scene.lights.length; index < maxSimultaneousLights; index++) {
+                if (defines["LIGHT" + lightIndex] !== undefined) {
+                    defines["LIGHT" + lightIndex] = false;
+                }
+            }
             var caps = scene.getEngine().getCaps();
             if (needShadows && caps.textureFloat && caps.textureFloatLinearFiltering && caps.textureFloatRender) {
                 if (defines["SHADOWFULLFLOAT"] === undefined) {
@@ -29824,12 +29840,36 @@ var BABYLON;
 (function (BABYLON) {
     var MaterialDefines = (function () {
         function MaterialDefines() {
+            this._isDirty = true;
         }
-        MaterialDefines.prototype.rebuild = function () {
+        MaterialDefines.prototype.rebuild = function (trackIsDirty) {
             if (this._keys) {
                 delete this._keys;
             }
             this._keys = Object.keys(this);
+            if (!trackIsDirty) {
+                return;
+            }
+            for (var key in Object.keys(this)) {
+                if (Object.getOwnPropertyDescriptor(this, key).get) {
+                    continue;
+                }
+                this["_" + key] = this[key];
+                Object.defineProperty(this, key, {
+                    get: function () {
+                        return this["_" + key];
+                    },
+                    set: function (value) {
+                        if (this["_" + key] === value) {
+                            return;
+                        }
+                        this["_" + key] = value;
+                        this._isDirty = true;
+                    },
+                    enumerable: true,
+                    configurable: true
+                });
+            }
         };
         MaterialDefines.prototype.isEqual = function (other) {
             if (this._keys.length !== other._keys.length) {
@@ -30274,7 +30314,7 @@ var BABYLON;
             _this.SHADOWFULLFLOAT = false;
             _this.CAMERACOLORGRADING = false;
             _this.CAMERACOLORCURVES = false;
-            _this.rebuild();
+            _this.rebuild(true);
             return _this;
         }
         return StandardMaterialDefines;
@@ -30333,8 +30373,6 @@ var BABYLON;
             _this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
             _this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
             _this._defines = new StandardMaterialDefines();
-            _this._cachedDefines = new StandardMaterialDefines();
-            _this._cachedDefines.BonesPerMesh = -1;
             _this.getRenderTargetTextures = function () {
                 _this._renderTargets.reset();
                 if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
@@ -30395,7 +30433,6 @@ var BABYLON;
             var engine = scene.getEngine();
             var needUVs = false;
             var needNormals = false;
-            this._defines.reset();
             // Lights
             if (scene.lightsEnabled && !this.disableLighting) {
                 needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
@@ -30655,8 +30692,7 @@ var BABYLON;
                 }
             }
             // Get correct effect      
-            if (!this._defines.isEqual(this._cachedDefines)) {
-                this._defines.cloneTo(this._cachedDefines);
+            if (this._defines._isDirty) {
                 scene.resetCachedMaterial();
                 // Fallbacks
                 var fallbacks = new BABYLON.EffectFallbacks();
@@ -57054,7 +57090,7 @@ var BABYLON;
     })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
 })(BABYLON || (BABYLON = {}));
 
-//# sourceMappingURL=babylon.tools.pmremGenerator.js.map
+//# sourceMappingURL=babylon.tools.pmremgenerator.js.map
 
 
 var BABYLON;

File diff suppressed because it is too large
+ 879 - 836
dist/preview release/babylon.module.d.ts


File diff suppressed because it is too large
+ 37 - 38
dist/preview release/babylon.noworker.js


+ 54 - 3
src/Materials/babylon.material.ts

@@ -2,12 +2,55 @@
     export class MaterialDefines {
         _keys: string[];
         _isDirty = true;
+        _trackIsDirty = false;
+
+        constructor(trackIsDirty?: boolean) {
+            this._trackIsDirty = trackIsDirty;
+        }
+
+        private _reBind(key: string): void {
+            this["_" + key] = this[key]; 
+
+            Object.defineProperty(this, key, {
+                get: function () {
+                    return this["_" + key];
+                },
+                set: function (value) {
+                    if (this["_" + key] === value) {
+                        return;
+                    }
+                    this["_" + key] = value;
+                    this._isDirty = true;
+                },
+                enumerable: true,
+                configurable: true
+            });
+        }
 
         public rebuild() {
             if (this._keys) {
                 delete this._keys;
             }
-            this._keys = Object.keys(this);
+
+            this._keys = [];
+
+            for (var key of Object.keys(this)) {
+                if (key[0] === "_") {
+                    continue;
+                }
+
+                this._keys.push(key);
+
+                if (!this._trackIsDirty) {
+                    continue;
+                }
+            
+                if (Object.getOwnPropertyDescriptor(this, key).get) {
+                    continue;
+                }
+
+                this._reBind(key);
+            }
         } 
 
         public isEqual(other: MaterialDefines): boolean {
@@ -55,11 +98,12 @@
             var result = "";
             for (var index = 0; index < this._keys.length; index++) {
                 var prop = this._keys[index];
+                var value = this[prop];
 
-                if (typeof (this[prop]) === "number") {
+                if (typeof (value) === "number") {
                     result += "#define " + prop + " " + this[prop] + "\n";
 
-                } else if (this[prop]) {
+                } else if (value) {
                     result += "#define " + prop + "\n";
                 }
             }
@@ -237,6 +281,13 @@
             }
             return ret;
         } 
+
+        /**
+         * Child classes can use it to update shaders
+         */
+        public markAsDirty() {
+
+        }
         
         public getClassName(): string {
             return "Material";

+ 17 - 16
src/Materials/babylon.materialHelper.ts

@@ -74,11 +74,10 @@
                 defines[type] = true;
 
                 // Specular
-                if (!light.specular.equalsFloats(0, 0, 0) && defines["SPECULARTERM"] !== undefined) {
-                    defines["SPECULARTERM"] = true;
-                }
+                defines["SPECULARTERM"] = (!light.specular.equalsFloats(0, 0, 0) && defines["SPECULARTERM"] !== undefined);
 
                 // Shadows
+                var shadowEnabled = false;
                 if (scene.shadowsEnabled) {
                     var shadowGenerator = <ShadowGenerator>light.getShadowGenerator();
                     if (mesh && mesh.receiveShadows && shadowGenerator) {
@@ -87,7 +86,7 @@
                         }
                         defines["SHADOW" + lightIndex] = true;
 
-                        defines["SHADOWS"] = true;
+                        shadowEnabled = true;
 
                         if (shadowGenerator.usePoissonSampling) {
                             if (defines["SHADOWPCF" + lightIndex] === undefined) {
@@ -105,9 +104,13 @@
                         }
 
                         needShadows = true;
+                    } else {
+                        defines["SHADOW" + lightIndex] = false;
                     }
                 }
 
+                defines["SHADOWS"] = shadowEnabled;
+
                 if (light.lightmapMode != Light.LIGHTMAP_DEFAULT ) {
                     lightmapMode = true;
                     if (defines["LIGHTMAPEXCLUDED" + lightIndex] === undefined) {
@@ -117,9 +120,10 @@
                         needRebuild = true;
                     }
                     defines["LIGHTMAPEXCLUDED" + lightIndex] = true;
-                    if (light.lightmapMode == Light.LIGHTMAP_SHADOWSONLY) {
-                        defines["LIGHTMAPNOSPECULAR" + lightIndex] = true;
-                    }
+                    defines["LIGHTMAPNOSPECULAR" + lightIndex] = (light.lightmapMode == Light.LIGHTMAP_SHADOWSONLY);
+                } else {
+                    defines["LIGHTMAPEXCLUDED" + lightIndex] = false;
+                    defines["LIGHTMAPNOSPECULAR" + lightIndex] = false;
                 }
 
                 lightIndex++;
@@ -135,20 +139,17 @@
             }
 
             let caps = scene.getEngine().getCaps();
-            if (needShadows && caps.textureFloat && caps.textureFloatLinearFiltering && caps.textureFloatRender) {
-                if (defines["SHADOWFULLFLOAT"] === undefined) {
-                    needRebuild = true;
-                }
-
-                defines["SHADOWFULLFLOAT"] = true;
+            if (defines["SHADOWFULLFLOAT"] === undefined) {
+                needRebuild = true;
             }
 
+            defines["SHADOWFULLFLOAT"] = (needShadows && caps.textureFloat && caps.textureFloatLinearFiltering && caps.textureFloatRender);
+
             if (defines["LIGHTMAPEXCLUDED"] === undefined) {
                 needRebuild = true;
             }
-            if (lightmapMode) {
-                defines["LIGHTMAPEXCLUDED"] = true;
-            }
+
+            defines["LIGHTMAPEXCLUDED"] = lightmapMode;
 
             if (needRebuild) {
                 defines.rebuild();

File diff suppressed because it is too large
+ 559 - 334
src/Materials/babylon.standardMaterial.ts


+ 13 - 0
src/babylon.engine.ts

@@ -223,6 +223,19 @@
             return lastCreatedEngine.scenes[lastCreatedEngine.scenes.length - 1];
         }
 
+        /**
+         * Will flag all materials in all scenes in all engines as dirty to trigger new shader compilation
+         */
+        public static MarkAllMaterialsAsDirty(): void {
+            for (var engineIndex = 0; engineIndex < Engine.Instances.length; engineIndex++) {
+                var engine = Engine.Instances[engineIndex];
+
+                for (var sceneIndex = 0; sceneIndex < engine.scenes.length; sceneIndex++) {
+                    engine.scenes[sceneIndex].markAllMaterialsAsDirty();
+                }
+            }
+        }
+
         // Const statics
         private static _ALPHA_DISABLE = 0;
         private static _ALPHA_ADD = 1;

+ 23 - 1
src/babylon.scene.ts

@@ -466,7 +466,20 @@
         }
 
         // Textures
-        public texturesEnabled = true;
+        private _texturesEnabled = true;
+
+        public set texturesEnabled(value : boolean) {
+            if (this._texturesEnabled === value) {
+                return;
+            }
+            this._texturesEnabled = value;
+            this.markAllMaterialsAsDirty();
+        }
+
+        public get texturesEnabled(): boolean {
+            return this._texturesEnabled;
+        }     
+
         public textures = new Array<BaseTexture>();
 
         // Particles
@@ -3465,5 +3478,14 @@
             stencil = true): void {
             this._renderingManager.setRenderingAutoClearDepthStencil(renderingGroupId, autoClearDepthStencil, depth, stencil);
         }
+
+        /**
+         * Will flag all materials as dirty to trigger new shader compilation
+         */
+        public markAllMaterialsAsDirty(): void {
+            for (var index = 0; index < this.materials.length; index++) {
+                this.materials[index].markAsDirty();
+            }
+        }
     }
 }