瀏覽代碼

Merge pull request #4674 from sebavan/master

Light Type Decoupling
David Catuhe 7 年之前
父節點
當前提交
379df6408c

+ 1 - 2
src/Lights/Shadows/babylon.shadowGenerator.ts

@@ -1403,8 +1403,7 @@
          * @returns The parsed shadow generator
          */
         public static Parse(parsedShadowGenerator: any, scene: Scene): ShadowGenerator {
-            //casting to point light, as light is missing the position attr and typescript complains.
-            var light = <PointLight>scene.getLightByID(parsedShadowGenerator.lightId);
+            var light = <IShadowLight>scene.getLightByID(parsedShadowGenerator.lightId);
             var shadowGenerator = new ShadowGenerator(parsedShadowGenerator.mapSize, light);
             var shadowMap = shadowGenerator.getShadowMap();
 

+ 13 - 0
src/Lights/babylon.directionalLight.ts

@@ -1,4 +1,8 @@
 module BABYLON {
+    Node.AddNodeConstructor("Light_Type_1", (name, scene) => {
+        return () => new DirectionalLight(name, Vector3.Zero(), scene);
+    });
+
     /**
      * A directional light is defined by a direction (what a surprise!). 
      * The light is emitted from everywhere in the specified direction, and has an infinite range. 
@@ -216,5 +220,14 @@
         public getDepthMaxZ(activeCamera: Camera): number {
             return 1;
         }
+
+        /**
+         * Prepares the list of defines specific to the light type.
+         * @param defines the list of defines
+         * @param lightIndex defines the index of the light for the effect
+         */
+        public prepareLightSpecificDefines(defines: any, lightIndex: number): void {
+            defines["DIRLIGHT" + lightIndex] = true;
+        }
     }
 }  

+ 13 - 0
src/Lights/babylon.hemisphericLight.ts

@@ -1,4 +1,8 @@
 module BABYLON {
+    Node.AddNodeConstructor("Light_Type_3", (name, scene) => {
+        return () => new HemisphericLight(name, Vector3.Zero(), scene);
+    });
+
     /**
      * The HemisphericLight simulates the ambient environment light,
      * so the passed direction is the light reflection direction, not the incoming direction.
@@ -105,5 +109,14 @@
         public getTypeID(): number {
             return Light.LIGHTTYPEID_HEMISPHERICLIGHT;
         }
+
+        /**
+         * Prepares the list of defines specific to the light type.
+         * @param defines the list of defines
+         * @param lightIndex defines the index of the light for the effect
+         */
+        public prepareLightSpecificDefines(defines: any, lightIndex: number): void {
+            defines["HEMILIGHT" + lightIndex] = true;
+        }
     }
 } 

+ 25 - 70
src/Lights/babylon.light.ts

