Pārlūkot izejas kodu

Migrate Projection to spot only

Sebastien Vandenberghe 7 gadi atpakaļ
vecāks
revīzija
07fe59af48

+ 2 - 58
src/Lights/babylon.light.ts

@@ -384,59 +384,7 @@ module BABYLON {
             }
             }
             return b.renderPriority - a.renderPriority;
             return b.renderPriority - a.renderPriority;
         }
         }
-        // Projection texture, if needed
-        protected _computeTextureMatrix(): void{
-            //Leave out for different light type
-        }
-        protected _light_far  :number;
-        @serialize()
-        /**
-         * Allows reading the far clip of the Spotlight for texture projection.
-         */
-        public get light_far(): number {
-            return this._light_far;
-        }
-        /**
-         * Allows setting the far clip of the Spotlight for texture projection.
-         */
-        public set light_far(value: number) {
-            this._light_far = value;
-            this._computeTextureMatrix();
-        }
 
 
-        protected _light_near :number;
-        @serialize()
-        /**
-         * Allows reading the near clip of the Spotlight for texture projection.
-         */
-        public get light_near(): number {
-            return this._light_near;
-        }
-        /**
-         * Allows setting the near clip of the Spotlight for texture projection.
-         */
-        public set light_near(value: number) {
-            this._light_near = value;
-            this._computeTextureMatrix();
-        }
-
-        @serializeAsTexture("projectedLightTexture")
-        private _projectedLightTexture: Nullable<BaseTexture>;;
-        /** 
-         * Allows reading the projection texture of the light.
-        */
-        public get projectedLightTexture(): Nullable<BaseTexture> {
-            return this._projectedLightTexture;
-        }
-        /**
-        * Allows setting the projection texture of the light.
-        */
-        public set projectedLightTexture(value: Nullable<BaseTexture>) {
-            this._projectedLightTexture = value;
-            this._light_far = 1000.0;
-            this._light_near = 1e-6;
-            this._computeTextureMatrix();
-        }
         /**
         /**
          * Disposes the light.  
          * Disposes the light.  
          */
          */
@@ -455,9 +403,7 @@ module BABYLON {
             }
             }
 
 
             this._uniformBuffer.dispose();
             this._uniformBuffer.dispose();
-            if (this._projectedLightTexture){
-                this._projectedLightTexture.dispose();
-            }
+
             // Remove from scene
             // Remove from scene
             this.getScene().removeLight(this);
             this.getScene().removeLight(this);
             super.dispose();
             super.dispose();
@@ -717,6 +663,7 @@ module BABYLON {
             }
             }
             return photometricScale;
             return photometricScale;
         }
         }
+
         public _reorderLightsInScene(): void {
         public _reorderLightsInScene(): void {
             var scene = this.getScene();
             var scene = this.getScene();
             if (this._renderPriority != 0) {
             if (this._renderPriority != 0) {
@@ -724,8 +671,5 @@ module BABYLON {
             }
             }
             this.getScene().sortLightsByPriority();
             this.getScene().sortLightsByPriority();
         }
         }
-
-
-
     }
     }
 }
 }

+ 90 - 32
src/Lights/babylon.spotLight.ts

@@ -17,38 +17,6 @@
             projection matrix. Need to call computeTextureMatrix() to recompute manually. Add inheritance
             projection matrix. Need to call computeTextureMatrix() to recompute manually. Add inheritance
             to the setting function of the 2 attributes will solve the problem.
             to the setting function of the 2 attributes will solve the problem.
         */
         */
-        /**
-         * Main function for light texture projection matrix computing.
-         */
-        protected _computeTextureMatrix(): void{    
-
-            var viewLightMatrix = Matrix.Zero();
-            Matrix.LookAtLHToRef(this.position, this.position.add(this.direction), Vector3.Up(), viewLightMatrix);
-
-            var light_far = this.light_far;
-            var light_near = this.light_near;
-
-            var P = light_far / (light_far - light_near);
-            var Q = - P * light_near;
-            var S = 1.0 / Math.tan(this._angle / 2.0);
-            var A = 1.0;
-            
-            var projectionLightMatrix = Matrix.Zero();
-            Matrix.FromValuesToRef(S/A, 0.0, 0.0, 0.0,
-                0.0, S, 0.0, 0.0,
-                0.0, 0.0, P, 1.0,
-                0.0, 0.0, Q, 0.0, projectionLightMatrix);
-
-            var scaleMatrix = Matrix.Zero();
-            Matrix.FromValuesToRef(0.5, 0.0, 0.0, 0.0,
-                0.0, 0.5, 0.0, 0.0,
-                0.0, 0.0, 0.5, 0.0,
-                0.5, 0.5, 0.5, 1.0, scaleMatrix);
-                
-            this._textureProjectionMatrix.copyFrom(viewLightMatrix);
-            this._textureProjectionMatrix.multiplyToRef(projectionLightMatrix, this._textureProjectionMatrix);
-            this._textureProjectionMatrix.multiplyToRef(scaleMatrix, this._textureProjectionMatrix);
-        }
 
 
         private _angle: number;
         private _angle: number;
         @serialize()
         @serialize()
@@ -93,6 +61,53 @@
             this._textureProjectionMatrix = value;
             this._textureProjectionMatrix = value;
         }
         }
 
 
