From cc363a77d7e30f5a8aaeeeaecaa710b0765816d8 Mon Sep 17 00:00:00 2001 From: MirceaKitsune Date: Sun, 1 Jun 2025 00:28:48 +0300 Subject: [PATCH] Universal skybox type checks, simplify sky type application, formatting fixes --- doc/lua_api.md | 2 + src/client/game.cpp | 70 +++++++++++------------------ src/client/sky.cpp | 9 ++-- src/client/sky.h | 6 +-- src/network/clientpackethandler.cpp | 9 ++-- src/script/lua_api/l_object.cpp | 11 ++--- src/server.cpp | 6 +-- src/skyparams.h | 10 +++++ 8 files changed, 55 insertions(+), 68 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 74e5c7d44..a7219e202 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -8811,7 +8811,9 @@ child will follow movement and rotation of that bone. * `"regular"`: Uses 0 textures, `base_color` ignored * `"skybox"`: Uses 6 textures, `base_color` used as fog. * `"skybox_back"`: Uses 6 textures, `sky_color` used as fog, stars / sun / moon in foreground. + Note: Requires Luanti client version 5.13 or greater. * `"skybox_front"`: Uses 6 textures, `sky_color` used as fog, stars / sun / moon in background. + Note: Requires Luanti client version 5.13 or greater. * `"plain"`: Uses 0 textures, `base_color` used as both fog and sky. (default: `"regular"`) * `textures`: A table containing up to six textures in the following diff --git a/src/client/game.cpp b/src/client/game.cpp index 862ca50e6..fac6b4be4 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -2851,67 +2851,51 @@ void Game::handleClientEvent_HudChange(ClientEvent *event, CameraOrientation *ca void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam) { - sky->setVisible(false); + // Handle invalid sky type. + if (!event->set_sky->isSkybox() && !event->set_sky->isTransparent() && event->set_sky->type != "plain") + infostream << "Unknown sky type: " << (event->set_sky->type) << std::endl; + + // Show the mesh sky if transparent. + sky->setVisible(event->set_sky->isTransparent()); sky->setType(event->set_sky->type); // Whether clouds are visible in front of a custom skybox. sky->setCloudsEnabled(event->set_sky->clouds); - // Clear the old textures out in case we switch rendering type. - sky->clearSkyboxTextures(); - // Handle according to type - if (event->set_sky->type == "regular") { - // Shows the mesh skybox - sky->setVisible(true); - // Update mesh based skybox colours if applicable. + // Show the mesh sky and use skybox colours if transparent. + if (event->set_sky->isTransparent()) sky->setSkyColors(event->set_sky->sky_color); - sky->setHorizonTint( - event->set_sky->fog_sun_tint, - event->set_sky->fog_moon_tint, - event->set_sky->fog_tint_type - ); - } else if ((event->set_sky->type == "skybox" || event->set_sky->type == "skybox_back" || - event->set_sky->type == "skybox_front") && event->set_sky->textures.size() == 6) { - const bool transparent = event->set_sky->type == "skybox_back" || event->set_sky->type == "skybox_front"; - // Show the mesh and sky colors only if transparency is used. - if(transparent) { - sky->setVisible(true); - sky->setSkyColors(event->set_sky->sky_color); - } else { - sky->setVisible(false); - sky->setFallbackBgColor(event->set_sky->bgcolor); - } - // Set sunrise and sunset fog tinting: - sky->setHorizonTint( - event->set_sky->fog_sun_tint, - event->set_sky->fog_moon_tint, - event->set_sky->fog_tint_type - ); - // Add textures to skybox. - for (int i = 0; i < 6; i++) - sky->addTextureToSkybox(event->set_sky->textures[i], i, texture_src, transparent); - } else { - // Handle everything else as plain color. - if (event->set_sky->type != "plain") - infostream << "Unknown sky type: " - << (event->set_sky->type) << std::endl; - sky->setVisible(false); + else sky->setFallbackBgColor(event->set_sky->bgcolor); - // Disable directional sun/moon tinting on plain or invalid skyboxes. + + // Use horizon tint for regular or skybox skies. + if (event->set_sky->isSkybox() || event->set_sky->isTransparent()) + sky->setHorizonTint( + event->set_sky->fog_sun_tint, + event->set_sky->fog_moon_tint, + event->set_sky->fog_tint_type + ); + else sky->setHorizonTint( event->set_sky->bgcolor, event->set_sky->bgcolor, "custom" ); + + // Clear the old textures out in case we switch rendering type. + sky->clearSkyboxTextures(); + // Add textures to skybox. + if(event->set_sky->isSkybox()) { + for (int i = 0; i < 6; i++) + sky->addTextureToSkybox(event->set_sky->textures[i], i, texture_src, event->set_sky->isTransparent()); } // Orbit Tilt: sky->setBodyOrbitTilt(event->set_sky->body_orbit_tilt); - // fog - // do not override a potentially smaller client setting. + // Fog, do not override a potentially smaller client setting. sky->setFogDistance(event->set_sky->fog_distance); - // if the fog distance is reset, switch back to the client's viewing_range + // If the fog distance is reset, switch back to the client's viewing_range if (event->set_sky->fog_distance < 0) draw_control->wanted_range = g_settings->getS16("viewing_range"); diff --git a/src/client/sky.cpp b/src/client/sky.cpp index b4c589749..f190f8210 100644 --- a/src/client/sky.cpp +++ b/src/client/sky.cpp @@ -197,7 +197,6 @@ void Sky::render() const f32 t = 1.0f; const f32 o = 0.0f; - const bool has_tex = m_sky_params.textures.size() == 6; static const u16 indices[6] = {0, 1, 2, 0, 2, 3}; video::S3DVertex vertices[4]; @@ -211,12 +210,12 @@ void Sky::render() return; // Draw the six sided skybox, solid or transparent background. - if(has_tex && (m_type == "skybox" || m_type == "skybox_back")) + if (m_sky_params.type == "skybox" || m_sky_params.type == "skybox_back") renderTextures(driver); // Draw far cloudy fog thing blended with skycolor // Disabled when using a textured skybox to prevent clipping - if (m_visible && !has_tex) { + if (m_visible && !m_sky_params.isSkybox()) { driver->setMaterial(m_materials[1]); for (u32 j = 0; j < 4; j++) { vertices[0] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, m_bgcolor, t, t); @@ -281,7 +280,7 @@ void Sky::render() // Draw far cloudy fog thing below all horizons in front of sun, moon and stars. // Disabled when using a textured skybox to prevent clipping - if (m_visible && !has_tex) { + if (m_visible && !m_sky_params.isSkybox()) { driver->setMaterial(m_materials[1]); for (u32 j = 0; j < 4; j++) { @@ -317,7 +316,7 @@ void Sky::render() } // Draw the six sided skybox, transparent foreground. - if(has_tex && m_type == "skybox_front") + if (m_sky_params.type == "skybox_front") renderTextures(driver); } } diff --git a/src/client/sky.h b/src/client/sky.h index 0589ff50b..f10cb203f 100644 --- a/src/client/sky.h +++ b/src/client/sky.h @@ -33,7 +33,6 @@ public: virtual void OnRegisterSceneNode(); //! renders the node. - virtual void renderTextures(video::IVideoDriver *driver); virtual void render(); virtual const aabb3f &getBoundingBox() const { return m_box; } @@ -83,7 +82,7 @@ public: const video::SColorf &getCloudColor() const { return m_cloudcolor_f; } void setVisible(bool visible) { m_visible = visible; } - void setType(std::string type) { m_type = type; } + void setType(std::string type) { m_sky_params.type = type; } // Set only from set_sky API void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; } @@ -126,6 +125,8 @@ public: private: aabb3f m_box{{0.0f, 0.0f, 0.0f}}; video::SMaterial m_materials[SKY_MATERIAL_COUNT]; + virtual void renderTextures(video::IVideoDriver *driver); + // How much sun & moon transition should affect horizon color float m_horizon_blend() { @@ -164,7 +165,6 @@ private: } bool m_visible = true; - std::string m_type = "regular"; // Used when m_visible=false video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255); bool m_first_update = true; // Set before the sky is updated for the first time diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 5732b3ce2..773a6c61a 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -1303,8 +1303,7 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt) StarParams stars = SkyboxDefaults::getStarDefaults(); // Fix for "regular", "skybox_back", "skybox_front" skies as color isn't kept: - if (skybox.type == "regular" || - skybox.type == "skybox_back" || skybox.type == "skybox_front") { + if (skybox.isTransparent()) { skybox.sky_color = SkyboxDefaults::getSkyColorDefaults(); skybox.fog_tint_type = "default"; skybox.fog_moon_tint = video::SColor(255, 255, 255, 255); @@ -1344,8 +1343,7 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt) *pkt >> skybox.bgcolor >> skybox.type >> skybox.clouds >> skybox.fog_sun_tint >> skybox.fog_moon_tint >> skybox.fog_tint_type; - if (skybox.type == "skybox" || - skybox.type == "skybox_back" || skybox.type == "skybox_front") { + if (skybox.isSkybox()) { u16 texture_count; std::string texture; *pkt >> texture_count; @@ -1354,8 +1352,7 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt) skybox.textures.emplace_back(texture); } } - if (skybox.type == "regular" || - skybox.type == "skybox_back" || skybox.type == "skybox_front") { + if (skybox.isTransparent()) { auto &c = skybox.sky_color; *pkt >> c.day_sky >> c.day_horizon >> c.dawn_sky >> c.dawn_horizon >> c.night_sky >> c.night_horizon >> c.indoors; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 16fdbe6a0..5267bd2e9 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -2065,8 +2065,7 @@ int ObjectRef::l_set_sky(lua_State *L) lua_getfield(L, 2, "textures"); sky_params.textures.clear(); - if (lua_istable(L, -1) && (sky_params.type == "skybox" || - sky_params.type == "skybox_back" || sky_params.type == "skybox_front")) { + if (lua_istable(L, -1) && sky_params.isSkybox()) { lua_pushnil(L); while (lua_next(L, -2) != 0) { // Key is at index -2 and value at index -1 @@ -2160,8 +2159,7 @@ int ObjectRef::l_set_sky(lua_State *L) // Preserve old behavior of the sun, moon and stars // when using the old set_sky call. - if (sky_params.type == "regular" || - sky_params.type == "skybox_back" || sky_params.type == "skybox_front") { + if (sky_params.isTransparent()) { sun_params.visible = true; sun_params.sunrise_visible = true; moon_params.visible = true; @@ -2183,7 +2181,7 @@ int ObjectRef::l_set_sky(lua_State *L) lua_pop(L, 1); } } - if (sky_params.type == "skybox" && sky_params.textures.size() != 6) + if (sky_params.isSkybox() && sky_params.textures.size() != 6) throw LuaError("Skybox expects 6 textures."); sky_params.clouds = true; @@ -2202,8 +2200,7 @@ int ObjectRef::l_set_sky(lua_State *L) static void push_sky_color(lua_State *L, const SkyboxParams ¶ms) { lua_newtable(L); - if (params.type == "regular" || - params.type == "skybox_back" || params.type == "skybox_front") { + if (params.isTransparent()) { push_ARGB8(L, params.sky_color.day_sky); lua_setfield(L, -2, "day_sky"); push_ARGB8(L, params.sky_color.day_horizon); diff --git a/src/server.cpp b/src/server.cpp index 6d8beed9f..4d30a6067 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1865,14 +1865,12 @@ void Server::SendSetSky(session_t peer_id, const SkyboxParams ¶ms) << params.clouds << params.fog_sun_tint << params.fog_moon_tint << params.fog_tint_type; - if (params.type == "skybox" || - params.type == "skybox_back" || params.type == "skybox_front") { + if (params.isSkybox()) { pkt << (u16) params.textures.size(); for (const std::string &texture : params.textures) pkt << texture; } - if (params.type == "regular" || - params.type == "skybox_back" || params.type == "skybox_front") { + if (params.isTransparent()) { auto &c = params.sky_color; pkt << c.day_sky << c.day_horizon << c.dawn_sky << c.dawn_horizon << c.night_sky << c.night_horizon << c.indoors; diff --git a/src/skyparams.h b/src/skyparams.h index c5cd574cd..16925a53f 100644 --- a/src/skyparams.h +++ b/src/skyparams.h @@ -38,6 +38,16 @@ struct SkyboxParams s16 fog_distance { -1 }; float fog_start { -1.0f }; video::SColor fog_color { 0 }; // override, only used if alpha > 0 + + // Check if this is a textured skybox and whether transparency is used + bool isSkybox() const + { + return type == "skybox" || type == "skybox_back" || type == "skybox_front"; + } + bool isTransparent() const + { + return type == "regular" || type == "skybox_back" || type == "skybox_front"; + } }; struct SunParams