ToneMapShader.js 1.7 KB

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