From 463a43908944dddfbd3d117927211b8ea0838169 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 2 Nov 2026 00:03:25 +0300 Subject: [PATCH] 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;