mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 01:49:44 +00:00
add 'uv-lock' block property
This commit is contained in:
parent
c2777eba09
commit
463a439089
8 changed files with 131 additions and 12 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#include "ContentGfxCache.hpp"
|
||||
|
||||
#include <string>
|
||||
#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 <cmath>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<model::Model>(variant.model.name);
|
||||
const auto& srcModel = assets.require<model::Model>(variant.model.name);
|
||||
|
||||
for (auto& mesh : model.meshes) {
|
||||
std::vector<std::optional<UVRegion>> 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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue