From 17a75185e1b1343e293dcc010028151f55bea89f Mon Sep 17 00:00:00 2001 From: Andrey2470T Date: Sat, 3 Feb 2024 22:53:24 +0300 Subject: [PATCH] Update loaded mapblocks meshes if luminance was set, added doc --- client/shaders/nodes_shader/opengl_fragment.glsl | 5 ++++- client/shaders/object_shader/opengl_fragment.glsl | 4 +++- doc/lua_api.md | 3 +++ src/client/clientmap.cpp | 8 ++++++++ src/client/clientmap.h | 4 ++++ src/client/game.cpp | 14 +++++++++----- src/network/clientpackethandler.cpp | 6 +++++- 7 files changed, 36 insertions(+), 8 deletions(-) diff --git a/client/shaders/nodes_shader/opengl_fragment.glsl b/client/shaders/nodes_shader/opengl_fragment.glsl index e1bbab09f..82246e12d 100644 --- a/client/shaders/nodes_shader/opengl_fragment.glsl +++ b/client/shaders/nodes_shader/opengl_fragment.glsl @@ -9,6 +9,7 @@ uniform float fogShadingParameter; uniform highp vec3 cameraOffset; uniform float animationTimer; +uniform float ambientLight; uniform vec3 ambientColor; #ifdef ENABLE_DYNAMIC_SHADOWS // shadow texture @@ -377,7 +378,9 @@ void main(void) #endif color = base.rgb; - vec4 col = vec4(color.rgb * varColor.rgb * ambientColor.rgb, 1.0); + vec4 col = vec4(color.rgb * varColor.rgb, 1.0); + + col.rgb += ambientLight * ambientColor.rgb; #ifdef ENABLE_DYNAMIC_SHADOWS if (f_shadow_strength > 0.0) { diff --git a/client/shaders/object_shader/opengl_fragment.glsl b/client/shaders/object_shader/opengl_fragment.glsl index 2beaeddd0..9c3ee84a7 100644 --- a/client/shaders/object_shader/opengl_fragment.glsl +++ b/client/shaders/object_shader/opengl_fragment.glsl @@ -9,6 +9,7 @@ uniform float fogShadingParameter; uniform highp vec3 cameraOffset; uniform float animationTimer; +uniform float ambientLight; uniform vec3 ambientColor; #ifdef ENABLE_DYNAMIC_SHADOWS // shadow texture @@ -380,7 +381,8 @@ void main(void) #endif color = base.rgb; - vec4 col = vec4(color.rgb * varColor.rgb * ambientColor.rgb, 1.0); + vec4 col = vec4(color.rgb * varColor.rgb, 1.0); + col.rgb += ambientLight * ambientColor.rgb; col.rgb *= vIDiff; #ifdef ENABLE_DYNAMIC_SHADOWS diff --git a/doc/lua_api.md b/doc/lua_api.md index 4eea2f7f3..123e650b3 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -8395,6 +8395,9 @@ child will follow movement and rotation of that bone. * `set_lighting(light_definition)`: sets lighting for the player * Passing no arguments resets lighting to its default values. * `light_definition` is a table with the following optional fields: + * `ambient_light` is a table controlling amount and color of ambient light (global lighting) + * `luminance` sets the amount of ambient light in range (0... LIGHT_SUN) like `light_source` has (default: `0`) + * `color` sets the color of ambient light (ColorSpec) (default: `{a = 0, r = 0, g = 0, b = 0}`) * `saturation` sets the saturation (vividness; default: `1.0`). * values > 1 increase the saturation * values in [0,1] decrease the saturation diff --git a/src/client/clientmap.cpp b/src/client/clientmap.cpp index 45995c0ea..848e4ace9 100644 --- a/src/client/clientmap.cpp +++ b/src/client/clientmap.cpp @@ -623,6 +623,14 @@ void ClientMap::updateDrawList() } } + // Force update meshes of all loaded mapblocks + if (m_update_mapblocks_meshes) { + m_update_mapblocks_meshes = false; + + for (auto &p : m_drawlist) + m_client->addUpdateMeshTask(p.first, false, true); + } + g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled); g_profiler->avg("MapBlocks frustum culled [#]", blocks_frustum_culled); g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size()); diff --git a/src/client/clientmap.h b/src/client/clientmap.h index 05c33d67c..91838df5f 100644 --- a/src/client/clientmap.h +++ b/src/client/clientmap.h @@ -112,6 +112,8 @@ class ClientMap : public Map, public scene::ISceneNode f32 getWantedRange() const { return m_control.wanted_range; } f32 getCameraFov() const { return m_camera_fov; } + void forceUpdateMapblocksMeshes() { m_update_mapblocks_meshes = true; } + void onSettingChanged(const std::string &name); protected: @@ -195,4 +197,6 @@ class ClientMap : public Map, public scene::ISceneNode bool m_loops_occlusion_culler; bool m_enable_raytraced_culling; + + bool m_update_mapblocks_meshes; }; diff --git a/src/client/game.cpp b/src/client/game.cpp index 861136fb3..862234bf9 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -382,6 +382,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter CachedPixelShaderSetting m_animation_timer_delta_pixel{"animationTimerDelta"}; CachedPixelShaderSetting m_day_light{"dayLight"}; + CachedPixelShaderSetting m_ambient_light{"ambientLight"}; CachedPixelShaderSetting m_ambient_color{"ambientColor"}; CachedPixelShaderSetting m_minimap_yaw{"yawVec"}; CachedPixelShaderSetting m_camera_offset_pixel{"cameraOffset"}; @@ -474,7 +475,13 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter get_sunlight_color(&sunlight, daynight_ratio); m_day_light.set(sunlight, services); - video::SColor ambient_color = m_client->getEnv().getLocalPlayer()->getLighting().ambient_light.color; + auto lighting = m_client->getEnv().getLocalPlayer()->getLighting(); + + float ambient_light = lighting.ambient_light.luminance / 16.f; + + m_ambient_light.set(&ambient_light, services); + + video::SColor ambient_color = lighting.ambient_light.color; float ac_f[3] = { ambient_color.getRed()/255.f, @@ -514,7 +521,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter m_texel_size0_vertex.set(m_texel_size0, services); m_texel_size0_pixel.set(m_texel_size0, services); - const AutoExposure &exposure_params = m_client->getEnv().getLocalPlayer()->getLighting().exposure; + const AutoExposure &exposure_params = lighting.exposure; std::array exposure_buffer = { std::pow(2.0f, exposure_params.luminance_min), std::pow(2.0f, exposure_params.luminance_max), @@ -531,8 +538,6 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter m_bloom_radius_pixel.set(&m_bloom_radius, services); m_bloom_strength_pixel.set(&m_bloom_strength, services); } - - const auto &lighting = m_client->getEnv().getLocalPlayer()->getLighting(); float saturation = lighting.saturation; m_saturation_pixel.set(&saturation, services); @@ -575,7 +580,6 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter float moon_brightness = 0.f; m_moon_brightness_pixel.set(&moon_brightness, services); } - float volumetric_light_strength = lighting.volumetric_light_strength; m_volumetric_light_strength_pixel.set(&volumetric_light_strength, services); } diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 92236ce04..0b1b59541 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -47,6 +47,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "skyparams.h" #include "particles.h" #include +#include "client/clientmap.h" void Client::handleCommand_Deprecated(NetworkPacket* pkt) { @@ -1805,9 +1806,12 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt) *pkt >> lighting.shadow_intensity; if (pkt->getRemainingBytes() >= 4) *pkt >> lighting.saturation; - if (pkt->getRemainingBytes() >= 1) + if (pkt->getRemainingBytes() >= 1) { *pkt >> lighting.ambient_light.luminance >> lighting.ambient_light.color; + + getEnv().getClientMap().forceUpdateMapblocksMeshes(); + } if (pkt->getRemainingBytes() >= 24) { *pkt >> lighting.exposure.luminance_min >> lighting.exposure.luminance_max