mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
commit
8124b550df
11 changed files with 175 additions and 38 deletions
|
|
@ -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).
|
||||
|
|
|
|||
|
|
@ -72,6 +72,13 @@
|
|||
- "stairs" - профиль "ступеньки" ("pane" + перевернутые варианты)
|
||||
- "ladder" - профиль "лестница" (как "pipe", но на горизонтальных поверхностях как "pane")
|
||||
|
||||
### Блокировка UV - *uv-lock*
|
||||
|
||||
Сохраняет ориентацию текстур кастомной модели относительно мировых осей при вращении блока,
|
||||
чтобы текстуры повёрнутых блоков бесшовно стыковались с соседними.
|
||||
Работает для выровненных по осям граней `custom`-моделей с именованными текстурами.
|
||||
Не включайте для направленных текстур (двери, стрелки, бревна). По умолчанию `false`.
|
||||
|
||||
### Испускаемые частицы - *particles*
|
||||
|
||||
Частицы указываются в виде JSON объекта. Имена свойств можно найти [в разделе, посвященном частицам](particles.md).
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -249,6 +249,7 @@ template<> void ContentUnitLoader<Block>::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);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "ContentGfxCache.hpp"
|
||||
|
||||
#include <string>
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
#include "UiDocument.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
|
|
@ -8,13 +8,80 @@
|
|||
#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>
|
||||
#include <stdexcept>
|
||||
|
||||
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 +118,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 +133,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 +186,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");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,12 @@
|
|||
|
||||
#include "typedefs.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "maths/UVRegion.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
class Content;
|
||||
class Assets;
|
||||
class Atlas;
|
||||
|
|
@ -17,28 +16,9 @@ struct UVRegion;
|
|||
struct Variant;
|
||||
struct GraphicsSettings;
|
||||
|
||||
inline constexpr int GFXC_MAX_VARIANTS = 16;
|
||||
inline constexpr int GFXC_SIDES = 6;
|
||||
|
||||
class ContentGfxCache {
|
||||
const Content& content;
|
||||
const Assets& assets;
|
||||
const GraphicsSettings& settings;
|
||||
|
||||
// array of block sides uv regions (6 per block)
|
||||
std::unique_ptr<UVRegion[]> sideregions;
|
||||
std::unordered_map<uint64_t, model::Model> models;
|
||||
|
||||
static inline uint64_t modelKey(blockid_t id, uint8_t variant) {
|
||||
return (uint64_t(id) << 8) | uint64_t(variant & 0xFF);
|
||||
}
|
||||
|
||||
void refreshVariant(
|
||||
const Block& def,
|
||||
const Variant& variant,
|
||||
uint8_t variantIndex,
|
||||
const Atlas& atlas
|
||||
);
|
||||
static constexpr int GFXC_MAX_VARIANTS = 16;
|
||||
static constexpr int GFXC_SIDES = 6;
|
||||
public:
|
||||
ContentGfxCache(
|
||||
const Content& content,
|
||||
|
|
@ -50,16 +30,44 @@ public:
|
|||
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;
|
||||
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 {
|
||||
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) const;
|
||||
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;
|
||||
|
||||
// array of block sides uv regions (6 per block)
|
||||
std::unique_ptr<UVRegion[]> sideregions;
|
||||
std::unordered_map<uint64_t, model::Model> models;
|
||||
|
||||
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(
|
||||
const Block& def,
|
||||
const Variant& variant,
|
||||
uint8_t variantIndex,
|
||||
const Atlas& atlas
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue