From e6ea4fe37e435f98c868dced638ea3340e442b94 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 26 Feb 2025 04:49:45 +0300 Subject: [PATCH] add fog_curve parameter & update shaders --- res/presets/weather/rain.json | 1 + res/shaders/entity.glslf | 6 ++---- res/shaders/entity.glslv | 13 +++++++++++-- res/shaders/main.glslf | 2 -- res/shaders/main.glslv | 4 +++- src/graphics/render/WorldRenderer.cpp | 23 ++++++----------------- src/presets/WeatherPreset.cpp | 2 ++ src/presets/WeatherPreset.hpp | 5 ++++- 8 files changed, 29 insertions(+), 27 deletions(-) diff --git a/res/presets/weather/rain.json b/res/presets/weather/rain.json index 45a3a42c..a193978c 100644 --- a/res/presets/weather/rain.json +++ b/res/presets/weather/rain.json @@ -16,5 +16,6 @@ }, "fog_opacity": 0.8, "fog_dencity": 2.0, + "fog_curve": 0.5, "clouds": 0.5 } diff --git a/res/shaders/entity.glslf b/res/shaders/entity.glslf index 0c2ec520..e7a8a754 100644 --- a/res/shaders/entity.glslf +++ b/res/shaders/entity.glslf @@ -1,7 +1,7 @@ in vec4 a_color; in vec2 a_texCoord; -in float a_distance; in vec3 a_dir; +in float a_fog; out vec4 f_color; uniform sampler2D u_texture0; @@ -14,12 +14,10 @@ uniform bool u_alphaClip; void main() { vec3 fogColor = texture(u_cubemap, a_dir).rgb; vec4 tex_color = texture(u_texture0, a_texCoord); - float depth = (a_distance/256.0); float alpha = a_color.a * tex_color.a; // anyway it's any alpha-test alternative required if (alpha < (u_alphaClip ? 0.5f : 0.15f)) discard; - f_color = mix(a_color * tex_color, vec4(fogColor,1.0), - min(1.0, pow(depth*u_fogFactor, u_fogCurve))); + f_color = mix(a_color * tex_color, vec4(fogColor,1.0), a_fog); f_color.a = alpha; } diff --git a/res/shaders/entity.glslv b/res/shaders/entity.glslv index 1313bc16..e318db38 100644 --- a/res/shaders/entity.glslv +++ b/res/shaders/entity.glslv @@ -7,7 +7,7 @@ layout (location = 3) in float v_light; out vec4 a_color; out vec2 a_texCoord; -out float a_distance; +out float a_fog; out vec3 a_dir; uniform mat4 u_model; @@ -16,6 +16,11 @@ uniform mat4 u_view; uniform vec3 u_cameraPos; uniform float u_gamma; uniform float u_opacity; +uniform float u_fogFactor; +uniform float u_fogCurve; +uniform float u_weatherFogOpacity; +uniform float u_weatherFogDencity; +uniform float u_weatherFogCurve; uniform samplerCube u_cubemap; uniform vec3 u_torchlightColor; @@ -38,6 +43,10 @@ void main() { vec3 skyLightColor = pick_sky_color(u_cubemap); a_color.rgb = max(a_color.rgb, skyLightColor.rgb*decomp_light.a) * v_color; a_color.a = u_opacity; - a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0)); + + float dist = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0)); + float depth = (dist / 256.0); + a_fog = min(1.0, max(pow(depth * u_fogFactor, u_fogCurve), + min(pow(depth * u_weatherFogDencity, u_weatherFogCurve), u_weatherFogOpacity))); gl_Position = u_proj * u_view * modelpos; } diff --git a/res/shaders/main.glslf b/res/shaders/main.glslf index 17ca954d..bccbd53a 100644 --- a/res/shaders/main.glslf +++ b/res/shaders/main.glslf @@ -1,13 +1,11 @@ in vec4 a_color; in vec2 a_texCoord; -in float a_distance; in float a_fog; in vec3 a_dir; out vec4 f_color; uniform sampler2D u_texture0; uniform samplerCube u_cubemap; -uniform vec3 u_fogColor; uniform bool u_alphaClip; void main() { diff --git a/res/shaders/main.glslv b/res/shaders/main.glslv index 7219b799..c8d05d5d 100644 --- a/res/shaders/main.glslv +++ b/res/shaders/main.glslv @@ -19,6 +19,8 @@ uniform float u_fogFactor; uniform float u_fogCurve; uniform float u_weatherFogOpacity; uniform float u_weatherFogDencity; +uniform float u_weatherFogCurve; +uniform float u_timer; uniform samplerCube u_cubemap; uniform vec3 u_torchlightColor; @@ -44,6 +46,6 @@ void main() { a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0)); float depth = (a_distance / 256.0); a_fog = min(1.0, max(pow(depth * u_fogFactor, u_fogCurve), - min(depth * u_weatherFogDencity, u_weatherFogOpacity))); + min(pow(depth * u_weatherFogDencity, u_weatherFogCurve), u_weatherFogOpacity))); gl_Position = u_proj * u_view * modelpos; } diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index e093cfb1..59ee5635 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -103,21 +103,7 @@ WorldRenderer::WorldRenderer( ); weather = {}; - auto& fall = weather.fall; - fall.vspeed = 2.0f; - fall.texture = "misc/rain"; - fall.noise = "ambient/rain"; - - ParticlesPreset rainSplash; - rainSplash.frames = { - "particles:rain_splash_0", - "particles:rain_splash_1", - "particles:rain_splash_2" - }; - rainSplash.lifetime = 0.2f; - rainSplash.spawnInterval = 0.0f; - rainSplash.size = {0.2f, 0.2f, 0.2f}; - fall.splash = std::move(rainSplash); + weather.deserialize(io::read_json("res:presets/weather/rain.json")); } WorldRenderer::~WorldRenderer() = default; @@ -138,6 +124,7 @@ void WorldRenderer::setupWorldShader( shader.uniform1f("u_fogCurve", settings.graphics.fogCurve.get()); shader.uniform1f("u_weatherFogOpacity", weather.fogOpacity * weather.intensity); shader.uniform1f("u_weatherFogDencity", weather.fogDencity); + shader.uniform1f("u_weatherFogCurve", weather.fogCurve); shader.uniform1f("u_dayTime", level.getWorld()->getInfo().daytime); shader.uniform2f("u_lightDir", skybox->getLightDir()); shader.uniform3f("u_cameraPos", camera.position); @@ -216,8 +203,10 @@ void WorldRenderer::renderLevel( entityShader.uniform1i("u_alphaClip", false); float zero = weather.fall.minOpacity; entityShader.uniform1f("u_opacity", (weather.intensity * (1.0f - zero)) + zero); - precipitation->render(camera, pause ? 0.0f : delta, weather); - weather.intensity = -glm::cos(timer * 0.2f) * 0.5f + 0.5f; + if (weather.intensity > 1.e-3f && !weather.fall.texture.empty()) { + precipitation->render(camera, pause ? 0.0f : delta, weather); + } + //weather.intensity = -glm::cos(timer * 0.2f) * 0.5f + 0.5f; skybox->unbind(); } diff --git a/src/presets/WeatherPreset.cpp b/src/presets/WeatherPreset.cpp index 5738b6fa..6a55c7ec 100644 --- a/src/presets/WeatherPreset.cpp +++ b/src/presets/WeatherPreset.cpp @@ -18,6 +18,7 @@ dv::value WeatherPreset::serialize() const { root["fog_opacity"] = fogOpacity; root["fog_dencity"] = fogDencity; + root["fog_curve"] = fogCurve; root["clouds"] = clouds; return root; @@ -40,5 +41,6 @@ void WeatherPreset::deserialize(const dv::value& src) { } src.at("fog_opacity").get(fogOpacity); src.at("fog_dencity").get(fogDencity); + src.at("fog_curve").get(fogCurve); src.at("clouds").get(clouds); } diff --git a/src/presets/WeatherPreset.hpp b/src/presets/WeatherPreset.hpp index 2e676b83..8b449e90 100644 --- a/src/presets/WeatherPreset.hpp +++ b/src/presets/WeatherPreset.hpp @@ -31,10 +31,13 @@ struct WeatherPreset : Serializable { /// @brief Weather fog depth multiplier float fogDencity = 2.0f; + /// @brief Weather fog curve + float fogCurve = 0.5f; + float clouds = 0.5f; /// @brief Weather effects intensity - float intensity = 0.9f; + float intensity = 1.0f; dv::value serialize() const override; void deserialize(const dv::value& src) override;