From 1bc2c7cb123fc22a22cfcffb171618dafbd98e3b Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Mon, 30 Jan 2023 01:50:46 +0100 Subject: [PATCH] Create default World navigation maps on demand only Changes that the default navigation maps of World resources are only created on first use. --- scene/resources/world_2d.cpp | 18 ++++++++++-------- scene/resources/world_2d.h | 2 +- scene/resources/world_3d.cpp | 17 ++++++++++------- scene/resources/world_3d.h | 2 +- servers/navigation_server_3d.cpp | 10 ++++++++-- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/scene/resources/world_2d.cpp b/scene/resources/world_2d.cpp index c35f7360b5f..2a70139bcb8 100644 --- a/scene/resources/world_2d.cpp +++ b/scene/resources/world_2d.cpp @@ -47,6 +47,13 @@ RID World2D::get_space() const { } RID World2D::get_navigation_map() const { + if (navigation_map.is_null()) { + navigation_map = NavigationServer2D::get_singleton()->map_create(); + NavigationServer2D::get_singleton()->map_set_active(navigation_map, true); + NavigationServer2D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_GET("navigation/2d/default_cell_size")); + NavigationServer2D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_GET("navigation/2d/default_edge_connection_margin")); + NavigationServer2D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_GET("navigation/2d/default_link_connection_radius")); + } return navigation_map; } @@ -77,13 +84,6 @@ World2D::World2D() { PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_DEF_BASIC("physics/2d/default_gravity_vector", Vector2(0, 1))); PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_LINEAR_DAMP, GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/default_linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), 0.1)); PhysicsServer2D::get_singleton()->area_set_param(space, PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/default_angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), 1.0)); - - // Create and configure the navigation_map to be more friendly with pixels than meters. - navigation_map = NavigationServer2D::get_singleton()->map_create(); - NavigationServer2D::get_singleton()->map_set_active(navigation_map, true); - NavigationServer2D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_DEF("navigation/2d/default_cell_size", 1)); - NavigationServer2D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_DEF("navigation/2d/default_edge_connection_margin", 1)); - NavigationServer2D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_DEF("navigation/2d/default_link_connection_radius", 4)); } World2D::~World2D() { @@ -92,5 +92,7 @@ World2D::~World2D() { ERR_FAIL_NULL(NavigationServer2D::get_singleton()); RenderingServer::get_singleton()->free(canvas); PhysicsServer2D::get_singleton()->free(space); - NavigationServer2D::get_singleton()->free(navigation_map); + if (navigation_map.is_valid()) { + NavigationServer2D::get_singleton()->free(navigation_map); + } } diff --git a/scene/resources/world_2d.h b/scene/resources/world_2d.h index 92239ed1671..f02dddd2fe3 100644 --- a/scene/resources/world_2d.h +++ b/scene/resources/world_2d.h @@ -44,7 +44,7 @@ class World2D : public Resource { RID canvas; RID space; - RID navigation_map; + mutable RID navigation_map; HashSet viewports; diff --git a/scene/resources/world_3d.cpp b/scene/resources/world_3d.cpp index 536edd334b0..cc4d261c0da 100644 --- a/scene/resources/world_3d.cpp +++ b/scene/resources/world_3d.cpp @@ -55,6 +55,13 @@ RID World3D::get_space() const { } RID World3D::get_navigation_map() const { + if (navigation_map.is_null()) { + navigation_map = NavigationServer3D::get_singleton()->map_create(); + NavigationServer3D::get_singleton()->map_set_active(navigation_map, true); + NavigationServer3D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_GET("navigation/3d/default_cell_size")); + NavigationServer3D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_GET("navigation/3d/default_edge_connection_margin")); + NavigationServer3D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_GET("navigation/3d/default_link_connection_radius")); + } return navigation_map; } @@ -146,12 +153,6 @@ World3D::World3D() { PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_DEF_BASIC("physics/3d/default_gravity_vector", Vector3(0, -1, 0))); PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_LINEAR_DAMP, GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/3d/default_linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), 0.1)); PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/3d/default_angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), 0.1)); - - navigation_map = NavigationServer3D::get_singleton()->map_create(); - NavigationServer3D::get_singleton()->map_set_active(navigation_map, true); - NavigationServer3D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_DEF("navigation/3d/default_cell_size", 0.25)); - NavigationServer3D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_DEF("navigation/3d/default_edge_connection_margin", 0.25)); - NavigationServer3D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_DEF("navigation/3d/default_link_connection_radius", 1.0)); } World3D::~World3D() { @@ -160,5 +161,7 @@ World3D::~World3D() { ERR_FAIL_NULL(NavigationServer3D::get_singleton()); PhysicsServer3D::get_singleton()->free(space); RenderingServer::get_singleton()->free(scenario); - NavigationServer3D::get_singleton()->free(navigation_map); + if (navigation_map.is_valid()) { + NavigationServer3D::get_singleton()->free(navigation_map); + } } diff --git a/scene/resources/world_3d.h b/scene/resources/world_3d.h index 7cbc9f08c90..ad17daf4661 100644 --- a/scene/resources/world_3d.h +++ b/scene/resources/world_3d.h @@ -46,8 +46,8 @@ class World3D : public Resource { private: RID space; - RID navigation_map; RID scenario; + mutable RID navigation_map; Ref environment; Ref fallback_environment; diff --git a/servers/navigation_server_3d.cpp b/servers/navigation_server_3d.cpp index 5b95317a38f..8f2cff0e048 100644 --- a/servers/navigation_server_3d.cpp +++ b/servers/navigation_server_3d.cpp @@ -30,9 +30,7 @@ #include "navigation_server_3d.h" -#ifdef DEBUG_ENABLED #include "core/config/project_settings.h" -#endif // DEBUG_ENABLED NavigationServer3D *NavigationServer3D::singleton = nullptr; @@ -145,6 +143,14 @@ NavigationServer3D::NavigationServer3D() { ERR_FAIL_COND(singleton != nullptr); singleton = this; + GLOBAL_DEF("navigation/2d/default_cell_size", 1); + GLOBAL_DEF("navigation/2d/default_edge_connection_margin", 1); + GLOBAL_DEF("navigation/2d/default_link_connection_radius", 4); + + GLOBAL_DEF("navigation/3d/default_cell_size", 0.25); + GLOBAL_DEF("navigation/3d/default_edge_connection_margin", 0.25); + GLOBAL_DEF("navigation/3d/default_link_connection_radius", 1.0); + #ifdef DEBUG_ENABLED debug_navigation_edge_connection_color = GLOBAL_DEF("debug/shapes/navigation/edge_connection_color", Color(1.0, 0.0, 1.0, 1.0)); debug_navigation_geometry_edge_color = GLOBAL_DEF("debug/shapes/navigation/geometry_edge_color", Color(0.5, 1.0, 1.0, 1.0));