Parcourir la source

Merge pull request #7349 from Popov72/autoCalcShadowZBounds

Add DirectionalLight.autoCalcShadowZBounds property
David Catuhe il y a 5 ans
Parent
commit
a08b3934ac

+ 1 - 0
dist/preview release/what's new.md

@@ -51,6 +51,7 @@
 - Allow setting of `BABYLON.Basis.JSModuleURL` and `BABYLON.Basis.WasmModuleURL`, for hosting the Basis transcoder locally ([JasonAyre])(https://github.com/jasonyre))
 - PNG support for browsers not supporting SVG ([RaananW](https://github.com/RaananW/))
 - Device orientation event permissions for iOS 13+ ([RaananW](https://github.com/RaananW/))
+- Added `DirectionalLight.autoCalcShadowZBounds` to automatically compute the `shadowMinZ` and `shadowMaxZ` values ([Popov72](https://github.com/Popov72))
 
 ### Engine
 

+ 23 - 0
src/Lights/directionalLight.ts

@@ -63,6 +63,13 @@ export class DirectionalLight extends ShadowLight {
     @serialize()
     public autoUpdateExtends = true;
 
+    /**
+     * Automatically compute the shadowMinZ and shadowMaxZ for the projection matrix to best fit (including all the casters)
+     * on each frame. autoUpdateExtends must be set to true for this to work
+     */
+    @serialize()
+    public autoCalcShadowZBounds = false;
+
     // Cache
     private _orthoLeft = Number.MAX_VALUE;
     private _orthoRight = Number.MIN_VALUE;
@@ -148,6 +155,9 @@ export class DirectionalLight extends ShadowLight {
             this._orthoTop = Number.MIN_VALUE;
             this._orthoBottom = Number.MAX_VALUE;
 
+            var shadowMinZ = Number.MAX_VALUE;
+            var shadowMaxZ = Number.MIN_VALUE;
+
             for (var meshIndex = 0; meshIndex < renderList.length; meshIndex++) {
                 var mesh = renderList[meshIndex];
 
@@ -174,8 +184,21 @@ export class DirectionalLight extends ShadowLight {
                     if (tempVector3.y > this._orthoTop) {
                         this._orthoTop = tempVector3.y;
                     }
+                    if (this.autoCalcShadowZBounds) {
+                        if (tempVector3.z < shadowMinZ) {
+                            shadowMinZ = tempVector3.z;
+                        }
+                        if (tempVector3.z > shadowMaxZ) {
+                            shadowMaxZ = tempVector3.z;
+                        }
+                    }
                 }
             }
+
+            if (this.autoCalcShadowZBounds) {
+                this._shadowMinZ = shadowMinZ;
+                this._shadowMaxZ = shadowMaxZ;
+            }
         }
 
         var xOffset = this._orthoRight - this._orthoLeft;

+ 2 - 2
src/Lights/shadowLight.ts

@@ -165,7 +165,7 @@ export abstract class ShadowLight extends Light implements IShadowLight {
         this._setDirection(value);
     }
 
-    private _shadowMinZ: number;
+    protected _shadowMinZ: number;
     /**
      * Gets the shadow projection clipping minimum z value.
      */
@@ -181,7 +181,7 @@ export abstract class ShadowLight extends Light implements IShadowLight {
         this.forceProjectionMatrixCompute();
     }
 
-    private _shadowMaxZ: number;
+    protected _shadowMaxZ: number;
     /**
      * Sets the shadow projection clipping maximum z value.
      */