ToneMapShader.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. console.warn( "THREE.ToneMapShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author miibond
  4. *
  5. * Full-screen tone-mapping shader based on http://www.cis.rit.edu/people/faculty/ferwerda/publications/sig02_paper.pdf
  6. */
  7. THREE.ToneMapShader = {
  8. uniforms: {
  9. "tDiffuse": { value: null },
  10. "averageLuminance": { value: 1.0 },
  11. "luminanceMap": { value: null },
  12. "maxLuminance": { value: 16.0 },
  13. "minLuminance": { value: 0.01 },
  14. "middleGrey": { value: 0.6 }
  15. },
  16. vertexShader: [
  17. "varying vec2 vUv;",
  18. "void main() {",
  19. " vUv = uv;",
  20. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  21. "}"
  22. ].join( "\n" ),
  23. fragmentShader: [
  24. "#include <common>",
  25. "uniform sampler2D tDiffuse;",
  26. "varying vec2 vUv;",
  27. "uniform float middleGrey;",
  28. "uniform float minLuminance;",
  29. "uniform float maxLuminance;",
  30. "#ifdef ADAPTED_LUMINANCE",
  31. " uniform sampler2D luminanceMap;",
  32. "#else",
  33. " uniform float averageLuminance;",
  34. "#endif",
  35. "vec3 ToneMap( vec3 vColor ) {",
  36. " #ifdef ADAPTED_LUMINANCE",
  37. // Get the calculated average luminance
  38. " float fLumAvg = texture2D(luminanceMap, vec2(0.5, 0.5)).r;",
  39. " #else",
  40. " float fLumAvg = averageLuminance;",
  41. " #endif",
  42. // Calculate the luminance of the current pixel
  43. " float fLumPixel = linearToRelativeLuminance( vColor );",
  44. // Apply the modified operator (Eq. 4)
  45. " float fLumScaled = (fLumPixel * middleGrey) / max( minLuminance, fLumAvg );",
  46. " float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (maxLuminance * maxLuminance)))) / (1.0 + fLumScaled);",
  47. " return fLumCompressed * vColor;",
  48. "}",
  49. "void main() {",
  50. " vec4 texel = texture2D( tDiffuse, vUv );",
  51. " gl_FragColor = vec4( ToneMap( texel.xyz ), texel.w );",
  52. "}"
  53. ].join( "\n" )
  54. };