No Description

irradiancecube.frag 1.0KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Generates an irradiance cube from an environment map using convolution
  2. #version 450
  3. layout (location = 0) in vec3 inPos;
  4. layout (location = 0) out vec4 outColor;
  5. layout (binding = 0) uniform samplerCube samplerEnv;
  6. layout(push_constant) uniform PushConsts {
  7. layout (offset = 64) float deltaPhi;
  8. layout (offset = 68) float deltaTheta;
  9. } consts;
  10. #define PI 3.1415926535897932384626433832795
  11. void main()
  12. {
  13. vec3 N = normalize(inPos);
  14. vec3 up = vec3(0.0, 1.0, 0.0);
  15. vec3 right = normalize(cross(up, N));
  16. up = cross(N, right);
  17. const float TWO_PI = PI * 2.0;
  18. const float HALF_PI = PI * 0.5;
  19. vec3 color = vec3(0.0);
  20. uint sampleCount = 0u;
  21. for (float phi = 0.0; phi < TWO_PI; phi += consts.deltaPhi) {
  22. for (float theta = 0.0; theta < HALF_PI; theta += consts.deltaTheta) {
  23. vec3 tempVec = cos(phi) * right + sin(phi) * up;
  24. vec3 sampleVector = cos(theta) * N + sin(theta) * tempVec;
  25. color += texture(samplerEnv, sampleVector).rgb * cos(theta) * sin(theta);
  26. sampleCount++;
  27. }
  28. }
  29. outColor = vec4(PI * color / float(sampleCount), 1.0);
  30. }