sebavan 5 years ago
parent
commit
7643edc459
3 changed files with 88 additions and 52 deletions
  1. 64 1
      src/Lights/light.ts
  2. 23 14
      src/Lights/spotLight.ts
  3. 1 37
      src/Materials/materialHelper.ts

+ 64 - 1
src/Lights/light.ts

@@ -2,7 +2,7 @@ import { serialize, SerializationHelper, serializeAsColor3, expandToProperty } f
 import { Nullable } from "../types";
 import { Nullable } from "../types";
 import { Scene } from "../scene";
 import { Scene } from "../scene";
 import { Vector3 } from "../Maths/math.vector";
 import { Vector3 } from "../Maths/math.vector";
-import { Color3 } from "../Maths/math.color";
+import { Color3, TmpColors } from "../Maths/math.color";
 import { Node } from "../node";
 import { Node } from "../node";
 import { AbstractMesh } from "../Meshes/abstractMesh";
 import { AbstractMesh } from "../Meshes/abstractMesh";
 import { Effect } from "../Materials/effect";
 import { Effect } from "../Materials/effect";
@@ -365,6 +365,69 @@ export abstract class Light extends Node {
     public abstract transferToEffect(effect: Effect, lightIndex: string): Light;
     public abstract transferToEffect(effect: Effect, lightIndex: string): Light;
 
 
     /**
     /**
+     * Sets the passed Effect "effect" with the Light textures.
+     * @param effect The effect to update
+     * @param lightIndex The index of the light in the effect to update
+     * @returns The light
+     */
+    public transferTexturesToEffect(effect: Effect, lightIndex: string): Light {
+        // Do nothing by default.
+        return this;
+    }
+
+    /**
+     * Binds the lights information from the scene to the effect for the given mesh.
+     * @param lightIndex Light index
+     * @param scene The scene where the light belongs to
+     * @param effect The effect we are binding the data to
+     * @param useSpecular Defines if specular is supported
+     * @param usePhysicalLightFalloff Specifies whether the light falloff is defined physically or not
+     * @param rebuildInParallel Specifies whether the shader is rebuilding in parallel
+     */
+    public bindLight(lightIndex: number, scene: Scene, effect: Effect, useSpecular: boolean, usePhysicalLightFalloff = false, rebuildInParallel = false): void {
+        let iAsString = lightIndex.toString();
+        let needUpdate = false;
+
+        if (rebuildInParallel && this._uniformBuffer._alreadyBound) {
+            return;
+        }
+
+        this._uniformBuffer.bindToEffect(effect, "Light" + iAsString);
+
+        if (this._renderId !== scene.getRenderId() || !this._uniformBuffer.useUbo) {
+            this._renderId = scene.getRenderId();
+
+            let scaledIntensity = this.getScaledIntensity();
+
+            this.transferToEffect(effect, iAsString);
+
+            this.diffuse.scaleToRef(scaledIntensity, TmpColors.Color3[0]);
+            this._uniformBuffer.updateColor4("vLightDiffuse", TmpColors.Color3[0], usePhysicalLightFalloff ? this.radius : this.range, iAsString);
+            if (useSpecular) {
+                this.specular.scaleToRef(scaledIntensity, TmpColors.Color3[1]);
+                this._uniformBuffer.updateColor3("vLightSpecular", TmpColors.Color3[1], iAsString);
+            }
+            needUpdate = true;
+        }
+
+        // Textures might still need to be rebound.
+        this.transferTexturesToEffect(effect, iAsString);
+
+        // Shadows
+        if (scene.shadowsEnabled && this.shadowEnabled) {
+            var shadowGenerator = this.getShadowGenerator();
+            if (shadowGenerator) {
+                shadowGenerator.bindShadowLight(iAsString, effect);
+                needUpdate = true;
+            }
+        }
+
+        if (needUpdate) {
+            this._uniformBuffer.update();
+        }
+    }
+
+    /**
      * Sets the passed Effect "effect" with the Light information.
      * Sets the passed Effect "effect" with the Light information.
      * @param effect The effect to update
      * @param effect The effect to update
      * @param lightDataUniformName The uniform used to store light data (position or direction)
      * @param lightDataUniformName The uniform used to store light data (position or direction)

+ 23 - 14
src/Lights/spotLight.ts

@@ -323,6 +323,29 @@ export class SpotLight extends ShadowLight {
     }
     }
 
 
     /**
     /**
+     * Sets the passed Effect "effect" with the Light textures.
+     * @param effect The effect to update
+     * @param lightIndex The index of the light in the effect to update
+     * @returns The light
+     */
+    public transferTexturesToEffect(effect: Effect, lightIndex: string): Light {
+        if (this.projectionTexture && this.projectionTexture.isReady()) {
+            if (this._projectionTextureViewLightDirty) {
+                this._computeProjectionTextureViewLightMatrix();
+            }
+            if (this._projectionTextureProjectionLightDirty) {
+                this._computeProjectionTextureProjectionLightMatrix();
+            }
+            if (this._projectionTextureDirty) {
+                this._computeProjectionTextureMatrix();
+            }
+            effect.setMatrix("textureProjectionMatrix" + lightIndex, this._projectionTextureMatrix);
+            effect.setTexture("projectionLightSampler" + lightIndex, this.projectionTexture);
+        }
+        return this;
+    }
+
+    /**
      * Sets the passed Effect object with the SpotLight transfomed position (or position if not parented) and normalized direction.
      * Sets the passed Effect object with the SpotLight transfomed position (or position if not parented) and normalized direction.
      * @param effect The effect to update
      * @param effect The effect to update
      * @param lightIndex The index of the light in the effect to update
      * @param lightIndex The index of the light in the effect to update
@@ -374,20 +397,6 @@ export class SpotLight extends ShadowLight {
             this._lightAngleOffset,
             this._lightAngleOffset,
             lightIndex
             lightIndex
         );
         );
-
-        if (this.projectionTexture && this.projectionTexture.isReady()) {
-            if (this._projectionTextureViewLightDirty) {
-                this._computeProjectionTextureViewLightMatrix();
-            }
-            if (this._projectionTextureProjectionLightDirty) {
-                this._computeProjectionTextureProjectionLightMatrix();
-            }
-            if (this._projectionTextureDirty) {
-                this._computeProjectionTextureMatrix();
-            }
-            effect.setMatrix("textureProjectionMatrix" + lightIndex, this._projectionTextureMatrix);
-            effect.setTexture("projectionLightSampler" + lightIndex, this.projectionTexture);
-        }
         return this;
         return this;
     }
     }
 
 

+ 1 - 37
src/Materials/materialHelper.ts

@@ -665,43 +665,7 @@ export class MaterialHelper {
      * @param rebuildInParallel Specifies whether the shader is rebuilding in parallel
      * @param rebuildInParallel Specifies whether the shader is rebuilding in parallel
      */
      */
     public static BindLight(light: Light, lightIndex: number, scene: Scene, effect: Effect, useSpecular: boolean, usePhysicalLightFalloff = false, rebuildInParallel = false): void {
     public static BindLight(light: Light, lightIndex: number, scene: Scene, effect: Effect, useSpecular: boolean, usePhysicalLightFalloff = false, rebuildInParallel = false): void {
-        let iAsString = lightIndex.toString();
-        let needUpdate = false;
-
-        if (rebuildInParallel && light._uniformBuffer._alreadyBound) {
-            return;
-        }
-
-        light._uniformBuffer.bindToEffect(effect, "Light" + iAsString);
-
-        if (light._renderId !== scene.getRenderId() || !light._uniformBuffer.useUbo) {
-            light._renderId = scene.getRenderId();
-
-            let scaledIntensity = light.getScaledIntensity();
-
-            MaterialHelper.BindLightProperties(light, effect, lightIndex);
-
-            light.diffuse.scaleToRef(scaledIntensity, TmpColors.Color3[0]);
-            light._uniformBuffer.updateColor4("vLightDiffuse", TmpColors.Color3[0], usePhysicalLightFalloff ? light.radius : light.range, iAsString);
-            if (useSpecular) {
-                light.specular.scaleToRef(scaledIntensity, TmpColors.Color3[1]);
-                light._uniformBuffer.updateColor3("vLightSpecular", TmpColors.Color3[1], iAsString);
-            }
-            needUpdate = true;
-        }
-
-        // Shadows
-        if (scene.shadowsEnabled && light.shadowEnabled) {
-            var shadowGenerator = light.getShadowGenerator();
-            if (shadowGenerator) {
-                shadowGenerator.bindShadowLight(iAsString, effect);
-                needUpdate = true;
-            }
-        }
-
-        if (needUpdate) {
-            light._uniformBuffer.update();
-        }
+        light.bindLight(lightIndex, scene, effect, useSpecular, usePhysicalLightFalloff, rebuildInParallel);
     }
     }
 
 
     /**
     /**