Skip to content

Commit 0ade097

Browse files
authored
Allow the server to control fog_distance and fog_start via the sky-api (luanti-org#13448)
1 parent dde8f0e commit 0ade097

File tree

11 files changed

+80
-21
lines changed

11 files changed

+80
-21
lines changed

client/shaders/nodes_shader/opengl_fragment.glsl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ uniform sampler2D baseTexture;
33
uniform vec3 dayLight;
44
uniform vec4 skyBgColor;
55
uniform float fogDistance;
6+
uniform float fogShadingParameter;
67
uniform vec3 eyePosition;
78

89
// The cameraOffset is the current center of the visible world.
@@ -49,9 +50,6 @@ varying vec3 tsEyeVec;
4950
varying vec3 lightVec;
5051
varying vec3 tsLightVec;
5152

52-
const float fogStart = FOG_START;
53-
const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
54-
5553
#ifdef ENABLE_DYNAMIC_SHADOWS
5654

5755
// assuming near is always 1.0

client/shaders/object_shader/opengl_fragment.glsl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ uniform sampler2D baseTexture;
33
uniform vec3 dayLight;
44
uniform vec4 skyBgColor;
55
uniform float fogDistance;
6+
uniform float fogShadingParameter;
67
uniform vec3 eyePosition;
78

89
// The cameraOffset is the current center of the visible world.
@@ -48,9 +49,6 @@ varying float nightRatio;
4849

4950
varying float vIDiff;
5051

51-
const float fogStart = FOG_START;
52-
const float fogShadingParameter = 1.0 / (1.0 - fogStart);
53-
5452
#ifdef ENABLE_DYNAMIC_SHADOWS
5553

5654
// assuming near is always 1.0

doc/lua_api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7740,6 +7740,18 @@ child will follow movement and rotation of that bone.
77407740
abides by, `"custom"` uses `sun_tint` and `moon_tint`, while
77417741
`"default"` uses the classic Minetest sun and moon tinting.
77427742
Will use tonemaps, if set to `"default"`. (default: `"default"`)
7743+
* `fog`: A table with following optional fields:
7744+
* `fog_distance`: integer, set an upper bound the client's viewing_range (inluding range_all).
7745+
By default, fog_distance is controlled by the client's viewing_range, and this field is not set.
7746+
Any value >= 0 sets the desired upper bound for the client's viewing_range and disables range_all.
7747+
Any value < 0, resets the behavior to being client-controlled.
7748+
(default: -1)
7749+
* `fog_start`: float, override the client's fog_start.
7750+
Fraction of the visible distance at which fog starts to be rendered.
7751+
By default, fog_start is controlled by the client's `fog_start` setting, and this field is not set.
7752+
Any value between [0.0, 0.99] set the fog_start as a fraction of the viewing_range.
7753+
Any value < 0, resets the behavior to being client-controlled.
7754+
(default: -1)
77437755
* `set_sky(base_color, type, {texture names}, clouds)`
77447756
* Deprecated. Use `set_sky(sky_parameters)`
77457757
* `base_color`: ColorSpec, defaults to white

src/client/game.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
372372
bool m_fog_enabled;
373373
CachedPixelShaderSetting<float, 4> m_sky_bg_color;
374374
CachedPixelShaderSetting<float> m_fog_distance;
375+
CachedPixelShaderSetting<float> m_fog_shading_parameter;
375376
CachedVertexShaderSetting<float> m_animation_timer_vertex;
376377
CachedPixelShaderSetting<float> m_animation_timer_pixel;
377378
CachedVertexShaderSetting<float> m_animation_timer_delta_vertex;
@@ -431,6 +432,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
431432
m_fog_range(fog_range),
432433
m_sky_bg_color("skyBgColor"),
433434
m_fog_distance("fogDistance"),
435+
m_fog_shading_parameter("fogShadingParameter"),
434436
m_animation_timer_vertex("animationTimer"),
435437
m_animation_timer_pixel("animationTimer"),
436438
m_animation_timer_delta_vertex("animationTimerDelta"),
@@ -496,7 +498,10 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
496498
if (m_fog_enabled && !*m_force_fog_off)
497499
fog_distance = *m_fog_range;
498500

501+
float fog_shading_parameter = 1.0 / ( 1.0 - m_sky->getFogStart());
502+
499503
m_fog_distance.set(&fog_distance, services);
504+
m_fog_shading_parameter.set(&fog_shading_parameter, services);
500505

501506
u32 daynight_ratio = (float)m_client->getEnv().getDayNightRatio();
502507
video::SColorf sunlight;
@@ -961,7 +966,6 @@ class Game {
961966
f32 m_cache_joystick_frustum_sensitivity;
962967
f32 m_repeat_place_time;
963968
f32 m_cache_cam_smoothing;
964-
f32 m_cache_fog_start;
965969

966970
bool m_invert_mouse;
967971
bool m_enable_hotbar_mouse_wheel;
@@ -2490,6 +2494,9 @@ void Game::increaseViewRange()
24902494
range_new = 4000;
24912495
std::wstring msg = fwgettext("Viewing range is at maximum: %d", range_new);
24922496
m_game_ui->showStatusText(msg);
2497+
} else if (sky->getFogDistance() >= 0 && range_new > sky->getFogDistance()) {
2498+
std::wstring msg = fwgettext("Viewing range changed to %d, but limited to %d set by server", range_new, sky->getFogDistance());
2499+
m_game_ui->showStatusText(msg);
24932500
} else {
24942501
std::wstring msg = fwgettext("Viewing range changed to %d", range_new);
24952502
m_game_ui->showStatusText(msg);
@@ -2507,6 +2514,9 @@ void Game::decreaseViewRange()
25072514
range_new = 20;
25082515
std::wstring msg = fwgettext("Viewing range is at minimum: %d", range_new);
25092516
m_game_ui->showStatusText(msg);
2517+
} else if (sky->getFogDistance() >= 0 && range_new > sky->getFogDistance()) {
2518+
std::wstring msg = fwgettext("Viewing range changed to %d, but limited to %d set by server", range_new, sky->getFogDistance());
2519+
m_game_ui->showStatusText(msg);
25102520
} else {
25112521
std::wstring msg = fwgettext("Viewing range changed to %d", range_new);
25122522
m_game_ui->showStatusText(msg);
@@ -2518,10 +2528,15 @@ void Game::decreaseViewRange()
25182528
void Game::toggleFullViewRange()
25192529
{
25202530
draw_control->range_all = !draw_control->range_all;
2521-
if (draw_control->range_all)
2522-
m_game_ui->showTranslatedStatusText("Enabled unlimited viewing range");
2523-
else
2531+
if (draw_control->range_all) {
2532+
if (sky->getFogDistance() >= 0) {
2533+
m_game_ui->showTranslatedStatusText("The server has disabled unlimited viewing range");
2534+
} else {
2535+
m_game_ui->showTranslatedStatusText("Enabled unlimited viewing range");
2536+
}
2537+
} else {
25242538
m_game_ui->showTranslatedStatusText("Disabled unlimited viewing range");
2539+
}
25252540
}
25262541

25272542

@@ -2996,6 +3011,20 @@ void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam)
29963011
// Orbit Tilt:
29973012
sky->setBodyOrbitTilt(event->set_sky->body_orbit_tilt);
29983013

3014+
// fog
3015+
// do not override a potentially smaller client setting.
3016+
sky->setFogDistance(event->set_sky->fog_distance);
3017+
3018+
// if the fog distance is reset, switch back to the client's viewing_range
3019+
if (event->set_sky->fog_distance < 0)
3020+
draw_control->wanted_range = g_settings->getS16("viewing_range");
3021+
3022+
if (event->set_sky->fog_start >= 0)
3023+
sky->setFogStart(rangelim(event->set_sky->fog_start, 0.0f, 0.99f));
3024+
else
3025+
sky->setFogStart(rangelim(g_settings->getFloat("fog_start"), 0.0f, 0.99f));
3026+
3027+
29993028
delete event->set_sky;
30003029
}
30013030

