From e6494936d6e1ae48ff41369a97955187bdb62d4e Mon Sep 17 00:00:00 2001 From: Sergey Lapin Date: Tue, 14 Apr 2026 13:09:14 +0300 Subject: [PATCH] Atlas margin setting support --- .../editScene/components/ProceduralTexture.hpp | 4 ++++ src/features/editScene/systems/CellGridSystem.cpp | 11 ++++++----- .../editScene/systems/ProceduralMeshSystem.cpp | 4 ++-- src/features/editScene/systems/SceneSerializer.cpp | 2 ++ .../editScene/ui/ProceduralTextureEditor.cpp | 13 +++++++++++++ src/features/editScene/ui/TriangleBufferEditor.cpp | 2 +- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/features/editScene/components/ProceduralTexture.hpp b/src/features/editScene/components/ProceduralTexture.hpp index 669f50c..b64b6fc 100644 --- a/src/features/editScene/components/ProceduralTexture.hpp +++ b/src/features/editScene/components/ProceduralTexture.hpp @@ -43,6 +43,10 @@ struct ProceduralTextureComponent { // Texture size (default 512x512) int textureSize = 512; + // UV margin for texture mapping (default 0.01, range 0.01-0.025) + // Used to prevent color bleeding by adding padding around UV coordinates + float uvMargin = 0.01f; + // Whether the texture needs regeneration bool dirty = true; diff --git a/src/features/editScene/systems/CellGridSystem.cpp b/src/features/editScene/systems/CellGridSystem.cpp index 174e9f9..1bbdbe0 100644 --- a/src/features/editScene/systems/CellGridSystem.cpp +++ b/src/features/editScene/systems/CellGridSystem.cpp @@ -2480,8 +2480,8 @@ void CellGridSystem::buildDistrictPlaza(flecs::entity entity, const TextureRectInfo *rect = texture.getNamedRect(district.textureRectName); if (rect) { - // Apply UV mapping with 0.01 margin - const float margin = 0.01f; + // Apply UV mapping with configurable margin + const float margin = texture.uvMargin; float uRange = (rect->u2 - rect->u1) * (1.0f - 2.0f * margin); float vRange = (rect->v2 - rect->v1) * @@ -2851,7 +2851,8 @@ void CellGridSystem::buildLotBase(flecs::entity entity, LotComponent &lot) const TextureRectInfo *rect = texture.getNamedRect(textureRectToUse); if (rect) { - const float margin = 0.01f; + // Apply UV mapping with configurable margin + const float margin = texture.uvMargin; float uRange = (rect->u2 - rect->u1) * (1.0f - 2.0f * margin); float vRange = (rect->v2 - rect->v1) * @@ -3046,8 +3047,8 @@ void CellGridSystem::applyUVMappingToBuffer(Procedural::TriangleBuffer &tb, std::to_string(rect->u2) + " v=" + std::to_string(rect->v1) + "-" + std::to_string(rect->v2)); - // Apply texture rectangle UV mapping - const float margin = 0.01f; + // Apply texture rectangle UV mapping with configurable margin + const float margin = texture.uvMargin; float uRange = (rect->u2 - rect->u1) * (1.0f - 2.0f * margin); float vRange = (rect->v2 - rect->v1) * (1.0f - 2.0f * margin); float uOffset = rect->u1 + (rect->u2 - rect->u1) * margin; diff --git a/src/features/editScene/systems/ProceduralMeshSystem.cpp b/src/features/editScene/systems/ProceduralMeshSystem.cpp index 4699759..56686ec 100644 --- a/src/features/editScene/systems/ProceduralMeshSystem.cpp +++ b/src/features/editScene/systems/ProceduralMeshSystem.cpp @@ -204,8 +204,8 @@ void ProceduralMeshSystem::applyUVMapping(Procedural::TriangleBuffer* buffer, const TextureRectInfo* rect = texture.getNamedRect(rectName); if (!rect) return; - // Calculate UV mapping with margin - const float margin = 0.01f; + // Calculate UV mapping with configurable margin + const float margin = texture.uvMargin; float uRange = (rect->u2 - rect->u1) * (1.0f - 2.0f * margin); float vRange = (rect->v2 - rect->v1) * (1.0f - 2.0f * margin); float uOffset = rect->u1 + (rect->u2 - rect->u1) * margin; diff --git a/src/features/editScene/systems/SceneSerializer.cpp b/src/features/editScene/systems/SceneSerializer.cpp index 5c5e9e1..afaed35 100644 --- a/src/features/editScene/systems/SceneSerializer.cpp +++ b/src/features/editScene/systems/SceneSerializer.cpp @@ -1043,6 +1043,7 @@ nlohmann::json SceneSerializer::serializeProceduralTexture(flecs::entity entity) json["textureName"] = texture.textureName; json["textureSize"] = texture.textureSize; + json["uvMargin"] = texture.uvMargin; // Serialize colors array nlohmann::json colorsJson = nlohmann::json::array(); @@ -1079,6 +1080,7 @@ void SceneSerializer::deserializeProceduralTexture(flecs::entity entity, const n texture.textureName = json.value("textureName", ""); texture.textureSize = json.value("textureSize", 512); + texture.uvMargin = json.value("uvMargin", 0.01f); // Deserialize colors array if (json.contains("colors") && json["colors"].is_array()) { diff --git a/src/features/editScene/ui/ProceduralTextureEditor.cpp b/src/features/editScene/ui/ProceduralTextureEditor.cpp index c5dea88..43c1ea1 100644 --- a/src/features/editScene/ui/ProceduralTextureEditor.cpp +++ b/src/features/editScene/ui/ProceduralTextureEditor.cpp @@ -252,6 +252,19 @@ bool ProceduralTextureEditor::renderComponent(flecs::entity entity, ProceduralTe modified = true; } + // UV Margin control to prevent color bleeding + ImGui::Separator(); + ImGui::Text("UV Margin (prevents color bleeding):"); + if (ImGui::DragFloat("Margin", &texture.uvMargin, 0.001f, 0.01f, 0.025f, "%.3f")) { + // Clamp to valid range + if (texture.uvMargin < 0.01f) texture.uvMargin = 0.01f; + if (texture.uvMargin > 0.025f) texture.uvMargin = 0.025f; + texture.markDirty(); + modified = true; + } + ImGui::TextDisabled("Default: 0.010, Range: 0.010-0.025, Step: 0.001"); + ImGui::TextDisabled("Higher values = more padding, less color bleeding"); + ImGui::Separator(); // Color grid diff --git a/src/features/editScene/ui/TriangleBufferEditor.cpp b/src/features/editScene/ui/TriangleBufferEditor.cpp index 99904b9..6cafd0f 100644 --- a/src/features/editScene/ui/TriangleBufferEditor.cpp +++ b/src/features/editScene/ui/TriangleBufferEditor.cpp @@ -235,7 +235,7 @@ bool TriangleBufferEditor::renderComponent(flecs::entity entity, TriangleBufferC ImGui::Text("Texture Mapping:"); renderTextureSelector(entity, tb); renderRectSelector(entity, tb); - ImGui::TextDisabled("UVs will be mapped to rectangle with 0.01 margin"); + ImGui::TextDisabled("UVs will be mapped to rectangle with margin from texture"); ImGui::Separator();