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