diff --git a/doc/en/block-properties.md b/doc/en/block-properties.md index 1244da99..82811b63 100644 --- a/doc/en/block-properties.md +++ b/doc/en/block-properties.md @@ -98,6 +98,32 @@ Properties available for variance: Variants are managed via `block.set_variant(x, y, z, index)`. +### Custom model variants (geometry switching) + +You can use different custom models for different variants. Provide a separate `model-name` for each variant that needs different geometry. The renderer caches geometry per (block id, variant). + +The base model (specified in root) becomes variant 0. The variants array maps to indices 1+. + +Example (default + two custom variants): +```json +{ + "model": "custom", + "model-name": "stairs_middle", + "state-based": { + "bits": 4, + "variants": [ + { "model": "custom", "model-name": "stairs_left" }, + { "model": "custom", "model-name": "stairs_right" } + ] + } +} +``` + +In this example: +- Variant 0 = `stairs_middle` (from root) +- Variant 1 = `stairs_left` (from variants[0]) +- Variant 2 = `stairs_right` (from variants[1]) + ## Lighting ### *emission* diff --git a/doc/ru/block-properties.md b/doc/ru/block-properties.md index 0003cb92..4569cc49 100644 --- a/doc/ru/block-properties.md +++ b/doc/ru/block-properties.md @@ -106,6 +106,32 @@ Управление состоянием производится через `block.set_variant(x, y, z, index)`. +### Кастомные модели по вариантам (переключение геометрии) + +Для custom-моделей можно переключать геометрию по варианту. Укажите отдельный `model-name` в нужных вариантах — рендерер кэширует геометрию по паре (id блока, вариант). + +Базовая модель (из корня) становится вариантом 0. Массив variants соответствует индексам 1+. + +Пример (база + два кастомных варианта): +```json +{ + "model": "custom", + "model-name": "stairs_middle", + "state-based": { + "bits": 4, + "variants": [ + { "model": "custom", "model-name": "stairs_left" }, + { "model": "custom", "model-name": "stairs_right" } + ] + } +} +``` + +В этом примере: +- Вариант 0 = `stairs_middle` (из корня) +- Вариант 1 = `stairs_left` (из variants[0]) +- Вариант 2 = `stairs_right` (из variants[1]) + ## Освещение ### Излучение - *emission*: diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 47443aff..20b7cdaa 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -62,7 +62,7 @@ void ContentGfxCache::refreshVariant( } } } - models[def.rt.id] = std::move(model); + models[modelKey(def.rt.id, variantIndex)] = std::move(model); } } @@ -94,8 +94,8 @@ void ContentGfxCache::refresh() { ContentGfxCache::~ContentGfxCache() = default; -const model::Model& ContentGfxCache::getModel(blockid_t id) const { - const auto& found = models.find(id); +const model::Model& ContentGfxCache::getModel(blockid_t id, uint8_t variant) const { + const auto& found = models.find(modelKey(id, variant)); if (found == models.end()) { throw std::runtime_error("model not found"); } diff --git a/src/frontend/ContentGfxCache.hpp b/src/frontend/ContentGfxCache.hpp index 8eda04e5..2d2bacaa 100644 --- a/src/frontend/ContentGfxCache.hpp +++ b/src/frontend/ContentGfxCache.hpp @@ -27,7 +27,11 @@ class ContentGfxCache { // array of block sides uv regions (6 per block) std::unique_ptr sideregions; - std::unordered_map models; + std::unordered_map 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, @@ -53,7 +57,7 @@ public: return sideregions[getRegionIndex(id, variant, side, !dense)]; } - const model::Model& getModel(blockid_t id) const; + const model::Model& getModel(blockid_t id, uint8_t variant) const; void refresh(const Block& block, const Atlas& atlas); diff --git a/src/graphics/render/BlocksPreview.cpp b/src/graphics/render/BlocksPreview.cpp index 9a1bf97d..4b8bfee9 100644 --- a/src/graphics/render/BlocksPreview.cpp +++ b/src/graphics/render/BlocksPreview.cpp @@ -67,7 +67,7 @@ std::unique_ptr BlocksPreview::draw( glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f); offset.y += (1.0f - hitbox).y * 0.5f; shader.uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); - const auto& model = cache.getModel(def.rt.id); + const auto& model = cache.getModel(def.rt.id, 0); for (const auto& mesh : model.meshes) { for (const auto& vertex : mesh.vertices) { diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 1f14d2c7..6df7967e 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -308,7 +308,7 @@ void BlocksRenderer::blockCustomModel( Z = orient.axes[2]; } - const auto& model = cache.getModel(block.rt.id); + const auto& model = cache.getModel(block.rt.id, block.getVariantIndex(states.userbits)); for (const auto& mesh : model.meshes) { if (vertexCount + mesh.vertices.size() >= capacity) { overflow = true;