Ported from godot4: Add NavigationAgent Path Debug Visualization

Adds path debug visuals for NavigationAgent2D, NavigationAgent3D and NavigationServer.
- smix8
0ab764e84b
This commit is contained in:
Relintai 2023-06-09 14:17:54 +02:00
parent 11c84049fa
commit 268645ff0b
12 changed files with 644 additions and 2 deletions

View File

@ -520,6 +520,11 @@
Emitted when a navigation map is updated, when a region moves or is modified.
</description>
</signal>
<signal name="navigation_debug_changed">
<description>
Emitted when navigation debug settings are changed. Only available in debug builds.
</description>
</signal>
</signals>
<constants>
</constants>

View File

@ -106,6 +106,18 @@
<member name="avoidance_enabled" type="bool" setter="set_avoidance_enabled" getter="get_avoidance_enabled" default="false">
If [code]true[/code] the agent is registered for an RVO avoidance callback on the [NavigationServer]. When [method set_velocity] is used and the processing is completed a [code]safe_velocity[/code] Vector3 is received with a signal connection to [signal velocity_computed]. Avoidance processing with many registered agents has a significant performance cost and should only be enabled on agents that currently require it.
</member>
<member name="debug_enabled" type="bool" setter="set_debug_enabled" getter="get_debug_enabled" default="false">
If [code]true[/code] shows debug visuals for this agent.
</member>
<member name="debug_path_custom_color" type="Color" setter="set_debug_path_custom_color" getter="get_debug_path_custom_color" default="Color(1, 1, 1, 1)">
If [member debug_use_custom] is [code]true[/code] uses this color for this agent instead of global color.
</member>
<member name="debug_path_custom_point_size" type="float" setter="set_debug_path_custom_point_size" getter="get_debug_path_custom_point_size" default="4.0">
If [member debug_use_custom] is [code]true[/code] uses this rasterized point size for rendering path points for this agent instead of global point size.
</member>
<member name="debug_use_custom" type="bool" setter="set_debug_use_custom" getter="get_debug_use_custom" default="false">
If [code]true[/code] uses the defined [member debug_path_custom_color] for this agent instead of global color.
</member>
<member name="ignore_y" type="bool" setter="set_ignore_y" getter="get_ignore_y" default="false">
Ignores collisions on the Y axis. Must be [code]true[/code] to move on a horizontal plane.
</member>

View File

@ -103,6 +103,21 @@
<member name="avoidance_enabled" type="bool" setter="set_avoidance_enabled" getter="get_avoidance_enabled" default="false">
If [code]true[/code] the agent is registered for an RVO avoidance callback on the [Navigation2DServer]. When [method set_velocity] is used and the processing is completed a [code]safe_velocity[/code] Vector2 is received with a signal connection to [signal velocity_computed]. Avoidance processing with many registered agents has a significant performance cost and should only be enabled on agents that currently require it.
</member>
<member name="debug_enabled" type="bool" setter="set_debug_enabled" getter="get_debug_enabled" default="false">
If [code]true[/code] shows debug visuals for this agent.
</member>
<member name="debug_path_custom_color" type="Color" setter="set_debug_path_custom_color" getter="get_debug_path_custom_color" default="Color(1, 1, 1, 1)">
If [member debug_use_custom] is [code]true[/code] uses this color for this agent instead of global color.
</member>
<member name="debug_path_custom_line_width" type="float" setter="set_debug_path_custom_line_width" getter="get_debug_path_custom_line_width" default="1.0">
If [member debug_use_custom] is [code]true[/code] uses this line width for rendering paths for this agent instead of global line width.
</member>
<member name="debug_path_custom_point_size" type="float" setter="set_debug_path_custom_point_size" getter="get_debug_path_custom_point_size" default="4.0">
If [member debug_use_custom] is [code]true[/code] uses this rasterized point size for rendering path points for this agent instead of global point size.
</member>
<member name="debug_use_custom" type="bool" setter="set_debug_use_custom" getter="get_debug_use_custom" default="false">
If [code]true[/code] uses the defined [member debug_path_custom_color] for this agent instead of global color.
</member>
<member name="max_neighbors" type="int" setter="set_max_neighbors" getter="get_max_neighbors" default="0">
The maximum number of neighbors for the agent to consider.
</member>

View File

@ -529,9 +529,21 @@
<member name="debug/shapes/collision/shape_color" type="Color" setter="" getter="" default="Color( 0, 0.6, 0.7, 0.42 )">
Color of the collision shapes, visible when "Visible Collision Shapes" is enabled in the Debug menu.
</member>
<member name="debug/shapes/navigation/agent_path_color" type="Color" setter="" getter="" default="Color(1, 0, 0, 1)">
Color to display enabled navigation agent paths when an agent has debug enabled.
</member>
<member name="debug/shapes/navigation/agent_path_point_size" type="float" setter="" getter="" default="4.0">
Rasterized size (pixel) used to render navigation agent path points when an agent has debug enabled.
</member>
<member name="debug/shapes/navigation/edge_connection_color" type="Color" setter="" getter="" default="Color(1, 0, 1, 1)">
Color to display edge connections between navigation regions, visible when "Visible Navigation" is enabled in the Debug menu.
</member>
<member name="debug/shapes/navigation/enable_agent_paths" type="bool" setter="" getter="" default="true">
If enabled, displays navigation agent paths when an agent has debug enabled.
</member>
<member name="debug/shapes/navigation/enable_agent_paths_xray" type="bool" setter="" getter="" default="true">
If enabled, displays navigation agent paths through geometry when an agent has debug enabled.
</member>
<member name="debug/shapes/navigation/enable_edge_connections" type="bool" setter="" getter="" default="true">
If enabled, displays edge connections between navigation regions when "Visible Navigation" is enabled in the Debug menu.
</member>

View File

@ -105,6 +105,28 @@ void NavigationAgent2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_horizon", PROPERTY_HINT_RANGE, "0.1,10000,0.01"), "set_time_horizon", "get_time_horizon");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_speed", PROPERTY_HINT_RANGE, "0.1,100000,0.01"), "set_max_speed", "get_max_speed");
#ifdef DEBUG_ENABLED
ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent2D::set_debug_enabled);
ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent2D::get_debug_enabled);
ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent2D::set_debug_use_custom);
ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent2D::get_debug_use_custom);
ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent2D::set_debug_path_custom_color);
ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent2D::get_debug_path_custom_color);
ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent2D::set_debug_path_custom_point_size);
ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent2D::get_debug_path_custom_point_size);
ClassDB::bind_method(D_METHOD("set_debug_path_custom_line_width", "line_width"), &NavigationAgent2D::set_debug_path_custom_line_width);
ClassDB::bind_method(D_METHOD("get_debug_path_custom_line_width"), &NavigationAgent2D::get_debug_path_custom_line_width);
ClassDB::bind_method(D_METHOD("_navigation_debug_changed"), &NavigationAgent2D::_navigation_debug_changed);
ADD_GROUP("Debug", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "debug_path_custom_point_size", PROPERTY_HINT_RANGE, "1,50,1,suffix:px"), "set_debug_path_custom_point_size", "get_debug_path_custom_point_size");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "debug_path_custom_line_width", PROPERTY_HINT_RANGE, "1,50,1,suffix:px"), "set_debug_path_custom_line_width", "get_debug_path_custom_line_width");
#endif // DEBUG_ENABLED
ADD_SIGNAL(MethodInfo("path_changed"));
ADD_SIGNAL(MethodInfo("target_reached"));
ADD_SIGNAL(MethodInfo("navigation_finished"));
@ -134,6 +156,12 @@ void NavigationAgent2D::_notification(int p_what) {
// cannot use READY as ready does not get called if Node is readded to SceneTree
set_agent_parent(get_parent());
set_physics_process_internal(true);
#ifdef DEBUG_ENABLED
if (Navigation2DServer::get_singleton()->get_debug_enabled()) {
debug_path_dirty = true;
}
#endif // DEBUG_ENABLED
} break;
case NOTIFICATION_PARENTED: {
if (is_inside_tree() && (get_parent() != agent_parent)) {
@ -172,6 +200,12 @@ void NavigationAgent2D::_notification(int p_what) {
set_agent_parent(nullptr);
set_navigation(nullptr);
set_physics_process_internal(false);
#ifdef DEBUG_ENABLED
if (debug_path_instance.is_valid()) {
RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, false);
}
#endif // DEBUG_ENABLED
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (agent_parent) {
@ -182,6 +216,12 @@ void NavigationAgent2D::_notification(int p_what) {
}
_check_distance_to_target();
}
#ifdef DEBUG_ENABLED
if (debug_path_dirty) {
_update_debug_path();
}
#endif // DEBUG_ENABLED
} break;
}
}
@ -211,11 +251,31 @@ NavigationAgent2D::NavigationAgent2D() {
nav_path_index = 0;
update_frame_id = 0;
#ifdef DEBUG_ENABLED
debug_enabled = false;
debug_path_dirty = true;
debug_path_custom_point_size = 4.0;
debug_path_custom_line_width = 1.0;
debug_use_custom = false;
debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
Navigation2DServer::get_singleton_mut()->connect("navigation_debug_changed", this, "_navigation_debug_changed");
#endif // DEBUG_ENABLED
}
NavigationAgent2D::~NavigationAgent2D() {
Navigation2DServer::get_singleton()->free(agent);
agent = RID(); // Pointless
#ifdef DEBUG_ENABLED
Navigation2DServer::get_singleton_mut()->disconnect("navigation_debug_changed", this, "_navigation_debug_changed");
ERR_FAIL_NULL(RenderingServer::get_singleton());
if (debug_path_instance.is_valid()) {
RenderingServer::get_singleton()->free(debug_path_instance);
}
#endif // DEBUG_ENABLED
}
void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) {
@ -459,6 +519,10 @@ void NavigationAgent2D::update_navigation() {
navigation_path = Navigation2DServer::get_singleton()->map_get_path(agent_parent->get_world_2d()->get_navigation_map(), o, target_position, true, navigation_layers);
}
#ifdef DEBUG_ENABLED
debug_path_dirty = true;
#endif // DEBUG_ENABLED
navigation_finished = false;
nav_path_index = 0;
emit_signal("path_changed");
@ -499,3 +563,111 @@ void NavigationAgent2D::_check_distance_to_target() {
}
}
}
////////DEBUG////////////////////////////////////////////////////////////
#ifdef DEBUG_ENABLED
void NavigationAgent2D::set_debug_enabled(bool p_enabled) {
debug_enabled = p_enabled;
debug_path_dirty = true;
}
bool NavigationAgent2D::get_debug_enabled() const {
return debug_enabled;
}
void NavigationAgent2D::set_debug_use_custom(bool p_enabled) {
debug_use_custom = p_enabled;
debug_path_dirty = true;
}
bool NavigationAgent2D::get_debug_use_custom() const {
return debug_use_custom;
}
void NavigationAgent2D::set_debug_path_custom_color(Color p_color) {
debug_path_custom_color = p_color;
debug_path_dirty = true;
}
Color NavigationAgent2D::get_debug_path_custom_color() const {
return debug_path_custom_color;
}
void NavigationAgent2D::set_debug_path_custom_point_size(float p_point_size) {
debug_path_custom_point_size = MAX(0.1, p_point_size);
debug_path_dirty = true;
}
float NavigationAgent2D::get_debug_path_custom_point_size() const {
return debug_path_custom_point_size;
}
void NavigationAgent2D::set_debug_path_custom_line_width(float p_line_width) {
debug_path_custom_line_width = p_line_width;
debug_path_dirty = true;
}
float NavigationAgent2D::get_debug_path_custom_line_width() const {
return debug_path_custom_line_width;
}
void NavigationAgent2D::_navigation_debug_changed() {
debug_path_dirty = true;
}
void NavigationAgent2D::_update_debug_path() {
if (!debug_path_dirty) {
return;
}
debug_path_dirty = false;
if (!debug_path_instance.is_valid()) {
debug_path_instance = RenderingServer::get_singleton()->canvas_item_create();
}
RenderingServer::get_singleton()->canvas_item_clear(debug_path_instance);
if (!(debug_enabled && Navigation2DServer::get_singleton()->get_debug_navigation_enable_agent_paths())) {
return;
}
if (!(agent_parent && agent_parent->is_inside_tree())) {
return;
}
RenderingServer::get_singleton()->canvas_item_set_parent(debug_path_instance, agent_parent->get_canvas());
RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
//const Vector<Vector2> &navigation_path = navigation_result->get_path();
if (navigation_path.size() <= 1) {
return;
}
Color debug_path_color = Navigation2DServer::get_singleton()->get_debug_navigation_agent_path_color();
if (debug_use_custom) {
debug_path_color = debug_path_custom_color;
}
Vector<Color> debug_path_colors;
debug_path_colors.resize(navigation_path.size());
debug_path_colors.fill(debug_path_color);
RenderingServer::get_singleton()->canvas_item_add_polyline(debug_path_instance, navigation_path, debug_path_colors, debug_path_custom_line_width, false);
float point_size = Navigation2DServer::get_singleton()->get_debug_navigation_agent_path_point_size();
float half_point_size = point_size * 0.5;
if (debug_use_custom) {
point_size = debug_path_custom_point_size;
half_point_size = debug_path_custom_point_size * 0.5;
}
for (int i = 0; i < navigation_path.size(); i++) {
const Vector2 &vert = navigation_path[i];
Rect2 path_point_rect = Rect2(vert.x - half_point_size, vert.y - half_point_size, point_size, point_size);
RenderingServer::get_singleton()->canvas_item_add_rect(debug_path_instance, path_point_rect, debug_path_color);
}
}
#endif // DEBUG_ENABLED

View File

@ -70,6 +70,20 @@ class NavigationAgent2D : public Node {
// No initialized on purpose
uint32_t update_frame_id;
#ifdef DEBUG_ENABLED
bool debug_enabled;
bool debug_path_dirty;
RID debug_path_instance;
float debug_path_custom_point_size;
float debug_path_custom_line_width;
bool debug_use_custom;
Color debug_path_custom_color;
private:
void _navigation_debug_changed();
void _update_debug_path();
#endif // DEBUG_ENABLED
protected:
static void _bind_methods();
void _notification(int p_what);
@ -163,6 +177,23 @@ public:
virtual String get_configuration_warning() const;
#ifdef DEBUG_ENABLED
void set_debug_enabled(bool p_enabled);
bool get_debug_enabled() const;
void set_debug_use_custom(bool p_enabled);
bool get_debug_use_custom() const;
void set_debug_path_custom_color(Color p_color);
Color get_debug_path_custom_color() const;
void set_debug_path_custom_point_size(float p_point_size);
float get_debug_path_custom_point_size() const;
void set_debug_path_custom_line_width(float p_line_width);
float get_debug_path_custom_line_width() const;
#endif // DEBUG_ENABLED
private:
void update_navigation();
void _request_repath();

View File

@ -33,8 +33,10 @@
#include "core/config/engine.h"
#include "core/math/geometry.h"
#include "scene/3d/navigation.h"
#include "scene/resources/mesh.h"
#include "scene/resources/world_3d.h"
#include "servers/navigation_server.h"
#include "scene/resources/material.h"
void NavigationAgent::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent::get_rid);
@ -113,6 +115,24 @@ void NavigationAgent::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_speed", PROPERTY_HINT_RANGE, "0.1,10000,0.01"), "set_max_speed", "get_max_speed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_y"), "set_ignore_y", "get_ignore_y");
#ifdef DEBUG_ENABLED
ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent::set_debug_enabled);
ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent::get_debug_enabled);
ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent::set_debug_use_custom);
ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent::get_debug_use_custom);
ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent::set_debug_path_custom_color);
ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent::get_debug_path_custom_color);
ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent::set_debug_path_custom_point_size);
ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent::get_debug_path_custom_point_size);
ClassDB::bind_method(D_METHOD("_navigation_debug_changed"), &NavigationAgent::_navigation_debug_changed);
ADD_GROUP("Debug", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "debug_path_custom_point_size", PROPERTY_HINT_RANGE, "1,50,1,suffix:px"), "set_debug_path_custom_point_size", "get_debug_path_custom_point_size");
#endif // DEBUG_ENABLED
ADD_SIGNAL(MethodInfo("path_changed"));
ADD_SIGNAL(MethodInfo("target_reached"));
ADD_SIGNAL(MethodInfo("navigation_finished"));
@ -142,6 +162,12 @@ void NavigationAgent::_notification(int p_what) {
// cannot use READY as ready does not get called if Node is readded to SceneTree
set_agent_parent(get_parent());
set_physics_process_internal(true);
#ifdef DEBUG_ENABLED
if (NavigationServer::get_singleton()->get_debug_enabled()) {
debug_path_dirty = true;
}
#endif // DEBUG_ENABLED
} break;
case NOTIFICATION_PARENTED: {
if (is_inside_tree() && (get_parent() != agent_parent)) {
@ -162,6 +188,12 @@ void NavigationAgent::_notification(int p_what) {
set_agent_parent(nullptr);
set_navigation(nullptr);
set_physics_process_internal(false);
#ifdef DEBUG_ENABLED
if (debug_path_instance.is_valid()) {
RS::get_singleton()->instance_set_visible(debug_path_instance, false);
}
#endif // DEBUG_ENABLED
} break;
case NOTIFICATION_PAUSED: {
if (agent_parent && !agent_parent->can_process()) {
@ -190,6 +222,12 @@ void NavigationAgent::_notification(int p_what) {
}
_check_distance_to_target();
}
#ifdef DEBUG_ENABLED
if (debug_path_dirty) {
_update_debug_path();
}
#endif // DEBUG_ENABLED
} break;
}
}
@ -219,11 +257,34 @@ NavigationAgent::NavigationAgent() {
nav_path_index = 0;
update_frame_id = 0;
#ifdef DEBUG_ENABLED
debug_enabled = false;
debug_path_dirty = true;
debug_path_custom_point_size = 4.0;
debug_use_custom = false;
debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
NavigationServer::get_singleton_mut()->connect("navigation_debug_changed", this, "_navigation_debug_changed");
#endif // DEBUG_ENABLED
}
NavigationAgent::~NavigationAgent() {
ERR_FAIL_NULL(NavigationServer::get_singleton());
NavigationServer::get_singleton()->free(agent);
agent = RID(); // Pointless
#ifdef DEBUG_ENABLED
NavigationServer::get_singleton_mut()->disconnect("navigation_debug_changed", this, "_navigation_debug_changed");
ERR_FAIL_NULL(RenderingServer::get_singleton());
if (debug_path_instance.is_valid()) {
RenderingServer::get_singleton()->free(debug_path_instance);
}
if (debug_path_mesh.is_valid()) {
RenderingServer::get_singleton()->free(debug_path_mesh->get_rid());
}
#endif // DEBUG_ENABLED
}
void NavigationAgent::set_avoidance_enabled(bool p_enabled) {
@ -480,6 +541,10 @@ void NavigationAgent::update_navigation() {
navigation_path = NavigationServer::get_singleton()->map_get_path(agent_parent->get_world_3d()->get_navigation_map(), o, target_position, true, navigation_layers);
}
#ifdef DEBUG_ENABLED
debug_path_dirty = true;
#endif // DEBUG_ENABLED
navigation_finished = false;
nav_path_index = 0;
emit_signal("path_changed");
@ -520,3 +585,130 @@ void NavigationAgent::_check_distance_to_target() {
}
}
}
////////DEBUG////////////////////////////////////////////////////////////
#ifdef DEBUG_ENABLED
void NavigationAgent::set_debug_enabled(bool p_enabled) {
debug_enabled = p_enabled;
debug_path_dirty = true;
}
bool NavigationAgent::get_debug_enabled() const {
return debug_enabled;
}
void NavigationAgent::set_debug_use_custom(bool p_enabled) {
debug_use_custom = p_enabled;
debug_path_dirty = true;
}
bool NavigationAgent::get_debug_use_custom() const {
return debug_use_custom;
}
void NavigationAgent::set_debug_path_custom_color(Color p_color) {
debug_path_custom_color = p_color;
debug_path_dirty = true;
}
Color NavigationAgent::get_debug_path_custom_color() const {
return debug_path_custom_color;
}
void NavigationAgent::set_debug_path_custom_point_size(float p_point_size) {
debug_path_custom_point_size = p_point_size;
debug_path_dirty = true;
}
float NavigationAgent::get_debug_path_custom_point_size() const {
return debug_path_custom_point_size;
}
void NavigationAgent::_navigation_debug_changed() {
debug_path_dirty = true;
}
void NavigationAgent::_update_debug_path() {
if (!debug_path_dirty) {
return;
}
debug_path_dirty = false;
if (!debug_path_instance.is_valid()) {
debug_path_instance = RenderingServer::get_singleton()->instance_create();
}
if (!debug_path_mesh.is_valid()) {
debug_path_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
}
debug_path_mesh->clear_surfaces();
if (!(debug_enabled && NavigationServer::get_singleton()->get_debug_navigation_enable_agent_paths())) {
return;
}
if (!(agent_parent && agent_parent->is_inside_tree())) {
return;
}
//const Vector<Vector3> &navigation_path = navigation_result->get_path();
if (navigation_path.size() <= 1) {
return;
}
Vector<Vector3> debug_path_lines_vertex_array;
for (int i = 0; i < navigation_path.size() - 1; i++) {
debug_path_lines_vertex_array.push_back(navigation_path[i]);
debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
}
Array debug_path_lines_mesh_array;
debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
Ref<SpatialMaterial> debug_agent_path_line_material = NavigationServer::get_singleton_mut()->get_debug_navigation_agent_path_line_material();
if (debug_use_custom) {
if (!debug_agent_path_line_custom_material.is_valid()) {
debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
}
debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
} else {
debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
}
Vector<Vector3> debug_path_points_vertex_array;
for (int i = 0; i < navigation_path.size(); i++) {
debug_path_points_vertex_array.push_back(navigation_path[i]);
}
Array debug_path_points_mesh_array;
debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
Ref<SpatialMaterial> debug_agent_path_point_material = NavigationServer::get_singleton_mut()->get_debug_navigation_agent_path_point_material();
if (debug_use_custom) {
if (!debug_agent_path_point_custom_material.is_valid()) {
debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
}
debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
} else {
debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
}
RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
}
#endif // DEBUG_ENABLED

View File

@ -35,6 +35,7 @@
class Spatial;
class Navigation;
class SpatialMaterial;
class NavigationAgent : public Node {
GDCLASS(NavigationAgent, Node);
@ -73,6 +74,22 @@ class NavigationAgent : public Node {
// No initialized on purpose
uint32_t update_frame_id;
#ifdef DEBUG_ENABLED
bool debug_enabled;
bool debug_path_dirty;
RID debug_path_instance;
Ref<ArrayMesh> debug_path_mesh;
float debug_path_custom_point_size;
bool debug_use_custom;
Color debug_path_custom_color;
Ref<SpatialMaterial> debug_agent_path_line_custom_material;
Ref<SpatialMaterial> debug_agent_path_point_custom_material;
private:
void _navigation_debug_changed();
void _update_debug_path();
#endif // DEBUG_ENABLED
protected:
static void _bind_methods();
void _notification(int p_what);
@ -176,6 +193,20 @@ public:
virtual String get_configuration_warning() const;
#ifdef DEBUG_ENABLED
void set_debug_enabled(bool p_enabled);
bool get_debug_enabled() const;
void set_debug_use_custom(bool p_enabled);
bool get_debug_use_custom() const;
void set_debug_path_custom_color(Color p_color);
Color get_debug_path_custom_color() const;
void set_debug_path_custom_point_size(float p_point_size);
float get_debug_path_custom_point_size() const;
#endif // DEBUG_ENABLE
private:
void update_navigation();
void _request_repath();

View File

@ -111,10 +111,18 @@ void Navigation2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &Navigation2DServer::set_debug_enabled);
ClassDB::bind_method(D_METHOD("get_debug_enabled"), &Navigation2DServer::get_debug_enabled);
#ifdef DEBUG_ENABLED
ClassDB::bind_method(D_METHOD("_emit_navigation_debug_changed_signal"), &Navigation2DServer::_emit_navigation_debug_changed_signal);
#endif // DEBUG_ENABLED
ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
ADD_SIGNAL(MethodInfo("navigation_debug_changed"));
}
void Navigation2DServer::init() {
#ifdef DEBUG_ENABLED
NavigationServer::get_singleton_mut()->connect("navigation_debug_changed", this, "_emit_navigation_debug_changed_signal");
#endif // DEBUG_ENABLED
}
void Navigation2DServer::query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result) const {
@ -134,6 +142,12 @@ Navigation2DServer::~Navigation2DServer() {
singleton = nullptr;
}
#ifdef DEBUG_ENABLED
void Navigation2DServer::_emit_navigation_debug_changed_signal() {
emit_signal("navigation_debug_changed");
}
#endif // DEBUG_ENABLED
void Navigation2DServer::set_debug_enabled(bool p_enabled) {
NavigationServer::get_singleton_mut()->set_debug_enabled(p_enabled);
}
@ -220,6 +234,30 @@ Color Navigation2DServer::get_debug_navigation_link_connection_disabled_color()
return NavigationServer::get_singleton()->get_debug_navigation_link_connection_disabled_color();
}
void Navigation2DServer::set_debug_navigation_agent_path_color(const Color &p_color) {
NavigationServer::get_singleton_mut()->set_debug_navigation_agent_path_color(p_color);
}
Color Navigation2DServer::get_debug_navigation_agent_path_color() const {
return NavigationServer::get_singleton()->get_debug_navigation_agent_path_color();
}
void Navigation2DServer::set_debug_navigation_enable_agent_paths(const bool p_value) {
NavigationServer::get_singleton_mut()->set_debug_navigation_enable_agent_paths(p_value);
}
bool Navigation2DServer::get_debug_navigation_enable_agent_paths() const {
return NavigationServer::get_singleton()->get_debug_navigation_enable_agent_paths();
}
void Navigation2DServer::set_debug_navigation_agent_path_point_size(float p_point_size) {
NavigationServer::get_singleton_mut()->set_debug_navigation_agent_path_point_size(p_point_size);
}
float Navigation2DServer::get_debug_navigation_agent_path_point_size() const {
return NavigationServer::get_singleton()->get_debug_navigation_agent_path_point_size();
}
#endif
Vector<Navigation2DServerManager::ClassInfo> Navigation2DServerManager::navigation_servers;

View File

@ -271,6 +271,18 @@ public:
void set_debug_navigation_link_connection_disabled_color(const Color &p_color);
Color get_debug_navigation_link_connection_disabled_color() const;
void set_debug_navigation_agent_path_color(const Color &p_color);
Color get_debug_navigation_agent_path_color() const;
void set_debug_navigation_enable_agent_paths(const bool p_value);
bool get_debug_navigation_enable_agent_paths() const;
void set_debug_navigation_agent_path_point_size(float p_point_size);
float get_debug_navigation_agent_path_point_size() const;
private:
void _emit_navigation_debug_changed_signal();
#endif
};

View File

@ -135,8 +135,10 @@ void NavigationServer::_bind_methods() {
ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
#ifdef DEBUG_ENABLED
ADD_SIGNAL(MethodInfo("navigation_debug_changed"));
#ifdef DEBUG_ENABLED
ClassDB::bind_method(D_METHOD("_emit_navigation_debug_changed_signal"), &NavigationServer::_emit_navigation_debug_changed_signal);
#endif // DEBUG_ENABLED
}
@ -163,6 +165,7 @@ NavigationServer::NavigationServer() {
_debug_navigation_geometry_face_disabled_color = GLOBAL_DEF("debug/shapes/navigation/geometry_face_disabled_color", Color(0.5, 0.5, 0.5, 0.4));
_debug_navigation_link_connection_color = GLOBAL_DEF("debug/shapes/navigation/link_connection_color", Color(1.0, 0.5, 1.0, 1.0));
_debug_navigation_link_connection_disabled_color = GLOBAL_DEF("debug/shapes/navigation/link_connection_disabled_color", Color(0.5, 0.5, 0.5, 1.0));
_debug_navigation_agent_path_color = GLOBAL_DEF("debug/shapes/navigation/agent_path_color", Color(1.0, 0.0, 0.0, 1.0));
_debug_navigation_enable_edge_connections = GLOBAL_DEF("debug/shapes/navigation/enable_edge_connections", true);
_debug_navigation_enable_edge_connections_xray = GLOBAL_DEF("debug/shapes/navigation/enable_edge_connections_xray", true);
@ -172,8 +175,13 @@ NavigationServer::NavigationServer() {
_debug_navigation_enable_link_connections = GLOBAL_DEF("debug/shapes/navigation/enable_link_connections", true);
_debug_navigation_enable_link_connections_xray = GLOBAL_DEF("debug/shapes/navigation/enable_link_connections_xray", true);
_debug_navigation_enable_agent_paths = GLOBAL_DEF("debug/shapes/navigation/enable_agent_paths", true);
_debug_navigation_enable_agent_paths_xray = GLOBAL_DEF("debug/shapes/navigation/enable_agent_paths_xray", true);
_debug_navigation_agent_path_point_size = GLOBAL_DEF("debug/shapes/navigation/agent_path_point_size", 4.0);
if (Engine::get_singleton()->is_editor_hint()) {
// enable NavigationServer3D when in Editor or else navmesh edge connections are invisible
// enable NavigationServer when in Editor or else navmesh edge connections are invisible
// on runtime tests SceneTree has "Visible Navigation" set and main iteration takes care of this
set_debug_enabled(true);
}
@ -357,6 +365,42 @@ Ref<SpatialMaterial> NavigationServer::get_debug_navigation_link_connections_dis
return _debug_navigation_link_connections_disabled_material;
}
Ref<SpatialMaterial> NavigationServer::get_debug_navigation_agent_path_line_material() {
if (_debug_navigation_agent_path_line_material.is_valid()) {
return _debug_navigation_agent_path_line_material;
}
Ref<SpatialMaterial> material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
material->set_albedo(_debug_navigation_agent_path_color);
if (_debug_navigation_enable_agent_paths_xray) {
material->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
}
material->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MAX - 2);
_debug_navigation_agent_path_line_material = material;
return _debug_navigation_agent_path_line_material;
}
Ref<SpatialMaterial> NavigationServer::get_debug_navigation_agent_path_point_material() {
if (_debug_navigation_agent_path_point_material.is_valid()) {
return _debug_navigation_agent_path_point_material;
}
Ref<SpatialMaterial> material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
material->set_albedo(_debug_navigation_agent_path_color);
material->set_flag(SpatialMaterial::FLAG_USE_POINT_SIZE, true);
material->set_point_size(_debug_navigation_agent_path_point_size);
if (_debug_navigation_enable_agent_paths_xray) {
material->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
}
material->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MAX - 2);
_debug_navigation_agent_path_point_material = material;
return _debug_navigation_agent_path_point_material;
}
void NavigationServer::set_debug_navigation_edge_connection_color(const Color &p_color) {
_debug_navigation_edge_connection_color = p_color;
if (_debug_navigation_edge_connections_material.is_valid()) {
@ -434,6 +478,31 @@ Color NavigationServer::get_debug_navigation_link_connection_disabled_color() co
return _debug_navigation_link_connection_disabled_color;
}
void NavigationServer::set_debug_navigation_agent_path_point_size(float p_point_size) {
_debug_navigation_agent_path_point_size = MAX(0.1, p_point_size);
if (_debug_navigation_agent_path_point_material.is_valid()) {
_debug_navigation_agent_path_point_material->set_point_size(_debug_navigation_agent_path_point_size);
}
}
float NavigationServer::get_debug_navigation_agent_path_point_size() const {
return _debug_navigation_agent_path_point_size;
}
void NavigationServer::set_debug_navigation_agent_path_color(const Color &p_color) {
_debug_navigation_agent_path_color = p_color;
if (_debug_navigation_agent_path_line_material.is_valid()) {
_debug_navigation_agent_path_line_material->set_albedo(_debug_navigation_agent_path_color);
}
if (_debug_navigation_agent_path_point_material.is_valid()) {
_debug_navigation_agent_path_point_material->set_albedo(_debug_navigation_agent_path_color);
}
}
Color NavigationServer::get_debug_navigation_agent_path_color() const {
return _debug_navigation_agent_path_color;
}
void NavigationServer::set_debug_navigation_enable_edge_connections(const bool p_value) {
_debug_navigation_enable_edge_connections = p_value;
_debug_dirty = true;
@ -507,6 +576,36 @@ bool NavigationServer::get_debug_navigation_enable_link_connections_xray() const
return _debug_navigation_enable_link_connections_xray;
}
void NavigationServer::set_debug_navigation_enable_agent_paths(const bool p_value) {
if (_debug_navigation_enable_agent_paths != p_value) {
_debug_dirty = true;
}
_debug_navigation_enable_agent_paths = p_value;
if (_debug_dirty) {
call_deferred("_emit_navigation_debug_changed_signal");
}
}
bool NavigationServer::get_debug_navigation_enable_agent_paths() const {
return _debug_navigation_enable_agent_paths;
}
void NavigationServer::set_debug_navigation_enable_agent_paths_xray(const bool p_value) {
_debug_navigation_enable_agent_paths_xray = p_value;
if (_debug_navigation_agent_path_line_material.is_valid()) {
_debug_navigation_agent_path_line_material->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, _debug_navigation_enable_agent_paths_xray);
}
if (_debug_navigation_agent_path_point_material.is_valid()) {
_debug_navigation_agent_path_point_material->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, _debug_navigation_enable_agent_paths_xray);
}
}
bool NavigationServer::get_debug_navigation_enable_agent_paths_xray() const {
return _debug_navigation_enable_agent_paths_xray;
}
#endif // DEBUG_ENABLED
void NavigationServer::query_path(const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result) const {

View File

@ -303,6 +303,9 @@ public:
void set_debug_navigation_link_connection_disabled_color(const Color &p_color);
Color get_debug_navigation_link_connection_disabled_color() const;
void set_debug_navigation_agent_path_color(const Color &p_color);
Color get_debug_navigation_agent_path_color() const;
void set_debug_navigation_enable_edge_connections(const bool p_value);
bool get_debug_navigation_enable_edge_connections() const;
@ -324,6 +327,15 @@ public:
void set_debug_navigation_enable_link_connections_xray(const bool p_value);
bool get_debug_navigation_enable_link_connections_xray() const;
void set_debug_navigation_enable_agent_paths(const bool p_value);
bool get_debug_navigation_enable_agent_paths() const;
void set_debug_navigation_enable_agent_paths_xray(const bool p_value);
bool get_debug_navigation_enable_agent_paths_xray() const;
void set_debug_navigation_agent_path_point_size(float p_point_size);
float get_debug_navigation_agent_path_point_size() const;
Ref<SpatialMaterial> get_debug_navigation_geometry_face_material();
Ref<SpatialMaterial> get_debug_navigation_geometry_edge_material();
Ref<SpatialMaterial> get_debug_navigation_geometry_face_disabled_material();
@ -332,6 +344,9 @@ public:
Ref<SpatialMaterial> get_debug_navigation_link_connections_material();
Ref<SpatialMaterial> get_debug_navigation_link_connections_disabled_material();
Ref<SpatialMaterial> get_debug_navigation_agent_path_line_material();
Ref<SpatialMaterial> get_debug_navigation_agent_path_point_material();
void _emit_navigation_debug_changed_signal();
protected:
@ -345,6 +360,9 @@ protected:
Color _debug_navigation_geometry_face_disabled_color;
Color _debug_navigation_link_connection_color;
Color _debug_navigation_link_connection_disabled_color;
Color _debug_navigation_agent_path_color;
float _debug_navigation_agent_path_point_size;
bool _debug_navigation_enable_edge_connections;
bool _debug_navigation_enable_edge_connections_xray;
@ -353,6 +371,8 @@ protected:
bool _debug_navigation_enable_geometry_face_random_color;
bool _debug_navigation_enable_link_connections;
bool _debug_navigation_enable_link_connections_xray;
bool _debug_navigation_enable_agent_paths;
bool _debug_navigation_enable_agent_paths_xray;
Ref<SpatialMaterial> _debug_navigation_geometry_edge_material;
Ref<SpatialMaterial> _debug_navigation_geometry_face_material;
@ -362,6 +382,9 @@ protected:
Ref<SpatialMaterial> _debug_navigation_link_connections_material;
Ref<SpatialMaterial> _debug_navigation_link_connections_disabled_material;
Ref<SpatialMaterial> _debug_navigation_agent_path_line_material;
Ref<SpatialMaterial> _debug_navigation_agent_path_point_material;
#endif // DEBUG_ENABLED
};