From 463a43908944dddfbd3d117927211b8ea0838169 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 2 Nov 2026 00:03:25 +0300 Subject: [PATCH 1/4] add 'uv-lock' block property --- src/content/loading/BlockLoader.cpp | 1 + src/frontend/ContentGfxCache.cpp | 103 +++++++++++++++++++++++-- src/frontend/ContentGfxCache.hpp | 11 ++- src/graphics/render/BlocksRenderer.cpp | 6 +- src/maths/UVRegion.hpp | 2 +- src/maths/util.hpp | 15 ++++ src/voxels/Block.cpp | 1 + src/voxels/Block.hpp | 4 + 8 files changed, 131 insertions(+), 12 deletions(-) diff --git a/src/content/loading/BlockLoader.cpp b/src/content/loading/BlockLoader.cpp index 2a5014b92..2c29b4471 100644 --- a/src/content/loading/BlockLoader.cpp +++ b/src/content/loading/BlockLoader.cpp @@ -249,6 +249,7 @@ template<> void ContentUnitLoader::loadUnit( root.at("sky-light-passing").get(def.skyLightPassing); root.at("shadeless").get(def.shadeless); root.at("ambient-occlusion").get(def.ambientOcclusion); + root.at("uv-lock").get(def.uvLock); root.at("breakable").get(def.breakable); root.at("selectable").get(def.selectable); root.at("grounded").get(def.grounded); diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 9cdcdf70e..025546a90 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -1,6 +1,6 @@ #include "ContentGfxCache.hpp" -#include +#define GLM_ENABLE_EXPERIMENTAL #include "UiDocument.hpp" #include "assets/Assets.hpp" @@ -8,13 +8,79 @@ #include "content/ContentPack.hpp" #include "graphics/core/Atlas.hpp" #include "maths/UVRegion.hpp" +#include "maths/util.hpp" #include "voxels/Block.hpp" #include "debug/Logger.hpp" #include "core_defs.hpp" #include "settings.hpp" +#include +#include +#include +#include + static debug::Logger logger("content-gfx-cache"); +static void unrotate_triangle_uv( + const glm::vec3 (&coords)[3], glm::vec2 (&uvs)[3], const glm::vec3& normal +) { + glm::vec3 absN = glm::abs(normal); + if (glm::max(absN.x, glm::max(absN.y, absN.z)) < 0.999f) { + return; + } + glm::vec2 d1 = uvs[1] - uvs[0]; + glm::vec2 d2 = uvs[2] - uvs[0]; + + float det = d1.x * d2.y - d2.x * d1.y; + if (std::abs(det) < 1e-6f) { + return; + } + glm::vec3 e1 = coords[1] - coords[0]; + glm::vec3 e2 = coords[2] - coords[0]; + glm::vec3 pu = (e1 * d2.y - e2 * d1.y) / det; + glm::vec3 pv = (e2 * d1.x - e1 * d2.x) / det; + if (glm::length2(pu) < 1e-6f || glm::length2(pv) < 1e-6f) { + return; + } + pu = glm::normalize(pu); + pv = glm::normalize(pv); + + glm::vec3 cu; + glm::vec3 cv; + util::uv_by_normal(normal, cu, cv); + + glm::mat2 uvTransform( + std::round(glm::dot(pu, cu)), std::round(glm::dot(pu, cv)), + std::round(glm::dot(pv, cu)), std::round(glm::dot(pv, cv)) + ); + if (std::abs(std::abs(glm::determinant(uvTransform)) - 1.0f) > 1e-3f) { + return; + } + for (auto& uv : uvs) { + uv = uvTransform * (uv - 0.5f) + 0.5f; + } +} + +static void unrotate_mesh_uv(model::Mesh& mesh, const CoordSystem& orient) { + glm::vec3 X = orient.axes[0]; + glm::vec3 Y = orient.axes[1]; + glm::vec3 Z = orient.axes[2]; + for (size_t j = 0; j + 2 < mesh.vertices.size(); j += 3) { + glm::vec3 pos[3]; + glm::vec2 uv[3]; + for (int i = 0; i < 3; i++) { + auto origin = mesh.vertices[j + i].coord - 0.5f; + pos[i] = origin.x * X + origin.y * Y + origin.z * Z; + uv[i] = mesh.vertices[j + i].uv; + } + const auto& normal = mesh.vertices[j].normal; + unrotate_triangle_uv(pos, uv, normal.x * X + normal.y * Y + normal.z * Z); + for (int i = 0; i < 3; i++) { + mesh.vertices[j + i].uv = uv[i]; + } + } +} + ContentGfxCache::ContentGfxCache( const Content& content, const Assets& assets, @@ -51,11 +117,14 @@ void ContentGfxCache::refreshVariant( sideregions[index + 1] = atlas.get(texOpaque); } if (variant.model.type == BlockModelType::CUSTOM) { - auto model = assets.require(variant.model.name); + const auto& srcModel = assets.require(variant.model.name); - for (auto& mesh : model.meshes) { + std::vector> regions; + regions.reserve(srcModel.meshes.size()); + for (const auto& mesh : srcModel.meshes) { size_t pos = mesh.texture.find(':'); if (pos == std::string::npos) { + regions.emplace_back(std::nullopt); continue; } std::string texture = mesh.texture.substr(pos+1); @@ -63,13 +132,28 @@ void ContentGfxCache::refreshVariant( if (found != optionalTextures.end()) { texture = found->second; } - if (auto region = atlas.getIf(texture)) { + regions.emplace_back(atlas.getIf(texture)); + } + + bool uvLock = def.uvLock && def.rotatable; + int rotationsCount = uvLock ? def.rotations.variantsCount : 1; + for (int rotation = 0; rotation < rotationsCount; rotation++) { + model::Model model = srcModel; + for (size_t i = 0; i < model.meshes.size(); i++) { + auto& mesh = model.meshes[i]; + const auto& region = regions[i]; + if (!region) { + continue; + } + if (uvLock) { + unrotate_mesh_uv(mesh, def.rotations.variants[rotation]); + } for (auto& vertex : mesh.vertices) { vertex.uv = region->apply(vertex.uv); } } + models[modelKey(def.rt.id, variantIndex, rotation)] = std::move(model); } - models[modelKey(def.rt.id, variantIndex)] = std::move(model); } } @@ -101,8 +185,13 @@ void ContentGfxCache::refresh() { ContentGfxCache::~ContentGfxCache() = default; -const model::Model& ContentGfxCache::getModel(blockid_t id, uint8_t variant) const { - const auto& found = models.find(modelKey(id, variant)); +const model::Model& ContentGfxCache::getModel( + blockid_t id, uint8_t variant, uint8_t rotation +) const { + auto found = models.find(modelKey(id, variant, rotation)); + if (found == models.end() && rotation != 0) { + found = models.find(modelKey(id, variant, 0)); + } if (found == models.end()) { throw std::runtime_error("model not found"); } diff --git a/src/frontend/ContentGfxCache.hpp b/src/frontend/ContentGfxCache.hpp index 2d2bacaa1..8e3833492 100644 --- a/src/frontend/ContentGfxCache.hpp +++ b/src/frontend/ContentGfxCache.hpp @@ -29,8 +29,11 @@ class ContentGfxCache { std::unique_ptr sideregions; std::unordered_map models; - static inline uint64_t modelKey(blockid_t id, uint8_t variant) { - return (uint64_t(id) << 8) | uint64_t(variant & 0xFF); + static inline uint64_t modelKey( + blockid_t id, uint8_t variant, uint8_t rotation = 0 + ) { + return (uint64_t(id) << 16) | (uint64_t(rotation & 0xFF) << 8) | + uint64_t(variant & 0xFF); } void refreshVariant( @@ -57,7 +60,9 @@ public: return sideregions[getRegionIndex(id, variant, side, !dense)]; } - const model::Model& getModel(blockid_t id, uint8_t variant) const; + const model::Model& getModel( + blockid_t id, uint8_t variant, uint8_t rotation = 0 + ) const; void refresh(const Block& block, const Atlas& atlas); diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 517f7788e..14ac0ab15 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -317,7 +317,11 @@ void BlocksRenderer::blockCustomModel( ); } - const auto& model = cache.getModel(block.rt.id, block.getVariantIndex(states.userbits)); + const auto& model = cache.getModel( + block.rt.id, + block.getVariantIndex(states.userbits), + block.uvLock && block.rotatable ? states.rotation : 0 + ); for (const auto& mesh : model.meshes) { if (vertexCount + mesh.vertices.size() >= capacity || indexCount + mesh.vertices.size() >= capacity) { diff --git a/src/maths/UVRegion.hpp b/src/maths/UVRegion.hpp index f7e398d8e..fc6c0ff39 100644 --- a/src/maths/UVRegion.hpp +++ b/src/maths/UVRegion.hpp @@ -35,7 +35,7 @@ struct UVRegion { v2 = v1 + uvh * h; } - inline glm::vec2 apply(const glm::vec2& uv) { + glm::vec2 apply(const glm::vec2& uv) const { float w = getWidth(); float h = getHeight(); return glm::vec2(u1 + uv.x * w, v1 + uv.y * h); diff --git a/src/maths/util.hpp b/src/maths/util.hpp index 08f0f4585..c35450719 100644 --- a/src/maths/util.hpp +++ b/src/maths/util.hpp @@ -183,4 +183,19 @@ namespace util { } return false; } + + inline void uv_by_normal( + const glm::vec3& n, glm::vec3& u, glm::vec3& v + ) { + if (std::abs(n.y) > 0.5f) { + u = {n.y > 0 ? 1 : -1, 0, 0}; + v = {0, 0, -1}; + } else if (std::abs(n.z) > 0.5f) { + u = {n.z > 0 ? 1 : -1, 0, 0}; + v = {0, 1, 0}; + } else { + u = {0, 0, n.x > 0 ? -1 : 1}; + v = {0, 1, 0}; + } + } } diff --git a/src/voxels/Block.cpp b/src/voxels/Block.cpp index f6fb8dc7b..017ea36ee 100644 --- a/src/voxels/Block.cpp +++ b/src/voxels/Block.cpp @@ -154,6 +154,7 @@ void Block::cloneTo(Block& dst) { dst.replaceable = replaceable; dst.breakable = breakable; dst.rotatable = rotatable; + dst.uvLock = uvLock; dst.grounded = grounded; dst.hidden = hidden; dst.rotations = rotations; diff --git a/src/voxels/Block.hpp b/src/voxels/Block.hpp index 6db183e98..ad8d71606 100644 --- a/src/voxels/Block.hpp +++ b/src/voxels/Block.hpp @@ -250,6 +250,10 @@ public: /// @brief Can the block be oriented different ways bool rotatable = false; + /// @brief Keep custom model textures aligned to world axes + /// when the block is rotated + bool uvLock = false; + /// @brief Can the block exist without physical support be a solid block /// below bool grounded = false; From 42f28dd96e712e8d41e5f556c8f6ed3c847ad8d0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 2 Nov 2026 00:04:12 +0300 Subject: [PATCH 2/4] update post_content.lua --- res/scripts/post_content.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/scripts/post_content.lua b/res/scripts/post_content.lua index cc056f5d4..51b6e627f 100644 --- a/res/scripts/post_content.lua +++ b/res/scripts/post_content.lua @@ -14,7 +14,7 @@ local core_names = { "components", "sensors", "body-type", "save", "save-skeleton-pose", "save-skeleton-textures", "save-body-velocity", "save-body-settings", "skeleton-name", "lighting-mode", "mass", "elasticity", "step-height", - "blocking", "material", "translucent" + "blocking", "material", "translucent", "uv-lock" } for name, _ in pairs(user_props) do table.insert(core_names, name) From 7d3584629fcff0ce9bd855aa4dc25344b2c158a9 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 2 Nov 2026 00:07:47 +0300 Subject: [PATCH 3/4] cleanup --- src/frontend/ContentGfxCache.cpp | 1 + src/frontend/ContentGfxCache.hpp | 67 +++++++++++++++++--------------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 025546a90..6da851a56 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -18,6 +18,7 @@ #include #include #include +#include static debug::Logger logger("content-gfx-cache"); diff --git a/src/frontend/ContentGfxCache.hpp b/src/frontend/ContentGfxCache.hpp index 8e3833492..095850b12 100644 --- a/src/frontend/ContentGfxCache.hpp +++ b/src/frontend/ContentGfxCache.hpp @@ -2,13 +2,12 @@ #include "typedefs.hpp" -#include -#include -#include - #include "maths/UVRegion.hpp" #include "graphics/commons/Model.hpp" +#include +#include + class Content; class Assets; class Atlas; @@ -17,10 +16,39 @@ struct UVRegion; struct Variant; struct GraphicsSettings; -inline constexpr int GFXC_MAX_VARIANTS = 16; -inline constexpr int GFXC_SIDES = 6; - class ContentGfxCache { + static constexpr int GFXC_MAX_VARIANTS = 16; + static constexpr int GFXC_SIDES = 6; +public: + ContentGfxCache( + const Content& content, + const Assets& assets, + const GraphicsSettings& settings + ); + ~ContentGfxCache(); + + static inline size_t getRegionIndex( + blockid_t id, uint8_t variant, int side, bool opaque + ) { + return ((id * GFXC_SIDES + side) * GFXC_MAX_VARIANTS + variant) * 2 + + opaque; + } + + inline const UVRegion& getRegion( + blockid_t id, uint8_t variant, int side, bool dense + ) const { + return sideregions[getRegionIndex(id, variant, side, !dense)]; + } + + const model::Model& getModel( + blockid_t id, uint8_t variant, uint8_t rotation = 0 + ) const; + + void refresh(const Block& block, const Atlas& atlas); + + void refresh(); + +private: const Content& content; const Assets& assets; const GraphicsSettings& settings; @@ -42,29 +70,4 @@ class ContentGfxCache { uint8_t variantIndex, const Atlas& atlas ); -public: - ContentGfxCache( - const Content& content, - const Assets& assets, - const GraphicsSettings& settings - ); - ~ContentGfxCache(); - - static inline size_t getRegionIndex( - blockid_t id, uint8_t variant, int side, bool opaque - ) { - return ((id * GFXC_SIDES + side) * GFXC_MAX_VARIANTS + variant) * 2 + opaque; - } - - inline const UVRegion& getRegion(blockid_t id, uint8_t variant, int side, bool dense) const { - return sideregions[getRegionIndex(id, variant, side, !dense)]; - } - - const model::Model& getModel( - blockid_t id, uint8_t variant, uint8_t rotation = 0 - ) const; - - void refresh(const Block& block, const Atlas& atlas); - - void refresh(); }; From 46ce153eb267f223eb3017a7bbf3c7d04fc74e5d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 2 Nov 2026 00:12:33 +0300 Subject: [PATCH 4/4] update doc/*/block-properties.md --- doc/en/block-properties.md | 7 +++++++ doc/ru/block-properties.md | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/doc/en/block-properties.md b/doc/en/block-properties.md index 6c038c693..65ff4c75d 100644 --- a/doc/en/block-properties.md +++ b/doc/en/block-properties.md @@ -71,6 +71,13 @@ Rotation profile (set of available block rotations and behaviour of placing bloc - "stairs" - "pane" + flipped variants - "ladder" - like a "pipe", but on horizontal surfaces it is installed like "pane" +### *uv-lock* + +Keeps custom model textures aligned to world axes when the block is rotated, +so textures of rotated blocks join seamlessly with neighbours. +Applies to axis-aligned faces of `custom` models with named textures. +Leave it off for directional textures (doors, arrows, wood). `false` by default. + ### *particles* Particles are specified as a JSON object. Property names can be found [in the particles section](particles.md). diff --git a/doc/ru/block-properties.md b/doc/ru/block-properties.md index e87686619..8a64feb7a 100644 --- a/doc/ru/block-properties.md +++ b/doc/ru/block-properties.md @@ -72,6 +72,13 @@ - "stairs" - профиль "ступеньки" ("pane" + перевернутые варианты) - "ladder" - профиль "лестница" (как "pipe", но на горизонтальных поверхностях как "pane") +### Блокировка UV - *uv-lock* + +Сохраняет ориентацию текстур кастомной модели относительно мировых осей при вращении блока, +чтобы текстуры повёрнутых блоков бесшовно стыковались с соседними. +Работает для выровненных по осям граней `custom`-моделей с именованными текстурами. +Не включайте для направленных текстур (двери, стрелки, бревна). По умолчанию `false`. + ### Испускаемые частицы - *particles* Частицы указываются в виде JSON объекта. Имена свойств можно найти [в разделе, посвященном частицам](particles.md).