Parcourir la source

Small updates following comments

Popov72 il y a 5 ans
Parent
commit
80e47baebe
1 fichiers modifiés avec 7 ajouts et 8 suppressions
  1. 7 8
      src/Lights/Shadows/cascadedShadowGenerator.ts

+ 7 - 8
src/Lights/Shadows/cascadedShadowGenerator.ts

@@ -43,7 +43,7 @@ let tmpv1 = new Vector3(),
 /**
 /**
  * A CSM implementation allowing casting shadows on large scenes.
  * A CSM implementation allowing casting shadows on large scenes.
  * Documentation : https://doc.babylonjs.com/babylon101/cascadedShadows
  * Documentation : https://doc.babylonjs.com/babylon101/cascadedShadows
- * Based on: https://johanmedestrom.wordpress.com/2016/03/18/opengl-cascaded-shadow-maps/
+ * Based on: https://github.com/TheRealMJP/Shadows and https://johanmedestrom.wordpress.com/2016/03/18/opengl-cascaded-shadow-maps/
  */
  */
 export class CascadedShadowGenerator implements IShadowGenerator {
 export class CascadedShadowGenerator implements IShadowGenerator {
 
 
@@ -884,7 +884,7 @@ export class CascadedShadowGenerator implements IShadowGenerator {
             // Calculate the radius of a bounding sphere surrounding the frustum corners
             // Calculate the radius of a bounding sphere surrounding the frustum corners
             let sphereRadius = 0;
             let sphereRadius = 0;
             for (let cornerIndex = 0; cornerIndex < this._frustumCornersWorldSpace[cascadeIndex].length; ++cornerIndex) {
             for (let cornerIndex = 0; cornerIndex < this._frustumCornersWorldSpace[cascadeIndex].length; ++cornerIndex) {
-                const dist = this._frustumCornersWorldSpace[cascadeIndex][cornerIndex].subtract(this._frustumCenter[cascadeIndex]).length();
+                const dist = this._frustumCornersWorldSpace[cascadeIndex][cornerIndex].subtractToRef(this._frustumCenter[cascadeIndex], tmpv1).length();
                 sphereRadius = Math.max(sphereRadius, dist);
                 sphereRadius = Math.max(sphereRadius, dist);
             }
             }
 
 
@@ -1469,12 +1469,6 @@ export class CascadedShadowGenerator implements IShadowGenerator {
 
 
         const width = shadowMap.getSize().width;
         const width = shadowMap.getSize().width;
 
 
-        for (let cascadeIndex = 0; cascadeIndex < this._numCascades; ++cascadeIndex) {
-            this._lightSizeUVCorrection[cascadeIndex * 2 + 0] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[0].x - this._cascadeMinExtents[0].x) / (this._cascadeMaxExtents[cascadeIndex].x - this._cascadeMinExtents[cascadeIndex].x); // x correction
-            this._lightSizeUVCorrection[cascadeIndex * 2 + 1] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[0].y - this._cascadeMinExtents[0].y) / (this._cascadeMaxExtents[cascadeIndex].y - this._cascadeMinExtents[cascadeIndex].y); // y correction
-            this._depthCorrection[cascadeIndex] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[cascadeIndex].z - this._cascadeMinExtents[cascadeIndex].z) / (this._cascadeMaxExtents[0].z - this._cascadeMinExtents[0].z);
-        }
-
         effect.setMatrices("lightMatrix" + lightIndex, this._transformMatricesAsArray);
         effect.setMatrices("lightMatrix" + lightIndex, this._transformMatricesAsArray);
         effect.setArray("viewFrustumZ" + lightIndex, this._viewSpaceFrustumsZ);
         effect.setArray("viewFrustumZ" + lightIndex, this._viewSpaceFrustumsZ);
         effect.setFloat("cascadeBlendFactor" + lightIndex, this.cascadeBlendPercentage === 0 ? 10000 : 1 / this.cascadeBlendPercentage);
         effect.setFloat("cascadeBlendFactor" + lightIndex, this.cascadeBlendPercentage === 0 ? 10000 : 1 / this.cascadeBlendPercentage);
@@ -1485,6 +1479,11 @@ export class CascadedShadowGenerator implements IShadowGenerator {
             effect.setDepthStencilTexture("shadowSampler" + lightIndex, shadowMap);
             effect.setDepthStencilTexture("shadowSampler" + lightIndex, shadowMap);
             light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), width, 1 / width, this.frustumEdgeFalloff, lightIndex);
             light._uniformBuffer.updateFloat4("shadowsInfo", this.getDarkness(), width, 1 / width, this.frustumEdgeFalloff, lightIndex);
         } else if (this._filter === CascadedShadowGenerator.FILTER_PCSS) {
         } else if (this._filter === CascadedShadowGenerator.FILTER_PCSS) {
+            for (let cascadeIndex = 0; cascadeIndex < this._numCascades; ++cascadeIndex) {
+                this._lightSizeUVCorrection[cascadeIndex * 2 + 0] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[0].x - this._cascadeMinExtents[0].x) / (this._cascadeMaxExtents[cascadeIndex].x - this._cascadeMinExtents[cascadeIndex].x); // x correction
+                this._lightSizeUVCorrection[cascadeIndex * 2 + 1] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[0].y - this._cascadeMinExtents[0].y) / (this._cascadeMaxExtents[cascadeIndex].y - this._cascadeMinExtents[cascadeIndex].y); // y correction
+                this._depthCorrection[cascadeIndex] = cascadeIndex === 0 ? 1 : (this._cascadeMaxExtents[cascadeIndex].z - this._cascadeMinExtents[cascadeIndex].z) / (this._cascadeMaxExtents[0].z - this._cascadeMinExtents[0].z);
+            }
             effect.setDepthStencilTexture("shadowSampler" + lightIndex, shadowMap);
             effect.setDepthStencilTexture("shadowSampler" + lightIndex, shadowMap);
             effect.setTexture("depthSampler" + lightIndex, shadowMap);
             effect.setTexture("depthSampler" + lightIndex, shadowMap);
             effect.setArray2("lightSizeUVCorrection" + lightIndex, this._lightSizeUVCorrection);
             effect.setArray2("lightSizeUVCorrection" + lightIndex, this._lightSizeUVCorrection);