From 3e59b186d3ed6eaf24be56a8e89facb6c457084f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 13 Jul 2025 18:51:53 +0300 Subject: [PATCH] fix custom models shading flag & add more properties to vcm --- res/devtools/syntax/vcm.toml | 3 +++ src/coders/vcm.cpp | 25 +++++++++++++++++++++++-- src/graphics/commons/Model.hpp | 10 +++++----- src/graphics/render/BlocksRenderer.cpp | 6 +++--- src/graphics/render/ModelBatch.cpp | 6 +++--- src/graphics/render/ModelsGenerator.cpp | 6 +++--- 6 files changed, 40 insertions(+), 16 deletions(-) diff --git a/res/devtools/syntax/vcm.toml b/res/devtools/syntax/vcm.toml index ca05b6a1..524fdc80 100644 --- a/res/devtools/syntax/vcm.toml +++ b/res/devtools/syntax/vcm.toml @@ -1,3 +1,6 @@ language = "VCM" extensions = ["vcm"] line-comment-start = "#" +keywords = [ + "on", "off" +] diff --git a/src/coders/vcm.cpp b/src/coders/vcm.cpp index 20c50982..a12524c0 100644 --- a/src/coders/vcm.cpp +++ b/src/coders/vcm.cpp @@ -19,10 +19,15 @@ static const std::unordered_map side_indices { {"east", 5}, }; +static bool to_boolean(const xml::Attribute& attr) { + return attr.getText() != "off"; +} + static void perform_rect(const xmlelement& root, model::Model& model) { auto from = root.attr("from").asVec3(); auto right = root.attr("right").asVec3(); auto up = root.attr("up").asVec3(); + bool shading = true; right *= -1; from -= right; @@ -37,6 +42,10 @@ static void perform_rect(const xmlelement& root, model::Model& model) { region.scale(root.attr("region-scale").asVec2()); } + if (root.has("shading")) { + shading = to_boolean(root.attr("shading")); + } + auto flip = root.attr("flip", "").getText(); if (flip == "h") { std::swap(region.u1, region.u2); @@ -48,7 +57,7 @@ static void perform_rect(const xmlelement& root, model::Model& model) { from -= up; } std::string texture = root.attr("texture", "$0").getText(); - auto& mesh = model.addMesh(texture); + auto& mesh = model.addMesh(texture, shading); auto normal = glm::cross(glm::normalize(right), glm::normalize(up)); mesh.addRect( @@ -75,8 +84,20 @@ static void perform_box(const xmlelement& root, model::Model& model) { auto center = (from + to) * 0.5f; auto halfsize = (to - from) * 0.5f; + bool shading = true; std::string texfaces[6] {"$0","$1","$2","$3","$4","$5"}; + if (root.has("texture")) { + auto texture = root.attr("texture").getText(); + for (int i = 0; i < 6; i++) { + texfaces[i] = texture; + } + } + + if (root.has("shading")) { + shading = to_boolean(root.attr("shading")); + } + for (const auto& elem : root.getElements()) { if (elem->getTag() == "part") { // todo: replace by expression parsing @@ -120,7 +141,7 @@ static void perform_box(const xmlelement& root, model::Model& model) { } bool enabled[6] {}; enabled[i] = true; - auto& mesh = model.addMesh(texfaces[i]); + auto& mesh = model.addMesh(texfaces[i], shading); mesh.addBox(center, halfsize, regions, enabled); } } diff --git a/src/graphics/commons/Model.hpp b/src/graphics/commons/Model.hpp index 0aed1b76..73bee359 100644 --- a/src/graphics/commons/Model.hpp +++ b/src/graphics/commons/Model.hpp @@ -16,7 +16,7 @@ namespace model { struct Mesh { std::string texture; std::vector vertices; - bool lighting = true; + bool shading = true; void addPlane( const glm::vec3& pos, @@ -54,14 +54,14 @@ namespace model { /// @brief Add mesh to the model /// @param texture texture name /// @return writeable Mesh - Mesh& addMesh(const std::string& texture) { + Mesh& addMesh(const std::string& texture, bool shading = true) { for (auto& mesh : meshes) { - if (mesh.texture == texture) { + if (mesh.texture == texture && mesh.shading == shading) { return mesh; } } - meshes.push_back({texture, {}}); - return meshes[meshes.size()-1]; + meshes.push_back({texture, {}, shading}); + return meshes[meshes.size() - 1]; } /// @brief Remove all empty meshes void clean(); diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 2fedd15b..483ed89e 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -341,7 +341,7 @@ void BlocksRenderer::blockCustomModel( const auto& vcoord = vertex.coord - 0.5f; glm::vec4 aoColor {1.0f, 1.0f, 1.0f, 1.0f}; - if (ao) { + if (mesh.shading && ao) { auto p = coord + vcoord.x * X + vcoord.y * Y + vcoord.z * Z + r * 0.5f + t * 0.5f + n * 0.5f; aoColor = pickSoftLight(p.x, p.y, p.z, glm::ivec3(r), glm::ivec3(t)); @@ -350,9 +350,9 @@ void BlocksRenderer::blockCustomModel( coord + vcoord.x * X + vcoord.y * Y + vcoord.z * Z, vertex.uv.x, vertex.uv.y, - glm::vec4(d, d, d, d) * aoColor, + mesh.shading ? (glm::vec4(d, d, d, d) * aoColor) : glm::vec4(1, 1, 1, d), n, - 0.0f + mesh.shading ? 0.0f : 1.0 ); indexBuffer[indexCount++] = vertexOffset++; } diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 61123232..cefe94ac 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -70,7 +70,7 @@ void ModelBatch::draw( const auto& vertexData = mesh.vertices.data(); glm::vec4 lights(1, 1, 1, 0); - if (mesh.lighting) { + if (mesh.shading) { glm::vec3 gpos = matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); gpos += lightsOffset; lights = MainBatch::sampleLight(gpos, chunks, backlight); @@ -81,7 +81,7 @@ void ModelBatch::draw( const auto vert = vertexData[i * 3 + j]; float d = 1.0f; auto norm = rotation * vert.normal; - if (mesh.lighting) { + if (mesh.shading) { d = glm::dot(norm, SUN_VECTOR); d = 0.8f + d * 0.2f; } @@ -91,7 +91,7 @@ void ModelBatch::draw( lights * d, tint, norm, - mesh.lighting ? 0.0f : 1.0f + mesh.shading ? 0.0f : 1.0f ); } } diff --git a/src/graphics/render/ModelsGenerator.cpp b/src/graphics/render/ModelsGenerator.cpp index 230c4804..f1d878f2 100644 --- a/src/graphics/render/ModelsGenerator.cpp +++ b/src/graphics/render/ModelsGenerator.cpp @@ -97,7 +97,7 @@ model::Model ModelsGenerator::fromCustom( auto model = model::Model(); for (size_t i = 0; i < modelBoxes.size(); i++) { auto& mesh = model.addMesh("blocks:"); - mesh.lighting = lighting; + mesh.shading = lighting; UVRegion boxtexfaces[6] = { get_region_for(modelTextures[i * 6 + 5], assets), get_region_for(modelTextures[i * 6 + 4], assets), @@ -132,7 +132,7 @@ model::Model ModelsGenerator::fromCustom( norm = glm::normalize(norm); auto& mesh = model.addMesh(texture); - mesh.lighting = lighting; + mesh.shading = lighting; auto reg = get_region_for(texture, assets); mesh.vertices.push_back({v0, glm::vec2(reg.u1, reg.v1), norm}); @@ -163,7 +163,7 @@ model::Model ModelsGenerator::generate( return model; } for (auto& mesh : model.meshes) { - mesh.lighting = !blockDef.shadeless; + mesh.shading = !blockDef.shadeless; switch (blockDef.model.type) { case BlockModelType::AABB: { glm::vec3 size = blockDef.hitboxes.at(0).size();