diff --git a/src/graphics/commons/Model.cpp b/src/graphics/commons/Model.cpp index 12888d32..05835f2a 100644 --- a/src/graphics/commons/Model.cpp +++ b/src/graphics/commons/Model.cpp @@ -39,6 +39,22 @@ void Mesh::addPlane( vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm}); } +void Mesh::addRect( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm, + const UVRegion& uv +) { + vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm}); + vertices.push_back({pos+right-up, {uv.u2, uv.v1}, norm}); + vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm}); + + vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm}); + vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm}); + vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm}); +} + void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) { addPlane(pos+Z*size, X*size, Y*size, Z); addPlane(pos-Z*size, -X*size, Y*size, -Z); @@ -51,16 +67,23 @@ void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) { } void Mesh::addBox( - const glm::vec3& pos, const glm::vec3& size, const UVRegion (&uvs)[6] + const glm::vec3& pos, + const glm::vec3& size, + const UVRegion (&uvs)[6], + const bool enabledSides[6] ) { - addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]); - addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]); - - addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]); - addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]); - - addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]); - addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]); + if (enabledSides[0]) // north + addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]); + if (enabledSides[1]) // south + addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]); + if (enabledSides[2]) // top + addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]); + if (enabledSides[3]) // bottom + addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]); + if (enabledSides[4]) // west + addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]); + if (enabledSides[5]) // east + addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]); } void Mesh::scale(const glm::vec3& size) { diff --git a/src/graphics/commons/Model.hpp b/src/graphics/commons/Model.hpp index da295cbb..0aed1b76 100644 --- a/src/graphics/commons/Model.hpp +++ b/src/graphics/commons/Model.hpp @@ -31,11 +31,19 @@ namespace model { const glm::vec3& norm, const UVRegion& region ); + void addRect( + const glm::vec3& pos, + const glm::vec3& right, + const glm::vec3& up, + const glm::vec3& norm, + const UVRegion& region + ); void addBox(const glm::vec3& pos, const glm::vec3& size); void addBox( const glm::vec3& pos, const glm::vec3& size, - const UVRegion (&texfaces)[6] + const UVRegion (&texfaces)[6], + const bool enabledSides[6] ); void scale(const glm::vec3& size); }; @@ -47,6 +55,11 @@ namespace model { /// @param texture texture name /// @return writeable Mesh Mesh& addMesh(const std::string& texture) { + for (auto& mesh : meshes) { + if (mesh.texture == texture) { + return mesh; + } + } meshes.push_back({texture, {}}); return meshes[meshes.size()-1]; } diff --git a/src/graphics/render/ModelsGenerator.cpp b/src/graphics/render/ModelsGenerator.cpp index acd6366f..44c77418 100644 --- a/src/graphics/render/ModelsGenerator.cpp +++ b/src/graphics/render/ModelsGenerator.cpp @@ -50,16 +50,28 @@ static inline UVRegion get_region_for( void ModelsGenerator::prepare(Content& content, Assets& assets) { for (auto& [name, def] : content.blocks.getDefs()) { - if (def->model.type == BlockModelType::CUSTOM && def->model.name.empty()) { - assets.store( - std::make_unique( - loadCustomBlockModel( - def->model.customRaw, assets, !def->shadeless - ) - ), - name + ".model" - ); - def->model.name = def->name + ".model"; + if (def->model.type == BlockModelType::CUSTOM) { + if (def->model.name.empty()) { + assets.store( + std::make_unique( + loadCustomBlockModel( + def->model.customRaw, assets, !def->shadeless + ) + ), + name + ".model" + ); + def->model.name = def->name + ".model"; + } else { + auto model = assets.get(def->model.name); + if (model) { + for (auto& mesh : model->meshes) { + if (mesh.texture.length() && mesh.texture[0] == '$') { + int index = std::stoll(mesh.texture.substr(1)); + mesh.texture = "blocks:" + def->textureFaces[index]; + } + } + } + } } } for (auto& [name, def] : content.items.getDefs()) { @@ -91,8 +103,12 @@ model::Model ModelsGenerator::fromCustom( get_region_for(modelTextures[i * 6 + 1], assets), get_region_for(modelTextures[i * 6 + 0], assets) }; + bool enabled[6] {1,1,1,1,1,1}; mesh.addBox( - modelBoxes[i].center(), modelBoxes[i].size() * 0.5f, boxtexfaces + modelBoxes[i].center(), + modelBoxes[i].size() * 0.5f, + boxtexfaces, + enabled ); } for (size_t i = 0; i < points.size() / 4; i++) { diff --git a/src/maths/UVRegion.hpp b/src/maths/UVRegion.hpp index 019c8760..09a774b9 100644 --- a/src/maths/UVRegion.hpp +++ b/src/maths/UVRegion.hpp @@ -2,6 +2,7 @@ #include #include +#include struct UVRegion { float u1; @@ -51,4 +52,11 @@ struct UVRegion { u2 = cx + w * 0.5f * x; v2 = cy + h * 0.5f * y; } + + void set(const glm::vec4& vec) { + u1 = vec.x; + v1 = vec.y; + u2 = vec.z; + v2 = vec.w; + } };