Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. extern Image u_normals; //normal map
  2.  
  3. //values used for shading algorithm...
  4. extern vec3 LightPos; //light position, normalized
  5. extern vec4 LightColor; //light RGBA -- alpha is intensity
  6. extern vec4 AmbientColor; //ambient RGBA -- alpha is intensity
  7. extern vec3 Falloff; //attenuation coefficients
  8.  
  9. vec4 effect(vec4 vColor, Image u_texture, vec2 vTexCoord, vec2 pixcoord)
  10. {
  11. //RGBA of our diffuse color
  12. vec4 DiffuseColor = Texel(u_texture, vTexCoord);
  13.  
  14. //RGB of our normal map
  15. vec3 NormalMap = Texel(u_normals, vTexCoord).rgb;
  16.  
  17. //The delta position of light
  18. vec3 LightDir = vec3(LightPos.xy - (pixcoord / love_ScreenSize.xy), LightPos.z);
  19.  
  20. //Correct for aspect ratio
  21. LightDir.x *= love_ScreenSize.x / love_ScreenSize.y;
  22.  
  23. //Determine distance (used for attenuation) BEFORE we normalize our LightDir
  24. float D = length(LightDir);
  25.  
  26. //normalize our vectors
  27. vec3 N = normalize(NormalMap * 2.0 - 1.0);
  28. vec3 L = normalize(LightDir);
  29.  
  30. //Pre-multiply light color with intensity
  31. //Then perform "N dot L" to determine our diffuse term
  32. vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);
  33.  
  34. //pre-multiply ambient color with intensity
  35. vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
  36.  
  37. //calculate attenuation
  38. float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
  39.  
  40. //the calculation which brings it all together
  41. vec3 Intensity = Ambient + Diffuse * Attenuation;
  42. vec3 FinalColor = DiffuseColor.rgb * Intensity;
  43.  
  44. return vColor * vec4(FinalColor, DiffuseColor.a);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement