Browse Source

Move multimaterial to push mode

David Catuhe 8 years ago
parent
commit
dff6308464

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


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


+ 82 - 54
dist/preview release/babylon.max.js

@@ -23994,8 +23994,6 @@ var BABYLON;
         /**
          * Child classes can use it to update shaders
          */
-        Material.prototype.markAsDirty = function (flag) {
-        };
         Material.prototype.getClassName = function () {
             return "Material";
         };
@@ -24096,6 +24094,57 @@ var BABYLON;
             }
             return result;
         };
+        Material.prototype.markAsDirty = function (flag) {
+            if (flag & Material.TextureDirtyFlag) {
+                this._markAllSubMeshesAsTexturesDirty();
+            }
+            if (flag & Material.LightDirtyFlag) {
+                this._markAllSubMeshesAsLightsDirty();
+            }
+            if (flag & Material.FresnelDirtyFlag) {
+                this._markAllSubMeshesAsFresnelDirty();
+            }
+            if (flag & Material.AttributesDirtyFlag) {
+                this._markAllSubMeshesAsAttributesDirty();
+            }
+            if (flag & Material.MiscDirtyFlag) {
+                this._markAllSubMeshesAsMiscDirty();
+            }
+            this.getScene().resetCachedMaterial();
+        };
+        Material.prototype._markAllSubMeshesAsDirty = function (func) {
+            for (var _i = 0, _a = this.getScene().meshes; _i < _a.length; _i++) {
+                var mesh = _a[_i];
+                if (!mesh.subMeshes) {
+                    continue;
+                }
+                for (var _b = 0, _c = mesh.subMeshes; _b < _c.length; _b++) {
+                    var subMesh = _c[_b];
+                    if (subMesh.getMaterial() !== this) {
+                        continue;
+                    }
+                    if (!subMesh._materialDefines) {
+                        return;
+                    }
+                    func(subMesh._materialDefines);
+                }
+            }
+        };
+        Material.prototype._markAllSubMeshesAsTexturesDirty = function () {
+            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsTexturesDirty(); });
+        };
+        Material.prototype._markAllSubMeshesAsFresnelDirty = function () {
+            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsFresnelDirty(); });
+        };
+        Material.prototype._markAllSubMeshesAsLightsDirty = function () {
+            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsLightDirty(); });
+        };
+        Material.prototype._markAllSubMeshesAsAttributesDirty = function () {
+            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsAttributesDirty(); });
+        };
+        Material.prototype._markAllSubMeshesAsMiscDirty = function () {
+            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsMiscDirty(); });
+        };
         Material.prototype.dispose = function (forceDisposeEffect, forceDisposeTextures) {
             // Animations
             this.getScene().stopAnimation(this);
@@ -24744,57 +24793,6 @@ var BABYLON;
             if (visibility === void 0) { visibility = 0; }
             return scene.isCachedMaterialValid(this, effect, visibility);
         };
-        PushMaterial.prototype.markAsDirty = function (flag) {
-            if (flag & BABYLON.Material.TextureDirtyFlag) {
-                this._markAllSubMeshesAsTexturesDirty();
-            }
-            if (flag & BABYLON.Material.LightDirtyFlag) {
-                this._markAllSubMeshesAsLightsDirty();
-            }
-            if (flag & BABYLON.Material.FresnelDirtyFlag) {
-                this._markAllSubMeshesAsFresnelDirty();
-            }
-            if (flag & BABYLON.Material.AttributesDirtyFlag) {
-                this._markAllSubMeshesAsAttributesDirty();
-            }
-            if (flag & BABYLON.Material.MiscDirtyFlag) {
-                this._markAllSubMeshesAsMiscDirty();
-            }
-            this.getScene().resetCachedMaterial();
-        };
-        PushMaterial.prototype._markAllSubMeshesAsDirty = function (func) {
-            for (var _i = 0, _a = this.getScene().meshes; _i < _a.length; _i++) {
-                var mesh = _a[_i];
-                if (!mesh.subMeshes) {
-                    continue;
-                }
-                for (var _b = 0, _c = mesh.subMeshes; _b < _c.length; _b++) {
-                    var subMesh = _c[_b];
-                    if (subMesh.getMaterial() !== this) {
-                        continue;
-                    }
-                    if (!subMesh._materialDefines) {
-                        return;
-                    }
-                    func(subMesh._materialDefines);
-                }
-            }
-        };
-        PushMaterial.prototype._markAllSubMeshesAsTexturesDirty = function () {
-            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsTexturesDirty(); });
-        };
-        PushMaterial.prototype._markAllSubMeshesAsFresnelDirty = function () {
-            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsFresnelDirty(); });
-        };
-        PushMaterial.prototype._markAllSubMeshesAsLightsDirty = function () {
-            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsLightDirty(); });
-        };
-        PushMaterial.prototype._markAllSubMeshesAsAttributesDirty = function () {
-            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsAttributesDirty(); });
-        };
-        PushMaterial.prototype._markAllSubMeshesAsMiscDirty = function () {
-            this._markAllSubMeshesAsDirty(function (defines) { return defines.markAsMiscDirty(); });
-        };
         return PushMaterial;
     }(BABYLON.Material));
     BABYLON.PushMaterial = PushMaterial;
