From 86b5f5d82b302334f5d9054b829e9244dc785506 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 7 Nov 2024 07:27:29 +0300 Subject: [PATCH] fix custom block models --- src/frontend/ContentGfxCache.cpp | 20 ++++++++++++++++++-- src/maths/UVRegion.hpp | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 8890a24e..24297cb8 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -11,6 +11,8 @@ #include "maths/UVRegion.hpp" #include "voxels/Block.hpp" +#include + ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : content(content) { auto indices = content->getIndices(); @@ -29,8 +31,22 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) } } if (def->model == BlockModel::custom) { - models[def->rt.id] = - assets->require(def->modelName); + auto model = assets->require(def->modelName); + // temporary dirty fix tbh + if (def->modelName.find(':') == std::string::npos) { + for (auto& mesh : model.meshes) { + size_t pos = mesh.texture.find(':'); + if (pos == std::string::npos) { + continue; + } + if (auto region = atlas->getIf(mesh.texture.substr(pos+1))) { + for (auto& vertex : mesh.vertices) { + vertex.uv = region->apply(vertex.uv); + } + } + } + } + models[def->rt.id] = std::move(model); } } } diff --git a/src/maths/UVRegion.hpp b/src/maths/UVRegion.hpp index b11db4aa..75b0ab6b 100644 --- a/src/maths/UVRegion.hpp +++ b/src/maths/UVRegion.hpp @@ -38,6 +38,6 @@ struct UVRegion { inline glm::vec2 apply(const glm::vec2& uv) { float w = getWidth(); float h = getHeight(); - return glm::vec2(u1 + uv.x / w, v1 + uv.y / h); + return glm::vec2(u1 + uv.x * w, v1 + uv.y * h); } };