From 23daaf95a6cd7c28ad0b9f91db3359b90e2ddd3c Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 9 Jun 2023 11:29:33 +0200 Subject: [PATCH] Ported from godot4: Fix 2D navigation debug visuals ignoring half the ProjectSettings Fixes that NavigationRegion2D and TileMap debug visuals ignored more or less half the ProjectSetting. E.g. random color could not be disabled, edges did not display. https://github.com/godotengine/godot/commit/2b19c70664dafd3e5689fc612feb7f7ac17c1a0a --- modules/tile_map/tile_map.cpp | 52 +++++++++-- scene/2d/navigation_polygon_instance.cpp | 105 +++++++++++++---------- servers/navigation_2d_server.cpp | 33 +++++++ servers/navigation_2d_server.h | 12 +++ 4 files changed, 149 insertions(+), 53 deletions(-) diff --git a/modules/tile_map/tile_map.cpp b/modules/tile_map/tile_map.cpp index 1c7451c74..03ad3dc60 100644 --- a/modules/tile_map/tile_map.cpp +++ b/modules/tile_map/tile_map.cpp @@ -374,7 +374,14 @@ void TileMap::update_dirty_quadrants() { SceneTree *st = SceneTree::get_singleton(); Color debug_collision_color; - Color debug_navigation_color = Color(0.5, 1.0, 1.0, 1.0); + +#ifdef DEBUG_ENABLED + bool enabled_geometry_face_random_color = false; + bool enabled_edge_lines = false; + + Color debug_face_color; + Color debug_edge_color; +#endif // DEBUG_ENABLED bool debug_shapes = false; if (st) { @@ -392,10 +399,18 @@ void TileMap::update_dirty_quadrants() { bool debug_navigation = st && st->is_debugging_navigation_hint(); if (debug_navigation) { #ifdef DEBUG_ENABLED - debug_navigation_color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color(); + const Navigation2DServer *ns2d = Navigation2DServer::get_singleton(); + + enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color(); + enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines(); + + debug_face_color = ns2d->get_debug_navigation_geometry_face_color(); + debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color(); #endif // DEBUG_ENABLED } + RandomPCG rand; + while (dirty_quadrant_list.first()) { Quadrant &q = *dirty_quadrant_list.first()->self(); @@ -690,15 +705,27 @@ void TileMap::update_dirty_quadrants() { int vsize = navigation_polygon_vertices.size(); if (vsize > 2) { - Vector colors; - Vector vertices; - vertices.resize(vsize); - colors.resize(vsize); + Vector debug_polygon_vertices; + debug_polygon_vertices.resize(vsize); + + // Generate the polygon color, slightly randomly modified from the settings one. + Color random_variation_color = debug_face_color; + if (enabled_geometry_face_random_color) { + random_variation_color.set_hsv( + debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, + debug_face_color.get_s(), + debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2); + } + random_variation_color.a = debug_face_color.a; + + Vector debug_face_colors; + debug_face_colors.resize(vsize); + { PoolVector::Read vr = navigation_polygon_vertices.read(); for (int j = 0; j < vsize; j++) { - vertices.write[j] = vr[j]; - colors.write[j] = debug_navigation_color; + debug_polygon_vertices.write[j] = vr[j]; + debug_face_colors.write[j] = random_variation_color; } } @@ -721,7 +748,14 @@ void TileMap::update_dirty_quadrants() { _fix_cell_transform(navxform, c, npoly_ofs, s); vs->canvas_item_set_transform(debug_navigation_item, navxform); - vs->canvas_item_add_triangle_array(debug_navigation_item, indices, vertices, colors); + vs->canvas_item_add_triangle_array(debug_navigation_item, indices, debug_polygon_vertices, debug_face_colors); + + if (enabled_edge_lines) { + Vector debug_edge_colors; + debug_edge_colors.push_back(debug_edge_color); + debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline. + vs->canvas_item_add_polyline(debug_navigation_item, debug_polygon_vertices, debug_edge_colors); + } } } } diff --git a/scene/2d/navigation_polygon_instance.cpp b/scene/2d/navigation_polygon_instance.cpp index 8f1c86d2c..087d500ac 100644 --- a/scene/2d/navigation_polygon_instance.cpp +++ b/scene/2d/navigation_polygon_instance.cpp @@ -71,7 +71,7 @@ void NavigationPolygonInstance::set_enabled(bool p_enabled) { } #ifdef DEBUG_ENABLED - if (Engine::get_singleton()->is_editor_hint() || NavigationServer::get_singleton()->get_debug_enabled()) { + if (Engine::get_singleton()->is_editor_hint() || Navigation2DServer::get_singleton()->get_debug_enabled()) { update(); } #endif // DEBUG_ENABLED @@ -172,7 +172,7 @@ void NavigationPolygonInstance::_notification(int p_what) { } break; case NOTIFICATION_DRAW: { #ifdef DEBUG_ENABLED - if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || NavigationServer::get_singleton()->get_debug_enabled()) && navpoly.is_valid()) { + if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || Navigation2DServer::get_singleton()->get_debug_enabled()) && navpoly.is_valid()) { _update_debug_mesh(); _update_debug_edge_connections_mesh(); } @@ -373,7 +373,7 @@ void NavigationPolygonInstance::_update_debug_mesh() { return; } - const NavigationServer2D *ns2d = NavigationServer2D::get_singleton(); + const Navigation2DServer2D *ns2d = Navigation2DServer2D::get_singleton(); bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color(); bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines(); @@ -421,19 +421,25 @@ void NavigationPolygonInstance::_update_debug_mesh() { } */ - PoolVector verts = navpoly->get_vertices(); - if (verts.size() < 3) { + PoolVector navigation_polygon_vertices = navpoly->get_vertices(); + if (navigation_polygon_vertices.size() < 3) { return; } - Color color; - if (enabled) { - color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color(); - } else { - color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_disabled_color(); - } + const Navigation2DServer *ns2d = Navigation2DServer::get_singleton(); - Color doors_color = NavigationServer::get_singleton()->get_debug_navigation_edge_connection_color(); + bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color(); + bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines(); + bool enable_edge_connections = ns2d->get_debug_navigation_enable_edge_connections(); + + Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color(); + Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color(); + Color debug_edge_connection_color = ns2d->get_debug_navigation_edge_connection_color(); + + if (!enabled) { + debug_face_color = ns2d->get_debug_navigation_geometry_face_disabled_color(); + debug_edge_color = ns2d->get_debug_navigation_geometry_edge_disabled_color(); + } RandomPCG rand; @@ -441,44 +447,55 @@ void NavigationPolygonInstance::_update_debug_mesh() { // An array of vertices for this polygon. Vector polygon = navpoly->get_polygon(i); - Vector vertices; - vertices.resize(polygon.size()); + Vector debug_polygon_vertices; + debug_polygon_vertices.resize(polygon.size()); for (int j = 0; j < polygon.size(); j++) { - ERR_FAIL_INDEX(polygon[j], verts.size()); - vertices.write[j] = verts[polygon[j]]; + ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size()); + debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]]; } // Generate the polygon color, slightly randomly modified from the settings one. - Color random_variation_color; - random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.1, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.2); - random_variation_color.a = color.a; - Vector colors; - colors.push_back(random_variation_color); + Color random_variation_color = debug_face_color; + if (enabled_geometry_face_random_color) { + random_variation_color.set_hsv(debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_face_color.get_s(), debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2); + } + random_variation_color.a = debug_face_color.a; - RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), vertices, colors); + Vector debug_face_colors; + debug_face_colors.push_back(random_variation_color); + RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), debug_polygon_vertices, debug_face_colors); + + if (enabled_edge_lines) { + Vector debug_edge_colors; + debug_edge_colors.push_back(debug_edge_color); + debug_polygon_vertices.push_back(debug_polygon_vertices[0]); // Add first again for closing polyline. + RS::get_singleton()->canvas_item_add_polyline(get_canvas_item(), debug_polygon_vertices, debug_edge_colors); + } } - // Draw the region - Transform2D xform = get_global_transform(); - const Navigation2DServer *ns = Navigation2DServer::get_singleton(); - real_t radius = 1.0; - if (navigation != nullptr) { - radius = Navigation2DServer::get_singleton()->map_get_edge_connection_margin(navigation->get_rid()); - } else { - radius = Navigation2DServer::get_singleton()->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()); - } - radius = radius * 0.5; - for (int i = 0; i < ns->region_get_connections_count(region); i++) { - // Two main points - Vector2 a = ns->region_get_connection_pathway_start(region, i); - a = xform.affine_inverse().xform(a); - Vector2 b = ns->region_get_connection_pathway_end(region, i); - b = xform.affine_inverse().xform(b); - draw_line(a, b, doors_color); + if (enable_edge_connections) { + // Draw the region edge connections. + Transform2D xform = get_global_transform(); - // Draw a circle to illustrate the margins. - real_t angle = a.angle_to_point(b); - draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color); - draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color); + real_t radius = 1.0; + if (navigation != nullptr) { + radius = ns2d->map_get_edge_connection_margin(navigation->get_rid()) / 2.0; + } else { + radius = ns2d->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0; + } + + for (int i = 0; i < ns2d->region_get_connections_count(region); i++) { + // Two main points + Vector2 a = ns2d->region_get_connection_pathway_start(region, i); + a = xform.affine_inverse().xform(a); + Vector2 b = ns2d->region_get_connection_pathway_end(region, i); + b = xform.affine_inverse().xform(b); + draw_line(a, b, debug_edge_connection_color); + + // Draw a circle to illustrate the margins. + real_t angle = a.angle_to_point(b); + draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, debug_edge_connection_color); + draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, debug_edge_connection_color); + } } } #endif // DEBUG_ENABLED @@ -486,7 +503,7 @@ void NavigationPolygonInstance::_update_debug_mesh() { #ifdef DEBUG_ENABLED void NavigationPolygonInstance::_update_debug_edge_connections_mesh() { const Navigation2DServer *ns2d = Navigation2DServer::get_singleton(); - const NavigationServer *ns = NavigationServer::get_singleton(); + const Navigation2DServer *ns = Navigation2DServer::get_singleton(); bool enable_edge_connections = ns->get_debug_navigation_enable_edge_connections(); Color debug_edge_connection_color = ns->get_debug_navigation_edge_connection_color(); diff --git a/servers/navigation_2d_server.cpp b/servers/navigation_2d_server.cpp index fe66c5b14..269a293c6 100644 --- a/servers/navigation_2d_server.cpp +++ b/servers/navigation_2d_server.cpp @@ -166,6 +166,22 @@ Color Navigation2DServer::get_debug_navigation_geometry_face_disabled_color() co return NavigationServer::get_singleton()->get_debug_navigation_geometry_face_disabled_color(); } +void Navigation2DServer::set_debug_navigation_geometry_edge_color(const Color &p_color) { + NavigationServer::get_singleton_mut()->set_debug_navigation_geometry_edge_color(p_color); +} + +Color Navigation2DServer::get_debug_navigation_geometry_edge_color() const { + return NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_color(); +} + +void Navigation2DServer::set_debug_navigation_geometry_edge_disabled_color(const Color &p_color) { + NavigationServer::get_singleton_mut()->set_debug_navigation_geometry_edge_disabled_color(p_color); +} + +Color Navigation2DServer::get_debug_navigation_geometry_edge_disabled_color() const { + return NavigationServer::get_singleton()->get_debug_navigation_geometry_edge_disabled_color(); +} + void Navigation2DServer::set_debug_navigation_enable_edge_connections(const bool p_value) { NavigationServer::get_singleton_mut()->set_debug_navigation_enable_edge_connections(p_value); } @@ -174,6 +190,22 @@ bool Navigation2DServer::get_debug_navigation_enable_edge_connections() const { return NavigationServer::get_singleton()->get_debug_navigation_enable_edge_connections(); } +void Navigation2DServer::set_debug_navigation_enable_geometry_face_random_color(const bool p_value) { + NavigationServer::get_singleton_mut()->set_debug_navigation_enable_geometry_face_random_color(p_value); +} + +bool Navigation2DServer::get_debug_navigation_enable_geometry_face_random_color() const { + return NavigationServer::get_singleton()->get_debug_navigation_enable_geometry_face_random_color(); +} + +void Navigation2DServer::set_debug_navigation_enable_edge_lines(const bool p_value) { + NavigationServer::get_singleton_mut()->set_debug_navigation_enable_edge_lines(p_value); +} + +bool Navigation2DServer::get_debug_navigation_enable_edge_lines() const { + return NavigationServer::get_singleton()->get_debug_navigation_enable_edge_lines(); +} + void Navigation2DServer::set_debug_navigation_link_connection_color(const Color &p_color) { NavigationServer::get_singleton_mut()->set_debug_navigation_link_connection_color(p_color); } @@ -187,6 +219,7 @@ void Navigation2DServer::set_debug_navigation_link_connection_disabled_color(con Color Navigation2DServer::get_debug_navigation_link_connection_disabled_color() const { return NavigationServer::get_singleton()->get_debug_navigation_link_connection_disabled_color(); } + #endif Vector Navigation2DServerManager::navigation_servers; diff --git a/servers/navigation_2d_server.h b/servers/navigation_2d_server.h index 3f6e067ea..83c9e9650 100644 --- a/servers/navigation_2d_server.h +++ b/servers/navigation_2d_server.h @@ -251,9 +251,21 @@ public: void set_debug_navigation_geometry_face_disabled_color(const Color &p_color); Color get_debug_navigation_geometry_face_disabled_color() const; + void set_debug_navigation_geometry_edge_color(const Color &p_color); + Color get_debug_navigation_geometry_edge_color() const; + + void set_debug_navigation_geometry_edge_disabled_color(const Color &p_color); + Color get_debug_navigation_geometry_edge_disabled_color() const; + void set_debug_navigation_enable_edge_connections(const bool p_value); bool get_debug_navigation_enable_edge_connections() const; + void set_debug_navigation_enable_geometry_face_random_color(const bool p_value); + bool get_debug_navigation_enable_geometry_face_random_color() const; + + void set_debug_navigation_enable_edge_lines(const bool p_value); + bool get_debug_navigation_enable_edge_lines() const; + void set_debug_navigation_link_connection_color(const Color &p_color); Color get_debug_navigation_link_connection_color() const;