summaryrefslogtreecommitdiffstats
path: root/scene/3d/navigation_region_3d.cpp
diff options
context:
space:
mode:
authorsmix8 <52464204+smix8@users.noreply.github.com>2023-05-17 16:24:09 +0200
committersmix8 <52464204+smix8@users.noreply.github.com>2023-06-15 12:04:57 +0200
commit84647ab09eba232ec7466dda9ce5fa269aad8bf3 (patch)
tree2cf8fc14d2ec6e479fc08b058e85b764f1f0a788 /scene/3d/navigation_region_3d.cpp
parent773414606079fa745d1c37fce49324ab6a09e972 (diff)
downloadredot-engine-84647ab09eba232ec7466dda9ce5fa269aad8bf3.tar.gz
Add NavigationRegion function to change navigation map
Adds NavigationRegion function to change navigation map.
Diffstat (limited to 'scene/3d/navigation_region_3d.cpp')
-rw-r--r--scene/3d/navigation_region_3d.cpp114
1 files changed, 79 insertions, 35 deletions
diff --git a/scene/3d/navigation_region_3d.cpp b/scene/3d/navigation_region_3d.cpp
index 4c2f56b7b3..194d3082df 100644
--- a/scene/3d/navigation_region_3d.cpp
+++ b/scene/3d/navigation_region_3d.cpp
@@ -46,9 +46,9 @@ void NavigationRegion3D::set_enabled(bool p_enabled) {
}
if (!enabled) {
- NavigationServer3D::get_singleton()->region_set_map(region, RID());
+ _region_enter_navigation_map();
} else {
- NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
+ _region_exit_navigation_map();
}
#ifdef DEBUG_ENABLED
@@ -169,17 +169,7 @@ RID NavigationRegion3D::get_region_rid() const {
void NavigationRegion3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
- if (enabled) {
- NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
- }
- current_global_transform = get_global_transform();
- NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
-
-#ifdef DEBUG_ENABLED
- if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
- _update_debug_mesh();
- }
-#endif // DEBUG_ENABLED
+ _region_enter_navigation_map();
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
@@ -188,31 +178,11 @@ void NavigationRegion3D::_notification(int p_what) {
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
set_physics_process_internal(false);
- if (is_inside_tree()) {
- Transform3D new_global_transform = get_global_transform();
- if (current_global_transform != new_global_transform) {
- current_global_transform = new_global_transform;
- NavigationServer3D::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
- }
- }
+ _region_update_transform();
} break;
case NOTIFICATION_EXIT_TREE: {
- NavigationServer3D::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
+ _region_exit_navigation_map();
} break;
}
}
@@ -260,6 +230,25 @@ Ref<NavigationMesh> NavigationRegion3D::get_navigation_mesh() const {
return navigation_mesh;
}
+void NavigationRegion3D::set_navigation_map(RID p_navigation_map) {
+ if (map_override == p_navigation_map) {
+ return;
+ }
+
+ map_override = p_navigation_map;
+
+ NavigationServer3D::get_singleton()->region_set_map(region, map_override);
+}
+
+RID NavigationRegion3D::get_navigation_map() const {
+ if (map_override.is_valid()) {
+ return map_override;
+ } else if (is_inside_tree()) {
+ return get_world_3d()->get_navigation_map();
+ }
+ return RID();
+}
+
struct BakeThreadsArgs {
NavigationRegion3D *nav_region = nullptr;
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
@@ -330,6 +319,9 @@ void NavigationRegion3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion3D::set_enabled);
ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion3D::is_enabled);
+ ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion3D::set_navigation_map);
+ ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion3D::get_navigation_map);
+
ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion3D::set_use_edge_connections);
ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion3D::get_use_edge_connections);
@@ -397,6 +389,58 @@ void NavigationRegion3D::_navigation_map_changed(RID p_map) {
}
#endif // DEBUG_ENABLED
+void NavigationRegion3D::_region_enter_navigation_map() {
+ if (!is_inside_tree()) {
+ return;
+ }
+
+ if (enabled) {
+ if (map_override.is_valid()) {
+ NavigationServer3D::get_singleton()->region_set_map(region, map_override);
+ } else {
+ NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
+ }
+ }
+
+ current_global_transform = get_global_transform();
+ NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
+
+#ifdef DEBUG_ENABLED
+ if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
+ _update_debug_mesh();
+ }
+#endif // DEBUG_ENABLED
+}
+
+void NavigationRegion3D::_region_exit_navigation_map() {
+ NavigationServer3D::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 NavigationRegion3D::_region_update_transform() {
+ if (!is_inside_tree()) {
+ return;
+ }
+
+ Transform3D new_global_transform = get_global_transform();
+ if (current_global_transform != new_global_transform) {
+ current_global_transform = new_global_transform;
+ NavigationServer3D::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
+ }
+}
+
NavigationRegion3D::NavigationRegion3D() {
set_notify_transform(true);