ToneMapShader.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Full-screen tone-mapping shader based on http://www.cis.rit.edu/people/faculty/ferwerda/publications/sig02_paper.pdf
  3. */
  4. var ToneMapShader = {
  5. uniforms: {
  6. 'tDiffuse': { value: null },
  7. 'averageLuminance': { value: 1.0 },
  8. 'luminanceMap': { value: null },
  9. 'maxLuminance': { value: 16.0 },
  10. 'minLuminance': { value: 0.01 },
  11. 'middleGrey': { value: 0.6 }
  12. },
  13. vertexShader: /* glsl */`
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  18. }`,
  19. fragmentShader: /* glsl */`
  20. #include <common>
  21. uniform sampler2D tDiffuse;
  22. varying vec2 vUv;
  23. uniform float middleGrey;
  24. uniform float minLuminance;
  25. uniform float maxLuminance;
  26. #ifdef ADAPTED_LUMINANCE
  27. uniform sampler2D luminanceMap;
  28. #else
  29. uniform float averageLuminance;
  30. #endif
  31. vec3 ToneMap( vec3 vColor ) {
  32. #ifdef ADAPTED_LUMINANCE
  33. // Get the calculated average luminance
  34. float fLumAvg = texture2D(luminanceMap, vec2(0.5, 0.5)).r;
  35. #else
  36. float fLumAvg = averageLuminance;
  37. #endif
  38. // Calculate the luminance of the current pixel
  39. float fLumPixel = linearToRelativeLuminance( vColor );
  40. // Apply the modified operator (Eq. 4)
  41. float fLumScaled = (fLumPixel * middleGrey) / max( minLuminance, fLumAvg );
  42. float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (maxLuminance * maxLuminance)))) / (1.0 + fLumScaled);
  43. return fLumCompressed * vColor;
  44. }
  45. void main() {
  46. vec4 texel = texture2D( tDiffuse, vUv );
  47. gl_FragColor = vec4( ToneMap( texel.xyz ), texel.w );
  48. }`
  49. };
  50. export { ToneMapShader };