hdrFiltering.fragment.fx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. uniform samplerCube inputTexture;
  2. uniform float cubeWidth;
  3. uniform vec3 sampleDirections[NUM_SAMPLES];
  4. uniform float weights[NUM_SAMPLES];
  5. varying vec3 direction;
  6. void main() {
  7. // Rotation by PI around y is necessary for consistency with IBLBaker
  8. // vec3 n = vec3(-direction.x, direction.y, -direction.z);
  9. vec3 n = normalize(direction);
  10. vec3 tangent = abs(n.z) < 0.999 ? vec3(0., 0., 1.) : vec3(1., 0., 0.);
  11. tangent = normalize(cross(tangent, n));
  12. vec3 bitangent = cross(n, tangent);
  13. mat3 tbn = mat3(tangent, bitangent, n);
  14. vec3 color = vec3(0.);
  15. vec3 h;
  16. vec3 l;
  17. float NoH;
  18. float NoL;
  19. float totalWeight = 0.;
  20. for (int i = 0; i < NUM_SAMPLES; i++) {
  21. h = tbn * sampleDirections[i];
  22. l = 2. * dot(h, n) * h - n;
  23. NoH = clamp(dot(h, n), 0.0, 1.0);
  24. NoL = clamp(dot(l, n), 0.0, 1.0);
  25. if (NoL > 0.) {
  26. float solidAngleTexel = 4. * 3.14159 / (6. * cubeWidth * cubeWidth);
  27. float solidAngleSample = 1.5 * 4.0 / (float(NUM_SAMPLES) * weights[i]); // Multiplying by 1.5 gives better results
  28. float lod = 0.5 * log2(solidAngleSample / solidAngleTexel);
  29. // gamma correction needed ?
  30. color += textureCubeLodEXT(inputTexture, l, lod).xyz * NoL;
  31. totalWeight += NoL;
  32. }
  33. }
  34. if (totalWeight != 0.) {
  35. color /= totalWeight;
  36. }
  37. gl_FragColor = vec4(color, 1.0);
  38. // gl_FragColor = vec4(textureCube(inputTexture, normalize(direction)).xyz, 1.0);
  39. }