ACESFilmicToneMappingShader.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. name: 'ACESFilmicToneMappingShader',
  10. uniforms: {
  11. 'tDiffuse': { value: null },
  12. 'exposure': { value: 1.0 }
  13. },
  14. vertexShader: /* glsl */`
  15. varying vec2 vUv;
  16. void main() {
  17. vUv = uv;
  18. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  19. }`,
  20. fragmentShader: /* glsl */`
  21. #define saturate(a) clamp( a, 0.0, 1.0 )
  22. uniform sampler2D tDiffuse;
  23. uniform float exposure;
  24. varying vec2 vUv;
  25. vec3 RRTAndODTFit( vec3 v ) {
  26. vec3 a = v * ( v + 0.0245786 ) - 0.000090537;
  27. vec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;
  28. return a / b;
  29. }
  30. vec3 ACESFilmicToneMapping( vec3 color ) {
  31. // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
  32. const mat3 ACESInputMat = mat3(
  33. vec3( 0.59719, 0.07600, 0.02840 ), // transposed from source
  34. vec3( 0.35458, 0.90834, 0.13383 ),
  35. vec3( 0.04823, 0.01566, 0.83777 )
  36. );
  37. // ODT_SAT => XYZ => D60_2_D65 => sRGB
  38. const mat3 ACESOutputMat = mat3(
  39. vec3( 1.60475, -0.10208, -0.00327 ), // transposed from source
  40. vec3( -0.53108, 1.10813, -0.07276 ),
  41. vec3( -0.07367, -0.00605, 1.07602 )
  42. );
  43. color = ACESInputMat * color;
  44. // Apply RRT and ODT
  45. color = RRTAndODTFit( color );
  46. color = ACESOutputMat * color;
  47. // Clamp to [0, 1]
  48. return saturate( color );
  49. }
  50. void main() {
  51. vec4 tex = texture2D( tDiffuse, vUv );
  52. tex.rgb *= exposure / 0.6; // pre-exposed, outside of the tone mapping function
  53. gl_FragColor = vec4( ACESFilmicToneMapping( tex.rgb ), tex.a );
  54. }`
  55. };
  56. export { ACESFilmicToneMappingShader };