Skip to content

Commit a3b1272

Browse files
committed
Merge pull request #118867 from YeldhamDev/gridmap_navmesh_optimize
Improve `GridMap` collider navmesh baking performance when using bake bounds
2 parents e211515 + d243f7c commit a3b1272

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

modules/gridmap/grid_map.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ Array GridMap::get_collision_shapes() const {
263263

264264
return shapes;
265265
}
266+
267+
RID GridMap::get_physics_body_from_octant_coord(const Vector3i &p_octant_coords) const {
268+
OctantKey octantkey;
269+
octantkey.x = p_octant_coords.x;
270+
octantkey.y = p_octant_coords.y;
271+
octantkey.z = p_octant_coords.z;
272+
273+
const HashMap<OctantKey, Octant *, OctantKey>::ConstIterator octant_kv = octant_map.find(octantkey);
274+
275+
if (!octant_kv) {
276+
return RID();
277+
}
278+
279+
Octant *g = octant_kv->value;
280+
RID body_rid = g->static_body;
281+
282+
return body_rid;
283+
}
266284
#endif // PHYSICS_3D_DISABLED
267285

268286
void GridMap::set_bake_navigation(bool p_bake_navigation) {
@@ -1731,7 +1749,34 @@ void GridMap::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigat
17311749
}
17321750
#ifndef PHYSICS_3D_DISABLED
17331751
else if ((parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS || parsed_geometry_type == NavigationMesh::PARSED_GEOMETRY_BOTH) && (gridmap->get_collision_layer() & parsed_collision_mask)) {
1734-
Array shapes = gridmap->get_collision_shapes();
1752+
Array shapes;
1753+
1754+
// If we have a baking AABB set we can skip wasting time on parsing the entire GridMap.
1755+
AABB baking_aabb = p_navigation_mesh->get_filter_baking_aabb();
1756+
if (baking_aabb.has_volume()) {
1757+
Vector3 baking_aabb_offset = p_navigation_mesh->get_filter_baking_aabb_offset();
1758+
baking_aabb.position = baking_aabb.position + baking_aabb_offset;
1759+
1760+
const TypedArray<Vector3i> octant_coords = gridmap->get_used_octants_in_bounds(baking_aabb);
1761+
for (const Vector3i octant_coord : octant_coords) {
1762+
RID body = gridmap->get_physics_body_from_octant_coord(octant_coord);
1763+
if (body.is_null()) {
1764+
continue;
1765+
}
1766+
const Transform3D body_xform = PhysicsServer3D::get_singleton()->body_get_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM);
1767+
int nshapes = PhysicsServer3D::get_singleton()->body_get_shape_count(body);
1768+
for (int i = 0; i < nshapes; i++) {
1769+
RID shape = PhysicsServer3D::get_singleton()->body_get_shape(body, i);
1770+
Transform3D shape_xform = PhysicsServer3D::get_singleton()->body_get_shape_transform(body, i);
1771+
Transform3D xform = body_xform * shape_xform;
1772+
shapes.push_back(xform);
1773+
shapes.push_back(shape);
1774+
}
1775+
}
1776+
} else {
1777+
shapes = gridmap->get_collision_shapes();
1778+
}
1779+
17351780
for (int i = 0; i < shapes.size(); i += 2) {
17361781
RID shape = shapes[i + 1];
17371782
PhysicsServer3D::ShapeType type = PhysicsServer3D::get_singleton()->shape_get_type(shape);

modules/gridmap/grid_map.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ class GridMap : public Node3D {
327327

328328
Vector3i get_octant_coords_from_cell_coords(const Vector3i &p_cell_coords) const;
329329

330+
#ifndef PHYSICS_3D_DISABLED
331+
RID get_physics_body_from_octant_coord(const Vector3i &p_octant_coords) const;
332+
#endif
333+
330334
Array get_meshes() const;
331335

332336
void clear_baked_meshes();

0 commit comments

Comments
 (0)