ACESFilmicToneMappingShader.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /** @module ACESFilmicToneMappingShader */
  2. /**
  3. * ACES Filmic Tone Mapping Shader by Stephen Hill.
  4. * Reference: [ltc_blit.fs]{@link 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. * @constant
  10. * @type {Object}
  11. */
  12. const ACESFilmicToneMappingShader = {
  13. name: 'ACESFilmicToneMappingShader',
  14. uniforms: {
  15. 'tDiffuse': { value: null },
  16. 'exposure': { value: 1.0 }
  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. export { ACESFilmicToneMappingShader };
粤ICP备19079148号