ToneMapShader.js 1.6 KB

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