ToneMapShader.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/#manual/en/introduction/Installation." );
  2. /**
  3. * Full-screen tone-mapping shader based on http://www.cis.rit.edu/people/faculty/ferwerda/publications/sig02_paper.pdf
  4. */
  5. THREE.ToneMapShader = {
  6. uniforms: {
  7. "tDiffuse": { value: null },
  8. "averageLuminance": { value: 1.0 },
  9. "luminanceMap": { value: null },
  10. "maxLuminance": { value: 16.0 },
  11. "minLuminance": { value: 0.01 },
  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. "#include <common>",
  23. "uniform sampler2D tDiffuse;",
  24. "varying vec2 vUv;",
  25. "uniform float middleGrey;",
  26. "uniform float minLuminance;",
  27. "uniform float maxLuminance;",
  28. "#ifdef ADAPTED_LUMINANCE",
  29. " uniform sampler2D luminanceMap;",
  30. "#else",
  31. " uniform float averageLuminance;",
  32. "#endif",
  33. "vec3 ToneMap( vec3 vColor ) {",
  34. " #ifdef ADAPTED_LUMINANCE",
  35. // Get the calculated average luminance
  36. " float fLumAvg = texture2D(luminanceMap, vec2(0.5, 0.5)).r;",
  37. " #else",
  38. " float fLumAvg = averageLuminance;",
  39. " #endif",
  40. // Calculate the luminance of the current pixel
  41. " float fLumPixel = linearToRelativeLuminance( vColor );",
  42. // Apply the modified operator (Eq. 4)
  43. " float fLumScaled = (fLumPixel * middleGrey) / max( minLuminance, fLumAvg );",
  44. " float fLumCompressed = (fLumScaled * (1.0 + (fLumScaled / (maxLuminance * maxLuminance)))) / (1.0 + fLumScaled);",
  45. " return fLumCompressed * vColor;",
  46. "}",
  47. "void main() {",
  48. " vec4 texel = texture2D( tDiffuse, vUv );",
  49. " gl_FragColor = vec4( ToneMap( texel.xyz ), texel.w );",
  50. "}"
  51. ].join( "\n" )
  52. };