@@ -16,21 +16,16 @@ var LightLayer = new Class({
1616 Mixins : [
1717 Components . Alpha ,
1818 Components . BlendMode ,
19- Components . Flip ,
20- Components . GetBounds ,
2119 Components . Origin ,
2220 Components . RenderTarget ,
23- Components . ScaleMode ,
2421 Components . ScrollFactor ,
25- Components . Size ,
26- Components . Transform ,
2722 Components . Visible ,
2823 Render
2924 ] ,
3025
3126 initialize :
3227
33- function LightLayer ( scene , x , y , width , height )
28+ function LightLayer ( scene )
3429 {
3530 GameObject . call ( this , scene , 'LightLayer' ) ;
3631
@@ -62,6 +57,7 @@ var LightLayer = new Class({
6257 float attenuation;
6358 };
6459
60+ uniform vec2 uResolution;
6561 uniform sampler2D uMainTexture;
6662 uniform sampler2D uNormTexture;
6763 uniform vec3 uAmbientLightColor;
@@ -73,19 +69,31 @@ var LightLayer = new Class({
7369
7470 void main()
7571 {
76- /* Just Pass through for now */
77- vec4 finalColor = vec4(1.0, 0.0, 1.0, 1.0);
78- vec4 normal = texture2D(uNormTexture, v_tex_coord);
79- vec4 color = texture2D(uMainTexture, v_tex_coord);
80-
81- finalColor = color * normal;
82-
83- gl_FragColor = finalColor * vec4(v_color, v_alpha);
72+ vec3 finalColor = vec3(0.0, 0.0, 0.0);
73+ vec4 spriteColor = texture2D(uMainTexture, v_tex_coord) * vec4(v_color, v_alpha);
74+ vec3 spriteNormal = texture2D(uNormTexture, v_tex_coord).rgb;
75+ vec3 normal = normalize(vec3(spriteNormal * 2.0 - 1.0));
76+
77+ for (int index = 0; index < ` + Const . MAX_LIGHTS + `; ++index)
78+ {
79+ Light light = uLights[index];
80+ float lightY = uResolution.y - light.position.y;
81+ vec3 lightDir = vec3((vec2(light.position.x, lightY) / uResolution) - (gl_FragCoord.xy / uResolution), light.position.z);
82+ vec3 lightNormal = normalize(lightDir);
83+ float distToSurf = length(lightDir);
84+ float diffuseFactor = max(dot(normal, lightNormal), 0.0);
85+ float attenuation = 1.0 / (1.0 + light.attenuation * (distToSurf * distToSurf));
86+ vec3 diffuse = light.color * spriteColor.rgb * diffuseFactor;
87+ finalColor += attenuation * diffuse;
88+ }
89+
90+ gl_FragColor = vec4(uAmbientLightColor + finalColor, spriteColor.a);
8491 }
8592 ` } ) ;
8693 this . ambientLightColorLoc = this . passShader . getUniformLocation ( 'uAmbientLightColor' ) ;
8794 this . uMainTextureLoc = this . passShader . getUniformLocation ( 'uMainTexture' ) ;
8895 this . uNormTextureLoc = this . passShader . getUniformLocation ( 'uNormTexture' ) ;
96+ this . uResolutionLoc = this . passShader . getUniformLocation ( 'uResolution' ) ;
8997
9098 this . passShader . setConstantInt1 ( this . uMainTextureLoc , 0 ) ;
9199 this . passShader . setConstantInt1 ( this . uNormTextureLoc , 1 ) ;
@@ -100,8 +108,6 @@ var LightLayer = new Class({
100108 }
101109 }
102110
103- this . setPosition ( x , y ) ;
104- this . setSize ( width , height ) ;
105111 this . setOrigin ( 0 , 0 ) ;
106112 } ,
107113
@@ -176,7 +182,7 @@ var LightLayer = new Class({
176182 }
177183 } ,
178184
179- updateLights : function ( )
185+ updateLights : function ( renderer , camera )
180186 {
181187 if ( this . gl !== null )
182188 {
@@ -186,14 +192,15 @@ var LightLayer = new Class({
186192 var gl = this . gl ;
187193 var passShader = this . passShader ;
188194
195+ passShader . setConstantFloat2 ( this . uResolutionLoc , renderer . width * camera . zoom , renderer . height * camera . zoom ) ;
189196 passShader . setConstantFloat3 ( this . ambientLightColorLoc , this . ambientLightColorR , this . ambientLightColorG , this . ambientLightColorB ) ;
190197
191198 for ( var index = 0 ; index < length ; ++ index )
192199 {
193200 var light = lights [ index ] ;
194201 passShader . setConstantFloat3 ( locations [ index ] . position , light . x , light . y , light . z ) ;
195202 passShader . setConstantFloat3 ( locations [ index ] . color , light . r , light . g , light . b ) ;
196- passShader . setConstantFloat3 ( locations [ index ] . attenuation , light . attenuation ) ;
203+ passShader . setConstantFloat1 ( locations [ index ] . attenuation , light . attenuation ) ;
197204 }
198205 }
199206 }
0 commit comments