@@ -25,6 +25,38 @@ const float BS = 10.0;
25
25
const float fogStart = FOG_START;
26
26
const float fogShadingParameter = 1 / ( 1 - fogStart);
27
27
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
+
28
60
void get_texture_flags()
29
61
{
30
62
vec4 flags = texture2D (textureFlags, vec2 (0.0 , 0.0 ));
@@ -114,6 +146,11 @@ void main(void)
114
146
vec4 col = vec4 (color.rgb, base.a);
115
147
116
148
col.rgb *= emissiveColor.rgb * vIDiff;
149
+
150
+ #ifdef ENABLE_TONE_MAPPING
151
+ col = applyToneMapping(col);
152
+ #endif
153
+
117
154
// Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
118
155
// the fog will only be rendered correctly if the last operation before the
119
156
// clamp() is an addition. Else, the clamp() seems to be ignored.
0 commit comments