瀏覽代碼

Compute all the matrices at once before the RTT is starting work

Popov72 5 年之前
父節點
當前提交
cabfb08325
共有 1 個文件被更改,包括 19 次插入23 次删除
  1. 19 23
      src/Lights/Shadows/cascadedShadowGenerator.ts

+ 19 - 23
src/Lights/Shadows/cascadedShadowGenerator.ts

@@ -598,10 +598,8 @@ export class CascadedShadowGenerator implements IShadowGenerator {
     private _effect: Effect;
     private _effect: Effect;
 
 
     private _cascades: Array<ICascade>;
     private _cascades: Array<ICascade>;
-    private _cachedPosition: Vector3 = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
     private _cachedDirection: Vector3 = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
     private _cachedDirection: Vector3 = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
     private _cachedDefines: string;
     private _cachedDefines: string;
-    private _currentRenderID: Array<number>;
     private _mapSize: number;
     private _mapSize: number;
     private _currentLayer = 0;
     private _currentLayer = 0;
     private _textureType: number;
     private _textureType: number;
@@ -725,6 +723,15 @@ export class CascadedShadowGenerator implements IShadowGenerator {
         return cascadeNum >= 0 && cascadeNum < this._numCascades ? this._viewMatrices[cascadeNum] : null;
         return cascadeNum >= 0 && cascadeNum < this._numCascades ? this._viewMatrices[cascadeNum] : null;
     }
     }
 
 
+    /**
+     * Gets the projection matrix corresponding to a given cascade
+     * @param cascadeNum cascade to retrieve the projection matrix from
+     * @returns the cascade projection matrix
+     */
+    public getCascadeProjectionMatrix(cascadeNum: number): Nullable<Matrix> {
+        return cascadeNum >= 0 && cascadeNum < this._numCascades ? this._projectionMatrices[cascadeNum] : null;
+    }
+
     private _depthRenderer: Nullable<DepthRenderer>;
     private _depthRenderer: Nullable<DepthRenderer>;
     /**
     /**
      * Sets the depth renderer to use when autoCalcDepthBounds is enabled.
      * Sets the depth renderer to use when autoCalcDepthBounds is enabled.
@@ -859,21 +866,15 @@ export class CascadedShadowGenerator implements IShadowGenerator {
      * @returns The transform matrix used to create the CSM shadow map
      * @returns The transform matrix used to create the CSM shadow map
      */
      */
     public getCSMTransformMatrix(cascadeIndex: number): Matrix {
     public getCSMTransformMatrix(cascadeIndex: number): Matrix {
+        return this._transformMatrices[cascadeIndex];
+    }
+
+    private _computeMatrices(): void {
         var scene = this._scene;
         var scene = this._scene;
-        if (this._currentRenderID[cascadeIndex] === scene.getRenderId()) {
-            return this._transformMatrices[cascadeIndex];
-        }
 
 
         let camera = scene.activeCamera;
         let camera = scene.activeCamera;
         if (!camera) {
         if (!camera) {
-            return this._transformMatrices[cascadeIndex];
-        }
-
-        this._currentRenderID[cascadeIndex] = scene.getRenderId();
-
-        var lightPosition = this._light.position;
-        if (this._light.computeTransformedInformation()) {
-            lightPosition = this._light.transformedPosition;
+            return;
         }
         }
 
 
         Vector3.NormalizeToRef(this._light.getShadowDirection(0), this._lightDirection);
         Vector3.NormalizeToRef(this._light.getShadowDirection(0), this._lightDirection);
@@ -881,10 +882,9 @@ export class CascadedShadowGenerator implements IShadowGenerator {
             this._lightDirection.z = 0.0000000000001; // Required to avoid perfectly perpendicular light
             this._lightDirection.z = 0.0000000000001; // Required to avoid perfectly perpendicular light
         }
         }
 
 
-        if (this._light.needProjectionMatrixCompute() || !this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !this._lightDirection.equals(this._cachedDirection)) {
-            this._cachedPosition.copyFrom(lightPosition);
-            this._cachedDirection.copyFrom(this._lightDirection);
+        this._cachedDirection.copyFrom(this._lightDirection);
 
 
+        for (let cascadeIndex = 0; cascadeIndex < this._numCascades; ++cascadeIndex) {
             this._computeFrustumInWorldSpace(cascadeIndex);
             this._computeFrustumInWorldSpace(cascadeIndex);
             this._computeCascadeFrustum(cascadeIndex);
             this._computeCascadeFrustum(cascadeIndex);
 
 
@@ -936,11 +936,9 @@ export class CascadedShadowGenerator implements IShadowGenerator {
 
 
             this._projectionMatrices[cascadeIndex].multiplyToRef(matrix, this._projectionMatrices[cascadeIndex]);
             this._projectionMatrices[cascadeIndex].multiplyToRef(matrix, this._projectionMatrices[cascadeIndex]);
             this._viewMatrices[cascadeIndex].multiplyToRef(this._projectionMatrices[cascadeIndex], this._transformMatrices[cascadeIndex]);
             this._viewMatrices[cascadeIndex].multiplyToRef(this._projectionMatrices[cascadeIndex], this._transformMatrices[cascadeIndex]);
-        }
 
 
-        this._transformMatrices[cascadeIndex].copyToArray(this._transformMatricesAsArray, cascadeIndex * 16);
-
-        return this._transformMatrices[cascadeIndex];
+            this._transformMatrices[cascadeIndex].copyToArray(this._transformMatricesAsArray, cascadeIndex * 16);
+        }
     }
     }
 
 
     // Get the 8 points of the view frustum in world space
     // Get the 8 points of the view frustum in world space
@@ -1018,8 +1016,6 @@ export class CascadedShadowGenerator implements IShadowGenerator {
                 this._cascadeMaxExtents[cascadeIndex].maximizeInPlace(tmpv1);
                 this._cascadeMaxExtents[cascadeIndex].maximizeInPlace(tmpv1);
             }
             }
         }
         }
-
-        return;
     }
     }
 
 
     /** @hidden */
     /** @hidden */
@@ -1090,7 +1086,6 @@ export class CascadedShadowGenerator implements IShadowGenerator {
         this._transformMatricesAsArray = new Float32Array(this._numCascades * 16);
         this._transformMatricesAsArray = new Float32Array(this._numCascades * 16);
         this._viewSpaceFrustumsZ = new Array(this._numCascades);
         this._viewSpaceFrustumsZ = new Array(this._numCascades);
         this._frustumLengths = new Array(this._numCascades);
         this._frustumLengths = new Array(this._numCascades);
-        this._currentRenderID = new Array(this._numCascades);
         this._lightSizeUVCorrection = new Array(this._numCascades * 2);
         this._lightSizeUVCorrection = new Array(this._numCascades * 2);
         this._depthCorrection = new Array(this._numCascades);
         this._depthCorrection = new Array(this._numCascades);
 
 
@@ -1145,6 +1140,7 @@ export class CascadedShadowGenerator implements IShadowGenerator {
             if (this._breaksAreDirty) {
             if (this._breaksAreDirty) {
                 this._splitFrustum();
                 this._splitFrustum();
             }
             }
+            this._computeMatrices();
         });
         });
 
 
         // Record Face Index before render.
         // Record Face Index before render.