@@ -47123,10 +47121,40 @@ var BABYLON;
         __extends(MultiMaterial, _super);
         function MultiMaterial(name, scene) {
             var _this = _super.call(this, name, scene, true) || this;
-            _this.subMaterials = new Array();
             scene.multiMaterials.push(_this);
+            _this.subMaterials = new Array();
             return _this;
         }
+        Object.defineProperty(MultiMaterial.prototype, "subMaterials", {
+            get: function () {
+                return this._subMaterials;
+            },
+            set: function (value) {
+                this._subMaterials = value;
+                this._hookArray(value);
+            },
+            enumerable: true,
+            configurable: true
+        });
+        MultiMaterial.prototype._hookArray = function (array) {
+            var _this = this;
+            var oldPush = array.push;
+            array.push = function () {
+                var items = [];
+                for (var _i = 0; _i < arguments.length; _i++) {
+                    items[_i] = arguments[_i];
+                }
+                var result = oldPush.apply(array, items);
+                _this._markAllSubMeshesAsTexturesDirty();
+                return result;
+            };
+            var oldSplice = array.splice;
+            array.splice = function (index, deleteCount) {
+                var deleted = oldSplice.apply(array, [index, deleteCount]);
+                _this._markAllSubMeshesAsTexturesDirty();
+                return deleted;
+            };
+        };
         // Properties
         MultiMaterial.prototype.getSubMaterial = function (index) {
             if (index < 0 || index >= this.subMaterials.length) {

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


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


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

@@ -373,9 +373,6 @@
         /**
          * Child classes can use it to update shaders         
          */
-        public markAsDirty(flag: number): void {
-
-        }
         
         public getClassName(): string {
             return "Material";
@@ -503,6 +500,69 @@
             return result;
         }
 
+        public markAsDirty(flag: number): void {
+            if (flag & Material.TextureDirtyFlag) {
+                this._markAllSubMeshesAsTexturesDirty();
+            }
+
+            if (flag & Material.LightDirtyFlag) {
+                this._markAllSubMeshesAsLightsDirty();
+            }
+
+            if (flag & Material.FresnelDirtyFlag) {
+                this._markAllSubMeshesAsFresnelDirty();
+            }
+
+            if (flag & Material.AttributesDirtyFlag) {
+                this._markAllSubMeshesAsAttributesDirty();
+            }
+
+            if (flag & Material.MiscDirtyFlag) {
+                this._markAllSubMeshesAsMiscDirty();
+            }
+
+            this.getScene().resetCachedMaterial();
+        }
+
+        protected _markAllSubMeshesAsDirty(func: (defines: MaterialDefines) => void) {
+            for (var mesh of this.getScene().meshes) {
+                if (!mesh.subMeshes) {
+                    continue;
+                }
+                for (var subMesh of mesh.subMeshes) {
+                    if (subMesh.getMaterial() !== this) {
+                        continue;
+                    }
+
+                    if (!subMesh._materialDefines) {
+                        return;
+                    }
+
+                    func(subMesh._materialDefines);
+                }
+            }
+        }
+
+        protected _markAllSubMeshesAsTexturesDirty() {
+            this._markAllSubMeshesAsDirty(defines => defines.markAsTexturesDirty());
+        }
+
+        protected _markAllSubMeshesAsFresnelDirty() {
+            this._markAllSubMeshesAsDirty(defines => defines.markAsFresnelDirty());
+        }
+
+        protected _markAllSubMeshesAsLightsDirty() {
+            this._markAllSubMeshesAsDirty(defines => defines.markAsLightDirty());
+        }
+
+        protected _markAllSubMeshesAsAttributesDirty() {
+            this._markAllSubMeshesAsDirty(defines => defines.markAsAttributesDirty());
+        }
+
+        protected _markAllSubMeshesAsMiscDirty() {
+            this._markAllSubMeshesAsDirty(defines => defines.markAsMiscDirty());
+        }        
+
         public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean): void {
             // Animations
             this.getScene().stopAnimation(this);

+ 32 - 2
src/Materials/babylon.multiMaterial.ts

@@ -1,13 +1,43 @@
 module BABYLON {
     export class MultiMaterial extends Material {
-        public subMaterials = new Array<Material>();
+        private _subMaterials: Material[];
+        public get subMaterials(): Material[] {
+            return this._subMaterials;
+        }
 
+        public set subMaterials(value: Material[]) {
+            this._subMaterials = value;
+            this._hookArray(value);
+        }
+        
         constructor(name: string, scene: Scene) {
             super(name, scene, true);
-
+            
             scene.multiMaterials.push(this);
+
+            this.subMaterials = new Array<Material>();
         }
 
+        private _hookArray(array: Material[]): void {
+            var oldPush = array.push;
+            array.push = (...items: Material[]) => {
+                var result = oldPush.apply(array, items);
+
+                this._markAllSubMeshesAsTexturesDirty();
+
+                return result;
+            }
+
+            var oldSplice = array.splice;
+            array.splice = (index: number, deleteCount?: number) => {
+                var deleted = oldSplice.apply(array, [index, deleteCount]);
+
+                this._markAllSubMeshesAsTexturesDirty();
+
+                return deleted;
+            }
+        }    
+
         // Properties
         public getSubMaterial(index) {
             if (index < 0 || index >= this.subMaterials.length) {

+ 0 - 63
src/Materials/babylon.pushMaterial.ts

@@ -44,68 +44,5 @@
         protected _mustRebind(scene: Scene, effect: Effect, visibility: number = 0) {
             return scene.isCachedMaterialValid(this, effect, visibility);
         }
-
-        public markAsDirty(flag: number): void {
-            if (flag & Material.TextureDirtyFlag) {
-                this._markAllSubMeshesAsTexturesDirty();
-            }
-
-            if (flag & Material.LightDirtyFlag) {
-                this._markAllSubMeshesAsLightsDirty();
-            }
-
-            if (flag & Material.FresnelDirtyFlag) {
-                this._markAllSubMeshesAsFresnelDirty();
-            }
-
-            if (flag & Material.AttributesDirtyFlag) {
-                this._markAllSubMeshesAsAttributesDirty();
-            }
-
-            if (flag & Material.MiscDirtyFlag) {
-                this._markAllSubMeshesAsMiscDirty();
-            }
-
-            this.getScene().resetCachedMaterial();
-        }
-
-        protected _markAllSubMeshesAsDirty(func: (defines: MaterialDefines) => void) {
-            for (var mesh of this.getScene().meshes) {
-                if (!mesh.subMeshes) {
-                    continue;
-                }
-                for (var subMesh of mesh.subMeshes) {
-                    if (subMesh.getMaterial() !== this) {
-                        continue;
-                    }
-
-                    if (!subMesh._materialDefines) {
-                        return;
-                    }
-
-                    func(subMesh._materialDefines);
-                }
-            }
-        }
-
-        protected _markAllSubMeshesAsTexturesDirty() {
-            this._markAllSubMeshesAsDirty(defines => defines.markAsTexturesDirty());
-        }
-
-        protected _markAllSubMeshesAsFresnelDirty() {
-            this._markAllSubMeshesAsDirty(defines => defines.markAsFresnelDirty());
-        }
-
-        protected _markAllSubMeshesAsLightsDirty() {
-            this._markAllSubMeshesAsDirty(defines => defines.markAsLightDirty());
-        }
-
-        protected _markAllSubMeshesAsAttributesDirty() {
-            this._markAllSubMeshesAsDirty(defines => defines.markAsAttributesDirty());
-        }
-
-        protected _markAllSubMeshesAsMiscDirty() {
-            this._markAllSubMeshesAsDirty(defines => defines.markAsMiscDirty());
-        }
     }
 }