Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef GL_ES
- #define LOWP lowp
- precision mediump float;
- #else
- #define LOWP
- #endif
- #define N_LIGHTS 4
- //#define N_LIGHTS 1
- varying LOWP vec4 vColor;
- varying vec2 vTexCoord;
- uniform sampler2D u_texture;
- uniform sampler2D u_normals;
- uniform vec2 Resolution;
- uniform vec3 LightPos[N_LIGHTS];
- uniform LOWP vec4 LightColor[N_LIGHTS];
- uniform LOWP vec4 AmbientColor;
- uniform vec3 Falloff;
- void main()
- {
- //RGBA of our diffuse color
- vec4 DiffuseColor = texture2D(u_texture, vTexCoord);
- //RGB of our normal map
- vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;
- vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
- vec3 Sum = Ambient * DiffuseColor.xyz;
- vec3 N = normalize(NormalMap * 2.0 - 1.0);
- for (int i=0; i < N_LIGHTS; i++)
- {
- //The delta position of light
- vec3 LightDir = vec3(LightPos[i].xy - (gl_FragCoord.xy / Resolution.xy), LightPos[i].z);
- //Correct for aspect ratio
- LightDir.x *= Resolution.x / Resolution.y;
- //Determine distance (used for attenuation) BEFORE we normalize our LightDir
- float D = length(LightDir);
- //normalize our vectors
- vec3 L = normalize(LightDir);
- vec3 Diffuse = (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
- float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
- //float Attenuation = 1.0 / ( Falloff[i].x + (Falloff[i].y*D) + (Falloff[i].z*D*D) );
- //the calculation which brings it all together
- vec3 Intensity = Diffuse * Attenuation;
- vec3 FinalColor = DiffuseColor.rgb * Intensity;
- Sum += FinalColor;
- }
- gl_FragColor = vColor * vec4(Sum, DiffuseColor.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement