From ca4f04ecab64b78cc8861a05a0212ef897ec0afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gef=C3=BCllte=20Taubenbrust?= <72752000+GefullteTaubenbrust2@users.noreply.github.com> Date: Sun, 12 May 2024 14:44:13 +0200 Subject: [PATCH] Lots of changes some redundant stuff in vertex shaders needs fixing --- builtin/settingtypes.txt | 24 +++++++ .../shaders/nodes_shader/opengl_fragment.glsl | 68 +++++++++++++++++-- .../shaders/nodes_shader/opengl_vertex.glsl | 12 ++-- .../shaders/object_shader/opengl_vertex.glsl | 12 ++-- .../shaders/second_stage/opengl_fragment.glsl | 18 ++++- src/client/game.cpp | 12 +++- src/client/shader.cpp | 9 +++ src/defaultsettings.cpp | 4 ++ src/lighting.h | 2 +- 9 files changed, 138 insertions(+), 23 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 11dfbf128..b02d5f2ef 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -611,6 +611,23 @@ enable_auto_exposure (Enable Automatic Exposure) bool false # Requires: shaders, enable_post_processing, enable_auto_exposure exposure_compensation (Exposure compensation) float 0.0 -1.0 1.0 +# Set the gamma value. +# Higher values give lower contrast and vice versa. +# Range: from 1.0 to 5.0 +# +# Requires: shaders, enable_post_processing, tone_mapping +gamma (Gamma) float 1.6 1.0 5.0 + +# Apply color grading to make brighter colors warmer and darker colors cooler. +# +# Requires: shaders, enable_post_processing +enable_color_grading (Color grading) bool false + +# Apply vignette effect to darken the edges of the screen. +# +# Requires: shaders, enable_post_processing +enable_vignette (Vignette) bool false + # Apply dithering to reduce color banding artifacts. # Dithering significantly increases the size of losslessly-compressed # screenshots and it works incorrectly if the display or operating system @@ -663,6 +680,13 @@ bloom_radius (Bloom Radius) float 1 0.1 8 # Requires: shaders, enable_post_processing, enable_bloom enable_volumetric_lighting (Volumetric lighting) bool false +[**Other Effects] + +# Apply bump maps to nodes based on their textures. It is recommended to use a tilted sun orbit to go with this (Sky Body Orbit Tilt). +# +# Requires: shaders, enable_dynamic_shadows +enable_bumpmaps (Bump maps) bool false + [*Audio] # Volume of all sounds. diff --git a/client/shaders/nodes_shader/opengl_fragment.glsl b/client/shaders/nodes_shader/opengl_fragment.glsl index 46977b147..cc3af027c 100644 --- a/client/shaders/nodes_shader/opengl_fragment.glsl +++ b/client/shaders/nodes_shader/opengl_fragment.glsl @@ -1,4 +1,12 @@ +#if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT) +#define MATERIAL_WAVING_LIQUID 1 +#define MATERIAL_LIQUID 1 +#elif (MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) +#define MATERIAL_LIQUID 1 +#endif + uniform sampler2D baseTexture; +uniform vec2 texelSize0; uniform vec3 dayLight; uniform lowp vec4 fogColor; @@ -48,6 +56,36 @@ varying float nightRatio; #ifdef ENABLE_DYNAMIC_SHADOWS +#ifdef ENABLE_BUMPMAPS +vec4 perm(vec4 x) +{ + return mod(((x * 34.0) + 1.0) * x, 289.0); +} + +float snoise(vec3 p) +{ + vec3 a = floor(p); + vec3 d = p - a; + d = d * d * (3.0 - 2.0 * d); + + vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); + vec4 k1 = perm(b.xyxy); + vec4 k2 = perm(k1.xyxy + b.zzww); + + vec4 c = k2 + a.zzzz; + vec4 k3 = perm(c); + vec4 k4 = perm(c + 1.0); + + vec4 o1 = fract(k3 * (1.0 / 41.0)); + vec4 o2 = fract(k4 * (1.0 / 41.0)); + + vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); + vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); + + return o4.y * d.y + o4.x * (1.0 - d.y); +} +#endif + // assuming near is always 1.0 float getLinearDepth() { @@ -378,6 +416,26 @@ void main(void) vec4 col = vec4(color.rgb * varColor.rgb, 1.0); #ifdef ENABLE_DYNAMIC_SHADOWS + // Fragment normal, can differ from vNormal which is derived from vertex normals. + vec3 fNormal = vNormal; + +#if (defined(ENABLE_BUMPMAPS) && !defined(MATERIAL_LIQUID)) + vec2 dr = vec2(0.25) * texelSize0; + // Sample the texture to then compute the derivative + float fx0y0 = texture2D(baseTexture, uv).r; + float fx1y0 = texture2D(baseTexture, uv + vec2(dr.x, 0.0)).r; + float fx0y1 = texture2D(baseTexture, uv + vec2(0.0, dr.y)).r; + // Compute a set of orthogonal basis vectors representing the node's surface plane. + vec3 orth1 = normalize(cross(vNormal, mix(vec3(0.0, -1.0, 0.0), vec3(0.0, 0.0, -1.0), step(0.9, abs(vNormal.y))))); + vec3 orth2 = normalize(cross(vNormal, orth1)); + // The normal is computed using the partial derivatives along the texture space x and y axes. + // These axes in world space are assumed to be parallel to the basis vectors we defined before. + fNormal = normalize(vNormal + (orth1 * (fx1y0 - fx0y0) / dr.x + orth2 * (fx0y1 - fx0y0) / dr.y) * 0.25 * snoise(vec3(uv / texelSize0, 0.0))); + float adj_cosLight = max(1e-5, dot(fNormal, -v_LightDirection)); +#else + float adj_cosLight = cosLight; +#endif + if (f_shadow_strength > 0.0) { float shadow_int = 0.0; vec3 shadow_color = vec3(0.0, 0.0, 0.0); @@ -392,14 +450,14 @@ void main(void) #ifdef COLORED_SHADOWS vec4 visibility; - if (cosLight > 0.0 || f_normal_length < 1e-3) + if (adj_cosLight > 0.0 || f_normal_length < 1e-3) visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z); else visibility = vec4(1.0, 0.0, 0.0, 0.0); shadow_int = visibility.r; shadow_color = visibility.gba; #else - if (cosLight > 0.0 || f_normal_length < 1e-3) + if (adj_cosLight > 0.0 || f_normal_length < 1e-3) shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z); else shadow_int = 1.0; @@ -417,9 +475,9 @@ void main(void) // Apply self-shadowing when light falls at a narrow angle to the surface // Cosine of the cut-off angle. const float self_shadow_cutoff_cosine = 0.035; - if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) { - shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine); - shadow_color = mix(vec3(0.0), shadow_color, min(cosLight, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine); + if (f_normal_length != 0 && adj_cosLight < self_shadow_cutoff_cosine) { + shadow_int = max(shadow_int, 1 - clamp(adj_cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine); + shadow_color = mix(vec3(0.0), shadow_color, min(adj_cosLight, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine); } shadow_int *= f_adj_shadow_strength; diff --git a/client/shaders/nodes_shader/opengl_vertex.glsl b/client/shaders/nodes_shader/opengl_vertex.glsl index 2c1e5981b..415c51c29 100644 --- a/client/shaders/nodes_shader/opengl_vertex.glsl +++ b/client/shaders/nodes_shader/opengl_vertex.glsl @@ -259,16 +259,16 @@ void main(void) shadow_position.z -= z_bias; perspective_factor = pFactor; - if (f_timeofday < 0.2) { + if (f_timeofday < 0.21) { adj_shadow_strength = f_shadow_strength * 0.5 * - (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday)); - } else if (f_timeofday >= 0.8) { + (1.0 - mtsmoothstep(0.18, 0.21, f_timeofday)); + } else if (f_timeofday >= 0.793) { adj_shadow_strength = f_shadow_strength * 0.5 * - mtsmoothstep(0.8, 0.83, f_timeofday); + mtsmoothstep(0.793, 0.823, f_timeofday); } else { adj_shadow_strength = f_shadow_strength * - mtsmoothstep(0.20, 0.25, f_timeofday) * - (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday)); + mtsmoothstep(0.21, 0.26, f_timeofday) * + (1.0 - mtsmoothstep(0.743, 0.793, f_timeofday)); } } #endif diff --git a/client/shaders/object_shader/opengl_vertex.glsl b/client/shaders/object_shader/opengl_vertex.glsl index 53f487a56..1cefb13fa 100644 --- a/client/shaders/object_shader/opengl_vertex.glsl +++ b/client/shaders/object_shader/opengl_vertex.glsl @@ -162,16 +162,16 @@ void main(void) shadow_position.z -= z_bias; perspective_factor = pFactor; - if (f_timeofday < 0.2) { + if (f_timeofday < 0.21) { adj_shadow_strength = f_shadow_strength * 0.5 * - (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday)); - } else if (f_timeofday >= 0.8) { + (1.0 - mtsmoothstep(0.18, 0.21, f_timeofday)); + } else if (f_timeofday >= 0.793) { adj_shadow_strength = f_shadow_strength * 0.5 * - mtsmoothstep(0.8, 0.83, f_timeofday); + mtsmoothstep(0.793, 0.823, f_timeofday); } else { adj_shadow_strength = f_shadow_strength * - mtsmoothstep(0.20, 0.25, f_timeofday) * - (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday)); + mtsmoothstep(0.21, 0.26, f_timeofday) * + (1.0 - mtsmoothstep(0.743, 0.793, f_timeofday)); } } #endif diff --git a/client/shaders/second_stage/opengl_fragment.glsl b/client/shaders/second_stage/opengl_fragment.glsl index 3b0d4c844..f68310844 100644 --- a/client/shaders/second_stage/opengl_fragment.glsl +++ b/client/shaders/second_stage/opengl_fragment.glsl @@ -20,6 +20,7 @@ uniform vec2 texelSize0; uniform ExposureParams exposureParams; uniform lowp float bloomIntensity; uniform lowp float saturation; +uniform float gamma; #ifdef GL_ES varying mediump vec2 varTexCoord; @@ -69,7 +70,6 @@ vec3 uncharted2Tonemap(vec3 x) vec4 applyToneMapping(vec4 color) { 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 @@ -102,6 +102,11 @@ vec3 screen_space_dither(highp vec2 frag_coord) { } #endif +float sFunction(float x, float a) { + x = 2.0 * x - 1.0; + return 0.5 * sign(x) * pow(abs(x), a) + 0.5; +} + void main(void) { vec2 uv = varTexCoord.st; @@ -131,8 +136,6 @@ void main(void) #ifdef ENABLE_BLOOM color = applyBloom(color, uv); #endif - - color.rgb = clamp(color.rgb, vec3(0.), vec3(1.)); // return to sRGB colorspace (approximate) @@ -142,6 +145,15 @@ void main(void) if (uv.x > 0.5 || uv.y > 0.5) #endif { + +#ifdef ENABLE_VIGNETTE + color.rgb *= 0.8 * pow(1.0 - length(uv - vec2(0.5)) * 1.4, 0.9) + 0.3; +#endif + +#ifdef ENABLE_COLOR_GRADING + color.rgb = pow(color.rgb * vec3(1.3, 1.0, 0.8), vec3(1.3, 1.0, 0.9)); +#endif + #if ENABLE_TONE_MAPPING color = applyToneMapping(color); #endif diff --git a/src/client/game.cpp b/src/client/game.cpp index 8c1fb407f..c38a92483 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -409,6 +409,8 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter CachedPixelShaderSetting m_bloom_radius_pixel{"bloomRadius"}; float m_bloom_radius; CachedPixelShaderSetting m_saturation_pixel{"saturation"}; + float m_gamma; + CachedPixelShaderSetting m_gamma_pixel{"gamma"}; bool m_volumetric_light_enabled; CachedPixelShaderSetting m_sun_position_pixel{"sunPositionScreen"}; @@ -419,11 +421,12 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter CachedPixelShaderSetting m_volumetric_light_strength_pixel{"volumetricLightStrength"}; - static constexpr std::array SETTING_CALLBACKS = { + static constexpr std::array SETTING_CALLBACKS = { "exposure_compensation", "bloom_intensity", "bloom_strength_factor", - "bloom_radius" + "bloom_radius", + "gamma" }; public: @@ -437,6 +440,8 @@ public: m_bloom_strength = RenderingEngine::BASE_BLOOM_STRENGTH * g_settings->getFloat("bloom_strength_factor", 0.1f, 10.0f); if (name == "bloom_radius") m_bloom_radius = g_settings->getFloat("bloom_radius", 0.1f, 8.0f); + if (name == "gamma") + m_gamma = g_settings->getFloat("gamma", 1.0f, 5.0f); } static void settingsCallback(const std::string &name, void *userdata) @@ -458,6 +463,7 @@ public: m_bloom_intensity = g_settings->getFloat("bloom_intensity", 0.01f, 1.0f); m_bloom_strength = RenderingEngine::BASE_BLOOM_STRENGTH * g_settings->getFloat("bloom_strength_factor", 0.1f, 10.0f); m_bloom_radius = g_settings->getFloat("bloom_radius", 0.1f, 8.0f); + m_gamma = g_settings->getFloat("gamma", 1.0f, 5.0f); m_volumetric_light_enabled = g_settings->getBool("enable_volumetric_lighting") && m_bloom_enabled; } @@ -523,6 +529,8 @@ public: m_bloom_strength_pixel.set(&m_bloom_strength, services); } + m_gamma_pixel.set(&m_gamma, services); + const auto &lighting = m_client->getEnv().getLocalPlayer()->getLighting(); float saturation = lighting.saturation; m_saturation_pixel.set(&saturation, services); diff --git a/src/client/shader.cpp b/src/client/shader.cpp index 3c7aeac39..0f80d9ab5 100644 --- a/src/client/shader.cpp +++ b/src/client/shader.cpp @@ -688,6 +688,9 @@ ShaderInfo ShaderSource::generateShader(const std::string &name, if (g_settings->getBool("shadow_poisson_filter")) shaders_header << "#define POISSON_FILTER 1\n"; + if (g_settings->getBool("enable_bumpmaps")) + shaders_header << "#define ENABLE_BUMPMAPS 1\n"; + s32 shadow_filter = g_settings->getS32("shadow_filters"); shaders_header << "#define SHADOW_FILTER " << shadow_filter << "\n"; @@ -706,6 +709,12 @@ ShaderInfo ShaderSource::generateShader(const std::string &name, if (g_settings->getBool("enable_auto_exposure")) shaders_header << "#define ENABLE_AUTO_EXPOSURE 1\n"; + if (g_settings->getBool("enable_color_grading")) + shaders_header << "#define ENABLE_COLOR_GRADING 1\n"; + + if (g_settings->getBool("enable_vignette")) + shaders_header << "#define ENABLE_VIGNETTE 1\n"; + if (g_settings->get("antialiasing") == "ssaa") { shaders_header << "#define ENABLE_SSAA 1\n"; u16 ssaa_scale = MYMAX(2, g_settings->getU16("fsaa")); diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 3e62a79e5..6675c3661 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -331,6 +331,9 @@ void set_default_settings() settings->setDefault("enable_waving_plants", "false"); settings->setDefault("exposure_compensation", "0.0"); settings->setDefault("enable_auto_exposure", "false"); + settings->setDefault("enable_color_grading", "false"); + settings->setDefault("enable_vignette", "false"); + settings->setDefault("gamma", "1.6"); settings->setDefault("debanding", "true"); settings->setDefault("antialiasing", "none"); settings->setDefault("enable_bloom", "false"); @@ -339,6 +342,7 @@ void set_default_settings() settings->setDefault("bloom_intensity", "0.05"); settings->setDefault("bloom_radius", "1"); settings->setDefault("enable_volumetric_lighting", "false"); + settings->setDefault("enable_bumpmaps", "false"); // Effects Shadows settings->setDefault("enable_dynamic_shadows", "false"); diff --git a/src/lighting.h b/src/lighting.h index 2a324cff4..991dd6d31 100644 --- a/src/lighting.h +++ b/src/lighting.h @@ -56,5 +56,5 @@ struct Lighting float shadow_intensity {0.0f}; float saturation {1.0f}; float volumetric_light_strength {0.0f}; - video::SColor artificial_light_color{ 133, 133, 133, 255 }; + video::SColor artificial_light_color{ 255, 133, 133, 133 }; };