Advertisement
Guest User

Untitled

a guest
May 24th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. //attributes from vertex shader
  2. varying vec4 vColor;
  3. varying vec2 vTexCoord;
  4.  
  5. //our texture samplers
  6. uniform sampler2D u_texture; //diffuse map
  7. uniform sampler2D u_normals; //normal map
  8.  
  9. //values used for shading algorithm...
  10. uniform vec2 Resolution; //resolution of screen
  11. uniform vec3 LightPos; //light position, normalized
  12. uniform vec4 LightColor; //light RGBA -- alpha is intensity
  13. uniform vec4 AmbientColor; //ambient RGBA -- alpha is intensity
  14. uniform vec3 Falloff; //attenuation coefficients
  15. uniform float lightX; //X Position of light. Can also feed Y for times of the year.
  16.  
  17. void main()
  18. {
  19. //RGBA of our diffuse color
  20. vec4 DiffuseColor = texture2D(u_texture, vTexCoord);
  21.  
  22. //RGB of our normal map
  23. vec3 NormalMap = texture2D(u_normals, vTexCoord).rgb;
  24.  
  25. int numberOfLights = 5;
  26. vec3 lightPoses[5];
  27. lightPoses[0] = vec3(lightX - 1.0, LightPos.y, LightPos.z);
  28. lightPoses[1] = vec3(lightX - 0.5, LightPos.y, LightPos.z);
  29. lightPoses[2] = vec3(lightX, LightPos.y, LightPos.z);
  30. lightPoses[3] = vec3(lightX + 0.5, LightPos.y, LightPos.z);
  31. lightPoses[4] = vec3(lightX + 1.0, LightPos.y, LightPos.z);
  32.  
  33. vec3 Sum = vec3(0.0);
  34.  
  35. //Go though both lights.
  36. for(int index=0; index < numberOfLights; index++)
  37. {
  38. //The delta position of light
  39. vec3 LightDir = vec3(lightPoses[index].xy - (gl_FragCoord.xy / Resolution.xy), LightPos.z * 2.0);
  40.  
  41. //Aspect ratio
  42. float aspectRatio = Resolution.x / Resolution.y;
  43.  
  44. //Correct for aspect ratio
  45. LightDir.x = LightDir.x * (Resolution.x / Resolution.y);
  46.  
  47. //Determine distance (used for attenuation) BEFORE we normalize our LightDir
  48. float D = length(LightDir);
  49.  
  50. //normalize our vectors
  51. vec3 N = normalize(NormalMap * 2.0 - 1.0);
  52. vec3 L = normalize(LightDir);
  53.  
  54. //Pre-multiply light color with intensity
  55. //Then perform "N dot L" to determine our diffuse term
  56. vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);
  57.  
  58. //pre-multiply ambient color with intensity
  59. vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
  60. //Because there are more lights, take off total ambient power.
  61. Ambient *= vec3(1.0 / float(numberOfLights));
  62.  
  63. //calculate attenuation
  64. float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D));
  65.  
  66. //the calculation which brings it all together
  67. vec3 Intensity = Ambient + Diffuse * Attenuation;
  68. vec3 FinalColor = DiffuseColor.rgb * Intensity;
  69.  
  70. Sum += FinalColor;
  71. }
  72.  
  73. gl_FragColor = vec4(Sum, DiffuseColor.a);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement