From 6d687de9b84f85c87846e6a48b81b1c495bb926b Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Thu, 24 Dec 2020 10:59:57 +0000 Subject: [PATCH] Fix BVH to world_aabb, and call update The calls to the BVH need to use the world space AABB, rather than local space for it to work. Also, update was not being called which is required to update the AABB as objects move. --- core/math/dynamic_bvh.cpp | 9 ++++++++- servers/rendering/renderer_scene_cull.cpp | 10 ++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/math/dynamic_bvh.cpp b/core/math/dynamic_bvh.cpp index 5f87f75b61e8..a53f8d1eb8e3 100644 --- a/core/math/dynamic_bvh.cpp +++ b/core/math/dynamic_bvh.cpp @@ -354,10 +354,17 @@ void DynamicBVH::_update(Node *leaf, int lookahead) { void DynamicBVH::update(const ID &p_id, const AABB &p_box) { ERR_FAIL_COND(!p_id.is_valid()); Node *leaf = p_id.node; - Node *base = _remove_leaf(leaf); + Volume volume; volume.min = p_box.position; volume.max = p_box.position + p_box.size; + + if ((leaf->volume.min == volume.min) && (leaf->volume.max == volume.max)) { + // noop + return; + } + + Node *base = _remove_leaf(leaf); if (base) { if (lkhd >= 0) { for (int i = 0; (i < lkhd) && base->parent; ++i) { diff --git a/servers/rendering/renderer_scene_cull.cpp b/servers/rendering/renderer_scene_cull.cpp index 1db678a441f3..8bb9e6b8b07f 100644 --- a/servers/rendering/renderer_scene_cull.cpp +++ b/servers/rendering/renderer_scene_cull.cpp @@ -1070,9 +1070,15 @@ void RendererSceneCull::_update_instance(Instance *p_instance) { if (!p_instance->indexer_id.is_valid()) { if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) { - p_instance->indexer_id = p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY].insert(p_instance->aabb, p_instance); + p_instance->indexer_id = p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY].insert(p_instance->transformed_aabb, p_instance); } else { - p_instance->indexer_id = p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES].insert(p_instance->aabb, p_instance); + p_instance->indexer_id = p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES].insert(p_instance->transformed_aabb, p_instance); + } + } else { + if ((1 << p_instance->base_type) & RS::INSTANCE_GEOMETRY_MASK) { + p_instance->scenario->indexers[Scenario::INDEXER_GEOMETRY].update(p_instance->indexer_id, p_instance->transformed_aabb); + } else { + p_instance->scenario->indexers[Scenario::INDEXER_VOLUMES].update(p_instance->indexer_id, p_instance->transformed_aabb); } }