Irradiance.frag.glsl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // Compute the irradiance given an environment map
  6. #include "shaders/Functions.glsl"
  7. const float INDIRECT_BUMP = 2.5; // A sort of hack
  8. layout(location = 0) in vec2 in_uv;
  9. layout(location = 0) out vec3 out_color;
  10. layout(ANKI_TEX_BINDING(0, 0)) uniform samplerCubeArray u_envTex;
  11. layout(ANKI_UBO_BINDING(0, 0)) uniform u0_
  12. {
  13. // x: The face index to render to
  14. // y: The array index to read from the u_envTex
  15. uvec4 u_faceIdxArrayIdxPad2;
  16. };
  17. // Integrate the environment map to compute the irradiance for a single fragment
  18. void main()
  19. {
  20. uint face = u_faceIdxArrayIdxPad2.x;
  21. float texArrIdx = float(u_faceIdxArrayIdxPad2.y);
  22. // Get the r coordinate of the current face and fragment
  23. vec3 ri = getCubemapDirection(UV_TO_NDC(in_uv), face);
  24. vec3 outCol = vec3(0.0);
  25. float weight = EPSILON;
  26. // For all the faces and texels of the environment map calculate a color sum
  27. for(uint f = 0u; f < 6u; ++f)
  28. {
  29. for(uint i = 0u; i < CUBEMAP_SIZE; ++i)
  30. {
  31. for(uint j = 0u; j < CUBEMAP_SIZE; ++j)
  32. {
  33. vec2 uv = vec2(j, i) / float(CUBEMAP_SIZE);
  34. vec3 r = getCubemapDirection(UV_TO_NDC(uv), f);
  35. float lambert = dot(r, ri);
  36. if(lambert > 0.0)
  37. {
  38. vec3 col = textureLod(u_envTex, vec4(r, texArrIdx), 0.0).rgb;
  39. outCol += col * lambert;
  40. weight += lambert;
  41. }
  42. }
  43. }
  44. }
  45. out_color = outCol / weight * (2.0 * PI);
  46. }