@@ -7,115 +7,67 @@ module BABYLON {
     export abstract class Light extends Node {
 
         //lightmapMode Consts
-        private static _LIGHTMAP_DEFAULT = 0;
-        private static _LIGHTMAP_SPECULAR = 1;
-        private static _LIGHTMAP_SHADOWSONLY = 2;
-
         /**
          * If every light affecting the material is in this lightmapMode,
          * material.lightmapTexture adds or multiplies
          * (depends on material.useLightmapAsShadowmap)
          * after every other light calculations.
          */
-        public static get LIGHTMAP_DEFAULT(): number {
-            return Light._LIGHTMAP_DEFAULT;
-        }
-
+        public static readonly LIGHTMAP_DEFAULT = 0;
         /**
          * material.lightmapTexture as only diffuse lighting from this light
          * adds only specular lighting from this light
          * adds dynamic shadows
          */
-        public static get LIGHTMAP_SPECULAR(): number {
-            return Light._LIGHTMAP_SPECULAR;
-        }
-
+        public static readonly LIGHTMAP_SPECULAR = 1;
         /**
          * material.lightmapTexture as only lighting
          * no light calculation from this light
          * only adds dynamic shadows from this light
          */
-        public static get LIGHTMAP_SHADOWSONLY(): number {
-            return Light._LIGHTMAP_SHADOWSONLY;
-        }
+        public static readonly LIGHTMAP_SHADOWSONLY = 2;
 
         // Intensity Mode Consts
-        private static _INTENSITYMODE_AUTOMATIC = 0;
-        private static _INTENSITYMODE_LUMINOUSPOWER = 1;
-        private static _INTENSITYMODE_LUMINOUSINTENSITY = 2;
-        private static _INTENSITYMODE_ILLUMINANCE = 3;
-        private static _INTENSITYMODE_LUMINANCE = 4;
-
         /**
          * Each light type uses the default quantity according to its type:
          *      point/spot lights use luminous intensity
          *      directional lights use illuminance
          */
-        public static get INTENSITYMODE_AUTOMATIC(): number {
-            return Light._INTENSITYMODE_AUTOMATIC;
-        }
-
+        public static readonly INTENSITYMODE_AUTOMATIC = 0;
         /**
          * lumen (lm)
          */
-        public static get INTENSITYMODE_LUMINOUSPOWER(): number {
-            return Light._INTENSITYMODE_LUMINOUSPOWER;
-        }
-
+        public static readonly INTENSITYMODE_LUMINOUSPOWER = 1;
         /**
          * candela (lm/sr)
          */
-        public static get INTENSITYMODE_LUMINOUSINTENSITY(): number {
-            return Light._INTENSITYMODE_LUMINOUSINTENSITY;
-        }
-
+        public static readonly INTENSITYMODE_LUMINOUSINTENSITY = 2;
         /**
          * lux (lm/m^2)
          */
-        public static get INTENSITYMODE_ILLUMINANCE(): number {
-            return Light._INTENSITYMODE_ILLUMINANCE;
-        }
-
+        public static readonly INTENSITYMODE_ILLUMINANCE = 3;
         /**
          * nit (cd/m^2)
          */
-        public static get INTENSITYMODE_LUMINANCE(): number {
-            return Light._INTENSITYMODE_LUMINANCE;
-        }
+        public static readonly INTENSITYMODE_LUMINANCE = 4;
 
         // Light types ids const.
-        private static _LIGHTTYPEID_POINTLIGHT = 0;
-        private static _LIGHTTYPEID_DIRECTIONALLIGHT = 1;
-        private static _LIGHTTYPEID_SPOTLIGHT = 2;
-        private static _LIGHTTYPEID_HEMISPHERICLIGHT = 3;
-
         /**
          * Light type const id of the point light.
          */
-        public static get LIGHTTYPEID_POINTLIGHT(): number {
-            return Light._LIGHTTYPEID_POINTLIGHT;
-        }
-
+        public static readonly LIGHTTYPEID_POINTLIGHT = 0;
         /**
          * Light type const id of the directional light.
          */
-        public static get LIGHTTYPEID_DIRECTIONALLIGHT(): number {
-            return Light._LIGHTTYPEID_DIRECTIONALLIGHT;
-        }
-
+        public static readonly LIGHTTYPEID_DIRECTIONALLIGHT = 1;
         /**
          * Light type const id of the spot light.
          */
-        public static get LIGHTTYPEID_SPOTLIGHT(): number {
-            return Light._LIGHTTYPEID_SPOTLIGHT;
-        }
-
+        public static readonly LIGHTTYPEID_SPOTLIGHT = 2;
         /**
          * Light type const id of the hemispheric light.
          */
-        public static get LIGHTTYPEID_HEMISPHERICLIGHT(): number {
-            return Light._LIGHTTYPEID_HEMISPHERICLIGHT;
-        }
+        public static readonly LIGHTTYPEID_HEMISPHERICLIGHT = 3;
 
         /**
          * Diffuse gives the basic color to an object.
@@ -581,18 +533,14 @@ module BABYLON {
          * @param scene The scene the new light will belong to
          * @returns the constructor function
          */
-        public static GetConstructorFromName(type: number, name: string, scene: Scene): Nullable<() => Light> {
-            switch (type) {
-                case 0:
-                    return () => new PointLight(name, Vector3.Zero(), scene);
-                case 1:
-                    return () => new DirectionalLight(name, Vector3.Zero(), scene);
-                case 2:
-                    return () => new SpotLight(name, Vector3.Zero(), Vector3.Zero(), 0, 0, scene);
-                case 3:
-                    return () => new HemisphericLight(name, Vector3.Zero(), scene);
+        static GetConstructorFromName(type: number, name: string, scene: Scene): Nullable<() => Light> {
+            let constructorFunc = Node.Construct("Light_Type_" + type, name, scene);
+
+            if (constructorFunc) {
+                return <() => Light>constructorFunc;
             }
 
+            // Default to no light for none present once.
             return null;
         }
 
@@ -788,5 +736,12 @@ module BABYLON {
             }
             this.getScene().sortLightsByPriority();
         }
+
+        /**
+         * Prepares the list of defines specific to the light type.
+         * @param defines the list of defines
+         * @param lightIndex defines the index of the light for the effect
+         */
+        public abstract prepareLightSpecificDefines(defines: any, lightIndex: number): void;
     }
 }

+ 13 - 0
src/Lights/babylon.pointLight.ts

@@ -1,4 +1,8 @@
 module BABYLON {
+    Node.AddNodeConstructor("Light_Type_0", (name, scene) => {
+        return () => new PointLight(name, Vector3.Zero(), scene);
+    });
+
     /**
      * A point light is a light defined by an unique point in world space. 
      * The light is emitted in every direction from this point.
@@ -166,5 +170,14 @@
             this._uniformBuffer.updateFloat4("vLightData", this.position.x, this.position.y, this.position.z, 0, lightIndex);
             return this;
         }
+
+        /**
+         * Prepares the list of defines specific to the light type.
+         * @param defines the list of defines
+         * @param lightIndex defines the index of the light for the effect
+         */
+        public prepareLightSpecificDefines(defines: any, lightIndex: number): void {
+            defines["POINTLIGHT" + lightIndex] = true;
+        }
     }
 } 

