ToneMapShader.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var 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:
  27. /* glsl */
  28. `
  29. varying vec2 vUv;
  30. void main() {
  31. vUv = uv;
  32. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  33. }`,
  34. fragmentShader:
  35. /* glsl */
  36. `
  37. #include <common>
  38. uniform sampler2D tDiffuse;
  39. varying vec2 vUv;
  40. uniform float middleGrey;
  41. uniform float minLuminance;
  42. uniform float maxLuminance;
  43. #ifdef ADAPTED_LUMINANCE
  44. uniform sampler2D luminanceMap;
  45. #else
  46. uniform float averageLuminance;
  47. #endif
  48. vec3 ToneMap( vec3 vColor ) {
  49. #ifdef ADAPTED_LUMINANCE
  50. // Get the calculated average luminance
  51. float fLumAvg = texture2D(luminanceMap, vec2(0.5, 0.5)).r;
  52. #else
  53. float fLumAvg = averageLuminance;
  54. #endif
  55. // Calculate the luminance of the current pixel
  56. float fLumPixel = linearToRelativeLuminance( vColor );
  57. // Apply the modified operator (Eq. 4)
  58. float fLumScaled = (fLumPixel * middleGrey) / max( minLuminance, fLumAvg );
  59. float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (maxLuminance * maxLuminance)))) / (1.0 + fLumScaled);
  60. return fLumCompressed * vColor;
  61. }
  62. void main() {
  63. vec4 texel = texture2D( tDiffuse, vUv );
  64. gl_FragColor = vec4( ToneMap( texel.xyz ), texel.w );
  65. }`
  66. };
  67. THREE.ToneMapShader = ToneMapShader;
  68. } )();