Backported form godot4: Add NavigationRegion function to change navigation map

Adds NavigationRegion function to change navigation map.
- smix8
84647ab09e
This commit is contained in:
Relintai 2023-09-04 21:17:49 +02:00
parent a222159442
commit 7001325758
4 changed files with 133 additions and 124 deletions

View File

@ -56,27 +56,6 @@ void NavigationPolygonInstance::set_enabled(bool p_enabled) {
Navigation2DServer::get_singleton()->region_set_enabled(region, enabled); Navigation2DServer::get_singleton()->region_set_enabled(region, enabled);
if (!enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, RID());
#ifdef DEBUG_ENABLED
Navigation2DServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed");
#endif
} else {
if (navigation != nullptr) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {
if (map_override.is_valid()) {
Navigation2DServer::get_singleton()->region_set_map(region, map_override);
} else {
Navigation2DServer::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
}
}
#ifdef DEBUG_ENABLED
Navigation2DServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed");
#endif
}
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint() || Navigation2DServer::get_singleton()->get_debug_navigation_enabled()) { if (Engine::get_singleton()->is_editor_hint() || Navigation2DServer::get_singleton()->get_debug_navigation_enabled()) {
update(); update();
@ -182,77 +161,26 @@ void NavigationPolygonInstance::_notification(int p_what) {
navigation = Object::cast_to<Navigation2D>(c); navigation = Object::cast_to<Navigation2D>(c);
if (navigation) { if (navigation) {
if (enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
}
break; break;
} }
c = Object::cast_to<Node2D>(c->get_parent()); c = Object::cast_to<Node2D>(c->get_parent());
} }
if (enabled) { _region_enter_navigation_map();
RID map = get_navigation_map();
Navigation2DServer::get_singleton()->region_set_map(region, map);
#ifdef DEBUG_ENABLED
Navigation2DServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed");
#endif
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map);
Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
}
}
}
current_global_transform = get_global_transform();
Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform);
} break; } break;
case NOTIFICATION_TRANSFORM_CHANGED: { case NOTIFICATION_TRANSFORM_CHANGED: {
set_physics_process_internal(true); set_physics_process_internal(true);
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
}
}
} break; } break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
set_physics_process_internal(false); set_physics_process_internal(false);
if (is_inside_tree()) {
Transform2D new_global_transform = get_global_transform();
if (current_global_transform != new_global_transform) {
current_global_transform = new_global_transform;
Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform);
update();
//TODO reenable! _region_update_transform();
//for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
// if (constrain_avoidance_obstacles[i].is_valid()) {
// Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
// }
//}
}
}
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
Navigation2DServer::get_singleton()->region_set_map(region, RID()); _region_exit_navigation_map();
navigation = nullptr; navigation = nullptr;
#ifdef DEBUG_ENABLED
if (enabled) {
Navigation2DServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed");
}
#endif
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], RID());
}
}
} break; } break;
case NOTIFICATION_DRAW: { case NOTIFICATION_DRAW: {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
@ -299,10 +227,10 @@ void NavigationPolygonInstance::set_navigation_map(RID p_navigation_map) {
} }
RID NavigationPolygonInstance::get_navigation_map() const { RID NavigationPolygonInstance::get_navigation_map() const {
if (navigation) { if (map_override.is_valid()) {
return navigation->get_rid();
} else if (map_override.is_valid()) {
return map_override; return map_override;
} else if (navigation) {
return navigation->get_rid();
} else if (is_inside_tree()) { } else if (is_inside_tree()) {
return get_world_2d()->get_navigation_map(); return get_world_2d()->get_navigation_map();
} }
@ -465,6 +393,7 @@ NavigationPolygonInstance::NavigationPolygonInstance() {
Navigation2DServer::get_singleton()->region_set_travel_cost(region, get_travel_cost()); Navigation2DServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
Navigation2DServer::get_singleton()->connect("map_changed", this, "_navigation_map_changed");
NavigationServer::get_singleton()->connect("navigation_debug_changed", this, "_navigation_debug_changed"); NavigationServer::get_singleton()->connect("navigation_debug_changed", this, "_navigation_debug_changed");
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
} }
@ -482,6 +411,7 @@ NavigationPolygonInstance::~NavigationPolygonInstance() {
constrain_avoidance_obstacles.clear(); constrain_avoidance_obstacles.clear();
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
Navigation2DServer::get_singleton()->disconnect("map_changed", this, "_navigation_map_changed");
NavigationServer::get_singleton()->disconnect("navigation_debug_changed", this, "_navigation_debug_changed"); NavigationServer::get_singleton()->disconnect("navigation_debug_changed", this, "_navigation_debug_changed");
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
} }
@ -601,6 +531,61 @@ bool NavigationPolygonInstance::get_avoidance_layer_value(int p_layer_number) co
return get_avoidance_layers() & (1 << (p_layer_number - 1)); return get_avoidance_layers() & (1 << (p_layer_number - 1));
} }
void NavigationPolygonInstance::_region_enter_navigation_map() {
if (!is_inside_tree()) {
return;
}
if (enabled) {
RID map = get_navigation_map();
Navigation2DServer::get_singleton()->region_set_map(region, map);
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], map);
}
}
}
current_global_transform = get_global_transform();
Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform);
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
}
}
update();
}
void NavigationPolygonInstance::_region_exit_navigation_map() {
Navigation2DServer::get_singleton()->region_set_map(region, RID());
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_map(constrain_avoidance_obstacles[i], RID());
}
}
}
void NavigationPolygonInstance::_region_update_transform() {
if (!is_inside_tree()) {
return;
}
Transform2D new_global_transform = get_global_transform();
if (current_global_transform != new_global_transform) {
current_global_transform = new_global_transform;
Navigation2DServer::get_singleton()->region_set_transform(region, current_global_transform);
for (uint32_t i = 0; i < constrain_avoidance_obstacles.size(); i++) {
if (constrain_avoidance_obstacles[i].is_valid()) {
Navigation2DServer::get_singleton()->obstacle_set_position(constrain_avoidance_obstacles[i], get_global_position());
}
}
}
update();
}
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
void NavigationPolygonInstance::_update_debug_mesh() { void NavigationPolygonInstance::_update_debug_mesh() {
PoolVector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices(); PoolVector<Vector2> navigation_polygon_vertices = navigation_polygon->get_vertices();

View File

@ -126,6 +126,9 @@ public:
private: private:
void _update_avoidance_constrain(); void _update_avoidance_constrain();
void _region_enter_navigation_map();
void _region_exit_navigation_map();
void _region_update_transform();
}; };
#endif // NAVIGATIONPOLYGON_H #endif // NAVIGATIONPOLYGON_H

View File

@ -163,33 +163,13 @@ void NavigationMeshInstance::_notification(int p_what) {
while (c) { while (c) {
navigation = Object::cast_to<Navigation>(c); navigation = Object::cast_to<Navigation>(c);
if (navigation) { if (navigation) {
if (enabled) {
NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
}
break; break;
} }
c = c->get_parent_spatial(); c = c->get_parent_spatial();
} }
if (enabled && navigation == nullptr) { _region_enter_navigation_map();
// did not find a valid navigation node parent, fallback to default navigation map on world resource
if (map_override.is_valid()) {
NavigationServer::get_singleton()->region_set_map(region, map_override);
} else {
NavigationServer::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
}
}
current_global_transform = get_global_transform();
NavigationServer::get_singleton()->region_set_transform(region, current_global_transform);
#ifdef DEBUG_ENABLED
if (NavigationServer::get_singleton()->get_debug_navigation_enabled()) {
_update_debug_mesh();
}
#endif // DEBUG_ENABLED
} break; } break;
case NOTIFICATION_TRANSFORM_CHANGED: { case NOTIFICATION_TRANSFORM_CHANGED: {
set_physics_process_internal(true); set_physics_process_internal(true);
@ -197,32 +177,11 @@ void NavigationMeshInstance::_notification(int p_what) {
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
set_physics_process_internal(false); set_physics_process_internal(false);
if (is_inside_tree()) {
Transform new_global_transform = get_global_transform(); _region_update_transform();
if (current_global_transform != new_global_transform) {
current_global_transform = new_global_transform;
NavigationServer::get_singleton()->region_set_transform(region, current_global_transform);
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
}
#endif // DEBUG_ENABLED
}
}
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
NavigationServer::get_singleton()->region_set_map(region, RID()); _region_exit_navigation_map();
navigation = nullptr;
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_instance, false);
}
if (debug_edge_connections_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
}
#endif // DEBUG_ENABLED
} break; } break;
} }
@ -259,6 +218,8 @@ void NavigationMeshInstance::set_navigation_map(RID p_navigation_map) {
RID NavigationMeshInstance::get_navigation_map() const { RID NavigationMeshInstance::get_navigation_map() const {
if (map_override.is_valid()) { if (map_override.is_valid()) {
return map_override; return map_override;
} else if (navigation) {
return navigation->get_rid();
} else if (is_inside_tree()) { } else if (is_inside_tree()) {
return get_world_3d()->get_navigation_map(); return get_world_3d()->get_navigation_map();
} }
@ -449,6 +410,61 @@ NavigationMeshInstance::~NavigationMeshInstance() {
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
} }
void NavigationMeshInstance::_region_enter_navigation_map() {
if (!is_inside_tree()) {
return;
}
if (enabled) {
RID map = get_navigation_map();
NavigationServer::get_singleton()->region_set_map(region, map);
}
current_global_transform = get_global_transform();
NavigationServer::get_singleton()->region_set_transform(region, current_global_transform);
#ifdef DEBUG_ENABLED
if (NavigationServer::get_singleton()->get_debug_navigation_enabled()) {
_update_debug_mesh();
}
#endif // DEBUG_ENABLED
}
void NavigationMeshInstance::_region_exit_navigation_map() {
NavigationServer::get_singleton()->region_set_map(region, RID());
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_instance, false);
}
if (debug_edge_connections_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
}
#endif // DEBUG_ENABLED
}
void NavigationMeshInstance::_region_update_transform() {
if (!is_inside_tree()) {
return;
}
Transform new_global_transform = get_global_transform();
if (current_global_transform != new_global_transform) {
current_global_transform = new_global_transform;
NavigationServer::get_singleton()->region_set_transform(region, current_global_transform);
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {
RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
}
#endif // DEBUG_ENABLED
}
}
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
void NavigationMeshInstance::_update_debug_mesh() { void NavigationMeshInstance::_update_debug_mesh() {
if (Engine::get_singleton()->is_editor_hint()) { if (Engine::get_singleton()->is_editor_hint()) {

View File

@ -112,6 +112,11 @@ public:
NavigationMeshInstance(); NavigationMeshInstance();
~NavigationMeshInstance(); ~NavigationMeshInstance();
private:
void _region_enter_navigation_map();
void _region_exit_navigation_map();
void _region_update_transform();
}; };
#endif // NAVIGATION_MESH_INSTANCE_H #endif // NAVIGATION_MESH_INSTANCE_H