Browse Source

Merge pull request #7981 from Popov72/shadowdepthmat

ShadowDepthWrapper: bind worldView and worldViewProjection matrices
David Catuhe 5 years ago
parent
commit
d45d66dd50

+ 18 - 6
src/Lights/Shadows/cascadedShadowGenerator.ts

@@ -2,6 +2,7 @@ import { Nullable } from "../../types";
 import { Scene } from "../../scene";
 import { Matrix, Vector3 } from "../../Maths/math.vector";
 import { SubMesh } from "../../Meshes/subMesh";
+import { AbstractMesh } from "../../Meshes/abstractMesh";
 
 import { IShadowLight } from "../../Lights/shadowLight";
 import { Effect } from "../../Materials/effect";
@@ -34,7 +35,8 @@ const ZeroVec = Vector3.Zero();
 
 let tmpv1 = new Vector3(),
     tmpv2 = new Vector3(),
-    matrix = new Matrix();
+    tmpMatrix = new Matrix(),
+    tmpMatrix2 = new Matrix();
 
 /**
  * A CSM implementation allowing casting shadows on large scenes.
@@ -601,9 +603,9 @@ export class CascadedShadowGenerator extends ShadowGenerator {
             tmpv2.copyFromFloats(Math.round(tmpv1.x), Math.round(tmpv1.y), Math.round(tmpv1.z)); // tmpv2 = roundedOrigin
             tmpv2.subtractInPlace(tmpv1).scaleInPlace(2 / this._mapSize); // tmpv2 = roundOffset
 
-            Matrix.TranslationToRef(tmpv2.x, tmpv2.y, 0.0, matrix);
+            Matrix.TranslationToRef(tmpv2.x, tmpv2.y, 0.0, tmpMatrix);
 
-            this._projectionMatrices[cascadeIndex].multiplyToRef(matrix, this._projectionMatrices[cascadeIndex]);
+            this._projectionMatrices[cascadeIndex].multiplyToRef(tmpMatrix, this._projectionMatrices[cascadeIndex]);
             this._viewMatrices[cascadeIndex].multiplyToRef(this._projectionMatrices[cascadeIndex], this._transformMatrices[cascadeIndex]);
 
             this._transformMatrices[cascadeIndex].copyToArray(this._transformMatricesAsArray, cascadeIndex * 16);
@@ -675,11 +677,11 @@ export class CascadedShadowGenerator extends ShadowGenerator {
 
             this._frustumCenter[cascadeIndex].addToRef(this._lightDirection, tmpv1); // tmpv1 = look at
 
-            Matrix.LookAtLHToRef(lightCameraPos, tmpv1, UpDir, matrix); // matrix = lightView
+            Matrix.LookAtLHToRef(lightCameraPos, tmpv1, UpDir, tmpMatrix); // matrix = lightView
 
             // Calculate an AABB around the frustum corners
             for (let cornerIndex = 0; cornerIndex < this._frustumCornersWorldSpace[cascadeIndex].length; ++cornerIndex) {
-                Vector3.TransformCoordinatesToRef(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex], matrix, tmpv1);
+                Vector3.TransformCoordinatesToRef(this._frustumCornersWorldSpace[cascadeIndex][cornerIndex], tmpMatrix, tmpv1);
 
                 this._cascadeMinExtents[cascadeIndex].minimizeInPlace(tmpv1);
                 this._cascadeMaxExtents[cascadeIndex].maximizeInPlace(tmpv1);
@@ -815,12 +817,22 @@ export class CascadedShadowGenerator extends ShadowGenerator {
         this._splitFrustum();
     }
 
-    protected _bindCustomEffectForRenderSubMeshForShadowMap(subMesh: SubMesh, effect: Effect, matriceNames: any): void {
+    protected _bindCustomEffectForRenderSubMeshForShadowMap(subMesh: SubMesh, effect: Effect, matriceNames: any, mesh: AbstractMesh): void {
         effect.setMatrix(matriceNames?.viewProjection ?? "viewProjection", this.getCascadeTransformMatrix(this._currentLayer)!);
 
         effect.setMatrix(matriceNames?.view ?? "view", this.getCascadeViewMatrix(this._currentLayer)!);
 
         effect.setMatrix(matriceNames?.projection ?? "projection", this.getCascadeProjectionMatrix(this._currentLayer)!);
+
+        const world = mesh.getWorldMatrix();
+
+        world.multiplyToRef(this.getCascadeTransformMatrix(this._currentLayer)!, tmpMatrix);
+
+        effect.setMatrix(matriceNames?.worldViewProjection ?? "worldViewProjection", tmpMatrix);
+
+        world.multiplyToRef(this.getCascadeViewMatrix(this._currentLayer)!, tmpMatrix2);
+
+        effect.setMatrix(matriceNames?.worldView ?? "worldView", tmpMatrix2);
     }
 
     protected _isReadyCustomDefines(defines: any, subMesh: SubMesh, useInstances: boolean): void {

+ 15 - 2
src/Lights/Shadows/shadowGenerator.ts

@@ -28,6 +28,9 @@ import { _DevTools } from '../../Misc/devTools';
 import { EffectFallbacks } from '../../Materials/effectFallbacks';
 import { RenderingManager } from '../../Rendering/renderingManager';
 
+const tmpMatrix = new Matrix(),
+      tmpMatrix2 = new Matrix();
+
 /**
  * Defines the options associated with the creation of a custom shader for a shadow generator.
  */
