ACESFilmicToneMappingShader.js 1.9 KB

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