Skip to content

Commit eb3840a

Browse files
RealBadAngelkwolekr
authored andcommittedFeb 9, 2016
Filmic HDR tone mapping
1 parent 180893e commit eb3840a

File tree

6 files changed

+101
-14
lines changed

6 files changed

+101
-14
lines changed
 

‎builtin/settingtypes.txt

+5
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ fsaa (FSAA) enum 0 0,1,2,4,8,16
326326
# Thy only work with the OpenGL video backend.
327327
enable_shaders (Shaders) bool true
328328

329+
[****Tone Mapping]
330+
331+
# Enables filmic tone mapping
332+
tone_mapping (Filmic tone mapping) bool false
333+
329334
[****Bumpmapping]
330335

331336
# Enables bumpmapping for textures. Normalmaps need to be supplied by the texture pack

‎client/shaders/nodes_shader/opengl_fragment.glsl

+42-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,38 @@ bool normalTexturePresent = false;
2020
const float e = 2.718281828459;
2121
const float BS = 10.0;
2222

23+
#ifdef ENABLE_TONE_MAPPING
24+
25+
/* Hable's UC2 Tone mapping parameters
26+
A = 0.22;
27+
B = 0.30;
28+
C = 0.10;
29+
D = 0.20;
30+
E = 0.01;
31+
F = 0.30;
32+
W = 11.2;
33+
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
34+
*/
35+
36+
vec3 uncharted2Tonemap(vec3 x)
37+
{
38+
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03334;
39+
}
40+
41+
vec4 applyToneMapping(vec4 color)
42+
{
43+
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
44+
const float gamma = 1.6;
45+
const float exposureBias = 5.5;
46+
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
47+
// Precalculated white_scale from
48+
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
49+
vec3 whiteScale = vec3(1.036015346);
50+
color.rgb *= whiteScale;
51+
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
52+
}
53+
#endif
54+
2355
void get_texture_flags()
2456
{
2557
vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
@@ -160,22 +192,26 @@ void main(void)
160192
color = base.rgb;
161193
#endif
162194

195+
vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0);
196+
163197
#if MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE
164198
float alpha = gl_Color.a;
165-
vec4 col = vec4(color.rgb, alpha);
166-
col *= gl_Color;
167199
if (fogDistance != 0.0) {
168200
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
169201
alpha = mix(alpha, 0.0, d);
170202
}
171-
gl_FragColor = vec4(col.rgb, alpha);
203+
col = vec4(col.rgb, alpha);
172204
#else
173-
vec4 col = vec4(color.rgb, base.a);
174-
col *= gl_Color;
175205
if (fogDistance != 0.0) {
176206
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
177207
col = mix(col, skyBgColor, d);
178208
}
179-
gl_FragColor = vec4(col.rgb, base.a);
209+
col = vec4(col.rgb, base.a);
210+
#endif
211+
212+
#ifdef ENABLE_TONE_MAPPING
213+
gl_FragColor = applyToneMapping(col);
214+
#else
215+
gl_FragColor = col;
180216
#endif
181217
}

‎client/shaders/water_surface_shader/opengl_fragment.glsl

+44-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ bool texSeamless = false;
2222
const float e = 2.718281828459;
2323
const float BS = 10.0;
2424

25+
#ifdef ENABLE_TONE_MAPPING
26+
27+
/* Hable's UC2 Tone mapping parameters
28+
A = 0.22;
29+
B = 0.30;
30+
C = 0.10;
31+
D = 0.20;
32+
E = 0.01;
33+
F = 0.30;
34+
W = 11.2;
35+
equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
36+
*/
37+
38+
vec3 uncharted2Tonemap(vec3 x)
39+
{
40+
return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03334;
41+
}
42+
43+
vec4 applyToneMapping(vec4 color)
44+
{
45+
color = vec4(pow(color.rgb, vec3(2.2)), color.a);
46+
const float gamma = 1.6;
47+
const float exposureBias = 5.5;
48+
color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
49+
// Precalculated white_scale from
50+
//vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
51+
vec3 whiteScale = vec3(1.036015346);
52+
color.rgb *= whiteScale;
53+
return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
54+
}
55+
#endif
56+
2557
void get_texture_flags()
2658
{
2759
vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
@@ -116,22 +148,26 @@ vec4 base = texture2D(baseTexture, uv).rgba;
116148
color = base.rgb;
117149
#endif
118150

151+
vec4 col = vec4(color.rgb * gl_Color.rgb, 1.0);
152+
119153
#if MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE
120154
float alpha = gl_Color.a;
121-
vec4 col = vec4(color.rgb, alpha);
122-
col *= gl_Color;
123-
if(fogDistance != 0.0){
155+
if (fogDistance != 0.0) {
124156
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
125157
alpha = mix(alpha, 0.0, d);
126158
}
127-
gl_FragColor = vec4(col.rgb, alpha);
159+
col = vec4(col.rgb, alpha);
128160
#else
129-
vec4 col = vec4(color.rgb, base.a);
130-
col *= gl_Color;
131-
if(fogDistance != 0.0){
161+
if (fogDistance != 0.0) {
132162
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
133163
col = mix(col, skyBgColor, d);
134164
}
135-
gl_FragColor = vec4(col.rgb, base.a);
165+
col = vec4(col.rgb, base.a);
166+
#endif
167+
168+
#ifdef ENABLE_TONE_MAPPING
169+
gl_FragColor = applyToneMapping(col);
170+
#else
171+
gl_FragColor = col;
136172
#endif
137173
}

‎minetest.conf.example

+6
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@
353353
# type: bool
354354
# enable_shaders = true
355355

356+
##### Tone mapping
357+
# Enables filmic tone mapping.
358+
# Requires shaders to be enabled.
359+
# type: bool
360+
# tone_mapping = false
361+
356362
##### Bumpmapping
357363

358364
# Enables bumpmapping for textures. Normalmaps need to be supplied by the texture pack

‎src/defaultsettings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ void set_default_settings(Settings *settings)
160160
settings->setDefault("texture_clean_transparent", "false");
161161
settings->setDefault("texture_min_size", "64");
162162
settings->setDefault("preload_item_visuals", "false");
163+
settings->setDefault("tone_mapping", "false");
163164
settings->setDefault("enable_bumpmapping", "false");
164165
settings->setDefault("enable_parallax_occlusion", "false");
165166
settings->setDefault("generate_normalmaps", "false");

‎src/shader.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,9 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
764764
else
765765
shaders_header += "0\n";
766766

767+
if (g_settings->getBool("tone_mapping"))
768+
shaders_header += "#define ENABLE_TONE_MAPPING\n";
769+
767770
if(pixel_program != "")
768771
pixel_program = shaders_header + pixel_program;
769772
if(vertex_program != "")

0 commit comments

Comments
 (0)
Please sign in to comment.