@@ -3915,7 +3944,10 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
39153944
Fog range
39163945
*/
39173946

3918-
if (draw_control->range_all) {
3947+
if (sky->getFogDistance() >= 0) {
3948+
draw_control->wanted_range = MYMIN(draw_control->wanted_range, sky->getFogDistance());
3949+
}
3950+
if (draw_control->range_all && sky->getFogDistance() < 0) {
39193951
runData.fog_range = 100000 * BS;
39203952
} else {
39213953
runData.fog_range = draw_control->wanted_range * BS;
@@ -4006,12 +4038,11 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
40064038
/*
40074039
Fog
40084040
*/
4009-
40104041
if (m_cache_enable_fog) {
40114042
driver->setFog(
40124043
sky->getBgColor(),
40134044
video::EFT_FOG_LINEAR,
4014-
runData.fog_range * m_cache_fog_start,
4045+
runData.fog_range * sky->getFogStart(),
40154046
runData.fog_range * 1.0,
40164047
0.01,
40174048
false, // pixel fog
@@ -4284,15 +4315,12 @@ void Game::readSettings()
42844315
m_cache_enable_noclip = g_settings->getBool("noclip");
42854316
m_cache_enable_free_move = g_settings->getBool("free_move");
42864317

4287-
m_cache_fog_start = g_settings->getFloat("fog_start");
4288-
42894318
m_cache_cam_smoothing = 0;
42904319
if (g_settings->getBool("cinematic"))
42914320
m_cache_cam_smoothing = 1 - g_settings->getFloat("cinematic_camera_smoothing");
42924321
else
42934322
m_cache_cam_smoothing = 1 - g_settings->getFloat("camera_smoothing");
42944323

4295-
m_cache_fog_start = rangelim(m_cache_fog_start, 0.0f, 0.99f);
42964324
m_cache_cam_smoothing = rangelim(m_cache_cam_smoothing, 0.01f, 1.0f);
42974325
m_cache_mouse_sensitivity = rangelim(m_cache_mouse_sensitivity, 0.001, 100.0);
42984326

src/client/shader.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,6 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
752752
shaders_header << "#define ENABLE_WAVING_PLANTS " << g_settings->getBool("enable_waving_plants") << "\n";
753753
shaders_header << "#define ENABLE_TONE_MAPPING " << g_settings->getBool("tone_mapping") << "\n";
754754

755-
shaders_header << "#define FOG_START " << core::clamp(g_settings->getFloat("fog_start"), 0.0f, 0.99f) << "\n";
756-
757755
if (g_settings->getBool("enable_dynamic_shadows")) {
758756
shaders_header << "#define ENABLE_DYNAMIC_SHADOWS 1\n";
759757
if (g_settings->getBool("shadow_map_color"))

src/client/sky.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Sky::Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShade
9898

9999
m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
100100
m_sky_params.body_orbit_tilt = g_settings->getFloat("shadow_sky_body_orbit_tilt", -60., 60.);
101+
m_sky_params.fog_start = rangelim(g_settings->getFloat("fog_start"), 0.0f, 0.99f);
101102

102103
setStarCount(1000);
103104
}

src/client/sky.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ class Sky : public scene::ISceneNode
114114
void addTextureToSkybox(const std::string &texture, int material_id,
115115
ITextureSource *tsrc);
116116
const video::SColorf &getCurrentStarColor() const { return m_star_color; }
117+
void setFogDistance(s16 fog_distance) { m_sky_params.fog_distance = fog_distance; }
118+
s16 getFogDistance() const { return m_sky_params.fog_distance; }
119+
120+
void setFogStart(float fog_start) { m_sky_params.fog_start = fog_start; }
121+
float getFogStart() const { return m_sky_params.fog_start; }
117122

118123
private:
119124
aabb3f m_box;

src/network/clientpackethandler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,9 +1395,13 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
13951395
>> skybox.sky_color.indoors;
13961396
}
13971397

1398-
try {
1398+
if (pkt->getRemainingBytes() >= 4) {
13991399
*pkt >> skybox.body_orbit_tilt;
1400-
} catch (PacketError &e) {}
1400+
}
1401+
1402+
if (pkt->getRemainingBytes() >= 6) {
1403+
*pkt >> skybox.fog_distance >> skybox.fog_start;
1404+
}
14011405

14021406
ClientEvent *event = new ClientEvent();
14031407
event->type = CE_SET_SKY;

src/script/lua_api/l_object.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,11 @@ int ObjectRef::l_set_sky(lua_State *L)
18031803
// pop "sky_color" table
18041804
lua_pop(L, 1);
18051805
}
1806+
lua_getfield(L, 2, "fog");
1807+
if (lua_istable(L, -1)) {
1808+
sky_params.fog_distance = getintfield_default(L, -1, "fog_distance", sky_params.fog_distance);
1809+
sky_params.fog_start = getfloatfield_default(L, -1, "fog_start", sky_params.fog_start);
1810+
}
18061811
} else {
18071812
// Handle old set_sky calls, and log deprecated:
18081813
log_deprecated(L, "Deprecated call to set_sky, please check lua_api.md");
@@ -1923,7 +1928,6 @@ int ObjectRef::l_get_sky(lua_State *L)
19231928
lua_pushnumber(L, skybox_params.body_orbit_tilt);
19241929
lua_setfield(L, -2, "body_orbit_tilt");
19251930
}
1926-
19271931
lua_newtable(L);
19281932
s16 i = 1;
19291933
for (const std::string &texture : skybox_params.textures) {
@@ -1936,6 +1940,14 @@ int ObjectRef::l_get_sky(lua_State *L)
19361940

19371941
push_sky_color(L, skybox_params);
19381942
lua_setfield(L, -2, "sky_color");
1943+
1944+
lua_newtable(L); // fog
1945+
lua_pushinteger(L, skybox_params.fog_distance >= 0 ? skybox_params.fog_distance : -1);
1946+
lua_setfield(L, -2, "fog_distance");
1947+
lua_pushnumber(L, skybox_params.fog_start >= 0 ? skybox_params.fog_start : -1.0f);
1948+
lua_setfield(L, -2, "fog_start");
1949+
lua_setfield(L, -2, "fog");
1950+
19391951
return 1;
19401952
}
19411953

src/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,7 @@ void Server::SendSetSky(session_t peer_id, const SkyboxParams &params)
18461846
}
18471847

18481848
pkt << params.body_orbit_tilt;
1849+
pkt << params.fog_distance << params.fog_start;
18491850
}
18501851

18511852
Send(&pkt);

0 commit comments

Comments
 (0)