ACESFilmicToneMappingShader.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * ACES Filmic Tone Mapping Shader by Stephen Hill
  3. * source: https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs
  4. *
  5. * this implementation of ACES is modified to accommodate a brighter viewing environment.
  6. * the scale factor of 1/0.6 is subjective. see discussion in #19621.
  7. */
  8. const ACESFilmicToneMappingShader = {
  9. uniforms: {
  10. 'tDiffuse': { value: null },
  11. 'exposure': { value: 1.0 }
  12. },
  13. vertexShader: /* glsl */`
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  18. }`,
  19. fragmentShader: /* glsl */`
  20. #define saturate(a) clamp( a, 0.0, 1.0 )
  21. uniform sampler2D tDiffuse;
  22. uniform float exposure;
  23. varying vec2 vUv;
  24. vec3 RRTAndODTFit( vec3 v ) {
  25. vec3 a = v * ( v + 0.0245786 ) - 0.000090537;
  26. vec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;
  27. return a / b;
  28. }
  29. vec3 ACESFilmicToneMapping( vec3 color ) {
  30. // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
  31. const mat3 ACESInputMat = mat3(
  32. vec3( 0.59719, 0.07600, 0.02840 ), // transposed from source
  33. vec3( 0.35458, 0.90834, 0.13383 ),
  34. vec3( 0.04823, 0.01566, 0.83777 )
  35. );
  36. // ODT_SAT => XYZ => D60_2_D65 => sRGB
  37. const mat3 ACESOutputMat = mat3(
  38. vec3( 1.60475, -0.10208, -0.00327 ), // transposed from source
  39. vec3( -0.53108, 1.10813, -0.07276 ),
  40. vec3( -0.07367, -0.00605, 1.07602 )
  41. );
  42. color = ACESInputMat * color;
  43. // Apply RRT and ODT
  44. color = RRTAndODTFit( color );
  45. color = ACESOutputMat * color;
  46. // Clamp to [0, 1]
  47. return saturate( color );
  48. }
  49. void main() {
  50. vec4 tex = texture2D( tDiffuse, vUv );
  51. tex.rgb *= exposure / 0.6; // pre-exposed, outside of the tone mapping function
  52. gl_FragColor = vec4( ACESFilmicToneMapping( tex.rgb ), tex.a );
  53. }`
  54. };
  55. export { ACESFilmicToneMappingShader };