mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
- 'shader' property of block materials: shaders/materials/<name>.glslv, .glslf with material_vertex / material_fragment hooks and #param uniforms - material shaders code is included into main, translucent and shadows programs through generated headers, no extra draw calls - chunk vertex normal.w byte replaced with 'flags': bit 7 - emission flag, bits 0-6 - material shader index (vertex format size is not changed) - doc/*/block-materials.md
179 lines
5.7 KiB
C++
179 lines
5.7 KiB
C++
#include "ContentBuilder.hpp"
|
|
|
|
#include <set>
|
|
#include <stdexcept>
|
|
#include <unordered_map>
|
|
|
|
#include "constants.hpp"
|
|
|
|
ContentBuilder::~ContentBuilder() = default;
|
|
|
|
void ContentBuilder::add(std::unique_ptr<ContentPackRuntime> pack) {
|
|
packs[pack->getId()] = std::move(pack);
|
|
}
|
|
|
|
BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) {
|
|
blockMaterials[id] = std::make_unique<BlockMaterial>();
|
|
auto& material = *blockMaterials[id];
|
|
material.name = id;
|
|
return material;
|
|
}
|
|
|
|
/// @brief Assign indices to the material shaders (materials using the same
|
|
/// shader share its index)
|
|
static void index_material_shaders(
|
|
const UptrsMap<std::string, BlockMaterial>& blockMaterials
|
|
) {
|
|
std::set<std::string> shaders;
|
|
for (const auto& [name, material] : blockMaterials) {
|
|
if (!material->shader.empty()) {
|
|
shaders.insert(material->shader);
|
|
}
|
|
}
|
|
if (shaders.size() > MAX_BLOCK_MATERIAL_SHADERS) {
|
|
throw std::runtime_error(
|
|
"too many material shaders (max " +
|
|
std::to_string(MAX_BLOCK_MATERIAL_SHADERS) + ")"
|
|
);
|
|
}
|
|
std::unordered_map<std::string, uint8_t> indices;
|
|
for (const auto& shader : shaders) {
|
|
indices[shader] = indices.size() + 1;
|
|
}
|
|
for (const auto& [name, material] : blockMaterials) {
|
|
if (!material->shader.empty()) {
|
|
material->rt.shaderId = indices.at(material->shader);
|
|
}
|
|
}
|
|
}
|
|
|
|
static uint8_t material_shader_id(
|
|
const UptrsMap<std::string, BlockMaterial>& blockMaterials,
|
|
const std::string& material
|
|
) {
|
|
const auto& found = blockMaterials.find(material);
|
|
if (found == blockMaterials.end()) {
|
|
return 0;
|
|
}
|
|
return found->second->rt.shaderId;
|
|
}
|
|
|
|
std::unique_ptr<Content> ContentBuilder::build() {
|
|
index_material_shaders(blockMaterials);
|
|
|
|
std::vector<Block*> blockDefsIndices;
|
|
auto groups = std::make_unique<DrawGroups>();
|
|
for (const std::string& name : blocks.names) {
|
|
Block& def = *blocks.defs[name];
|
|
|
|
// Generating runtime info
|
|
def.rt.id = blockDefsIndices.size();
|
|
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
|
|
for (const auto& tag : def.tags) {
|
|
def.rt.tags.insert(tags.add(tag));
|
|
}
|
|
|
|
auto isSolid = [](const Variant& variant) {
|
|
return variant.model.type == BlockModelType::BLOCK ||
|
|
variant.culling == CullingMode::OPTIONAL;
|
|
};
|
|
|
|
if (def.variants) {
|
|
for (auto& variant : def.variants->variants) {
|
|
variant.rt.solid = isSolid(variant) || def.explictlySolid;
|
|
}
|
|
def.defaults = def.variants->variants.at(0);
|
|
} else {
|
|
def.defaults.rt.solid = isSolid(def.defaults) || def.explictlySolid;
|
|
}
|
|
def.rt.solid = def.defaults.rt.solid;
|
|
|
|
const float EPSILON = 0.01f;
|
|
def.rt.solid = def.rt.solid &&
|
|
def.obstacle &&
|
|
(glm::i8vec3(def.hitboxes[0].size() + EPSILON) == def.size);
|
|
def.rt.extended = def.size.x > 1 || def.size.y > 1 || def.size.z > 1;
|
|
|
|
if (def.material.empty()) {
|
|
defaults.at("block-material").get(def.material);
|
|
}
|
|
def.rt.materialShader = material_shader_id(blockMaterials, def.material);
|
|
|
|
if (def.rotatable) {
|
|
for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) {
|
|
def.rt.hitboxes[i].reserve(def.hitboxes.size());
|
|
for (AABB aabb : def.hitboxes) {
|
|
def.rotations.variants[i].transform(aabb);
|
|
aabb.fix();
|
|
def.rt.hitboxes[i].push_back(aabb);
|
|
}
|
|
}
|
|
} else {
|
|
def.rt.hitboxes[0] = def.hitboxes;
|
|
}
|
|
|
|
blockDefsIndices.push_back(&def);
|
|
groups->insert(def.defaults.drawGroup); // FIXME: variants
|
|
}
|
|
|
|
std::vector<ItemDef*> itemDefsIndices;
|
|
for (const std::string& name : items.names) {
|
|
ItemDef& def = *items.defs[name];
|
|
|
|
// Generating runtime info
|
|
def.rt.id = itemDefsIndices.size();
|
|
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
|
|
itemDefsIndices.push_back(&def);
|
|
}
|
|
|
|
std::vector<EntityDef*> entityDefsIndices;
|
|
for (const std::string& name : entities.names) {
|
|
EntityDef& def = *entities.defs[name];
|
|
|
|
// Generating runtime info
|
|
def.rt.id = entityDefsIndices.size();
|
|
entityDefsIndices.push_back(&def);
|
|
}
|
|
|
|
auto content = std::make_unique<Content>(
|
|
std::make_unique<ContentIndices>(
|
|
blockDefsIndices, itemDefsIndices, entityDefsIndices
|
|
),
|
|
std::move(groups),
|
|
blocks.build(),
|
|
items.build(),
|
|
entities.build(),
|
|
generators.build(),
|
|
std::move(packs),
|
|
std::move(blockMaterials),
|
|
std::move(resourceIndices),
|
|
std::move(defaults),
|
|
std::move(tags.map)
|
|
);
|
|
|
|
// Now, it's time to resolve foreign keys
|
|
for (Block* def : blockDefsIndices) {
|
|
def->rt.pickingItem = content->items.require(def->pickingItem).rt.id;
|
|
def->rt.surfaceReplacement = content->blocks.require(def->surfaceReplacement).rt.id;
|
|
if (def->properties == nullptr) {
|
|
def->properties = dv::object();
|
|
}
|
|
def->properties["name"] = def->name;
|
|
def->properties["script-file"] = def->scriptFile;
|
|
}
|
|
|
|
for (ItemDef* def : itemDefsIndices) {
|
|
def->rt.placingBlock = content->blocks.require(def->placingBlock).rt.id;
|
|
if (def->properties == nullptr) {
|
|
def->properties = dv::object();
|
|
}
|
|
def->properties["name"] = def->name;
|
|
def->properties["script-file"] = def->scriptFile;
|
|
}
|
|
|
|
for (auto& [name, def] : content->generators.getDefs()) {
|
|
def->prepare(content.get());
|
|
}
|
|
|
|
return content;
|
|
}
|