Skip to content

Commit 661b4a1

Browse files
authoredApr 6, 2020
Add tone mapping for entities (#9521)
fixes #9301
1 parent f91124a commit 661b4a1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎client/shaders/object_shader/opengl_fragment.glsl

+37
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,38 @@ const float BS = 10.0;
2525
const float fogStart = FOG_START;
2626
const float fogShadingParameter = 1 / ( 1 - fogStart);
2727

28+
#ifdef ENABLE_TONE_MAPPING
29+
30+
/* Hable's UC2 Tone mapping parameters
31+
A = 0.22;
32+
B = 0.30;
33+
C = 0.10;
34+
D = 0.20;
35+
E = 0.01;
36+
F = 0.30;
37+
W = 11.2;
38+
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
39+
*/
40+
41+
vec3 uncharted2Tonemap(vec3 x)
42+
{
43+
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
44+
}
45+
46+
vec4 applyToneMapping(vec4 color)
47+
{
48+
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
49+
const float gamma = 1.6;
50+
const float exposureBias = 5.5;
51+
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
52+
// Precalculated white_scale from
53+
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
54+
vec3 whiteScale = vec3(1.036015346);
55+
color.rgb *= whiteScale;
56+
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
57+
}
58+
#endif
59+
2860
void get_texture_flags()
2961
{
3062
vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
@@ -114,6 +146,11 @@ void main(void)
114146
vec4 col = vec4(color.rgb, base.a);
115147

116148
col.rgb *= emissiveColor.rgb * vIDiff;
149+
150+
#ifdef ENABLE_TONE_MAPPING
151+
col = applyToneMapping(col);
152+
#endif
153+
117154
// Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
118155
// the fog will only be rendered correctly if the last operation before the
119156
// clamp() is an addition. Else, the clamp() seems to be ignored.

0 commit comments

Comments
 (0)
Please sign in to comment.