Debian Codegraph @

scene/3d/navigation/navigation_link_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* navigation_link_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_link_3d.h"
32
33 #include "servers/navigation_3d/navigation_server_3d.h"
34
35 #ifdef DEBUG_ENABLED
36 void NavigationLink3D::_update_debug_mesh() {
37 if (!is_inside_tree()) {
38 return;
39 }
40
41 if (Engine::get_singleton()->is_editor_hint()) {
42 // don't update inside Editor as node 3d gizmo takes care of this
43 // as collisions and selections for Editor Viewport need to be updated
44 return;
45 }
46
47 if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
48 if (debug_instance.is_valid()) {
49 RS::get_singleton()->instance_set_visible(debug_instance, false);
50 }
51 return;
52 }
53
54 if (!debug_instance.is_valid()) {
55 debug_instance = RenderingServer::get_singleton()->instance_create();
56 }
57
58 if (debug_mesh.is_null()) {
59 debug_mesh.instantiate();
60 }
61
62 RID nav_map = get_world_3d()->get_navigation_map();
63 real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
64 Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
65 Vector3::Axis up_axis = up_vector.max_axis_index();
66
67 debug_mesh->clear_surfaces();
68
69 Vector<Vector3> lines;
70
71 // Draw line between the points.
72 lines.push_back(start_position);
73 lines.push_back(end_position);
74
75 // Draw start position search radius
76 for (int i = 0; i < 30; i++) {
77 // Create a circle
78 const float ra = Math::deg_to_rad((float)(i * 12));
79 const float rb = Math::deg_to_rad((float)((i + 1) * 12));
80 const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
81 const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
82
83 // Draw axis-aligned circle
84 switch (up_axis) {
85 case Vector3::AXIS_X:
86 lines.append(start_position + Vector3(0, a.x, a.y));
87 lines.append(start_position + Vector3(0, b.x, b.y));
88 break;
89 case Vector3::AXIS_Y:
90 lines.append(start_position + Vector3(a.x, 0, a.y));
91 lines.append(start_position + Vector3(b.x, 0, b.y));
92 break;
93 case Vector3::AXIS_Z:
94 lines.append(start_position + Vector3(a.x, a.y, 0));
95 lines.append(start_position + Vector3(b.x, b.y, 0));
96 break;
97 }
98 }
99
100 // Draw end position search radius
101 for (int i = 0; i < 30; i++) {
102 // Create a circle
103 const float ra = Math::deg_to_rad((float)(i * 12));
104 const float rb = Math::deg_to_rad((float)((i + 1) * 12));
105 const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
106 const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;
107
108 // Draw axis-aligned circle
109 switch (up_axis) {
110 case Vector3::AXIS_X:
111 lines.append(end_position + Vector3(0, a.x, a.y));
112 lines.append(end_position + Vector3(0, b.x, b.y));
113 break;
114 case Vector3::AXIS_Y:
115 lines.append(end_position + Vector3(a.x, 0, a.y));
116 lines.append(end_position + Vector3(b.x, 0, b.y));
117 break;
118 case Vector3::AXIS_Z:
119 lines.append(end_position + Vector3(a.x, a.y, 0));
120 lines.append(end_position + Vector3(b.x, b.y, 0));
121 break;
122 }
123 }
124
125 const Vector3 link_segment = end_position - start_position;
126 const Vector3 up = Vector3(0.0, 1.0, 0.0);
127 const float arror_len = 0.5;
128
129 {
130 Vector3 anchor = start_position + (link_segment * 0.75);
131 Vector3 direction = start_position.direction_to(end_position);
132 Vector3 arrow_dir = direction.cross(up);
133 lines.push_back(anchor);
134 lines.push_back(anchor + (arrow_dir - direction) * arror_len);
135
136 arrow_dir = -direction.cross(up);
137 lines.push_back(anchor);
138 lines.push_back(anchor + (arrow_dir - direction) * arror_len);
139 }
140
141 if (is_bidirectional()) {
142 Vector3 anchor = start_position + (link_segment * 0.25);
143 Vector3 direction = end_position.direction_to(start_position);
144 Vector3 arrow_dir = direction.cross(up);
145 lines.push_back(anchor);
146 lines.push_back(anchor + (arrow_dir - direction) * arror_len);
147
148 arrow_dir = -direction.cross(up);
149 lines.push_back(anchor);
150 lines.push_back(anchor + (arrow_dir - direction) * arror_len);
151 }
152
153 Array mesh_array;
154 mesh_array.resize(Mesh::ARRAY_MAX);
155 mesh_array[Mesh::ARRAY_VERTEX] = lines;
156
157 debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array);
158
159 RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
160 RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
161 RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
162
163 Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
164 Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
165
166 if (enabled) {
167 RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
168 } else {
169 RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
170 }
171
172 RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
173 }
174 #endif // DEBUG_ENABLED
175
176 void NavigationLink3D::_bind_methods() {
177 ClassDB::bind_method(D_METHOD("get_rid"), &NavigationLink3D::get_rid);
178
179 ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationLink3D::set_enabled);
180 ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationLink3D::is_enabled);
181
182 ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationLink3D::set_navigation_map);
183 ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationLink3D::get_navigation_map);
184
185 ClassDB::bind_method(D_METHOD("set_bidirectional", "bidirectional"), &NavigationLink3D::set_bidirectional);
186 ClassDB::bind_method(D_METHOD("is_bidirectional"), &NavigationLink3D::is_bidirectional);
187
188 ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationLink3D::set_navigation_layers);
189 ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationLink3D::get_navigation_layers);
190
191 ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationLink3D::set_navigation_layer_value);
192 ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationLink3D::get_navigation_layer_value);
193
194 ClassDB::bind_method(D_METHOD("set_start_position", "position"), &NavigationLink3D::set_start_position);
195 ClassDB::bind_method(D_METHOD("get_start_position"), &NavigationLink3D::get_start_position);
196
197 ClassDB::bind_method(D_METHOD("set_end_position", "position"), &NavigationLink3D::set_end_position);
198 ClassDB::bind_method(D_METHOD("get_end_position"), &NavigationLink3D::get_end_position);
199
200 ClassDB::bind_method(D_METHOD("set_global_start_position", "position"), &NavigationLink3D::set_global_start_position);
201 ClassDB::bind_method(D_METHOD("get_global_start_position"), &NavigationLink3D::get_global_start_position);
202
203 ClassDB::bind_method(D_METHOD("set_global_end_position", "position"), &NavigationLink3D::set_global_end_position);
204 ClassDB::bind_method(D_METHOD("get_global_end_position"), &NavigationLink3D::get_global_end_position);
205
206 ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationLink3D::set_enter_cost);
207 ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationLink3D::get_enter_cost);
208
209 ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationLink3D::set_travel_cost);
210 ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationLink3D::get_travel_cost);
211
212 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
213 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bidirectional"), "set_bidirectional", "is_bidirectional");
214 ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
215 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "start_position"), "set_start_position", "get_start_position");
216 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "end_position"), "set_end_position", "get_end_position");
217 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
218 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
219 }
220
221 #ifndef DISABLE_DEPRECATED
222 bool NavigationLink3D::_set(const StringName &p_name, const Variant &p_value) {
223 if (p_name == "start_location") {
224 set_start_position(p_value);
225 return true;
226 }
227 if (p_name == "end_location") {
228 set_end_position(p_value);
229 return true;
230 }
231 return false;
232 }
233
234 bool NavigationLink3D::_get(const StringName &p_name, Variant &r_ret) const {
235 if (p_name == "start_location") {
236 r_ret = get_start_position();
237 return true;
238 }
239 if (p_name == "end_location") {
240 r_ret = get_end_position();
241 return true;
242 }
243 return false;
244 }
245 #endif // DISABLE_DEPRECATED
246
247 void NavigationLink3D::_notification(int p_what) {
248 switch (p_what) {
249 case NOTIFICATION_ENTER_TREE: {
250 _link_enter_navigation_map();
251 } break;
252
253 case NOTIFICATION_TRANSFORM_CHANGED: {
254 set_physics_process_internal(true);
255 } break;
256
257 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
258 set_physics_process_internal(false);
259 _link_update_transform();
260 } break;
261
262 case NOTIFICATION_EXIT_TREE: {
263 _link_exit_navigation_map();
264 } break;
265
266 #ifdef DEBUG_ENABLED
267 case NOTIFICATION_VISIBILITY_CHANGED: {
268 _update_debug_mesh();
269 } break;
270 #endif // DEBUG_ENABLED
271 }
272 }
273
274 NavigationLink3D::NavigationLink3D() {
275 link = NavigationServer3D::get_singleton()->link_create();
276
277 NavigationServer3D::get_singleton()->link_set_owner_id(link, get_instance_id());
278 NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
279 NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
280 NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
281 NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
282 NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
283
284 set_notify_transform(true);
285 }
286
287 NavigationLink3D::~NavigationLink3D() {
288 ERR_FAIL_NULL(NavigationServer3D::get_singleton());
289 NavigationServer3D::get_singleton()->free_rid(link);
290 link = RID();
291
292 #ifdef DEBUG_ENABLED
293 ERR_FAIL_NULL(RenderingServer::get_singleton());
294 if (debug_instance.is_valid()) {
295 RenderingServer::get_singleton()->free_rid(debug_instance);
296 }
297 if (debug_mesh.is_valid()) {
298 RenderingServer::get_singleton()->free_rid(debug_mesh->get_rid());
299 }
300 #endif // DEBUG_ENABLED
301 }
302
303 RID NavigationLink3D::get_rid() const {
304 return link;
305 }
306
307 void NavigationLink3D::set_enabled(bool p_enabled) {
308 if (enabled == p_enabled) {
309 return;
310 }
311
312 enabled = p_enabled;
313
314 NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
315
316 #ifdef DEBUG_ENABLED
317 if (debug_instance.is_valid() && debug_mesh.is_valid()) {
318 if (enabled) {
319 Ref<StandardMaterial3D> link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_material();
320 RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, link_material->get_rid());
321 } else {
322 Ref<StandardMaterial3D> disabled_link_material = NavigationServer3D::get_singleton()->get_debug_navigation_link_connections_disabled_material();
323 RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, disabled_link_material->get_rid());
324 }
325 }
326 #endif // DEBUG_ENABLED
327
328 update_gizmos();
329 }
330
331 void NavigationLink3D::set_navigation_map(RID p_navigation_map) {
332 if (map_override == p_navigation_map) {
333 return;
334 }
335
336 map_override = p_navigation_map;
337
338 NavigationServer3D::get_singleton()->link_set_map(link, map_override);
339 }
340
341 RID NavigationLink3D::get_navigation_map() const {
342 if (map_override.is_valid()) {
343 return map_override;
344 } else if (is_inside_tree()) {
345 return get_world_3d()->get_navigation_map();
346 }
347 return RID();
348 }
349
350 void NavigationLink3D::set_bidirectional(bool p_bidirectional) {
351 if (bidirectional == p_bidirectional) {
352 return;
353 }
354
355 bidirectional = p_bidirectional;
356
357 NavigationServer3D::get_singleton()->link_set_bidirectional(link, bidirectional);
358
359 #ifdef DEBUG_ENABLED
360 _update_debug_mesh();
361 #endif // DEBUG_ENABLED
362
363 update_gizmos();
364 }
365
366 void NavigationLink3D::set_navigation_layers(uint32_t p_navigation_layers) {
367 if (navigation_layers == p_navigation_layers) {
368 return;
369 }
370
371 navigation_layers = p_navigation_layers;
372
373 NavigationServer3D::get_singleton()->link_set_navigation_layers(link, navigation_layers);
374 }
375
376 void NavigationLink3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
377 ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
378 ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
379
380 uint32_t _navigation_layers = get_navigation_layers();
381
382 if (p_value) {
383 _navigation_layers |= 1 << (p_layer_number - 1);
384 } else {
385 _navigation_layers &= ~(1 << (p_layer_number - 1));
386 }
387
388 set_navigation_layers(_navigation_layers);
389 }
390
391 bool NavigationLink3D::get_navigation_layer_value(int p_layer_number) const {
392 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
393 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
394
395 return get_navigation_layers() & (1 << (p_layer_number - 1));
396 }
397
398 void NavigationLink3D::set_start_position(Vector3 p_position) {
399 if (start_position.is_equal_approx(p_position)) {
400 return;
401 }
402
403 start_position = p_position;
404
405 if (!is_inside_tree()) {
406 return;
407 }
408
409 NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
410
411 #ifdef DEBUG_ENABLED
412 _update_debug_mesh();
413 #endif // DEBUG_ENABLED
414
415 update_gizmos();
416 update_configuration_warnings();
417 }
418
419 void NavigationLink3D::set_end_position(Vector3 p_position) {
420 if (end_position.is_equal_approx(p_position)) {
421 return;
422 }
423
424 end_position = p_position;
425
426 if (!is_inside_tree()) {
427 return;
428 }
429
430 NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
431
432 #ifdef DEBUG_ENABLED
433 _update_debug_mesh();
434 #endif // DEBUG_ENABLED
435
436 update_gizmos();
437 update_configuration_warnings();
438 }
439
440 void NavigationLink3D::set_global_start_position(Vector3 p_position) {
441 if (is_inside_tree()) {
442 set_start_position(to_local(p_position));
443 } else {
444 set_start_position(p_position);
445 }
446 }
447
448 Vector3 NavigationLink3D::get_global_start_position() const {
449 if (is_inside_tree()) {
450 return to_global(start_position);
451 } else {
452 return start_position;
453 }
454 }
455
456 void NavigationLink3D::set_global_end_position(Vector3 p_position) {
457 if (is_inside_tree()) {
458 set_end_position(to_local(p_position));
459 } else {
460 set_end_position(p_position);
461 }
462 }
463
464 Vector3 NavigationLink3D::get_global_end_position() const {
465 if (is_inside_tree()) {
466 return to_global(end_position);
467 } else {
468 return end_position;
469 }
470 }
471
472 void NavigationLink3D::set_enter_cost(real_t p_enter_cost) {
473 ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
474 if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
475 return;
476 }
477
478 enter_cost = p_enter_cost;
479
480 NavigationServer3D::get_singleton()->link_set_enter_cost(link, enter_cost);
481 }
482
483 void NavigationLink3D::set_travel_cost(real_t p_travel_cost) {
484 ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
485 if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
486 return;
487 }
488
489 travel_cost = p_travel_cost;
490
491 NavigationServer3D::get_singleton()->link_set_travel_cost(link, travel_cost);
492 }
493
494 PackedStringArray NavigationLink3D::get_configuration_warnings() const {
495 PackedStringArray warnings = Node3D::get_configuration_warnings();
496
497 if (start_position.is_equal_approx(end_position)) {
498 warnings.push_back(RTR("NavigationLink3D start position should be different than the end position to be useful."));
499 }
500
501 return warnings;
502 }
503
504 void NavigationLink3D::_link_enter_navigation_map() {
505 if (!is_inside_tree()) {
506 return;
507 }
508
509 if (map_override.is_valid()) {
510 NavigationServer3D::get_singleton()->link_set_map(link, map_override);
511 } else {
512 NavigationServer3D::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
513 }
514
515 current_global_transform = get_global_transform();
516 NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
517 NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
518 NavigationServer3D::get_singleton()->link_set_enabled(link, enabled);
519
520 #ifdef DEBUG_ENABLED
521 if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
522 _update_debug_mesh();
523 }
524 #endif // DEBUG_ENABLED
525 }
526
527 void NavigationLink3D::_link_exit_navigation_map() {
528 NavigationServer3D::get_singleton()->link_set_map(link, RID());
529 #ifdef DEBUG_ENABLED
530 if (debug_instance.is_valid()) {
531 RS::get_singleton()->instance_set_visible(debug_instance, false);
532 }
533 #endif // DEBUG_ENABLED
534 }
535
536 void NavigationLink3D::_link_update_transform() {
537 if (!is_inside_tree()) {
538 return;
539 }
540
541 Transform3D new_global_transform = get_global_transform();
542 if (current_global_transform != new_global_transform) {
543 current_global_transform = new_global_transform;
544 NavigationServer3D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position));
545 NavigationServer3D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position));
546 #ifdef DEBUG_ENABLED
547 if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
548 _update_debug_mesh();
549 }
550 #endif // DEBUG_ENABLED
551 }
552 }