+ 14 - 0
src/Lights/babylon.spotLight.ts

@@ -1,4 +1,8 @@
 module BABYLON {
+    Node.AddNodeConstructor("Light_Type_2", (name, scene) => {
+        return () => new SpotLight(name, Vector3.Zero(), Vector3.Zero(), 0, 0, scene);
+    });
+
     /**
      * A spot light is defined by a position, a direction, an angle, and an exponent. 
      * These values define a cone of light starting from the position, emitting toward the direction.
@@ -323,5 +327,15 @@
                 this._projectionTexture.dispose();
             }
         }
+
+        /**
+         * Prepares the list of defines specific to the light type.
+         * @param defines the list of defines
+         * @param lightIndex defines the index of the light for the effect
+         */
+        public prepareLightSpecificDefines(defines: any, lightIndex: number): void {
+            defines["SPOTLIGHT" + lightIndex] = true;
+            defines["PROJECTEDLIGHTTEXTURE" + lightIndex] = this.projectionTexture ? true : false;
+        }
     }
 }

+ 1 - 14
src/Materials/babylon.materialHelper.ts

@@ -216,20 +216,7 @@ module BABYLON {
                     defines["POINTLIGHT" + lightIndex] = false;
                     defines["DIRLIGHT" + lightIndex] = false;
 
-                    var type;
-                    if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
-                        type = "SPOTLIGHT" + lightIndex;
-                        let spotLight = light as SpotLight;
-                        defines["PROJECTEDLIGHTTEXTURE" + lightIndex] = spotLight.projectionTexture ? true : false;
-                    } else if (light.getTypeID() === Light.LIGHTTYPEID_HEMISPHERICLIGHT) {
-                        type = "HEMILIGHT" + lightIndex;
-                    } else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
-                        type = "POINTLIGHT" + lightIndex;
-                    } else {
-                        type = "DIRLIGHT" + lightIndex;
-                    }
-
-                    defines[type] = true;
+                    light.prepareLightSpecificDefines(defines, lightIndex);
 
                     // Specular
                     if (specularSupported && !light.specular.equalsFloats(0, 0, 0)) {