ACESFilmicToneMappingShader.js 1.9 KB

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