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.
2b19c70664
This commit is contained in:
Relintai 2023-06-09 11:29:33 +02:00
parent c11500d480
commit 23daaf95a6
4 changed files with 149 additions and 53 deletions

View File

@ -374,7 +374,14 @@ void TileMap::update_dirty_quadrants() {
SceneTree *st = SceneTree::get_singleton(); SceneTree *st = SceneTree::get_singleton();
Color debug_collision_color; 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; bool debug_shapes = false;
if (st) { if (st) {
@ -392,10 +399,18 @@ void TileMap::update_dirty_quadrants() {
bool debug_navigation = st && st->is_debugging_navigation_hint(); bool debug_navigation = st && st->is_debugging_navigation_hint();
if (debug_navigation) { if (debug_navigation) {
#ifdef DEBUG_ENABLED #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 #endif // DEBUG_ENABLED
} }
RandomPCG rand;
while (dirty_quadrant_list.first()) { while (dirty_quadrant_list.first()) {
Quadrant &q = *dirty_quadrant_list.first()->self(); Quadrant &q = *dirty_quadrant_list.first()->self();
@ -690,15 +705,27 @@ void TileMap::update_dirty_quadrants() {
int vsize = navigation_polygon_vertices.size(); int vsize = navigation_polygon_vertices.size();
if (vsize > 2) { if (vsize > 2) {
Vector<Color> colors; Vector<Vector2> debug_polygon_vertices;
Vector<Vector2> vertices; debug_polygon_vertices.resize(vsize);
vertices.resize(vsize);
colors.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<Color> debug_face_colors;
debug_face_colors.resize(vsize);
{ {
PoolVector<Vector2>::Read vr = navigation_polygon_vertices.read(); PoolVector<Vector2>::Read vr = navigation_polygon_vertices.read();
for (int j = 0; j < vsize; j++) { for (int j = 0; j < vsize; j++) {
vertices.write[j] = vr[j]; debug_polygon_vertices.write[j] = vr[j];
colors.write[j] = debug_navigation_color; 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); _fix_cell_transform(navxform, c, npoly_ofs, s);
vs->canvas_item_set_transform(debug_navigation_item, navxform); 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<Color> 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);
}
} }
} }
} }

View File

@ -71,7 +71,7 @@ void NavigationPolygonInstance::set_enabled(bool p_enabled) {
} }
#ifdef DEBUG_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(); update();
} }
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
@ -172,7 +172,7 @@ void NavigationPolygonInstance::_notification(int p_what) {
} break; } break;
case NOTIFICATION_DRAW: { case NOTIFICATION_DRAW: {
#ifdef DEBUG_ENABLED #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_mesh();
_update_debug_edge_connections_mesh(); _update_debug_edge_connections_mesh();
} }
@ -373,7 +373,7 @@ void NavigationPolygonInstance::_update_debug_mesh() {
return; 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_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 enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
@ -421,19 +421,25 @@ void NavigationPolygonInstance::_update_debug_mesh() {
} }
*/ */
PoolVector<Vector2> verts = navpoly->get_vertices(); PoolVector<Vector2> navigation_polygon_vertices = navpoly->get_vertices();
if (verts.size() < 3) { if (navigation_polygon_vertices.size() < 3) {
return; return;
} }
Color color; const Navigation2DServer *ns2d = Navigation2DServer::get_singleton();
if (enabled) {
color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_color();
} else {
color = NavigationServer::get_singleton()->get_debug_navigation_geometry_face_disabled_color();
}
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; RandomPCG rand;
@ -441,44 +447,55 @@ void NavigationPolygonInstance::_update_debug_mesh() {
// An array of vertices for this polygon. // An array of vertices for this polygon.
Vector<int> polygon = navpoly->get_polygon(i); Vector<int> polygon = navpoly->get_polygon(i);
Vector<Vector2> vertices; Vector<Vector2> debug_polygon_vertices;
vertices.resize(polygon.size()); debug_polygon_vertices.resize(polygon.size());
for (int j = 0; j < polygon.size(); j++) { for (int j = 0; j < polygon.size(); j++) {
ERR_FAIL_INDEX(polygon[j], verts.size()); ERR_FAIL_INDEX(polygon[j], navigation_polygon_vertices.size());
vertices.write[j] = verts[polygon[j]]; debug_polygon_vertices.write[j] = navigation_polygon_vertices[polygon[j]];
} }
// Generate the polygon color, slightly randomly modified from the settings one. // Generate the polygon color, slightly randomly modified from the settings one.
Color random_variation_color; Color random_variation_color = debug_face_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); if (enabled_geometry_face_random_color) {
random_variation_color.a = color.a; 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);
Vector<Color> colors; }
colors.push_back(random_variation_color); random_variation_color.a = debug_face_color.a;
RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), vertices, colors); Vector<Color> 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<Color> 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 if (enable_edge_connections) {
Transform2D xform = get_global_transform(); // Draw the region edge connections.
const Navigation2DServer *ns = Navigation2DServer::get_singleton(); Transform2D xform = get_global_transform();
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);
// Draw a circle to illustrate the margins. real_t radius = 1.0;
real_t angle = a.angle_to_point(b); if (navigation != nullptr) {
draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color); radius = ns2d->map_get_edge_connection_margin(navigation->get_rid()) / 2.0;
draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color); } 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 #endif // DEBUG_ENABLED
@ -486,7 +503,7 @@ void NavigationPolygonInstance::_update_debug_mesh() {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
void NavigationPolygonInstance::_update_debug_edge_connections_mesh() { void NavigationPolygonInstance::_update_debug_edge_connections_mesh() {
const Navigation2DServer *ns2d = Navigation2DServer::get_singleton(); 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(); bool enable_edge_connections = ns->get_debug_navigation_enable_edge_connections();
Color debug_edge_connection_color = ns->get_debug_navigation_edge_connection_color(); Color debug_edge_connection_color = ns->get_debug_navigation_edge_connection_color();

View File

@ -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(); 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) { void Navigation2DServer::set_debug_navigation_enable_edge_connections(const bool p_value) {
NavigationServer::get_singleton_mut()->set_debug_navigation_enable_edge_connections(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(); 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) { void Navigation2DServer::set_debug_navigation_link_connection_color(const Color &p_color) {
NavigationServer::get_singleton_mut()->set_debug_navigation_link_connection_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 { Color Navigation2DServer::get_debug_navigation_link_connection_disabled_color() const {
return NavigationServer::get_singleton()->get_debug_navigation_link_connection_disabled_color(); return NavigationServer::get_singleton()->get_debug_navigation_link_connection_disabled_color();
} }
#endif #endif
Vector<Navigation2DServerManager::ClassInfo> Navigation2DServerManager::navigation_servers; Vector<Navigation2DServerManager::ClassInfo> Navigation2DServerManager::navigation_servers;

View File

@ -251,9 +251,21 @@ public:
void set_debug_navigation_geometry_face_disabled_color(const Color &p_color); void set_debug_navigation_geometry_face_disabled_color(const Color &p_color);
Color get_debug_navigation_geometry_face_disabled_color() const; 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); void set_debug_navigation_enable_edge_connections(const bool p_value);
bool get_debug_navigation_enable_edge_connections() const; 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); void set_debug_navigation_link_connection_color(const Color &p_color);
Color get_debug_navigation_link_connection_color() const; Color get_debug_navigation_link_connection_color() const;