diff --git a/src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js index fc835e71d78c70..bc6e82f1a09b93 100644 --- a/src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js +++ b/src/renderers/shaders/ShaderChunk/tonemapping_pars_fragment.glsl.js @@ -73,6 +73,20 @@ vec3 ACESFilmicToneMapping( vec3 color ) { } +// Matrices for rec 2020 <> rec 709 color space conversion +// matrix provided in row-major order so it has been transposed +// https://www.itu.int/pub/R-REP-BT.2407-2017 +const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3( + vec3( 1.6605, - 0.1246, - 0.0182 ), + vec3( - 0.5876, 1.1329, - 0.1006 ), + vec3( - 0.0728, - 0.0083, 1.1187 ) +); + +const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = mat3( + vec3( 0.6274, 0.0691, 0.0164 ), + vec3( 0.3293, 0.9195, 0.0880 ), + vec3( 0.0433, 0.0113, 0.8956 ) +); // https://iolite-engine.com/blog_posts/minimal_agx_implementation // @@ -95,36 +109,26 @@ vec3 agxDefaultContrastApprox( vec3 x ) { // Input and output encoded as Linear-sRGB. vec3 AgXToneMapping( vec3 color ) { - // Matrices for rec 2020 <> rec 709 color space conversion - // matrix provided in row-major order so it has been transposed - // https://www.itu.int/pub/R-REP-BT.2407-2017 - const mat3 LINEAR_REC2020_TO_LINEAR_SRGB = mat3( - vec3( 1.6605, - 0.1246, - 0.0182 ), - vec3( - 0.5876, 1.1329, - 0.1006 ), - vec3( - 0.0728, - 0.0083, 1.1187 ) - ); - - // TODO: add implementation - const mat3 LINEAR_SRGB_TO_LINEAR_REC2020 = inverse( LINEAR_REC2020_TO_LINEAR_SRGB ); - - // AGX Tone Mapping implementation based on Filament, which is in turn based - // on Blender's implementation for rec 2020 colors: - // https://github.com/google/filament/pull/7236 + // AgX constants const mat3 AgXInsetMatrix = mat3( vec3( 0.856627153315983, 0.137318972929847, 0.11189821299995 ), vec3( 0.0951212405381588, 0.761241990602591, 0.0767994186031903 ), vec3( 0.0482516061458583, 0.101439036467562, 0.811302368396859 ) ); - const mat3 AgXOutsetMatrixInv = mat3( - vec3( 0.899796955911611, 0.11142098895748, 0.11142098895748 ), - vec3( 0.0871996192028351, 0.875575586156966, 0.0871996192028349 ), - vec3( 0.013003424885555, 0.0130034248855548, 0.801379391839686 ) + + // explicit AgXOutsetMatrix generated from Filaments AgXOutsetMatixInv + const mat3 AgXOutsetMatrix = mat3( + vec3( 1.1271005818144368, - 0.1413297634984383, - 0.14132976349843826 ), + vec3( - 0.11060664309660323, 1.157823702216272, - 0.11060664309660294 ), + vec3( - 0.016493938717834573, - 0.016493938717834257, 1.2519364065950405 ) ); - const mat3 AgXOutsetMatrix = inverse( AgXOutsetMatrixInv ); const float AgxMinEv = - 12.47393; // log2(pow(2, LOG2_MIN) * MIDDLE_GRAY) const float AgxMaxEv = 4.026069; // log2(pow(2, LOG2_MAX) * MIDDLE_GRAY) + // AGX Tone Mapping implementation based on Filament, which is in turn based + // on Blender's implementation for rec 2020 colors: + // https://github.com/google/filament/pull/7236 color = LINEAR_SRGB_TO_LINEAR_REC2020 * color; color = max( vec3( 0.0 ), color );