소스 검색

Add fix shadow frustum on directional lights and shadow angle scale on spot lights.

Sebastien Vandenberghe 8 년 전
부모
커밋
d2e1133a83
2개의 변경된 파일62개의 추가작업 그리고 4개의 파일을 삭제
  1. 41 2
      src/Lights/babylon.directionalLight.ts
  2. 21 2
      src/Lights/babylon.spotLight.ts

+ 41 - 2
src/Lights/babylon.directionalLight.ts

@@ -3,8 +3,23 @@
 module BABYLON {
     export class DirectionalLight extends ShadowLight {
 
-        private _shadowOrthoScale = 0.5;
+        private _shadowFrustumSize = 0;
+        /**
+         * Fix frustum size for the shadow generation. This is disabled if the value is 0.
+         */
+        @serialize()
+        public get shadowFrustumSize(): number {
+            return this._shadowFrustumSize
+        }
+        /**
+         * Specifies a fix frustum size for the shadow generation.
+         */
+        public set shadowFrustumSize(value: number) {
+            this._shadowFrustumSize = value;
+            this.forceProjectionMatrixCompute();
+        }
 
+        private _shadowOrthoScale = 0.5;
         @serialize()
         public get shadowOrthoScale(): number {
             return this._shadowOrthoScale
@@ -51,9 +66,33 @@ module BABYLON {
 
         /**
          * Sets the passed matrix "matrix" as projection matrix for the shadows cast by the light according to the passed view matrix.  
-         * Returns the DirectionalLight.  
+         * Returns the DirectionalLight Shadow projection matrix.
          */
         protected _setDefaultShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
+            if (this.shadowFrustumSize > 0) {
+                this._setDefaultFixedFrustumShadowProjectionMatrix(matrix, viewMatrix);
+            }
+            else {
+                this._setDefaultAutoExtendShadowProjectionMatrix(matrix, viewMatrix, renderList);
+            }
+        }
+
+        /**
+         * Sets the passed matrix "matrix" as fixed frustum projection matrix for the shadows cast by the light according to the passed view matrix.
+         * Returns the DirectionalLight Shadow projection matrix.
+         */
+        protected _setDefaultFixedFrustumShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix): void {
+            var activeCamera = this.getScene().activeCamera;
+
+            Matrix.OrthoLHToRef(this.shadowFrustumSize, this.shadowFrustumSize,
+                this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera.minZ, this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera.maxZ, matrix);
+        }
+
+        /**
+         * Sets the passed matrix "matrix" as auto extend projection matrix for the shadows cast by the light according to the passed view matrix.  
+         * Returns the DirectionalLight Shadow projection matrix.
+         */
+        protected _setDefaultAutoExtendShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
             var activeCamera = this.getScene().activeCamera;
 
             // Check extends

+ 21 - 2
src/Lights/babylon.spotLight.ts

@@ -1,7 +1,6 @@
 module BABYLON {
     export class SpotLight extends ShadowLight {
         private _angle: number;
-
         @serialize()
         public get angle(): number {
             return this._angle
@@ -11,6 +10,22 @@
             this.forceProjectionMatrixCompute();
         }
 
+        private _shadowAngleScale: number;
+        @serialize()
+        /**
+         * Allows scaling the angle of the light for shadow generation only.
+         */
+        public get shadowAngleScale(): number {
+            return this._shadowAngleScale
+        }
+        /**
+         * Allows scaling the angle of the light for shadow generation only.
+         */
+        public set shadowAngleScale(value: number) {
+            this._shadowAngleScale = value;
+            this.forceProjectionMatrixCompute();
+        }
+
         @serialize()
         public exponent: number;
         
@@ -53,7 +68,11 @@
          */
         protected _setDefaultShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
             var activeCamera = this.getScene().activeCamera;
-            Matrix.PerspectiveFovLHToRef(this.angle, 1.0, 
+
+            this._shadowAngleScale = this._shadowAngleScale || 1;
+            var angle = this._shadowAngleScale * this._angle;
+            
+            Matrix.PerspectiveFovLHToRef(angle, 1.0, 
             this.shadowMinZ !== undefined ? this.shadowMinZ : activeCamera.minZ, this.shadowMaxZ !== undefined ? this.shadowMaxZ : activeCamera.maxZ, matrix);
         }