浏览代码

First step of light auto affectation

David Catuhe 8 年之前
父节点
当前提交
3f1840f045

+ 1 - 0
Tools/Gulp/config.json

@@ -22,6 +22,7 @@
             "../../src/Tools/babylon.database.js",
             "../../src/Tools/babylon.tools.tga.js",
             "../../src/Tools/babylon.smartArray.js",
+            "../../src/Tools/babylon.observableArray.js",
             "../../src/Tools/babylon.stringDictionary.js",
             "../../src/Tools/babylon.tools.js",
             "../../src/States/babylon.alphaCullingState.js",

文件差异内容过多而无法显示
+ 5325 - 5305
dist/preview release/babylon.d.ts


文件差异内容过多而无法显示
+ 5325 - 5305
dist/preview release/babylon.module.d.ts


+ 23 - 2
src/Lights/babylon.light.ts

@@ -73,8 +73,25 @@
         @serialize()
         public includeOnlyWithLayerMask = 0;
 
-        public includedOnlyMeshes = new Array<AbstractMesh>();
-        public excludedMeshes = new Array<AbstractMesh>();
+        private _includedOnlyMeshes = new ObservableArray<AbstractMesh>();
+        public get includedOnlyMeshes(): any {
+            return this._includedOnlyMeshes;
+        }
+        public set includedOnlyMeshes(value: any) {
+            this._includedOnlyMeshes = new ObservableArray<AbstractMesh>();
+
+            this._includedOnlyMeshes.push(value);
+        }
+
+        private _excludedMeshes = new ObservableArray<AbstractMesh>();
+        public get excludedMeshes(): any {
+            return this._excludedMeshes;
+        }
+        public set excludedMeshes(value: any) {
+            this._excludedMeshes = new ObservableArray<AbstractMesh>();
+
+            this._excludedMeshes.push(value);
+        }        
 
         @serialize()
         public excludeWithLayerMask = 0;
@@ -98,6 +115,10 @@
         constructor(name: string, scene: Scene) {
             super(name, scene);
             this.getScene().addLight(this);
+
+            this._excludedMeshes.onDataAdded.add((mesh) => {
+                
+            });
         }
         /**
          * Returns the string "Light".  

+ 31 - 0
src/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -191,6 +191,7 @@
                         }
                     }
                 }
+
                 return true;
 
             } catch (err) {
@@ -501,6 +502,36 @@
                         ShadowGenerator.Parse(parsedShadowGenerator, scene);
                     }
                 }
+                
+                // Lights exclusions / inclusions
+                for (index = 0, cache = scene.lights.length; index < cache; index++) {
+                    var light = scene.lights[index];
+                    // Excluded check
+                    if (light._excludedMeshesIds.length > 0) {
+                        for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
+                            var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
+
+                            if (excludedMesh) {
+                                light.excludedMeshes.push(excludedMesh);
+                            }
+                        }
+
+                        light._excludedMeshesIds = [];
+                    }
+
+                    // Included check
+                    if (light._includedOnlyMeshesIds.length > 0) {
+                        for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
+                            var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
+
+                            if (includedOnlyMesh) {
+                                light.includedOnlyMeshes.push(includedOnlyMesh);
+                            }
+                        }
+
+                        light._includedOnlyMeshesIds = [];
+                    }
+                }
     
                 // Actions (scene)
                 if (parsedData.actions) {

+ 0 - 26
src/Materials/babylon.materialHelper.ts

@@ -17,32 +17,6 @@
                     continue;
                 }
 
-                // Excluded check
-                if (light._excludedMeshesIds.length > 0) {
-                    for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
-                        var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
-
-                        if (excludedMesh) {
-                            light.excludedMeshes.push(excludedMesh);
-                        }
-                    }
-
-                    light._excludedMeshesIds = [];
-                }
-
-                // Included check
-                if (light._includedOnlyMeshesIds.length > 0) {
-                    for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
-                        var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
-
-                        if (includedOnlyMesh) {
-                            light.includedOnlyMeshes.push(includedOnlyMesh);
-                        }
-                    }
-
-                    light._includedOnlyMeshesIds = [];
-                }
-
                 if (!light.canAffectMesh(mesh)) {
                     if (defines["LIGHT" + lightIndex] !== undefined) {
                         defines["LIGHT" + lightIndex] = false;

+ 26 - 26
src/Materials/babylon.standardMaterial.ts

@@ -60,6 +60,9 @@ module BABYLON {
         public CAMERACOLORGRADING = false;
         public CAMERACOLORCURVES = false;
 
+        public _areTexturesDirty = false;
+        public _needUVs = false;
+
         constructor() {
             super(true);
             this.rebuild();
@@ -88,7 +91,7 @@ module BABYLON {
                 return;
             }
             this._diffuseTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get diffuseTexture(): BaseTexture {
@@ -103,7 +106,7 @@ module BABYLON {
                 return;
             }
             this._ambientTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get ambientTexture(): BaseTexture {
@@ -118,7 +121,7 @@ module BABYLON {
                 return;
             }
             this._opacityTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get opacityTexture(): BaseTexture {
@@ -133,7 +136,7 @@ module BABYLON {
                 return;
             }
             this._reflectionTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get reflectionTexture(): BaseTexture {
@@ -148,7 +151,7 @@ module BABYLON {
                 return;
             }
             this._emissiveTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get emissiveTexture(): BaseTexture {
@@ -163,7 +166,7 @@ module BABYLON {
                 return;
             }
             this._specularTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get specularTexture(): BaseTexture {
@@ -178,7 +181,7 @@ module BABYLON {
                 return;
             }
             this._bumpTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get bumpTexture(): BaseTexture {
@@ -193,7 +196,7 @@ module BABYLON {
                 return;
             }
             this._lightmapTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get lightmapTexture(): BaseTexture {
@@ -208,7 +211,7 @@ module BABYLON {
                 return;
             }
             this._refractionTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get refractionTexture(): BaseTexture {
@@ -323,7 +326,7 @@ module BABYLON {
                 return;
             }
             this._cameraColorGradingTexture = value;
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public get cameraColorGradingTexture(): BaseTexture {
@@ -348,9 +351,6 @@ module BABYLON {
 
         protected _useLogarithmicDepth: boolean;
 
-        protected _areTexturesDirty = false;
-        protected _needUVs = false;
-
         constructor(name: string, scene: Scene) {
             super(name, scene);
 
@@ -415,7 +415,7 @@ module BABYLON {
          * Child classes can use it to update shaders
          */
         public markAsDirty() {
-            this._areTexturesDirty = true;
+            this._defines._areTexturesDirty = true;
         }
 
         public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
