From 25cee0284febac4358a1dd7ac45dbe094af7cef9 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 5 Oct 2026 23:15:20 +0300 Subject: [PATCH] update advanced render lighting model --- res/shaders/lib/shadows.glsl | 20 +++++++++----------- src/frontend/screens/LevelScreen.cpp | 8 ++++++-- src/graphics/render/BlocksRenderer.cpp | 11 +++++++---- src/graphics/render/BlocksRenderer.hpp | 4 ++++ 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/res/shaders/lib/shadows.glsl b/res/shaders/lib/shadows.glsl index 658e9a699..c4a10c49c 100644 --- a/res/shaders/lib/shadows.glsl +++ b/res/shaders/lib/shadows.glsl @@ -19,24 +19,22 @@ float calc_shadow( float bias ) { float step = 1.0 / float(u_shadowsRes); - vec4 mpos = shadowMatrix * vec4(modelPos.xyz + normalOffset, 1.0); + vec4 mpos = shadowMatrix * vec4(modelPos.xyz + normalOffset * 2.0, 1.0); vec3 projCoords = mpos.xyz / mpos.w; projCoords = projCoords * 0.5 + 0.5; projCoords.z -= 0.00001 / u_shadowsRes + bias; float shadow = 0.0; - if (dot(realnormal, u_sunDir) < 0.0) { - // 3x3 kernel - for (int y = -1; y <= 1; y++) { - for (int x = -1; x <= 1; x++) { - vec3 offset = vec3(x, y, -(abs(x) + abs(y)) * 0.8) * step * 1.0 * u_shadowsSoftness; - shadow += texture(shadowsMap, projCoords + offset); - } + // 3x3 kernel + for (int y = -1; y <= 1; y++) { + for (int x = -1; x <= 1; x++) { + vec3 offset = vec3(x, y, -(abs(x) + abs(y)) * 0.1) * step * 1.0 * u_shadowsSoftness; + shadow += texture(shadowsMap, projCoords + offset); } - shadow /= 9.0; - } else { - shadow = 0.0; } + shadow /= 9.0; + + shadow *= max(0.0, -dot(realnormal, u_sunDir)); return shadow; } diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index 24ee44378..3c79505ad 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -85,8 +85,12 @@ LevelScreen::LevelScreen( }; keepAlive(settings.graphics.backlight.observe(resetChunks)); keepAlive(settings.graphics.softLighting.observe(resetChunks)); - keepAlive(settings.graphics.denseRender.observe([=](bool flag) { - resetChunks(flag); + keepAlive(settings.graphics.denseRender.observe([=](bool) { + resetChunks(false); + frontend->getContentGfxCache().refresh(); + })); + keepAlive(settings.graphics.shadowsQuality.observe([=](int quality) { + resetChunks(false); frontend->getContentGfxCache().refresh(); })); keepAlive(settings.camera.fov.observe([=](double value) { diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 8b8a667eb..3c5050e3d 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -10,7 +10,6 @@ #include "frontend/ContentGfxCache.hpp" const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265, 0.833149, -0.163704); -constexpr float DIRECTIONAL_LIGHT_FACTOR = 0.3f; BlocksRenderer::BlocksRenderer( size_t capacity, @@ -128,7 +127,7 @@ void BlocksRenderer::faceAO( float s = 0.5f; if (lights) { float d = glm::dot(glm::normalize(Z), SUN_VECTOR); - d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR; + d = (1.0f - directionalLightFactor) + d * directionalLightFactor; auto axisX = glm::normalize(X); auto axisY = glm::normalize(Y); @@ -167,7 +166,7 @@ void BlocksRenderer::face( float s = 0.5f; if (lights) { float d = glm::dot(glm::normalize(Z), SUN_VECTOR); - d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR; + d = (1.0f - directionalLightFactor) + d * directionalLightFactor; tint *= d; } vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, Z, lights ? 0 : 1); @@ -349,7 +348,7 @@ void BlocksRenderer::blockCustomModel( } float d = glm::dot(n, SUN_VECTOR); - d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR; + d = (1.0f - directionalLightFactor) + d * directionalLightFactor; glm::vec3 t = glm::cross(r, n); for (int i = 0; i < 3; i++) { @@ -692,6 +691,10 @@ SortingMeshData BlocksRenderer::renderTranslucent( void BlocksRenderer::build( const Chunk* chunk, const VoxelsRenderVolume& volume ) { + directionalLightFactor = settings.graphics.shadowsQuality.get() > 0.0f + ? 0.0f + : DEFAULT_DIRECTIONAL_LIGHT_FACTOR; + meshAABB = AABB(glm::vec3(CHUNK_W, CHUNK_H, CHUNK_D)); this->chunk = chunk; this->voxelsBuffer = &volume; diff --git a/src/graphics/render/BlocksRenderer.hpp b/src/graphics/render/BlocksRenderer.hpp index 819e79bca..cedc62b5a 100644 --- a/src/graphics/render/BlocksRenderer.hpp +++ b/src/graphics/render/BlocksRenderer.hpp @@ -23,6 +23,8 @@ struct UVRegion; class BlocksRenderer final { public: + static constexpr float DEFAULT_DIRECTIONAL_LIGHT_FACTOR = 0.3f; + BlocksRenderer( size_t capacity, const Block* const* blockDefs, @@ -56,6 +58,8 @@ private: bool cancelled = false; bool densePass = false; bool denseRender = false; + float directionalLightFactor = 0.0f; + AABB meshAABB {}; const Chunk* chunk = nullptr; const VoxelsRenderVolume* voxelsBuffer = nullptr;