@@ -1017,12 +1020,22 @@ export class ShadowGenerator implements IShadowGenerator {
         }
     }
 
-    protected _bindCustomEffectForRenderSubMeshForShadowMap(subMesh: SubMesh, effect: Effect, matriceNames: any): void {
+    protected _bindCustomEffectForRenderSubMeshForShadowMap(subMesh: SubMesh, effect: Effect, matriceNames: any, mesh: AbstractMesh): void {
         effect.setMatrix(matriceNames?.viewProjection ?? "viewProjection", this.getTransformMatrix());
 
         effect.setMatrix(matriceNames?.view ?? "view", this._viewMatrix);
 
         effect.setMatrix(matriceNames?.projection ?? "projection", this._projectionMatrix);
+
+        const world = mesh.getWorldMatrix();
+
+        world.multiplyToRef(this.getTransformMatrix(), tmpMatrix);
+
+        effect.setMatrix(matriceNames?.worldViewProjection ?? "worldViewProjection", tmpMatrix);
+
+        world.multiplyToRef(this._viewMatrix, tmpMatrix2);
+
+        effect.setMatrix(matriceNames?.worldView ?? "worldView", tmpMatrix2);
     }
 
     protected _renderSubMeshForShadowMap(subMesh: SubMesh): void {
@@ -1118,7 +1131,7 @@ export class ShadowGenerator implements IShadowGenerator {
                 MaterialHelper.BindClipPlane(effect, scene);
             }
 
-            this._bindCustomEffectForRenderSubMeshForShadowMap(subMesh, effect, shadowDepthWrapper?._matriceNames);
+            this._bindCustomEffectForRenderSubMeshForShadowMap(subMesh, effect, shadowDepthWrapper?._matriceNames, effectiveMesh);
 
             if (this.forceBackFacesOnly) {
                 engine.setState(true, 0, false, true);

+ 2 - 0
src/Materials/shadowDepthWrapper.ts

@@ -97,6 +97,8 @@ export class ShadowDepthWrapper {
             "view": prefix + "view",
             "projection": prefix + "projection",
             "viewProjection": prefix + "viewProjection",
+            "worldView": prefix + "worldView",
+            "worldViewProjection": prefix + "worldViewProjection",
         };
 
         // Register for onEffectCreated to store the effect of the base material when it is (re)generated. This effect will be used