@@ -443,14 +443,14 @@ module BABYLON {
             }
 
             // Textures
-            if (this._areTexturesDirty) {
-                this._needUVs = false;
+            if (this._defines._areTexturesDirty) {
+                this._defines._needUVs = false;
                 if (scene.texturesEnabled) {
                     if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
                         if (!this._diffuseTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.DIFFUSE = true;
                         }
                     } else {
@@ -461,7 +461,7 @@ module BABYLON {
                         if (!this._ambientTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.AMBIENT = true;
                         }
                     } else {
@@ -472,7 +472,7 @@ module BABYLON {
                         if (!this._opacityTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.OPACITY = true;
                             this._defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
                         }
@@ -531,7 +531,7 @@ module BABYLON {
                         if (!this._emissiveTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.EMISSIVE = true;
                         }
                     } else {
@@ -542,7 +542,7 @@ module BABYLON {
                         if (!this._lightmapTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.LIGHTMAP = true;
                             this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
                         }
@@ -554,7 +554,7 @@ module BABYLON {
                         if (!this._specularTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.SPECULAR = true;
                             this._defines.GLOSSINESS = this.useGlossinessFromSpecularMapAlpha;
                         }
@@ -566,7 +566,7 @@ module BABYLON {
                         if (!this._bumpTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.BUMP = true;
 
                             this._defines.PARALLAX = this.useParallax;
@@ -588,7 +588,7 @@ module BABYLON {
                         if (!this._refractionTexture.isReady()) {
                             return false;
                         } else {
-                            this._needUVs = true;
+                            this._defines._needUVs = true;
                             this._defines.REFRACTION = true;
 
                             this._defines.REFRACTIONMAP_3D = this._refractionTexture.isCube;
@@ -613,7 +613,7 @@ module BABYLON {
                         this._defines.TWOSIDEDLIGHTING = false;
                     }
                 }
-                this._areTexturesDirty = false;
+                this._defines._areTexturesDirty = false;
             }
 
             // Effect
@@ -670,7 +670,7 @@ module BABYLON {
             if (mesh) {
                 this._defines.NORMAL = (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind));
 
-                if (this._needUVs) {
+                if (this._defines._needUVs) {
                     this._defines.UV1 = mesh.isVerticesDataPresent(VertexBuffer.UVKind);
                     this._defines.UV2 = mesh.isVerticesDataPresent(VertexBuffer.UV2Kind);
                 } else {

+ 4 - 0
src/Mesh/babylon.abstractMesh.ts

@@ -224,6 +224,8 @@
 
         public _poseMatrix: Matrix;
 
+        public _lightSources = new SmartArray<Light>(8);
+
         // Loading properties
         public _waitingActions: any;
         public _waitingFreezeWorldMatrix: boolean;
@@ -1437,6 +1439,8 @@
                 }
             });
 
+            this._lightSources.dispose();
+
             // Edges
             if (this._edgesRenderer) {
                 this._edgesRenderer.dispose();

+ 64 - 0
src/Tools/babylon.observableArray.ts

@@ -0,0 +1,64 @@
+module BABYLON {
+    export class ObservableArray<T> {
+        public data: Array<T>;
+        public length: number = 0;
+
+        public onDataAdded = new Observable<T>();
+        public onDataRemoved = new Observable<T>();
+
+        constructor() {
+            this.data = new Array<T>();
+        }
+
+        public splice(start: number, deleteCount?: number): T[] {
+            var removed = this.data.splice(start, deleteCount);
+
+            if (this.onDataRemoved.hasObservers) {
+                for (var item of removed) {
+                    this.onDataRemoved.notifyObservers(item);
+                }
+            }
+
+            return removed;
+        }
+
+        public push(...items: T[]): void {
+            this.data.push(...items);
+
+            if (this.onDataAdded.hasObservers) {
+                for (var item of items) {
+                    this.onDataAdded.notifyObservers(item);
+                }
+            }
+        }
+
+        public forEach(func: (content: T) => void): void {
+            for (var index = 0; index < this.length; index++) {
+                func(this.data[index]);
+            }
+        }
+
+        public sort(compareFn): void {
+            this.data.sort(compareFn);
+        }
+
+        public reset(): void {
+            this.length = 0;
+        }
+
+        public dispose(): void {
+            this.reset();
+            this.data.length = 0;
+        }
+
+        public indexOf(value): number {
+            var position = this.data.indexOf(value);
+
+            if (position >= this.length) {
+                return -1;
+            }
+
+            return position;
+        }
+    }
+}