ToneMapShader.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: [
  14. 'varying vec2 vUv;',
  15. 'void main() {',
  16. ' vUv = uv;',
  17. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  18. '}'
  19. ].join( '\n' ),
  20. fragmentShader: [
  21. '#include <common>',
  22. 'uniform sampler2D tDiffuse;',
  23. 'varying vec2 vUv;',
  24. 'uniform float middleGrey;',
  25. 'uniform float minLuminance;',
  26. 'uniform float maxLuminance;',
  27. '#ifdef ADAPTED_LUMINANCE',
  28. ' uniform sampler2D luminanceMap;',
  29. '#else',
  30. ' uniform float averageLuminance;',
  31. '#endif',
  32. 'vec3 ToneMap( vec3 vColor ) {',
  33. ' #ifdef ADAPTED_LUMINANCE',
  34. // Get the calculated average luminance
  35. ' float fLumAvg = texture2D(luminanceMap, vec2(0.5, 0.5)).r;',
  36. ' #else',
  37. ' float fLumAvg = averageLuminance;',
  38. ' #endif',
  39. // Calculate the luminance of the current pixel
  40. ' float fLumPixel = linearToRelativeLuminance( vColor );',
  41. // Apply the modified operator (Eq. 4)
  42. ' float fLumScaled = (fLumPixel * middleGrey) / max( minLuminance, fLumAvg );',
  43. ' float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (maxLuminance * maxLuminance)))) / (1.0 + fLumScaled);',
  44. ' return fLumCompressed * vColor;',
  45. '}',
  46. 'void main() {',
  47. ' vec4 texel = texture2D( tDiffuse, vUv );',
  48. ' gl_FragColor = vec4( ToneMap( texel.xyz ), texel.w );',
  49. '}'
  50. ].join( '\n' )
  51. };
  52. export { ToneMapShader };