+        protected _light_near :number;
+        @serialize()
+        /**
+         * Allows reading the near clip of the Spotlight for texture projection.
+         */
+        public get light_near(): number {
+            return this._light_near;
+        }
+        /**
+         * Allows setting the near clip of the Spotlight for texture projection.
+         */
+        public set light_near(value: number) {
+            this._light_near = value;
+            this._computeTextureMatrix();
+        }
+
+        @serializeAsTexture("projectedLightTexture")
+        private _projectedLightTexture: Nullable<BaseTexture>;;
+        /** 
+         * Allows reading the projection texture of the light.
+        */
+        public get projectedLightTexture(): Nullable<BaseTexture> {
+            return this._projectedLightTexture;
+        }
+
+        protected _light_far  :number;
+        @serialize()
+        /**
+         * Allows reading the far clip of the Spotlight for texture projection.
+         */
+        public get light_far(): number {
+            return this._light_far;
+        }
+        public set light_far(value: number) {
+            this._light_far = value;
+        }
+
+        /**
+        * Allows setting the projection texture of the light.
+        */
+        public set projectedLightTexture(value: Nullable<BaseTexture>) {
+            this._projectedLightTexture = value;
+            this._light_far = 1000.0;
+            this._light_near = 1e-6;
+            this._computeTextureMatrix();
+        }
+
         /**
         /**
          * Creates a SpotLight object in the scene with the passed parameters :   
          * Creates a SpotLight object in the scene with the passed parameters :   
          * - `position` (Vector3) is the initial SpotLight position,  
          * - `position` (Vector3) is the initial SpotLight position,  
@@ -144,6 +159,39 @@
             this.getDepthMinZ(activeCamera), this.getDepthMaxZ(activeCamera), matrix);
             this.getDepthMinZ(activeCamera), this.getDepthMaxZ(activeCamera), matrix);
         }
         }
 
 
+        /**
+         * Main function for light texture projection matrix computing.
+         */
+        protected _computeTextureMatrix(): void {
+
+            var viewLightMatrix = Matrix.Zero();
+            Matrix.LookAtLHToRef(this.position, this.position.add(this.direction), Vector3.Up(), viewLightMatrix);
+
+            var light_far = this.light_far;
+            var light_near = this.light_near;
+
+            var P = light_far / (light_far - light_near);
+            var Q = - P * light_near;
+            var S = 1.0 / Math.tan(this._angle / 2.0);
+            var A = 1.0;
+            
+            var projectionLightMatrix = Matrix.Zero();
+            Matrix.FromValuesToRef(S/A, 0.0, 0.0, 0.0,
+                0.0, S, 0.0, 0.0,
+                0.0, 0.0, P, 1.0,
+                0.0, 0.0, Q, 0.0, projectionLightMatrix);
+
+            var scaleMatrix = Matrix.Zero();
+            Matrix.FromValuesToRef(0.5, 0.0, 0.0, 0.0,
+                0.0, 0.5, 0.0, 0.0,
+                0.0, 0.0, 0.5, 0.0,
+                0.5, 0.5, 0.5, 1.0, scaleMatrix);
+                
+            this._textureProjectionMatrix.copyFrom(viewLightMatrix);
+            this._textureProjectionMatrix.multiplyToRef(projectionLightMatrix, this._textureProjectionMatrix);
+            this._textureProjectionMatrix.multiplyToRef(scaleMatrix, this._textureProjectionMatrix);
+        }
+
         protected _buildUniformLayout(): void {
         protected _buildUniformLayout(): void {
             this._uniformBuffer.addUniform("vLightData", 4);
             this._uniformBuffer.addUniform("vLightData", 4);
             this._uniformBuffer.addUniform("vLightDiffuse", 4);
             this._uniformBuffer.addUniform("vLightDiffuse", 4);
@@ -194,5 +242,15 @@
             }
             }
             return this;
             return this;
         }
         }
+
+        /**
+         * Disposes the light.
+         */
+        public dispose() : void {
+            super.dispose();
+            if (this._projectedLightTexture){
+                this._projectedLightTexture.dispose();
+            }
+        }
     }
     }
 }
 }

+ 1 - 6
src/Materials/babylon.materialHelper.ts

@@ -169,6 +169,7 @@ module BABYLON {
                     var type;
                     var type;
                     if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
                     if (light.getTypeID() === Light.LIGHTTYPEID_SPOTLIGHT) {
                         type = "SPOTLIGHT" + lightIndex;
                         type = "SPOTLIGHT" + lightIndex;
+                        defines["PROJECTEDLIGHTTEXTURE" + lightIndex] = (light as SpotLight).projectedLightTexture ? true : false;
                     } else if (light.getTypeID() === Light.LIGHTTYPEID_HEMISPHERICLIGHT) {
                     } else if (light.getTypeID() === Light.LIGHTTYPEID_HEMISPHERICLIGHT) {
                         type = "HEMILIGHT" + lightIndex;
                         type = "HEMILIGHT" + lightIndex;
                     } else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
                     } else if (light.getTypeID() === Light.LIGHTTYPEID_POINTLIGHT) {
@@ -207,12 +208,6 @@ module BABYLON {
                         defines["LIGHTMAPNOSPECULAR" + lightIndex] = false;
                         defines["LIGHTMAPNOSPECULAR" + lightIndex] = false;
                     }
                     }
 
 
-                    //Projection texture
-                    if (light.projectedLightTexture){
-                        defines["PROJECTEDLIGHTTEXTURE" + lightIndex] = true;
-                    }else{
-                        defines["PROJECTEDLIGHTTEXTURE" + lightIndex] = false;                        
-                    }
                     lightIndex++;
                     lightIndex++;
                     if (lightIndex === maxSimultaneousLights)
                     if (lightIndex === maxSimultaneousLights)
                         break;
                         break;