tonemap.fragment.fx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Samplers
  2. varying vec2 vUV;
  3. uniform sampler2D textureSampler;
  4. // Constants
  5. uniform float _ExposureAdjustment;
  6. #if defined(HABLE_TONEMAPPING)
  7. const float A = 0.15;
  8. const float B = 0.50;
  9. const float C = 0.10;
  10. const float D = 0.20;
  11. const float E = 0.02;
  12. const float F = 0.30;
  13. const float W = 11.2;
  14. #endif
  15. float Luminance(vec3 c)
  16. {
  17. return dot(c, vec3(0.22, 0.707, 0.071));
  18. }
  19. void main(void)
  20. {
  21. vec3 colour = texture2D(textureSampler, vUV).rgb;
  22. #if defined(REINHARD_TONEMAPPING)
  23. float lum = Luminance(colour.rgb);
  24. float lumTm = lum * _ExposureAdjustment;
  25. float scale = lumTm / (1.0 + lumTm);
  26. colour *= scale / lum;
  27. #elif defined(HABLE_TONEMAPPING)
  28. colour *= _ExposureAdjustment;
  29. const float ExposureBias = 2.0;
  30. vec3 x = ExposureBias * colour;
  31. vec3 curr = ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
  32. x = vec3(W, W, W);
  33. vec3 whiteScale = 1.0 / (((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F);
  34. colour = curr * whiteScale;
  35. #elif defined(OPTIMIZED_HEJIDAWSON_TONEMAPPING)
  36. colour *= _ExposureAdjustment;
  37. vec3 X = max(vec3(0.0, 0.0, 0.0), colour - 0.004);
  38. vec3 retColor = (X * (6.2 * X + 0.5)) / (X * (6.2 * X + 1.7) + 0.06);
  39. colour = retColor * retColor;
  40. #elif defined(PHOTOGRAPHIC_TONEMAPPING)
  41. colour = vec3(1.0, 1.0, 1.0) - exp2(-_ExposureAdjustment * colour);
  42. #endif
  43. gl_FragColor = vec4(colour.rgb, 1.0);
  44. }