From c21871c3c94a99dc3164f5b6762ebbb55db9f6a4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 31 Oct 2026 23:41:40 +0300 Subject: [PATCH] upgrade entities frustum culling --- src/animation/rigging.cpp | 129 +++++++++++++----- src/animation/rigging.hpp | 22 +++ src/coders/obj.cpp | 1 + src/coders/vcm.cpp | 1 + src/graphics/commons/Model.cpp | 20 +++ src/graphics/commons/Model.hpp | 5 + src/graphics/render/ModelsGenerator.cpp | 1 + .../scripting/lua/libs/lib__skeleton.cpp | 5 + src/objects/Entities.cpp | 85 ++++++++---- 9 files changed, 206 insertions(+), 63 deletions(-) diff --git a/src/animation/rigging.cpp b/src/animation/rigging.cpp index ee1448255..7d5397c76 100644 --- a/src/animation/rigging.cpp +++ b/src/animation/rigging.cpp @@ -88,6 +88,7 @@ void Skeleton::deserialize(const dv::value& root) { dv::get_mat(posearr[i], pose.matrices[i]); } } + boundsDirty = true; } void Skeleton::setConfig(std::shared_ptr rigConfig) { @@ -105,6 +106,7 @@ void Skeleton::setConfig(std::shared_ptr rigConfig) { for (size_t i = 0; i < bonesCount; i++) { flags[i].visible = true; } + boundsDirty = true; } static void get_all_nodes(std::vector& nodes, Bone* node) { @@ -173,6 +175,76 @@ void SkeletonConfig::update( update(0, skeleton, root.get(), build_matrix(rot, pos, scale)); } +static float max_component(const glm::vec3& v) { + return glm::max(glm::abs(v.x), glm::max(glm::abs(v.y), glm::abs(v.z))); +} + +static float farthest_corner_distance( + const glm::mat4& matrix, const AABB& aabb, const glm::vec3& origin +) { + glm::vec3 center = glm::vec3(matrix * glm::vec4(aabb.center(), 1.0f)); + glm::vec3 halfSize = aabb.size() * 0.5f; + glm::vec3 extent = glm::abs(glm::vec3(matrix[0])) * halfSize.x + + glm::abs(glm::vec3(matrix[1])) * halfSize.y + + glm::abs(glm::vec3(matrix[2])) * halfSize.z; + return glm::length(glm::abs(center - origin) + extent); +} + +void SkeletonConfig::processModels( + const Assets& assets, + Skeleton& skeleton, + const glm::vec3& position, + const glm::vec3& scale, + ModelBatch* batch, + ModelLightingMode lightingMode, + bool translucent +) const { + float radius = 0.0f; + bool pending = false; + for (size_t i = 0; i < nodes.size(); i++) { + auto* node = nodes[i]; + if (!skeleton.flags[i].visible) { + continue; + } + node->model.refresh(assets); + auto model = node->model.model.lock(); // TODO: cache model pointer during frame + auto& modelOverride = skeleton.modelOverrides.at(i); + if (modelOverride.updateFlag) { + modelOverride.refresh(assets); + } + if (auto foundOverride = modelOverride.model.lock()) { + model = std::move(foundOverride); + } + if (model == nullptr) { + if (!node->model.name.empty()) { + node->model.updateFlag = true; + pending = true; + } + continue; + } + const auto& matrix = skeleton.calculated.matrices[i]; + radius = glm::max( + radius, farthest_corner_distance(matrix, model->getBounds(), position) + ); + if (batch == nullptr) { + continue; + } + batch->draw( + matrix, + skeleton.tint * skeleton.boneTints[i], + lightingMode == ModelLightingMode::SOLID + ? position - glm::vec3(matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)) + : glm::vec3(), + model, + &skeleton.textures, + translucent + ); + } + float maxScale = max_component(scale); + skeleton.boundsRadius = maxScale > 0.0f ? radius / maxScale : radius; + skeleton.boundsDirty = pending; +} + void SkeletonConfig::render( const Assets& assets, ModelBatch& batch, @@ -191,40 +263,31 @@ void SkeletonConfig::render( if (!skeleton.visible) { return; } - for (size_t i = 0; i < nodes.size(); i++) { - auto* node = nodes[i]; - if (!skeleton.flags[i].visible) { - continue; - } - node->model.refresh(assets); - auto model = node->model.model.lock(); // TODO: cache model pointer during frame - auto& modelOverride = skeleton.modelOverrides.at(i); - if (modelOverride.updateFlag) { - modelOverride.refresh(assets); - } - if (auto foundOverride = modelOverride.model.lock()) { - model = std::move(foundOverride); - } - if (model == nullptr) { - if (!node->model.name.empty()) { - node->model.updateFlag = true; - } - continue; - } - batch.draw( - skeleton.calculated.matrices[i], - skeleton.tint * skeleton.boneTints[i], - lightingMode == ModelLightingMode::SOLID - ? position - glm::vec3( - skeleton.calculated.matrices[i] * - glm::vec4(0.0f, 0.0f, 0.0f, 1.0f) - ) - : glm::vec3(), - model, - &skeleton.textures, - translucent - ); + processModels( + assets, skeleton, position, scale, &batch, lightingMode, translucent + ); +} + +void SkeletonConfig::updateBounds( + const Assets& assets, + Skeleton& skeleton, + const glm::mat3& rotation, + const glm::vec3& position, + const glm::vec3& scale +) const { + if (skeleton.config->root == nullptr) { + return; } + update(skeleton, rotation, position, scale); + processModels( + assets, + skeleton, + position, + scale, + nullptr, + ModelLightingMode::SOLID, + false + ); } const Bone* SkeletonConfig::find(std::string_view str) const { diff --git a/src/animation/rigging.hpp b/src/animation/rigging.hpp index 2423f5c25..a35650a70 100644 --- a/src/animation/rigging.hpp +++ b/src/animation/rigging.hpp @@ -97,6 +97,10 @@ namespace rigging { std::vector modelOverrides; bool visible; bool culled = false; + /// @brief Bounding radius of visible models around entity position, + /// divided by the largest scale component + float boundsRadius = 0.0f; + bool boundsDirty = true; glm::vec4 tint {1.0f, 1.0f, 1.0f, 1.0f}; std::vector boneTints; @@ -127,6 +131,16 @@ namespace rigging { size_t update( size_t index, Skeleton& skeleton, Bone* node, const glm::mat4& matrix ) const; + + void processModels( + const Assets& assets, + Skeleton& skeleton, + const glm::vec3& position, + const glm::vec3& scale, + ModelBatch* batch, + ModelLightingMode lightingMode, + bool translucent + ) const; public: SkeletonConfig( const std::string& name, @@ -152,6 +166,14 @@ namespace rigging { bool translucent = false ) const; + void updateBounds( + const Assets& assets, + Skeleton& skeleton, + const glm::mat3& rotation, + const glm::vec3& position, + const glm::vec3& scale + ) const; + Skeleton instance() const { return Skeleton(shared_from_this()); } diff --git a/src/coders/obj.cpp b/src/coders/obj.cpp index 6da92a66c..1ea3b3834 100644 --- a/src/coders/obj.cpp +++ b/src/coders/obj.cpp @@ -134,6 +134,7 @@ public: auto model = std::make_unique(); buildMeshes(*model); model->clean(); + model->calcBounds(); return model; } }; diff --git a/src/coders/vcm.cpp b/src/coders/vcm.cpp index a1ca758de..c4323f4ba 100644 --- a/src/coders/vcm.cpp +++ b/src/coders/vcm.cpp @@ -469,6 +469,7 @@ static VcmModel load_model(const xmlelement& root) { perform_element(*elem, builder, ctx); } + model.calcBounds(); vcmModel.parts["root"] = std::move(model); vcmModel.skeleton = SkeletonConfig( "", diff --git a/src/graphics/commons/Model.cpp b/src/graphics/commons/Model.cpp index 8c2194775..eda9bc748 100644 --- a/src/graphics/commons/Model.cpp +++ b/src/graphics/commons/Model.cpp @@ -3,6 +3,7 @@ #include "Model.hpp" #include +#include #include #include @@ -168,6 +169,25 @@ void Mesh::translate(const glm::vec3& offset) { } } +void Model::calcBounds() { + glm::vec3 min(std::numeric_limits::max()); + glm::vec3 max(std::numeric_limits::lowest()); + for (const auto& mesh : meshes) { + for (const auto& vertex : mesh.vertices) { + min = glm::min(min, vertex.coord); + max = glm::max(max, vertex.coord); + } + } + if (min.x > max.x) { + min = max = glm::vec3(0.0f); + } + bounds = AABB(min, max); +} + +const AABB& Model::getBounds() const { + return bounds; +} + void Model::clean() { meshes.erase( std::remove_if(meshes.begin(), meshes.end(), diff --git a/src/graphics/commons/Model.hpp b/src/graphics/commons/Model.hpp index e42928fba..c2fdf2daf 100644 --- a/src/graphics/commons/Model.hpp +++ b/src/graphics/commons/Model.hpp @@ -5,6 +5,7 @@ #include #include "maths/UVRegion.hpp" +#include "maths/aabb.hpp" namespace model { struct Vertex { @@ -82,6 +83,7 @@ namespace model { struct Model { std::vector meshes; + AABB bounds {glm::vec3(0.0f), glm::vec3(0.0f)}; /// @brief Add mesh to the model /// @param texture texture name @@ -96,6 +98,9 @@ namespace model { return meshes[meshes.size() - 1]; } + void calcBounds(); + const AABB& getBounds() const; + /// @brief Remove all empty meshes void clean(); diff --git a/src/graphics/render/ModelsGenerator.cpp b/src/graphics/render/ModelsGenerator.cpp index 119d216c6..b1848e238 100644 --- a/src/graphics/render/ModelsGenerator.cpp +++ b/src/graphics/render/ModelsGenerator.cpp @@ -187,6 +187,7 @@ model::Model ModelsGenerator::fromCustom( mesh.vertices.push_back({v2, glm::vec2(reg.u2, reg.v2), norm}); mesh.vertices.push_back({v3, glm::vec2(reg.u1, reg.v2), norm}); } + model.calcBounds(); return model; } diff --git a/src/logic/scripting/lua/libs/lib__skeleton.cpp b/src/logic/scripting/lua/libs/lib__skeleton.cpp index 2c78d1a8a..d87a3bb9e 100644 --- a/src/logic/scripting/lua/libs/lib__skeleton.cpp +++ b/src/logic/scripting/lua/libs/lib__skeleton.cpp @@ -62,6 +62,7 @@ static int l_set_model(lua::State* L) { } else { modelOverride = {lua::require_string(L, 3), {}, true}; } + skeleton->boundsDirty = true; } return 0; } @@ -85,6 +86,7 @@ static int l_set_matrix(lua::State* L) { } auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); skeleton->pose.matrices[index] = lua::tomat4(L, 3); + skeleton->boundsDirty = true; } return 0; } @@ -104,6 +106,7 @@ static int l_reset_pose(lua::State* L) { if (auto skeleton = get_skeleton(L)) { auto& pose = skeleton->pose; std::fill(pose.matrices.begin(), pose.matrices.end(), glm::mat4(1.0f)); + skeleton->boundsDirty = true; } return 0; } @@ -157,6 +160,7 @@ static int l_set_visible(lua::State* L) { } else { skeleton->visible = lua::toboolean(L, 2); } + skeleton->boundsDirty = true; } return 0; } @@ -345,6 +349,7 @@ static int l_apply_track(lua::State* L) { return index > 0 && index <= MAX_TRACK_VALUES ? values[index] : fallback; }; auto& matrices = skeleton->pose.matrices; + skeleton->boundsDirty = true; for (size_t i = 0; i < track.bones.size(); i++) { int index = track.indices[i]; if (index < 0 || static_cast(index) >= matrices.size()) { diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index f1a058b14..b0027fb68 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -427,24 +427,49 @@ static void debug_render_skeleton( } } +static glm::vec3 calc_extent( + const Transform& transform, + const Rigidbody& rigidbody, + const rigging::Skeleton& skeleton +) { + const auto& size = transform.size; + float scale = glm::max( + glm::abs(size.x), glm::max(glm::abs(size.y), glm::abs(size.z)) + ); + return glm::max( + rigidbody.hitbox.getHalfSize(), glm::vec3(skeleton.boundsRadius * scale) + ); +} + +static bool check_frustum( + const Frustum& frustum, + const Transform& transform, + const Rigidbody& rigidbody, + const rigging::Skeleton& skeleton +) { + glm::vec3 ext = calc_extent(transform, rigidbody, skeleton); + return frustum.isBoxVisible(transform.pos - ext, transform.pos + ext); +} + void Entities::renderDebug( LineBatch& batch, const Frustum* frustum, const DrawContext& pctx, entityid_t fpsEntity ) { { auto ctx = pctx.sub(&batch); ctx.setLineWidth(1); - auto view = registry->view(); - for (auto [entity, transform, rigidbody] : view.each()) { + auto view = registry->view(); + for (auto [entity, transform, rigidbody, skeleton] : view.each()) { const auto& hitbox = rigidbody.hitbox; - const auto& pos = transform.pos; - const auto& size = transform.size; - if (frustum && !frustum->isBoxVisible(pos - size, pos + size)) { + if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) { continue; } batch.box( hitbox.position, hitbox.getHalfSize() * 2.0f, glm::vec4(1.0f) ); - + glm::vec3 ext = calc_extent(transform, rigidbody, skeleton); + batch.box( + transform.pos, ext * 2.0f, glm::vec4(0.0f, 0.0f, 1.0f, 1.0f) + ); for (auto& sensor : rigidbody.sensors) { if (sensor.type != SensorType::AABB) continue; batch.box( @@ -456,12 +481,12 @@ void Entities::renderDebug( } } { - auto view = registry->view(); + auto view = registry->view(); auto ctx = pctx.sub(&batch); ctx.setDepthTest(false); ctx.setDepthMask(false); ctx.setLineWidth(2); - for (auto [entity, eid, transform, skeleton] : view.each()) { + for (auto [entity, eid, transform, rigidbody, skeleton] : view.each()) { if (eid.uid == fpsEntity) { continue; } @@ -469,9 +494,7 @@ void Entities::renderDebug( if (config == nullptr) { continue; } - const auto& pos = transform.pos; - const auto& size = transform.size; - if (frustum && !frustum->isBoxVisible(pos - size, pos + size)) { + if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) { continue; } auto bone = config->getRoot(); @@ -494,25 +517,29 @@ void Entities::render( const auto& def = eid.def; const auto& pos = transform.pos; const auto& size = transform.size; - const auto& halfSize = rigidbody.hitbox.getHalfSize(); - // todo: more correct frustum culling - if (frustum && !frustum->isBoxVisible(pos - halfSize, pos + halfSize)) { + const auto& rigConfig = skeleton.config; + if (rigConfig == nullptr) { continue; } - - const auto& rigConfig = skeleton.config; - if (rigConfig) { - rigConfig->render( - assets, - batch, - skeleton, - def.lightingMode, - transform.rot, - pos, - size, - def.translucent - ); + if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) { + if (!skeleton.boundsDirty || !skeleton.visible) { + continue; + } + rigConfig->updateBounds(assets, skeleton, transform.rot, pos, size); + if (!check_frustum(*frustum, transform, rigidbody, skeleton)) { + continue; + } } + rigConfig->render( + assets, + batch, + skeleton, + def.lightingMode, + transform.rot, + pos, + size, + def.translucent + ); } } @@ -523,9 +550,7 @@ void Entities::updateCulling(const Frustum* frustum, entityid_t fpsEntity) { skeleton.culled = false; continue; } - const auto& pos = transform.pos; - const auto& halfSize = rigidbody.hitbox.getHalfSize(); - skeleton.culled = !frustum->isBoxVisible(pos - halfSize, pos + halfSize); + skeleton.culled = !check_frustum(*frustum, transform, rigidbody, skeleton); } }