From 4aaa5b2d7e4d4896ad740d1c36c732eb24f7a527 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 May 2026 20:45:48 +0300 Subject: [PATCH] move sky sprites to environment --- src/graphics/render/Skybox.cpp | 35 +++++---------------------------- src/graphics/render/Skybox.hpp | 17 ++++++---------- src/world/Environment.cpp | 26 ++++++++++++++++++++++-- src/world/Environment.hpp | 36 +++++++++++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 44 deletions(-) diff --git a/src/graphics/render/Skybox.cpp b/src/graphics/render/Skybox.cpp index ef34384fc..9cdf45785 100644 --- a/src/graphics/render/Skybox.cpp +++ b/src/graphics/render/Skybox.cpp @@ -23,7 +23,6 @@ using namespace advanced_pipeline; const int STARS_COUNT = 3000; - const int STARS_SEED = 632; Skybox::Skybox(uint size, const Assets& assets) @@ -41,31 +40,6 @@ Skybox::Skybox(uint size, const Assets& assets) {{1.0f, -1.0f}}}; mesh = std::make_unique>(vertices, 6); - - sprites.push_back(SkySprite { - "misc/moon", - glm::pi() * 0.5f, - 4.0f, - false, - glm::pi() * 0.25f, - }); - - sprites.push_back(SkySprite { - "misc/moon_flare", - glm::pi() * 0.5f, - 0.5f, - false, - glm::pi() * 0.25f, - }); - - sprites.push_back(SkySprite { - "misc/sun", - glm::pi() * 1.5f, - 4.0f, - true, - glm::pi() * 0.25f, - }); - setMode(mode); } @@ -117,7 +91,8 @@ void Skybox::drawStars(float angle, float opacity) { void Skybox::drawSkySprites( float daytime, float angle, - float opacity + float opacity, + const std::vector& sprites ) { float depthScale = 2e3; for (auto& sprite : sprites) { @@ -160,7 +135,7 @@ void Skybox::draw( DrawContext ctx = pctx.sub(); ctx.setBlendMode(BlendMode::addition); - if (!environment.sky.sprites && !environment.sky.stars) { + if (environment.sky.sprites.empty() && !environment.sky.stars) { return; } @@ -170,8 +145,8 @@ void Skybox::draw( shader->uniformMatrix("u_apply", glm::mat4(1.0f)); batch3d->begin(); - if (environment.sky.sprites) { - drawSkySprites(daytime, angle, opacity); + if (!environment.sky.sprites.empty()) { + drawSkySprites(daytime, angle, opacity, environment.sky.sprites); } if (environment.sky.stars) { drawStars(angle, opacity); diff --git a/src/graphics/render/Skybox.hpp b/src/graphics/render/Skybox.hpp index 6d6074e6e..0420bc55e 100644 --- a/src/graphics/render/Skybox.hpp +++ b/src/graphics/render/Skybox.hpp @@ -10,7 +10,6 @@ #include "typedefs.hpp" #include "world/Environment.hpp" - template class Mesh; class Shader; @@ -28,14 +27,6 @@ struct SkyboxVertex { {VertexAttribute::Type::FLOAT, false, 2}, {{}, 0}}; }; -struct SkySprite { - std::string texture; - float phase; - float distance; - bool emissive; - float altitude; -}; - class Skybox { SkyMode mode = SkyMode::SOLID; uint size; @@ -47,7 +38,6 @@ class Skybox { std::unique_ptr> mesh; std::unique_ptr batch3d; - std::vector sprites; int frameid = 0; float prevMie = -1.0f; @@ -58,7 +48,12 @@ class Skybox { void drawStars(float angle, float opacity); void drawBackground(const Camera& camera, int width, int height); - void drawSkySprites(float daytime, float angle, float opacity); + void drawSkySprites( + float daytime, + float angle, + float opacity, + const std::vector& sprites + ); void refreshFace(uint face, Cubemap& cubemap); public: Skybox(uint size, const Assets& assets); diff --git a/src/world/Environment.cpp b/src/world/Environment.cpp index d2ad414bf..932880200 100644 --- a/src/world/Environment.cpp +++ b/src/world/Environment.cpp @@ -5,12 +5,22 @@ #include "data/dv_util.hpp" dv::value Environment::serialize() const { + auto skySpritesList = dv::list(); + for (const auto& sprite : sky.sprites) { + skySpritesList.add(dv::object({ + {"texture", sprite.texture}, + {"phase", sprite.phase}, + {"distance", sprite.distance}, + {"emissive", sprite.emissive}, + {"altitude", sprite.altitude}, + })); + } return dv::object({ {"sky", dv::object({ {"mode", SkyModeMeta.getNameString(sky.mode)}, {"stars", sky.stars}, {"clouds", sky.clouds}, - {"sprites", sky.sprites}, + {"sprites", std::move(skySpritesList)}, })}, }); } @@ -20,6 +30,18 @@ void Environment::deserialize(const dv::value& src) { skyItem->at("mode").get(sky.mode, SkyModeMeta); skyItem->at("stars").get(sky.stars); skyItem->at("clouds").get(sky.clouds); - skyItem->at("sprites").get(sky.sprites); + + if (auto skySpritesList = skyItem->at("sprites")) { + for (int i = 0; i < skySpritesList->size(); i++) { + SkySprite sprite; + const auto& item = (*skySpritesList.ptr)[i]; + item.at("texture").get(sprite.texture); + item.at("phase").get(sprite.phase); + item.at("distance").get(sprite.distance); + item.at("emissive").get(sprite.emissive); + item.at("altitude").get(sprite.altitude); + sky.sprites.push_back(std::move(sprite)); + } + } } } diff --git a/src/world/Environment.hpp b/src/world/Environment.hpp index 5b5f668eb..feb244178 100644 --- a/src/world/Environment.hpp +++ b/src/world/Environment.hpp @@ -3,6 +3,9 @@ #include "interfaces/Serializable.hpp" #include "util/EnumMetadata.hpp" +#include +#include + enum class SkyMode { NONE, SOLID, BOX }; @@ -13,13 +16,44 @@ VC_ENUM_METADATA(SkyMode) {"box", SkyMode::BOX}, VC_ENUM_END +struct SkySprite { + std::string texture; + float phase; + float distance; + bool emissive; + float altitude; +}; + class Environment : public Serializable { public: struct { SkyMode mode = SkyMode::BOX; bool stars = true; bool clouds = true; - bool sprites = true; + // TODO: move defaults to config file + std::vector sprites { + SkySprite { + "misc/moon", + glm::pi() * 0.5f, + 4.0f, + false, + glm::pi() * 0.25f, + }, + SkySprite { + "misc/moon_flare", + glm::pi() * 0.5f, + 0.5f, + false, + glm::pi() * 0.25f, + }, + SkySprite { + "misc/sun", + glm::pi() * 1.5f, + 4.0f, + true, + glm::pi() * 0.25f, + }, + }; } sky; Environment() = default;