ToneMapShader.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @author miibond
  3. *
  4. * Full-screen tone-mapping shader based on http://www.graphics.cornell.edu/~jaf/publications/sig02_paper.pdf
  5. */
  6. THREE.ToneMapShader = {
  7. uniforms: {
  8. "tDiffuse": { value: null },
  9. "averageLuminance": { value: 1.0 },
  10. "luminanceMap": { value: null },
  11. "maxLuminance": { value: 16.0 },
  12. "middleGrey": { value: 0.6 }
  13. },
  14. vertexShader: [
  15. "varying vec2 vUv;",
  16. "void main() {",
  17. "vUv = uv;",
  18. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  19. "}"
  20. ].join( "\n" ),
  21. fragmentShader: [
  22. "uniform sampler2D tDiffuse;",
  23. "varying vec2 vUv;",
  24. "uniform float middleGrey;",
  25. "uniform float maxLuminance;",
  26. "#ifdef ADAPTED_LUMINANCE",
  27. "uniform sampler2D luminanceMap;",
  28. "#else",
  29. "uniform float averageLuminance;",
  30. "#endif",
  31. "const vec3 LUM_CONVERT = vec3(0.299, 0.587, 0.114);",
  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 = dot(vColor, LUM_CONVERT);",
  41. // Apply the modified operator (Eq. 4)
  42. "float fLumScaled = (fLumPixel * middleGrey) / 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. };