Light.frag.glsl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "shaders/Pack.glsl"
  6. #include "shaders/LightFunctions.glsl"
  7. layout(location = 0) out vec3 out_color;
  8. // Point light
  9. struct PointLight
  10. {
  11. vec4 projectionParams;
  12. vec4 posRadius; // xyz: Light pos in eye space. w: The -1/radius
  13. vec4 diffuseColorPad1; // xyz: diff color
  14. vec4 specularColorPad1; // xyz: spec color
  15. };
  16. // Spot light
  17. struct SpotLight
  18. {
  19. vec4 projectionParams;
  20. vec4 posRadius; // xyz: Light pos in eye space. w: The -1/radius
  21. vec4 diffuseColorOuterCos; // xyz: diff color, w: outer cosine of spot
  22. vec4 specularColorInnerCos; // xyz: spec color, w: inner cosine of spot
  23. vec4 lightDirPad1;
  24. };
  25. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msRt0;
  26. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_msRt1;
  27. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_msRt2;
  28. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_msDepthRt;
  29. layout(ANKI_UBO_BINDING(0, 1)) uniform u1_
  30. {
  31. #if defined(POINT_LIGHT)
  32. PointLight u_light;
  33. #elif defined(SPOT_LIGHT)
  34. SpotLight u_light;
  35. #else
  36. #error See file
  37. #endif
  38. };
  39. #if defined(POINT_LIGHT)
  40. #define u_ldiff u_light.diffuseColorPad1.xyz
  41. #define u_lspec u_light.specularColorPad1.xyz
  42. #else
  43. #define u_ldiff u_light.diffuseColorOuterCos.xyz
  44. #define u_lspec u_light.specularColorInnerCos.xyz
  45. #endif
  46. vec3 readPosition(in vec2 uv)
  47. {
  48. vec3 fragPosVspace;
  49. float depth = texture(u_msDepthRt, uv).r;
  50. fragPosVspace.z = u_light.projectionParams.z / (u_light.projectionParams.w + depth);
  51. fragPosVspace.xy = (2.0 * uv - 1.0) * u_light.projectionParams.xy * fragPosVspace.z;
  52. return fragPosVspace;
  53. }
  54. void main()
  55. {
  56. // Read G-buffer
  57. vec2 uv = vec2(gl_FragCoord.xy) / vec2(RENDERING_WIDTH, RENDERING_HEIGHT);
  58. GbufferInfo gbuffer;
  59. readGBuffer(u_msRt0, u_msRt1, u_msRt2, uv, 0.0, gbuffer);
  60. float a2 = pow(gbuffer.roughness, 2.0);
  61. // Calculate the light color
  62. vec3 fragPos = readPosition(uv);
  63. vec3 viewDir = normalize(-fragPos);
  64. vec3 frag2Light = u_light.posRadius.xyz - fragPos;
  65. vec3 l = normalize(frag2Light);
  66. float nol = max(0.0, dot(gbuffer.normal, l));
  67. vec3 specC = computeSpecularColorBrdf(viewDir, l, gbuffer.normal, gbuffer.specular, u_lspec, a2, nol);
  68. vec3 diffC = computeDiffuseColor(gbuffer.diffuse, u_ldiff);
  69. float att = computeAttenuationFactor(u_light.posRadius.w, frag2Light);
  70. float lambert = nol;
  71. #if defined(POINT_LIGHT)
  72. out_color = (specC + diffC) * (att * max(lambert, gbuffer.subsurface));
  73. #else
  74. float spot =
  75. computeSpotFactor(l, u_light.diffuseColorOuterCos.w, u_light.specularColorInnerCos.w, u_light.lightDirPad1.xyz);
  76. out_color = (diffC + specC) * (att * spot * lambert);
  77. #endif
  78. }