Skip to content

Commit

Permalink
Fix tonemapping and apply saturation even if tonemapping is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rollerozxa committed Dec 16, 2023
1 parent 16c2247 commit c9ab02a
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions client/shaders/second_stage/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,26 @@ vec3 uncharted2Tonemap(vec3 x)

vec4 applyToneMapping(vec4 color)
{
const float exposureBias = 2.0;
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
const float gamma = 1.6;
const float exposureBias = 5.5;
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
// Precalculated white_scale from
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
vec3 whiteScale = vec3(1.036015346);
color.rgb *= whiteScale;
return color;
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
}

#endif

vec3 applySaturation(vec3 color, float factor)
{
// Calculate the perceived luminosity from the RGB color.
// See also: https://www.w3.org/WAI/GL/wiki/Relative_luminance
float brightness = dot(color, vec3(0.2125, 0.7154, 0.0721));
return mix(vec3(brightness), color, factor);
}
#endif

void main(void)
{
Expand All @@ -92,6 +95,17 @@ void main(void)
vec4 color = texture2D(rendered, uv).rgba;
#endif

#ifdef ENABLE_BLOOM_DEBUG
if (uv.x > 0.5 || uv.y > 0.5)
#endif
{
#if ENABLE_TONE_MAPPING
color = applyToneMapping(color);
#endif

color.rgb = applySaturation(color.rgb, saturation);
}

// translate to linear colorspace (approximate)
color.rgb = pow(color.rgb, vec3(2.2));

Expand All @@ -110,15 +124,6 @@ void main(void)
color = applyBloom(color, uv);
#endif

#ifdef ENABLE_BLOOM_DEBUG
if (uv.x > 0.5 || uv.y > 0.5)
#endif
{
#if ENABLE_TONE_MAPPING
color = applyToneMapping(color);
color.rgb = applySaturation(color.rgb, saturation);
#endif
}

color.rgb = clamp(color.rgb, vec3(0.), vec3(1.));

Expand Down

0 comments on commit c9ab02a

Please sign in to comment.