Backported from godot4: Add GridMap function to change navigation map for baked navigation regions

Adds function to change the navigation map for baked navigation regions.
Before all cells with a baked navigation mesh were locked to the default navigation map of the world resource.
- smix8
41c529a94d
This commit is contained in:
Relintai 2023-09-02 12:10:06 +02:00
parent eb8943a4f6
commit 3a7ad7b883
3 changed files with 45 additions and 12 deletions

View File

@ -173,6 +173,13 @@
[code]pos[/code] should be in the GridMap's local coordinate space.
</description>
</method>
<method name="set_navigation_map">
<return type="void" />
<param index="0" name="navigation_map" type="RID" />
<description>
Sets the [RID] of the navigation map this GridMap node should use for its cell baked navigation meshes.
</description>
</method>
</methods>
<members>
<member name="bake_navigation" type="bool" setter="set_bake_navigation" getter="is_baking_navigation" default="false">

View File

@ -225,6 +225,30 @@ bool GridMap::is_baking_navigation() {
return bake_navigation;
}
void GridMap::set_navigation_map(RID p_navigation_map) {
map_override = p_navigation_map;
for (RBMap<OctantKey, Octant *>::Element *E = octant_map.front(); E; E = E->next()) {
Octant &g = *(E->get());
for (RBMap<IndexKey, Octant::NavMesh>::Element *F = g.navmesh_ids.front(); F; F = F->next()) {
if (F->get().region.is_valid()) {
NavigationServer::get_singleton()->region_set_map(F->get().region, map_override);
}
}
}
}
RID GridMap::get_navigation_map() const {
if (map_override.is_valid()) {
return map_override;
} else if (navigation) {
return navigation->get_rid();
} else if (is_inside_tree()) {
return get_world_3d()->get_navigation_map();
}
return RID();
}
void GridMap::set_navigation_layers(uint32_t p_navigation_layers) {
navigation_layers = p_navigation_layers;
_recreate_octant_data();
@ -587,11 +611,7 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
NavigationServer::get_singleton()->region_set_navmesh(region, navmesh);
NavigationServer::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
if (is_inside_tree()) {
if (navigation) {
NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {
NavigationServer::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
}
NavigationServer::get_singleton()->region_set_map(region, get_navigation_map());
}
nm.region = region;
@ -711,11 +731,7 @@ void GridMap::_octant_enter_world(const OctantKey &p_key) {
NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
NavigationServer::get_singleton()->region_set_navmesh(region, nm);
NavigationServer::get_singleton()->region_set_transform(region, get_global_transform() * E->get().xform);
if (navigation) {
NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {
NavigationServer::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
}
NavigationServer::get_singleton()->region_set_map(region, get_navigation_map());
E->get().region = region;
}
}
@ -1004,6 +1020,9 @@ void GridMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &GridMap::set_navigation_map);
ClassDB::bind_method(D_METHOD("get_navigation_map"), &GridMap::get_navigation_map);
ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &GridMap::set_navigation_layers);
ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers);
@ -1318,6 +1337,9 @@ GridMap::GridMap() {
clip_above = true;
cell_scale = 1.0;
bake_navigation = false;
navigation_layers = 1;
navigation = nullptr;
set_notify_transform(true);
recreating_octants = false;

View File

@ -140,8 +140,9 @@ class GridMap : public Spatial {
uint32_t collision_layer;
uint32_t collision_mask;
Ref<PhysicsMaterial> physics_material;
bool bake_navigation = false;
uint32_t navigation_layers = 1;
bool bake_navigation;
RID map_override;
uint32_t navigation_layers;
Transform last_transform;
@ -242,6 +243,9 @@ public:
void set_bake_navigation(bool p_bake_navigation);
bool is_baking_navigation();
void set_navigation_map(RID p_navigation_map);
RID get_navigation_map() const;
void set_navigation_layers(uint32_t p_navigation_layers);
uint32_t get_navigation_layers() const;