upgrade entities frustum culling

This commit is contained in:
MihailRis 2026-10-31 23:41:40 +03:00
parent 414a0947ab
commit c21871c3c9
9 changed files with 206 additions and 63 deletions

View file

@ -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<const SkeletonConfig> rigConfig) {
@ -105,6 +106,7 @@ void Skeleton::setConfig(std::shared_ptr<const SkeletonConfig> rigConfig) {
for (size_t i = 0; i < bonesCount; i++) {
flags[i].visible = true;
}
boundsDirty = true;
}
static void get_all_nodes(std::vector<Bone*>& 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 {

View file

@ -97,6 +97,10 @@ namespace rigging {
std::vector<ModelReference> 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<glm::vec4> 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());
}

View file

@ -134,6 +134,7 @@ public:
auto model = std::make_unique<Model>();
buildMeshes(*model);
model->clean();
model->calcBounds();
return model;
}
};

View file

@ -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(
"",

View file

@ -3,6 +3,7 @@
#include "Model.hpp"
#include <algorithm>
#include <limits>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/matrix_decompose.hpp>
@ -168,6 +169,25 @@ void Mesh::translate(const glm::vec3& offset) {
}
}
void Model::calcBounds() {
glm::vec3 min(std::numeric_limits<float>::max());
glm::vec3 max(std::numeric_limits<float>::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(),

View file

@ -5,6 +5,7 @@
#include <glm/glm.hpp>
#include "maths/UVRegion.hpp"
#include "maths/aabb.hpp"
namespace model {
struct Vertex {
@ -82,6 +83,7 @@ namespace model {
struct Model {
std::vector<Mesh> 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();

View file

@ -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;
}

View file

@ -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<size_t>(index) >= matrices.size()) {

View file

@ -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<Transform, Rigidbody>();
for (auto [entity, transform, rigidbody] : view.each()) {
auto view = registry->view<Transform, Rigidbody, rigging::Skeleton>();
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<EntityId, Transform, rigging::Skeleton>();
auto view = registry->view<EntityId, Transform, Rigidbody, rigging::Skeleton>();
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);
}
}