normalsShader.frag 958 B

12345678910111213141516171819202122232425262728293031323334
  1. #version 120
  2. // Input textures
  3. uniform sampler2D normalsTexture;
  4. uniform sampler2D lightTexture;
  5. uniform vec2 targetSize;
  6. uniform vec2 lightSize;
  7. // Light properties
  8. uniform vec3 lightPosition;
  9. uniform vec3 lightColor;
  10. void main()
  11. {
  12. // Sample our normals map
  13. vec2 coord = gl_TexCoord[0].xy;
  14. float lightPower = texture2D(lightTexture, coord).r;
  15. vec4 normalsColor = texture2D(normalsTexture, gl_FragCoord.xy / targetSize);
  16. // Get normal value from sample
  17. vec3 normals = normalize(normalsColor.rgb * 2.0 - 1.0);
  18. // Compute the light vector
  19. vec2 lightVector = lightPosition.xy - gl_FragCoord.xy;
  20. vec3 lightDir = vec3(lightVector / lightSize, lightPosition.z);
  21. lightDir = normalize(lightDir);
  22. // Compute the color intensity based on normals
  23. float colorIntensity = max(dot(normals, lightDir), 0.0);
  24. vec4 diffuse = vec4(lightColor * colorIntensity, lightPower);
  25. gl_FragColor = clamp(diffuse, 0.0, 1.0);
  26. }