From fe503d119209e569da59e0bfd38b374eda2b76d5 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Feb 2025 15:51:19 +0300 Subject: [PATCH] add weather intensity (WIP) --- res/shaders/entity.glslv | 2 ++ src/graphics/render/Decorator.cpp | 6 +++--- src/graphics/render/WorldRenderer.cpp | 16 +++++++++++++--- src/presets/WeatherPreset.cpp | 6 ++++++ src/presets/WeatherPreset.hpp | 14 ++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/res/shaders/entity.glslv b/res/shaders/entity.glslv index 42c72c04..1313bc16 100644 --- a/res/shaders/entity.glslv +++ b/res/shaders/entity.glslv @@ -15,6 +15,7 @@ uniform mat4 u_proj; uniform mat4 u_view; uniform vec3 u_cameraPos; uniform float u_gamma; +uniform float u_opacity; uniform samplerCube u_cubemap; uniform vec3 u_torchlightColor; @@ -36,6 +37,7 @@ void main() { a_dir = modelpos.xyz - u_cameraPos; 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)); gl_Position = u_proj * u_view * modelpos; } diff --git a/src/graphics/render/Decorator.cpp b/src/graphics/render/Decorator.cpp index bd3d23d0..baf174c1 100644 --- a/src/graphics/render/Decorator.cpp +++ b/src/graphics/render/Decorator.cpp @@ -115,7 +115,7 @@ void Decorator::updateRandom( return; } } - if (dst2 < 128 && rainSplash.has_value()) { + if (dst2 < 128 && random.randFloat() < glm::pow(weather.intensity, 2.0f) && rainSplash.has_value()) { auto treg = util::get_texture_region( assets, "particles:rain_splash_0", "" ); @@ -131,13 +131,13 @@ void Decorator::updateRandom( 2 )); } - if (random.rand() % 200 < 2 && pos.y < areaCenter.y + 1) { + if (random.rand() % 200 < 3 && pos.y < areaCenter.y + 1) { auto sound = assets.get(weather.fall.noise); audio::play( sound, pos, false, - 1.0f, + weather.intensity * weather.intensity, 1.0f, false, audio::PRIORITY_LOW, diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 2dbc1f16..e093cfb1 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -136,8 +136,8 @@ void WorldRenderer::setupWorldShader( shader.uniform1f("u_gamma", settings.graphics.gamma.get()); shader.uniform1f("u_fogFactor", fogFactor); shader.uniform1f("u_fogCurve", settings.graphics.fogCurve.get()); - shader.uniform1f("u_weatherFogOpacity", 0.8f); - shader.uniform1f("u_weatherFogDencity", 2.0f); + shader.uniform1f("u_weatherFogOpacity", weather.fogOpacity * weather.intensity); + shader.uniform1f("u_weatherFogDencity", weather.fogDencity); shader.uniform1f("u_dayTime", level.getWorld()->getInfo().daytime); shader.uniform2f("u_lightDir", skybox->getLightDir()); shader.uniform3f("u_cameraPos", camera.position); @@ -183,6 +183,7 @@ void WorldRenderer::renderLevel( } entityShader.uniform1i("u_alphaClip", true); + entityShader.uniform1f("u_opacity", 1.0f); level.entities->render( assets, *modelBatch, @@ -213,7 +214,10 @@ void WorldRenderer::renderLevel( setupWorldShader(entityShader, camera, settings, fogFactor); 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; skybox->unbind(); } @@ -344,7 +348,13 @@ void WorldRenderer::draw( const auto& settings = engine.getSettings(); const auto& worldInfo = world->getInfo(); - skybox->refresh(pctx, worldInfo.daytime, 1.0f + worldInfo.fog * 2.0f, 4); + skybox->refresh( + pctx, + worldInfo.daytime, + 1.0f + + glm::max(worldInfo.fog, weather.clouds * glm::sqrt(weather.intensity) * 0.5f) * 2.0f, + 4 + ); const auto& assets = *engine.getAssets(); auto& linesShader = assets.require("lines"); diff --git a/src/presets/WeatherPreset.cpp b/src/presets/WeatherPreset.cpp index 7326eb7d..5ed65405 100644 --- a/src/presets/WeatherPreset.cpp +++ b/src/presets/WeatherPreset.cpp @@ -12,6 +12,9 @@ dv::value WeatherPreset::serialize() const { froot["scale"] = fall.scale; froot["noise"] = fall.noise; root["fall"] = froot; + root["fog_opacity"] = fogOpacity; + root["fog_dencity"] = fogDencity; + root["clouds"] = clouds; return root; } @@ -24,5 +27,8 @@ void WeatherPreset::deserialize(const dv::value& src) { froot.at("hspeed").get(fall.hspeed); froot.at("scale").get(fall.scale); froot.at("noise").get(fall.noise); + src.at("fog_opacity").get(fogOpacity); + src.at("fog_dencity").get(fogDencity); + src.at("clouds").get(clouds); } } diff --git a/src/presets/WeatherPreset.hpp b/src/presets/WeatherPreset.hpp index 68f68c7e..2e676b83 100644 --- a/src/presets/WeatherPreset.hpp +++ b/src/presets/WeatherPreset.hpp @@ -18,10 +18,24 @@ struct WeatherPreset : Serializable { float hspeed = 0.1f; /// @brief UV scaling float scale = 0.1f; + /// @brief Fall opacity interpreted as zero. + /// @example if 0.8 then opacity range is 0.8-1.0 for 0.0-1.0 intensity + float minOpacity = 0.8f; /// @brief Fall splash std::optional splash; } fall {}; + /// @brief Max weather fog opacity + float fogOpacity = 0.8f; + + /// @brief Weather fog depth multiplier + float fogDencity = 2.0f; + + float clouds = 0.5f; + + /// @brief Weather effects intensity + float intensity = 0.9f; + dv::value serialize() const override; void deserialize(const dv::value& src) override; };