Advertisement
abs25

Shaders

May 9th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef GL_ES
  2. #define LOWP lowp
  3. precision mediump float;
  4. #else
  5. #define LOWP
  6. #endif
  7.  
  8. #define N_LIGHTS 4
  9. //#define N_LIGHTS 1
  10.  
  11. varying LOWP vec4 vColor;
  12. varying vec2 vTexCoord;
  13.  
  14. uniform sampler2D u_texture;
  15. uniform sampler2D u_normals;
  16. uniform vec2 Resolution;
  17. uniform vec3 LightPos[N_LIGHTS];
  18. uniform LOWP vec4 LightColor[N_LIGHTS];
  19. uniform LOWP vec4 AmbientColor;
  20. uniform vec3 Falloff;
  21.  
  22. void main()
  23. {
  24.     //RGBA of our diffuse color
  25.     vec4 DiffuseColor = texture2D(u_texture, vTexCoord);
  26.  
  27.     //RGB of our normal map
  28.     vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;
  29.     vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
  30.     vec3 Sum = Ambient * DiffuseColor.xyz;
  31.     vec3 N = normalize(NormalMap * 2.0 - 1.0);
  32.  
  33.     for (int i=0; i < N_LIGHTS; i++)
  34.     {
  35.         //The delta position of light
  36.         vec3 LightDir = vec3(LightPos[i].xy - (gl_FragCoord.xy / Resolution.xy), LightPos[i].z);
  37.  
  38.         //Correct for aspect ratio
  39.         LightDir.x *= Resolution.x / Resolution.y;
  40.  
  41.         //Determine distance (used for attenuation) BEFORE we normalize our LightDir
  42.         float D = length(LightDir);
  43.         //normalize our vectors
  44.         vec3 L = normalize(LightDir);
  45.  
  46.         vec3 Diffuse = (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
  47.  
  48.         float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
  49.         //float Attenuation = 1.0 / ( Falloff[i].x + (Falloff[i].y*D) + (Falloff[i].z*D*D) );
  50.  
  51.         //the calculation which brings it all together
  52.         vec3 Intensity = Diffuse * Attenuation;
  53.         vec3 FinalColor = DiffuseColor.rgb * Intensity;
  54.  
  55.         Sum += FinalColor;
  56.     }
  57.     gl_FragColor = vColor * vec4(Sum, DiffuseColor.a);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement