1/**************************************************************************/
2/* navigation_agent_3d.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "navigation_agent_3d.h"
32
33#include "scene/3d/navigation_link_3d.h"
34#include "servers/navigation_server_3d.h"
35
36void NavigationAgent3D::_bind_methods() {
37 ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent3D::get_rid);
38
39 ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent3D::set_avoidance_enabled);
40 ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent3D::get_avoidance_enabled);
41
42 ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent3D::set_path_desired_distance);
43 ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent3D::get_path_desired_distance);
44
45 ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent3D::set_target_desired_distance);
46 ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent3D::get_target_desired_distance);
47
48 ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent3D::set_radius);
49 ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent3D::get_radius);
50
51 ClassDB::bind_method(D_METHOD("set_height", "height"), &NavigationAgent3D::set_height);
52 ClassDB::bind_method(D_METHOD("get_height"), &NavigationAgent3D::get_height);
53
54 ClassDB::bind_method(D_METHOD("set_path_height_offset", "path_height_offset"), &NavigationAgent3D::set_path_height_offset);
55 ClassDB::bind_method(D_METHOD("get_path_height_offset"), &NavigationAgent3D::get_path_height_offset);
56
57 ClassDB::bind_method(D_METHOD("set_use_3d_avoidance", "enabled"), &NavigationAgent3D::set_use_3d_avoidance);
58 ClassDB::bind_method(D_METHOD("get_use_3d_avoidance"), &NavigationAgent3D::get_use_3d_avoidance);
59
60 ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent3D::set_neighbor_distance);
61 ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent3D::get_neighbor_distance);
62
63 ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
64 ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
65
66 ClassDB::bind_method(D_METHOD("set_time_horizon_agents", "time_horizon"), &NavigationAgent3D::set_time_horizon_agents);
67 ClassDB::bind_method(D_METHOD("get_time_horizon_agents"), &NavigationAgent3D::get_time_horizon_agents);
68
69 ClassDB::bind_method(D_METHOD("set_time_horizon_obstacles", "time_horizon"), &NavigationAgent3D::set_time_horizon_obstacles);
70 ClassDB::bind_method(D_METHOD("get_time_horizon_obstacles"), &NavigationAgent3D::get_time_horizon_obstacles);
71
72 ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
73 ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
74
75 ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
76 ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
77
78 ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent3D::set_navigation_layers);
79 ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent3D::get_navigation_layers);
80
81 ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent3D::set_navigation_layer_value);
82 ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent3D::get_navigation_layer_value);
83
84 ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent3D::set_pathfinding_algorithm);
85 ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent3D::get_pathfinding_algorithm);
86
87 ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent3D::set_path_postprocessing);
88 ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent3D::get_path_postprocessing);
89
90 ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent3D::set_path_metadata_flags);
91 ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent3D::get_path_metadata_flags);
92
93 ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent3D::set_navigation_map);
94 ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent3D::get_navigation_map);
95
96 ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent3D::set_target_position);
97 ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent3D::get_target_position);
98
99 ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent3D::get_next_path_position);
100
101 ClassDB::bind_method(D_METHOD("set_velocity_forced", "velocity"), &NavigationAgent3D::set_velocity_forced);
102
103 ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
104 ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationAgent3D::get_velocity);
105
106 ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
107
108 ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent3D::get_current_navigation_result);
109
110 ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent3D::get_current_navigation_path);
111 ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent3D::get_current_navigation_path_index);
112 ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
113 ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
114 ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
115 ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent3D::get_final_position);
116
117 ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
118
119 ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationAgent3D::set_avoidance_layers);
120 ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationAgent3D::get_avoidance_layers);
121 ClassDB::bind_method(D_METHOD("set_avoidance_mask", "mask"), &NavigationAgent3D::set_avoidance_mask);
122 ClassDB::bind_method(D_METHOD("get_avoidance_mask"), &NavigationAgent3D::get_avoidance_mask);
123 ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationAgent3D::set_avoidance_layer_value);
124 ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationAgent3D::get_avoidance_layer_value);
125 ClassDB::bind_method(D_METHOD("set_avoidance_mask_value", "mask_number", "value"), &NavigationAgent3D::set_avoidance_mask_value);
126 ClassDB::bind_method(D_METHOD("get_avoidance_mask_value", "mask_number"), &NavigationAgent3D::get_avoidance_mask_value);
127 ClassDB::bind_method(D_METHOD("set_avoidance_priority", "priority"), &NavigationAgent3D::set_avoidance_priority);
128 ClassDB::bind_method(D_METHOD("get_avoidance_priority"), &NavigationAgent3D::get_avoidance_priority);
129
130 ADD_GROUP("Pathfinding", "");
131 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
132 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_path_desired_distance", "get_path_desired_distance");
133 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_target_desired_distance", "get_target_desired_distance");
134 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01,or_greater,suffix:m"), "set_path_height_offset", "get_path_height_offset");
135 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "0.01,100,0.1,or_greater,suffix:m"), "set_path_max_distance", "get_path_max_distance");
136 ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
137 ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
138 ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered"), "set_path_postprocessing", "get_path_postprocessing");
139 ADD_PROPERTY(PropertyInfo(Variant::INT, "path_metadata_flags", PROPERTY_HINT_FLAGS, "Include Types,Include RIDs,Include Owners"), "set_path_metadata_flags", "get_path_metadata_flags");
140
141 ADD_GROUP("Avoidance", "");
142 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
143 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
144 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_height", "get_height");
145 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_radius", "get_radius");
146 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_distance", PROPERTY_HINT_RANGE, "0.1,10000,0.01,or_greater,suffix:m"), "set_neighbor_distance", "get_neighbor_distance");
147 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");
148 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_agents", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_agents", "get_time_horizon_agents");
149 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_obstacles", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_obstacles", "get_time_horizon_obstacles");
150 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.01,10000,0.01,or_greater,suffix:m/s"), "set_max_speed", "get_max_speed");
151 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance");
152 ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
153 ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_mask", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_mask", "get_avoidance_mask");
154 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "avoidance_priority", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_avoidance_priority", "get_avoidance_priority");
155
156 ADD_SIGNAL(MethodInfo("path_changed"));
157 ADD_SIGNAL(MethodInfo("target_reached"));
158 ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
159 ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
160 ADD_SIGNAL(MethodInfo("navigation_finished"));
161 ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
162
163 ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent3D::set_debug_enabled);
164 ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent3D::get_debug_enabled);
165 ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent3D::set_debug_use_custom);
166 ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent3D::get_debug_use_custom);
167 ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent3D::set_debug_path_custom_color);
168 ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent3D::get_debug_path_custom_color);
169 ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent3D::set_debug_path_custom_point_size);
170 ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent3D::get_debug_path_custom_point_size);
171
172 ADD_GROUP("Debug", "debug_");
173 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
174 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
175 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
176 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "debug_path_custom_point_size", PROPERTY_HINT_RANGE, "0,50,0.01,or_greater,suffix:px"), "set_debug_path_custom_point_size", "get_debug_path_custom_point_size");
177}
178
179#ifndef DISABLE_DEPRECATED
180// Compatibility with Godot 4.0 beta 10 or below.
181// Functions in block below all renamed or replaced in 4.0 beta 1X avoidance rework.
182bool NavigationAgent3D::_set(const StringName &p_name, const Variant &p_value) {
183 if (p_name == "time_horizon") {
184 set_time_horizon_agents(p_value);
185 return true;
186 }
187 if (p_name == "target_location") {
188 set_target_position(p_value);
189 return true;
190 }
191 if (p_name == "agent_height_offset") {
192 set_path_height_offset(p_value);
193 return true;
194 }
195 return false;
196}
197
198bool NavigationAgent3D::_get(const StringName &p_name, Variant &r_ret) const {
199 if (p_name == "time_horizon") {
200 r_ret = get_time_horizon_agents();
201 return true;
202 }
203 if (p_name == "target_location") {
204 r_ret = get_target_position();
205 return true;
206 }
207 if (p_name == "agent_height_offset") {
208 r_ret = get_path_height_offset();
209 return true;
210 }
211 return false;
212}
213#endif // DISABLE_DEPRECATED
214
215void NavigationAgent3D::_notification(int p_what) {
216 switch (p_what) {
217 case NOTIFICATION_POST_ENTER_TREE: {
218 // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
219 // cannot use READY as ready does not get called if Node is re-added to SceneTree
220 set_agent_parent(get_parent());
221 set_physics_process_internal(true);
222
223 if (agent_parent && avoidance_enabled) {
224 NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
225 }
226
227#ifdef DEBUG_ENABLED
228 if (NavigationServer3D::get_singleton()->get_debug_enabled()) {
229 debug_path_dirty = true;
230 }
231#endif // DEBUG_ENABLED
232
233 } break;
234
235 case NOTIFICATION_PARENTED: {
236 if (is_inside_tree() && (get_parent() != agent_parent)) {
237 // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
238 // PARENTED notification fires also when Node is added in scripts to a parent
239 // this would spam transforms fails and world fails while Node is outside SceneTree
240 // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
241 set_agent_parent(get_parent());
242 set_physics_process_internal(true);
243 }
244 } break;
245
246 case NOTIFICATION_UNPARENTED: {
247 // if agent has no parent no point in processing it until reparented
248 set_agent_parent(nullptr);
249 set_physics_process_internal(false);
250 } break;
251
252 case NOTIFICATION_EXIT_TREE: {
253 set_agent_parent(nullptr);
254 set_physics_process_internal(false);
255
256#ifdef DEBUG_ENABLED
257 if (debug_path_instance.is_valid()) {
258 RS::get_singleton()->instance_set_visible(debug_path_instance, false);
259 }
260#endif // DEBUG_ENABLED
261 } break;
262
263 case NOTIFICATION_PAUSED: {
264 if (agent_parent) {
265 NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
266 }
267 } break;
268
269 case NOTIFICATION_UNPAUSED: {
270 if (agent_parent) {
271 NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
272 }
273 } break;
274
275 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
276 if (agent_parent && avoidance_enabled) {
277 NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
278 }
279 if (agent_parent && target_position_submitted) {
280 if (velocity_submitted) {
281 velocity_submitted = false;
282 if (avoidance_enabled) {
283 if (!use_3d_avoidance) {
284 stored_y_velocity = velocity.y;
285 velocity.y = 0.0;
286 }
287 NavigationServer3D::get_singleton()->agent_set_velocity(agent, velocity);
288 }
289 }
290 if (velocity_forced_submitted) {
291 velocity_forced_submitted = false;
292 if (avoidance_enabled) {
293 NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, velocity_forced);
294 }
295 }
296 _check_distance_to_target();
297 }
298#ifdef DEBUG_ENABLED
299 if (debug_path_dirty) {
300 _update_debug_path();
301 }
302#endif // DEBUG_ENABLED
303 } break;
304 }
305}
306
307NavigationAgent3D::NavigationAgent3D() {
308 agent = NavigationServer3D::get_singleton()->agent_create();
309
310 NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
311 NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
312 NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
313 NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
314 NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
315 NavigationServer3D::get_singleton()->agent_set_height(agent, height);
316 NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
317
318 // Preallocate query and result objects to improve performance.
319 navigation_query = Ref<NavigationPathQueryParameters3D>();
320 navigation_query.instantiate();
321
322 navigation_result = Ref<NavigationPathQueryResult3D>();
323 navigation_result.instantiate();
324
325 set_avoidance_layers(avoidance_layers);
326 set_avoidance_mask(avoidance_mask);
327 set_avoidance_priority(avoidance_priority);
328 set_use_3d_avoidance(use_3d_avoidance);
329 set_avoidance_enabled(avoidance_enabled);
330
331#ifdef DEBUG_ENABLED
332 NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
333#endif // DEBUG_ENABLED
334}
335
336NavigationAgent3D::~NavigationAgent3D() {
337 ERR_FAIL_NULL(NavigationServer3D::get_singleton());
338 NavigationServer3D::get_singleton()->free(agent);
339 agent = RID(); // Pointless
340
341#ifdef DEBUG_ENABLED
342 NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
343
344 ERR_FAIL_NULL(RenderingServer::get_singleton());
345 if (debug_path_instance.is_valid()) {
346 RenderingServer::get_singleton()->free(debug_path_instance);
347 }
348 if (debug_path_mesh.is_valid()) {
349 RenderingServer::get_singleton()->free(debug_path_mesh->get_rid());
350 }
351#endif // DEBUG_ENABLED
352}
353
354void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
355 if (avoidance_enabled == p_enabled) {
356 return;
357 }
358
359 avoidance_enabled = p_enabled;
360
361 if (avoidance_enabled) {
362 NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, true);
363 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
364 } else {
365 NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, false);
366 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
367 }
368}
369
370bool NavigationAgent3D::get_avoidance_enabled() const {
371 return avoidance_enabled;
372}
373
374void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
375 if (agent_parent == p_agent_parent) {
376 return;
377 }
378
379 // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
380 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
381
382 if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
383 // place agent on navigation map first or else the RVO agent callback creation fails silently later
384 agent_parent = Object::cast_to<Node3D>(p_agent_parent);
385 if (map_override.is_valid()) {
386 NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_override);
387 } else {
388 NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
389 }
390
391 // create new avoidance callback if enabled
392 if (avoidance_enabled) {
393 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
394 }
395 } else {
396 agent_parent = nullptr;
397 NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
398 }
399}
400
401void NavigationAgent3D::set_navigation_layers(uint32_t p_navigation_layers) {
402 if (navigation_layers == p_navigation_layers) {
403 return;
404 }
405
406 navigation_layers = p_navigation_layers;
407
408 _request_repath();
409}
410
411uint32_t NavigationAgent3D::get_navigation_layers() const {
412 return navigation_layers;
413}
414
415void NavigationAgent3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
416 ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
417 ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
418 uint32_t _navigation_layers = get_navigation_layers();
419 if (p_value) {
420 _navigation_layers |= 1 << (p_layer_number - 1);
421 } else {
422 _navigation_layers &= ~(1 << (p_layer_number - 1));
423 }
424 set_navigation_layers(_navigation_layers);
425}
426
427bool NavigationAgent3D::get_navigation_layer_value(int p_layer_number) const {
428 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
429 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
430 return get_navigation_layers() & (1 << (p_layer_number - 1));
431}
432
433void NavigationAgent3D::set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm) {
434 if (pathfinding_algorithm == p_pathfinding_algorithm) {
435 return;
436 }
437
438 pathfinding_algorithm = p_pathfinding_algorithm;
439
440 navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
441}
442
443void NavigationAgent3D::set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing) {
444 if (path_postprocessing == p_path_postprocessing) {
445 return;
446 }
447
448 path_postprocessing = p_path_postprocessing;
449
450 navigation_query->set_path_postprocessing(path_postprocessing);
451}
452
453void NavigationAgent3D::set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_path_metadata_flags) {
454 if (path_metadata_flags == p_path_metadata_flags) {
455 return;
456 }
457
458 path_metadata_flags = p_path_metadata_flags;
459}
460
461void NavigationAgent3D::set_navigation_map(RID p_navigation_map) {
462 if (map_override == p_navigation_map) {
463 return;
464 }
465
466 map_override = p_navigation_map;
467
468 NavigationServer3D::get_singleton()->agent_set_map(agent, map_override);
469 _request_repath();
470}
471
472RID NavigationAgent3D::get_navigation_map() const {
473 if (map_override.is_valid()) {
474 return map_override;
475 } else if (agent_parent != nullptr) {
476 return agent_parent->get_world_3d()->get_navigation_map();
477 }
478 return RID();
479}
480
481void NavigationAgent3D::set_path_desired_distance(real_t p_path_desired_distance) {
482 if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
483 return;
484 }
485
486 path_desired_distance = p_path_desired_distance;
487}
488
489void NavigationAgent3D::set_target_desired_distance(real_t p_target_desired_distance) {
490 if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
491 return;
492 }
493
494 target_desired_distance = p_target_desired_distance;
495}
496
497void NavigationAgent3D::set_radius(real_t p_radius) {
498 ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
499 if (Math::is_equal_approx(radius, p_radius)) {
500 return;
501 }
502 radius = p_radius;
503
504 NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
505}
506
507void NavigationAgent3D::set_height(real_t p_height) {
508 ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive.");
509 if (Math::is_equal_approx(height, p_height)) {
510 return;
511 }
512 height = p_height;
513 NavigationServer3D::get_singleton()->agent_set_height(agent, height);
514}
515
516void NavigationAgent3D::set_path_height_offset(real_t p_path_height_offset) {
517 path_height_offset = p_path_height_offset;
518}
519
520void NavigationAgent3D::set_use_3d_avoidance(bool p_use_3d_avoidance) {
521 use_3d_avoidance = p_use_3d_avoidance;
522 NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
523 notify_property_list_changed();
524}
525
526void NavigationAgent3D::set_neighbor_distance(real_t p_distance) {
527 if (Math::is_equal_approx(neighbor_distance, p_distance)) {
528 return;
529 }
530
531 neighbor_distance = p_distance;
532
533 NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
534}
535
536void NavigationAgent3D::set_max_neighbors(int p_count) {
537 if (max_neighbors == p_count) {
538 return;
539 }
540
541 max_neighbors = p_count;
542
543 NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
544}
545
546void NavigationAgent3D::set_time_horizon_agents(real_t p_time_horizon) {
547 ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizion must be positive.");
548 if (Math::is_equal_approx(time_horizon_agents, p_time_horizon)) {
549 return;
550 }
551 time_horizon_agents = p_time_horizon;
552 NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
553}
554
555void NavigationAgent3D::set_time_horizon_obstacles(real_t p_time_horizon) {
556 ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizion must be positive.");
557 if (Math::is_equal_approx(time_horizon_obstacles, p_time_horizon)) {
558 return;
559 }
560 time_horizon_obstacles = p_time_horizon;
561 NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
562}
563
564void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
565 ERR_FAIL_COND_MSG(p_max_speed < 0.0, "Max speed must be positive.");
566 if (Math::is_equal_approx(max_speed, p_max_speed)) {
567 return;
568 }
569 max_speed = p_max_speed;
570
571 NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
572}
573
574void NavigationAgent3D::set_path_max_distance(real_t p_path_max_distance) {
575 if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
576 return;
577 }
578
579 path_max_distance = p_path_max_distance;
580}
581
582real_t NavigationAgent3D::get_path_max_distance() {
583 return path_max_distance;
584}
585
586void NavigationAgent3D::set_target_position(Vector3 p_position) {
587 // Intentionally not checking for equality of the parameter, as we want to update the path even if the target position is the same in case the world changed.
588 // Revisit later when the navigation server can update the path without requesting a new path.
589
590 target_position = p_position;
591 target_position_submitted = true;
592
593 _request_repath();
594}
595
596Vector3 NavigationAgent3D::get_target_position() const {
597 return target_position;
598}
599
600Vector3 NavigationAgent3D::get_next_path_position() {
601 update_navigation();
602
603 const Vector<Vector3> &navigation_path = navigation_result->get_path();
604 if (navigation_path.size() == 0) {
605 ERR_FAIL_NULL_V_MSG(agent_parent, Vector3(), "The agent has no parent.");
606 return agent_parent->get_global_position();
607 } else {
608 return navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
609 }
610}
611
612real_t NavigationAgent3D::distance_to_target() const {
613 ERR_FAIL_NULL_V_MSG(agent_parent, 0.0, "The agent has no parent.");
614 return agent_parent->get_global_position().distance_to(target_position);
615}
616
617bool NavigationAgent3D::is_target_reached() const {
618 return target_reached;
619}
620
621bool NavigationAgent3D::is_target_reachable() {
622 return target_desired_distance >= get_final_position().distance_to(target_position);
623}
624
625bool NavigationAgent3D::is_navigation_finished() {
626 update_navigation();
627 return navigation_finished;
628}
629
630Vector3 NavigationAgent3D::get_final_position() {
631 update_navigation();
632
633 const Vector<Vector3> &navigation_path = navigation_result->get_path();
634 if (navigation_path.size() == 0) {
635 return Vector3();
636 }
637 return navigation_path[navigation_path.size() - 1];
638}
639
640void NavigationAgent3D::set_velocity_forced(Vector3 p_velocity) {
641 // Intentionally not checking for equality of the parameter.
642 // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
643 // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
644
645 velocity_forced = p_velocity;
646 velocity_forced_submitted = true;
647}
648
649void NavigationAgent3D::set_velocity(const Vector3 p_velocity) {
650 velocity = p_velocity;
651 velocity_submitted = true;
652}
653
654void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
655 safe_velocity = p_new_velocity;
656 if (!use_3d_avoidance) {
657 safe_velocity.y = stored_y_velocity;
658 }
659 emit_signal(SNAME("velocity_computed"), safe_velocity);
660}
661
662PackedStringArray NavigationAgent3D::get_configuration_warnings() const {
663 PackedStringArray warnings = Node::get_configuration_warnings();
664
665 if (!Object::cast_to<Node3D>(get_parent())) {
666 warnings.push_back(RTR("The NavigationAgent3D can be used only under a Node3D inheriting parent node."));
667 }
668
669 return warnings;
670}
671
672void NavigationAgent3D::update_navigation() {
673 if (agent_parent == nullptr) {
674 return;
675 }
676 if (!agent_parent->is_inside_tree()) {
677 return;
678 }
679 if (!target_position_submitted) {
680 return;
681 }
682
683 update_frame_id = Engine::get_singleton()->get_physics_frames();
684
685 Vector3 origin = agent_parent->get_global_position();
686
687 bool reload_path = false;
688
689 if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
690 reload_path = true;
691 } else if (navigation_result->get_path().size() == 0) {
692 reload_path = true;
693 } else {
694 // Check if too far from the navigation path
695 if (navigation_path_index > 0) {
696 const Vector<Vector3> &navigation_path = navigation_result->get_path();
697
698 Vector3 segment[2];
699 segment[0] = navigation_path[navigation_path_index - 1];
700 segment[1] = navigation_path[navigation_path_index];
701 segment[0].y -= path_height_offset;
702 segment[1].y -= path_height_offset;
703 Vector3 p = Geometry3D::get_closest_point_to_segment(origin, segment);
704 if (origin.distance_to(p) >= path_max_distance) {
705 // To faraway, reload path
706 reload_path = true;
707 }
708 }
709 }
710
711 if (reload_path) {
712 navigation_query->set_start_position(origin);
713 navigation_query->set_target_position(target_position);
714 navigation_query->set_navigation_layers(navigation_layers);
715 navigation_query->set_metadata_flags(path_metadata_flags);
716
717 if (map_override.is_valid()) {
718 navigation_query->set_map(map_override);
719 } else {
720 navigation_query->set_map(agent_parent->get_world_3d()->get_navigation_map());
721 }
722
723 NavigationServer3D::get_singleton()->query_path(navigation_query, navigation_result);
724#ifdef DEBUG_ENABLED
725 debug_path_dirty = true;
726#endif // DEBUG_ENABLED
727 navigation_finished = false;
728 navigation_path_index = 0;
729 emit_signal(SNAME("path_changed"));
730 }
731
732 if (navigation_result->get_path().size() == 0) {
733 return;
734 }
735
736 // Check if we can advance the navigation path
737 if (navigation_finished == false) {
738 // Advances to the next far away position.
739 const Vector<Vector3> &navigation_path = navigation_result->get_path();
740 const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
741 const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
742 const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
743
744 while (origin.distance_to(navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0)) < path_desired_distance) {
745 Dictionary details;
746
747 const Vector3 waypoint = navigation_path[navigation_path_index];
748 details[SNAME("position")] = waypoint;
749
750 int waypoint_type = -1;
751 if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
752 const NavigationPathQueryResult3D::PathSegmentType type = NavigationPathQueryResult3D::PathSegmentType(navigation_path_types[navigation_path_index]);
753
754 details[SNAME("type")] = type;
755 waypoint_type = type;
756 }
757
758 if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
759 details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
760 }
761
762 if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
763 const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
764
765 // Get a reference to the owning object.
766 Object *owner = nullptr;
767 if (waypoint_owner_id.is_valid()) {
768 owner = ObjectDB::get_instance(waypoint_owner_id);
769 }
770
771 details[SNAME("owner")] = owner;
772
773 if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
774 const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
775 if (navlink) {
776 Vector3 link_global_start_position = navlink->get_global_start_position();
777 Vector3 link_global_end_position = navlink->get_global_end_position();
778 if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
779 details[SNAME("link_entry_position")] = link_global_start_position;
780 details[SNAME("link_exit_position")] = link_global_end_position;
781 } else {
782 details[SNAME("link_entry_position")] = link_global_end_position;
783 details[SNAME("link_exit_position")] = link_global_start_position;
784 }
785 }
786 }
787 }
788
789 // Emit a signal for the waypoint
790 emit_signal(SNAME("waypoint_reached"), details);
791
792 // Emit a signal if we've reached a navigation link
793 if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
794 emit_signal(SNAME("link_reached"), details);
795 }
796
797 // Move to the next waypoint on the list
798 navigation_path_index += 1;
799
800 // Check to see if we've finished our route
801 if (navigation_path_index == navigation_path.size()) {
802 _check_distance_to_target();
803 navigation_path_index -= 1;
804 navigation_finished = true;
805 target_position_submitted = false;
806 if (avoidance_enabled) {
807 NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
808 NavigationServer3D::get_singleton()->agent_set_velocity(agent, Vector3(0.0, 0.0, 0.0));
809 NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, Vector3(0.0, 0.0, 0.0));
810 }
811 emit_signal(SNAME("navigation_finished"));
812 break;
813 }
814 }
815 }
816}
817
818void NavigationAgent3D::_request_repath() {
819 navigation_result->reset();
820 target_reached = false;
821 navigation_finished = false;
822 update_frame_id = 0;
823}
824
825void NavigationAgent3D::_check_distance_to_target() {
826 if (!target_reached) {
827 if (distance_to_target() < target_desired_distance) {
828 target_reached = true;
829 emit_signal(SNAME("target_reached"));
830 }
831 }
832}
833
834void NavigationAgent3D::set_avoidance_layers(uint32_t p_layers) {
835 avoidance_layers = p_layers;
836 NavigationServer3D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);
837}
838
839uint32_t NavigationAgent3D::get_avoidance_layers() const {
840 return avoidance_layers;
841}
842
843void NavigationAgent3D::set_avoidance_mask(uint32_t p_mask) {
844 avoidance_mask = p_mask;
845 NavigationServer3D::get_singleton()->agent_set_avoidance_mask(get_rid(), avoidance_mask);
846}
847
848uint32_t NavigationAgent3D::get_avoidance_mask() const {
849 return avoidance_mask;
850}
851
852void NavigationAgent3D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
853 ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
854 ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
855 uint32_t avoidance_layers_new = get_avoidance_layers();
856 if (p_value) {
857 avoidance_layers_new |= 1 << (p_layer_number - 1);
858 } else {
859 avoidance_layers_new &= ~(1 << (p_layer_number - 1));
860 }
861 set_avoidance_layers(avoidance_layers_new);
862}
863
864bool NavigationAgent3D::get_avoidance_layer_value(int p_layer_number) const {
865 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
866 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
867 return get_avoidance_layers() & (1 << (p_layer_number - 1));
868}
869
870void NavigationAgent3D::set_avoidance_mask_value(int p_mask_number, bool p_value) {
871 ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");
872 ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");
873 uint32_t mask = get_avoidance_mask();
874 if (p_value) {
875 mask |= 1 << (p_mask_number - 1);
876 } else {
877 mask &= ~(1 << (p_mask_number - 1));
878 }
879 set_avoidance_mask(mask);
880}
881
882bool NavigationAgent3D::get_avoidance_mask_value(int p_mask_number) const {
883 ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");
884 ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");
885 return get_avoidance_mask() & (1 << (p_mask_number - 1));
886}
887
888void NavigationAgent3D::set_avoidance_priority(real_t p_priority) {
889 ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
890 ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
891 avoidance_priority = p_priority;
892 NavigationServer3D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);
893}
894
895real_t NavigationAgent3D::get_avoidance_priority() const {
896 return avoidance_priority;
897}
898
899////////DEBUG////////////////////////////////////////////////////////////
900
901void NavigationAgent3D::set_debug_enabled(bool p_enabled) {
902#ifdef DEBUG_ENABLED
903 if (debug_enabled == p_enabled) {
904 return;
905 }
906
907 debug_enabled = p_enabled;
908 debug_path_dirty = true;
909#endif // DEBUG_ENABLED
910}
911
912bool NavigationAgent3D::get_debug_enabled() const {
913 return debug_enabled;
914}
915
916void NavigationAgent3D::set_debug_use_custom(bool p_enabled) {
917#ifdef DEBUG_ENABLED
918 if (debug_use_custom == p_enabled) {
919 return;
920 }
921
922 debug_use_custom = p_enabled;
923 debug_path_dirty = true;
924#endif // DEBUG_ENABLED
925}
926
927bool NavigationAgent3D::get_debug_use_custom() const {
928 return debug_use_custom;
929}
930
931void NavigationAgent3D::set_debug_path_custom_color(Color p_color) {
932#ifdef DEBUG_ENABLED
933 if (debug_path_custom_color == p_color) {
934 return;
935 }
936
937 debug_path_custom_color = p_color;
938 debug_path_dirty = true;
939#endif // DEBUG_ENABLED
940}
941
942Color NavigationAgent3D::get_debug_path_custom_color() const {
943 return debug_path_custom_color;
944}
945
946void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
947#ifdef DEBUG_ENABLED
948 if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
949 return;
950 }
951
952 debug_path_custom_point_size = MAX(0.0, p_point_size);
953 debug_path_dirty = true;
954#endif // DEBUG_ENABLED
955}
956
957float NavigationAgent3D::get_debug_path_custom_point_size() const {
958 return debug_path_custom_point_size;
959}
960
961#ifdef DEBUG_ENABLED
962void NavigationAgent3D::_navigation_debug_changed() {
963 debug_path_dirty = true;
964}
965
966void NavigationAgent3D::_update_debug_path() {
967 if (!debug_path_dirty) {
968 return;
969 }
970 debug_path_dirty = false;
971
972 if (!debug_path_instance.is_valid()) {
973 debug_path_instance = RenderingServer::get_singleton()->instance_create();
974 }
975
976 if (!debug_path_mesh.is_valid()) {
977 debug_path_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
978 }
979
980 debug_path_mesh->clear_surfaces();
981
982 if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
983 return;
984 }
985
986 if (!(agent_parent && agent_parent->is_inside_tree())) {
987 return;
988 }
989
990 const Vector<Vector3> &navigation_path = navigation_result->get_path();
991
992 if (navigation_path.size() <= 1) {
993 return;
994 }
995
996 Vector<Vector3> debug_path_lines_vertex_array;
997
998 for (int i = 0; i < navigation_path.size() - 1; i++) {
999 debug_path_lines_vertex_array.push_back(navigation_path[i]);
1000 debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
1001 }
1002
1003 Array debug_path_lines_mesh_array;
1004 debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
1005 debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
1006
1007 debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
1008
1009 Ref<StandardMaterial3D> debug_agent_path_line_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_line_material();
1010 if (debug_use_custom) {
1011 if (!debug_agent_path_line_custom_material.is_valid()) {
1012 debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
1013 }
1014 debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
1015 debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
1016 } else {
1017 debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
1018 }
1019
1020 if (debug_path_custom_point_size > 0.0) {
1021 Vector<Vector3> debug_path_points_vertex_array;
1022
1023 for (int i = 0; i < navigation_path.size(); i++) {
1024 debug_path_points_vertex_array.push_back(navigation_path[i]);
1025 }
1026
1027 Array debug_path_points_mesh_array;
1028 debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
1029 debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_points_vertex_array;
1030
1031 debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
1032
1033 Ref<StandardMaterial3D> debug_agent_path_point_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_point_material();
1034 if (debug_use_custom) {
1035 if (!debug_agent_path_point_custom_material.is_valid()) {
1036 debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
1037 }
1038 debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
1039 debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
1040 debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
1041 } else {
1042 debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
1043 }
1044 }
1045
1046 RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
1047 RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
1048 RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
1049}
1050#endif // DEBUG_ENABLED
1051