ToneMapShader.js 1.6 KB

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