| 1 | /**************************************************************************/ |
| 2 | /* navigation_region_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_region_3d.h" |
| 32 | |
| 33 | #include "core/math/random_pcg.h" |
| 34 | #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h" |
| 35 | #include "servers/navigation_3d/navigation_server_3d.h" |
| 36 | |
| 37 | RID NavigationRegion3D::get_rid() const { |
| 38 | return region; |
| 39 | } |
| 40 | |
| 41 | void NavigationRegion3D::set_enabled(bool p_enabled) { |
| 42 | if (enabled == p_enabled) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | enabled = p_enabled; |
| 47 | |
| 48 | NavigationServer3D::get_singleton()->region_set_enabled(region, enabled); |
| 49 | |
| 50 | #ifdef DEBUG_ENABLED |
| 51 | if (debug_instance.is_valid()) { |
| 52 | if (!is_enabled()) { |
| 53 | if (debug_mesh.is_valid()) { |
| 54 | if (debug_mesh->get_surface_count() > 0) { |
| 55 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid()); |
| 56 | } |
| 57 | if (debug_mesh->get_surface_count() > 1) { |
| 58 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid()); |
| 59 | } |
| 60 | } |
| 61 | } else { |
| 62 | if (debug_mesh.is_valid()) { |
| 63 | if (debug_mesh->get_surface_count() > 0) { |
| 64 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID()); |
| 65 | } |
| 66 | if (debug_mesh->get_surface_count() > 1) { |
| 67 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID()); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | #endif // DEBUG_ENABLED |
| 73 | |
| 74 | update_gizmos(); |
| 75 | } |
| 76 | |
| 77 | bool NavigationRegion3D::is_enabled() const { |
| 78 | return enabled; |
| 79 | } |
| 80 | |
| 81 | void NavigationRegion3D::set_use_edge_connections(bool p_enabled) { |
| 82 | if (use_edge_connections == p_enabled) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | use_edge_connections = p_enabled; |
| 87 | |
| 88 | NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections); |
| 89 | } |
| 90 | |
| 91 | bool NavigationRegion3D::get_use_edge_connections() const { |
| 92 | return use_edge_connections; |
| 93 | } |
| 94 | |
| 95 | void NavigationRegion3D::set_navigation_layers(uint32_t p_navigation_layers) { |
| 96 | if (navigation_layers == p_navigation_layers) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | navigation_layers = p_navigation_layers; |
| 101 | |
| 102 | NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers); |
| 103 | } |
| 104 | |
| 105 | uint32_t NavigationRegion3D::get_navigation_layers() const { |
| 106 | return navigation_layers; |
| 107 | } |
| 108 | |
| 109 | void NavigationRegion3D::set_navigation_layer_value(int p_layer_number, bool p_value) { |
| 110 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive."); |
| 111 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive."); |
| 112 | |
| 113 | uint32_t _navigation_layers = get_navigation_layers(); |
| 114 | |
| 115 | if (p_value) { |
| 116 | _navigation_layers |= 1 << (p_layer_number - 1); |
| 117 | } else { |
| 118 | _navigation_layers &= ~(1 << (p_layer_number - 1)); |
| 119 | } |
| 120 | |
| 121 | set_navigation_layers(_navigation_layers); |
| 122 | } |
| 123 | |
| 124 | bool NavigationRegion3D::get_navigation_layer_value(int p_layer_number) const { |
| 125 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive."); |
| 126 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive."); |
| 127 | |
| 128 | return get_navigation_layers() & (1 << (p_layer_number - 1)); |
| 129 | } |
| 130 | |
| 131 | void NavigationRegion3D::set_enter_cost(real_t p_enter_cost) { |
| 132 | ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive."); |
| 133 | if (Math::is_equal_approx(enter_cost, p_enter_cost)) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | enter_cost = p_enter_cost; |
| 138 | |
| 139 | NavigationServer3D::get_singleton()->region_set_enter_cost(region, enter_cost); |
| 140 | } |
| 141 | |
| 142 | real_t NavigationRegion3D::get_enter_cost() const { |
| 143 | return enter_cost; |
| 144 | } |
| 145 | |
| 146 | void NavigationRegion3D::set_travel_cost(real_t p_travel_cost) { |
| 147 | ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive."); |
| 148 | if (Math::is_equal_approx(travel_cost, p_travel_cost)) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | travel_cost = p_travel_cost; |
| 153 | |
| 154 | NavigationServer3D::get_singleton()->region_set_travel_cost(region, travel_cost); |
| 155 | } |
| 156 | |
| 157 | real_t NavigationRegion3D::get_travel_cost() const { |
| 158 | return travel_cost; |
| 159 | } |
| 160 | |
| 161 | RID NavigationRegion3D::get_region_rid() const { |
| 162 | return get_rid(); |
| 163 | } |
| 164 | |
| 165 | void NavigationRegion3D::_notification(int p_what) { |
| 166 | switch (p_what) { |
| 167 | case NOTIFICATION_ENTER_TREE: { |
| 168 | _region_enter_navigation_map(); |
| 169 | } break; |
| 170 | |
| 171 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 172 | set_physics_process_internal(true); |
| 173 | } break; |
| 174 | |
| 175 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { |
| 176 | set_physics_process_internal(false); |
| 177 | _region_update_transform(); |
| 178 | } break; |
| 179 | |
| 180 | case NOTIFICATION_EXIT_TREE: { |
| 181 | _region_exit_navigation_map(); |
| 182 | } break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void NavigationRegion3D::set_navigation_mesh(const Ref<NavigationMesh> &p_navigation_mesh) { |
| 187 | if (navigation_mesh.is_valid()) { |
| 188 | navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed)); |
| 189 | } |
| 190 | |
| 191 | navigation_mesh = p_navigation_mesh; |
| 192 | |
| 193 | if (navigation_mesh.is_valid()) { |
| 194 | navigation_mesh->connect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed)); |
| 195 | } |
| 196 | |
| 197 | _navigation_mesh_changed(); |
| 198 | } |
| 199 | |
| 200 | Ref<NavigationMesh> NavigationRegion3D::get_navigation_mesh() const { |
| 201 | return navigation_mesh; |
| 202 | } |
| 203 | |
| 204 | void NavigationRegion3D::set_navigation_map(RID p_navigation_map) { |
| 205 | if (map_override == p_navigation_map) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | map_override = p_navigation_map; |
| 210 | |
| 211 | NavigationServer3D::get_singleton()->region_set_map(region, map_override); |
| 212 | } |
| 213 | |
| 214 | RID NavigationRegion3D::get_navigation_map() const { |
| 215 | if (map_override.is_valid()) { |
| 216 | return map_override; |
| 217 | } else if (is_inside_tree()) { |
| 218 | return get_world_3d()->get_navigation_map(); |
| 219 | } |
| 220 | return RID(); |
| 221 | } |
| 222 | |
| 223 | void NavigationRegion3D::bake_navigation_mesh(bool p_on_thread) { |
| 224 | ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred()."); |
| 225 | ERR_FAIL_COND_MSG(navigation_mesh.is_null(), "Baking the navigation mesh requires a valid `NavigationMesh` resource."); |
| 226 | |
| 227 | Ref<NavigationMeshSourceGeometryData3D> source_geometry_data; |
| 228 | source_geometry_data.instantiate(); |
| 229 | |
| 230 | NavigationServer3D::get_singleton()->parse_source_geometry_data(navigation_mesh, source_geometry_data, this); |
| 231 | |
| 232 | if (p_on_thread) { |
| 233 | NavigationServer3D::get_singleton()->bake_from_source_geometry_data_async(navigation_mesh, source_geometry_data, callable_mp(this, &NavigationRegion3D::_bake_finished)); |
| 234 | } else { |
| 235 | NavigationServer3D::get_singleton()->bake_from_source_geometry_data(navigation_mesh, source_geometry_data, callable_mp(this, &NavigationRegion3D::_bake_finished)); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void NavigationRegion3D::_bake_finished() { |
| 240 | if (!Thread::is_main_thread()) { |
| 241 | callable_mp(this, &NavigationRegion3D::_bake_finished).call_deferred(); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | emit_signal(SNAME("bake_finished")); |
| 246 | } |
| 247 | |
| 248 | bool NavigationRegion3D::is_baking() const { |
| 249 | return NavigationServer3D::get_singleton()->is_baking_navigation_mesh(navigation_mesh); |
| 250 | } |
| 251 | |
| 252 | PackedStringArray NavigationRegion3D::get_configuration_warnings() const { |
| 253 | PackedStringArray warnings = Node3D::get_configuration_warnings(); |
| 254 | |
| 255 | if (is_visible_in_tree() && is_inside_tree()) { |
| 256 | if (navigation_mesh.is_null()) { |
| 257 | warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work.")); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return warnings; |
| 262 | } |
| 263 | |
| 264 | void NavigationRegion3D::_bind_methods() { |
| 265 | ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion3D::get_rid); |
| 266 | |
| 267 | ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navigation_mesh"), &NavigationRegion3D::set_navigation_mesh); |
| 268 | ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationRegion3D::get_navigation_mesh); |
| 269 | |
| 270 | ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion3D::set_enabled); |
| 271 | ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion3D::is_enabled); |
| 272 | |
| 273 | ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion3D::set_navigation_map); |
| 274 | ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion3D::get_navigation_map); |
| 275 | |
| 276 | ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion3D::set_use_edge_connections); |
| 277 | ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion3D::get_use_edge_connections); |
| 278 | |
| 279 | ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion3D::set_navigation_layers); |
| 280 | ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion3D::get_navigation_layers); |
| 281 | |
| 282 | ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion3D::set_navigation_layer_value); |
| 283 | ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion3D::get_navigation_layer_value); |
| 284 | |
| 285 | ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion3D::get_region_rid); |
| 286 | |
| 287 | ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion3D::set_enter_cost); |
| 288 | ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion3D::get_enter_cost); |
| 289 | |
| 290 | ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion3D::set_travel_cost); |
| 291 | ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion3D::get_travel_cost); |
| 292 | |
| 293 | ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationRegion3D::bake_navigation_mesh, DEFVAL(true)); |
| 294 | ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion3D::is_baking); |
| 295 | |
| 296 | ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationRegion3D::get_bounds); |
| 297 | |
| 298 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh"); |
| 299 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled"); |
| 300 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections"); |
| 301 | ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers"); |
| 302 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost"); |
| 303 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost"); |
| 304 | |
| 305 | ADD_SIGNAL(MethodInfo("navigation_mesh_changed")); |
| 306 | ADD_SIGNAL(MethodInfo("bake_finished")); |
| 307 | } |
| 308 | |
| 309 | #ifndef DISABLE_DEPRECATED |
| 310 | // Compatibility with earlier 4.0 betas. |
| 311 | bool NavigationRegion3D::_set(const StringName &p_name, const Variant &p_value) { |
| 312 | if (p_name == "navmesh") { |
| 313 | set_navigation_mesh(p_value); |
| 314 | return true; |
| 315 | } |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | bool NavigationRegion3D::_get(const StringName &p_name, Variant &r_ret) const { |
| 320 | if (p_name == "navmesh") { |
| 321 | r_ret = get_navigation_mesh(); |
| 322 | return true; |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | #endif // DISABLE_DEPRECATED |
| 327 | |
| 328 | void NavigationRegion3D::_navigation_mesh_changed() { |
| 329 | _update_bounds(); |
| 330 | |
| 331 | NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh); |
| 332 | |
| 333 | #ifdef DEBUG_ENABLED |
| 334 | if (is_inside_tree() && NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) { |
| 335 | if (navigation_mesh.is_valid()) { |
| 336 | _update_debug_mesh(); |
| 337 | _update_debug_edge_connections_mesh(); |
| 338 | } else { |
| 339 | if (debug_instance.is_valid()) { |
| 340 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
| 341 | } |
| 342 | if (debug_edge_connections_instance.is_valid()) { |
| 343 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | _update_debug_edge_connections_mesh(); |
| 349 | #endif // DEBUG_ENABLED |
| 350 | |
| 351 | emit_signal(SNAME("navigation_mesh_changed")); |
| 352 | |
| 353 | update_gizmos(); |
| 354 | update_configuration_warnings(); |
| 355 | } |
| 356 | |
| 357 | #ifdef DEBUG_ENABLED |
| 358 | void NavigationRegion3D::_navigation_map_changed(RID p_map) { |
| 359 | if (is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) { |
| 360 | _update_debug_edge_connections_mesh(); |
| 361 | } |
| 362 | } |
| 363 | #endif // DEBUG_ENABLED |
| 364 | |
| 365 | #ifdef DEBUG_ENABLED |
| 366 | void NavigationRegion3D::_navigation_debug_changed() { |
| 367 | if (is_inside_tree()) { |
| 368 | _update_debug_mesh(); |
| 369 | _update_debug_edge_connections_mesh(); |
| 370 | } |
| 371 | } |
| 372 | #endif // DEBUG_ENABLED |
| 373 | |
| 374 | void NavigationRegion3D::_region_enter_navigation_map() { |
| 375 | if (!is_inside_tree()) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | if (map_override.is_valid()) { |
| 380 | NavigationServer3D::get_singleton()->region_set_map(region, map_override); |
| 381 | } else { |
| 382 | NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map()); |
| 383 | } |
| 384 | |
| 385 | current_global_transform = get_global_transform(); |
| 386 | NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform); |
| 387 | |
| 388 | NavigationServer3D::get_singleton()->region_set_enabled(region, enabled); |
| 389 | |
| 390 | #ifdef DEBUG_ENABLED |
| 391 | if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) { |
| 392 | _update_debug_mesh(); |
| 393 | } |
| 394 | #endif // DEBUG_ENABLED |
| 395 | } |
| 396 | |
| 397 | void NavigationRegion3D::_region_exit_navigation_map() { |
| 398 | NavigationServer3D::get_singleton()->region_set_map(region, RID()); |
| 399 | #ifdef DEBUG_ENABLED |
| 400 | if (debug_instance.is_valid()) { |
| 401 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
| 402 | } |
| 403 | if (debug_edge_connections_instance.is_valid()) { |
| 404 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 405 | } |
| 406 | #endif // DEBUG_ENABLED |
| 407 | } |
| 408 | |
| 409 | void NavigationRegion3D::_region_update_transform() { |
| 410 | if (!is_inside_tree()) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | Transform3D new_global_transform = get_global_transform(); |
| 415 | if (current_global_transform != new_global_transform) { |
| 416 | current_global_transform = new_global_transform; |
| 417 | NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform); |
| 418 | #ifdef DEBUG_ENABLED |
| 419 | if (debug_instance.is_valid()) { |
| 420 | RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform); |
| 421 | } |
| 422 | #endif // DEBUG_ENABLED |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | NavigationRegion3D::NavigationRegion3D() { |
| 427 | set_notify_transform(true); |
| 428 | |
| 429 | region = NavigationServer3D::get_singleton()->region_create(); |
| 430 | NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id()); |
| 431 | NavigationServer3D::get_singleton()->region_set_enter_cost(region, get_enter_cost()); |
| 432 | NavigationServer3D::get_singleton()->region_set_travel_cost(region, get_travel_cost()); |
| 433 | NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers); |
| 434 | NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections); |
| 435 | NavigationServer3D::get_singleton()->region_set_enabled(region, enabled); |
| 436 | |
| 437 | #ifdef DEBUG_ENABLED |
| 438 | NavigationServer3D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed)); |
| 439 | NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_navigation_debug_changed)); |
| 440 | #endif // DEBUG_ENABLED |
| 441 | } |
| 442 | |
| 443 | NavigationRegion3D::~NavigationRegion3D() { |
| 444 | if (navigation_mesh.is_valid()) { |
| 445 | navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed)); |
| 446 | } |
| 447 | ERR_FAIL_NULL(NavigationServer3D::get_singleton()); |
| 448 | NavigationServer3D::get_singleton()->free_rid(region); |
| 449 | |
| 450 | #ifdef DEBUG_ENABLED |
| 451 | NavigationServer3D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed)); |
| 452 | NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_navigation_debug_changed)); |
| 453 | |
| 454 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 455 | if (debug_instance.is_valid()) { |
| 456 | RenderingServer::get_singleton()->free_rid(debug_instance); |
| 457 | } |
| 458 | if (debug_mesh.is_valid()) { |
| 459 | RenderingServer::get_singleton()->free_rid(debug_mesh->get_rid()); |
| 460 | } |
| 461 | if (debug_edge_connections_instance.is_valid()) { |
| 462 | RenderingServer::get_singleton()->free_rid(debug_edge_connections_instance); |
| 463 | } |
| 464 | if (debug_edge_connections_mesh.is_valid()) { |
| 465 | RenderingServer::get_singleton()->free_rid(debug_edge_connections_mesh->get_rid()); |
| 466 | } |
| 467 | #endif // DEBUG_ENABLED |
| 468 | } |
| 469 | |
| 470 | #ifdef DEBUG_ENABLED |
| 471 | void NavigationRegion3D::_update_debug_mesh() { |
| 472 | if (Engine::get_singleton()->is_editor_hint()) { |
| 473 | // don't update inside Editor as node 3d gizmo takes care of this |
| 474 | // as collisions and selections for Editor Viewport need to be updated |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | if (!NavigationServer3D::get_singleton()->get_debug_enabled() || !NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) { |
| 479 | if (debug_instance.is_valid()) { |
| 480 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
| 481 | } |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | if (navigation_mesh.is_null()) { |
| 486 | if (debug_instance.is_valid()) { |
| 487 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
| 488 | } |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | if (!debug_instance.is_valid()) { |
| 493 | debug_instance = RenderingServer::get_singleton()->instance_create(); |
| 494 | } |
| 495 | |
| 496 | if (debug_mesh.is_null()) { |
| 497 | debug_mesh.instantiate(); |
| 498 | } |
| 499 | |
| 500 | debug_mesh->clear_surfaces(); |
| 501 | |
| 502 | Vector<Vector3> vertices = navigation_mesh->get_vertices(); |
| 503 | if (vertices.is_empty()) { |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | int polygon_count = navigation_mesh->get_polygon_count(); |
| 508 | if (polygon_count == 0) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color(); |
| 513 | bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines(); |
| 514 | |
| 515 | int vertex_count = 0; |
| 516 | int line_count = 0; |
| 517 | |
| 518 | for (int i = 0; i < polygon_count; i++) { |
| 519 | const Vector<int> &polygon = navigation_mesh->get_polygon(i); |
| 520 | int polygon_size = polygon.size(); |
| 521 | if (polygon_size < 3) { |
| 522 | continue; |
| 523 | } |
| 524 | line_count += polygon_size * 2; |
| 525 | vertex_count += (polygon_size - 2) * 3; |
| 526 | } |
| 527 | |
| 528 | Vector<Vector3> face_vertex_array; |
| 529 | face_vertex_array.resize(vertex_count); |
| 530 | |
| 531 | Vector<Color> face_color_array; |
| 532 | if (enabled_geometry_face_random_color) { |
| 533 | face_color_array.resize(vertex_count); |
| 534 | } |
| 535 | |
| 536 | Vector<Vector3> line_vertex_array; |
| 537 | if (enabled_edge_lines) { |
| 538 | line_vertex_array.resize(line_count); |
| 539 | } |
| 540 | |
| 541 | Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color(); |
| 542 | |
| 543 | RandomPCG rand; |
| 544 | Color polygon_color = debug_navigation_geometry_face_color; |
| 545 | |
| 546 | int face_vertex_index = 0; |
| 547 | int line_vertex_index = 0; |
| 548 | |
| 549 | Vector3 *face_vertex_array_ptrw = face_vertex_array.ptrw(); |
| 550 | Color *face_color_array_ptrw = face_color_array.ptrw(); |
| 551 | Vector3 *line_vertex_array_ptrw = line_vertex_array.ptrw(); |
| 552 | |
| 553 | for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) { |
| 554 | const Vector<int> &polygon_indices = navigation_mesh->get_polygon(polygon_index); |
| 555 | int polygon_indices_size = polygon_indices.size(); |
| 556 | if (polygon_indices_size < 3) { |
| 557 | continue; |
| 558 | } |
| 559 | |
| 560 | if (enabled_geometry_face_random_color) { |
| 561 | // Generate the polygon color, slightly randomly modified from the settings one. |
| 562 | polygon_color.set_hsv(debug_navigation_geometry_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_navigation_geometry_face_color.get_s(), debug_navigation_geometry_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2); |
| 563 | polygon_color.a = debug_navigation_geometry_face_color.a; |
| 564 | } |
| 565 | |
| 566 | for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) { |
| 567 | face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]]; |
| 568 | face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]]; |
| 569 | face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]]; |
| 570 | if (enabled_geometry_face_random_color) { |
| 571 | face_color_array_ptrw[face_vertex_index] = polygon_color; |
| 572 | face_color_array_ptrw[face_vertex_index + 1] = polygon_color; |
| 573 | face_color_array_ptrw[face_vertex_index + 2] = polygon_color; |
| 574 | } |
| 575 | face_vertex_index += 3; |
| 576 | } |
| 577 | |
| 578 | if (enabled_edge_lines) { |
| 579 | for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) { |
| 580 | line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]]; |
| 581 | line_vertex_index += 1; |
| 582 | if (polygon_indices_index + 1 == polygon_indices_size) { |
| 583 | line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]]; |
| 584 | line_vertex_index += 1; |
| 585 | } else { |
| 586 | line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]]; |
| 587 | line_vertex_index += 1; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | Ref<StandardMaterial3D> face_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_material(); |
| 594 | |
| 595 | Array face_mesh_array; |
| 596 | face_mesh_array.resize(Mesh::ARRAY_MAX); |
| 597 | face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array; |
| 598 | if (enabled_geometry_face_random_color) { |
| 599 | face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array; |
| 600 | } |
| 601 | debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array); |
| 602 | debug_mesh->surface_set_material(0, face_material); |
| 603 | |
| 604 | if (enabled_edge_lines) { |
| 605 | Ref<StandardMaterial3D> line_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_material(); |
| 606 | |
| 607 | Array line_mesh_array; |
| 608 | line_mesh_array.resize(Mesh::ARRAY_MAX); |
| 609 | line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array; |
| 610 | debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, line_mesh_array); |
| 611 | debug_mesh->surface_set_material(1, line_material); |
| 612 | } |
| 613 | |
| 614 | RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid()); |
| 615 | if (is_inside_tree()) { |
| 616 | RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario()); |
| 617 | RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform); |
| 618 | RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree()); |
| 619 | } |
| 620 | if (!is_enabled()) { |
| 621 | if (debug_mesh.is_valid()) { |
| 622 | if (debug_mesh->get_surface_count() > 0) { |
| 623 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid()); |
| 624 | } |
| 625 | if (debug_mesh->get_surface_count() > 1) { |
| 626 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid()); |
| 627 | } |
| 628 | } |
| 629 | } else { |
| 630 | if (debug_mesh.is_valid()) { |
| 631 | if (debug_mesh->get_surface_count() > 0) { |
| 632 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID()); |
| 633 | } |
| 634 | if (debug_mesh->get_surface_count() > 1) { |
| 635 | RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID()); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | #endif // DEBUG_ENABLED |
| 641 | |
| 642 | #ifdef DEBUG_ENABLED |
| 643 | void NavigationRegion3D::_update_debug_edge_connections_mesh() { |
| 644 | if (!NavigationServer3D::get_singleton()->get_debug_enabled() || !NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) { |
| 645 | if (debug_edge_connections_instance.is_valid()) { |
| 646 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 647 | } |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | if (!is_inside_tree()) { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (!use_edge_connections || !NavigationServer3D::get_singleton()->map_get_use_edge_connections(get_world_3d()->get_navigation_map())) { |
| 656 | if (debug_edge_connections_instance.is_valid()) { |
| 657 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 658 | } |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | if (navigation_mesh.is_null()) { |
| 663 | if (debug_edge_connections_instance.is_valid()) { |
| 664 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 665 | } |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | if (!debug_edge_connections_instance.is_valid()) { |
| 670 | debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create(); |
| 671 | } |
| 672 | |
| 673 | if (debug_edge_connections_mesh.is_null()) { |
| 674 | debug_edge_connections_mesh.instantiate(); |
| 675 | } |
| 676 | |
| 677 | debug_edge_connections_mesh->clear_surfaces(); |
| 678 | |
| 679 | float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map()); |
| 680 | float half_edge_connection_margin = edge_connection_margin * 0.5; |
| 681 | int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(region); |
| 682 | |
| 683 | if (connections_count == 0) { |
| 684 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | Vector<Vector3> vertex_array; |
| 689 | vertex_array.resize(connections_count * 6); |
| 690 | Vector3 *vertex_array_ptrw = vertex_array.ptrw(); |
| 691 | int vertex_array_index = 0; |
| 692 | |
| 693 | for (int i = 0; i < connections_count; i++) { |
| 694 | Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(region, i); |
| 695 | Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(region, i); |
| 696 | |
| 697 | Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end); |
| 698 | Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start); |
| 699 | |
| 700 | Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0)); |
| 701 | Vector3 start_left_dir = -start_right_dir; |
| 702 | |
| 703 | Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0)); |
| 704 | Vector3 end_left_dir = -end_right_dir; |
| 705 | |
| 706 | Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin); |
| 707 | Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin); |
| 708 | Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin); |
| 709 | Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin); |
| 710 | |
| 711 | vertex_array_ptrw[vertex_array_index++] = connection_pathway_start; |
| 712 | vertex_array_ptrw[vertex_array_index++] = connection_pathway_end; |
| 713 | vertex_array_ptrw[vertex_array_index++] = left_start_pos; |
| 714 | vertex_array_ptrw[vertex_array_index++] = right_start_pos; |
| 715 | vertex_array_ptrw[vertex_array_index++] = left_end_pos; |
| 716 | vertex_array_ptrw[vertex_array_index++] = right_end_pos; |
| 717 | } |
| 718 | |
| 719 | if (vertex_array.is_empty()) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material(); |
| 724 | |
| 725 | Array mesh_array; |
| 726 | mesh_array.resize(Mesh::ARRAY_MAX); |
| 727 | mesh_array[Mesh::ARRAY_VERTEX] = vertex_array; |
| 728 | |
| 729 | debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array); |
| 730 | debug_edge_connections_mesh->surface_set_material(0, edge_connections_material); |
| 731 | |
| 732 | RS::get_singleton()->instance_set_base(debug_edge_connections_instance, debug_edge_connections_mesh->get_rid()); |
| 733 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, is_visible_in_tree()); |
| 734 | if (is_inside_tree()) { |
| 735 | RS::get_singleton()->instance_set_scenario(debug_edge_connections_instance, get_world_3d()->get_scenario()); |
| 736 | } |
| 737 | |
| 738 | bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections(); |
| 739 | if (!enable_edge_connections) { |
| 740 | RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false); |
| 741 | } |
| 742 | } |
| 743 | #endif // DEBUG_ENABLED |
| 744 | |
| 745 | void NavigationRegion3D::_update_bounds() { |
| 746 | if (navigation_mesh.is_null()) { |
| 747 | bounds = AABB(); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | const Vector<Vector3> &vertices = navigation_mesh->get_vertices(); |
| 752 | if (vertices.is_empty()) { |
| 753 | bounds = AABB(); |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | const Transform3D gt = is_inside_tree() ? get_global_transform() : get_transform(); |
| 758 | |
| 759 | AABB new_bounds; |
| 760 | new_bounds.position = gt.xform(vertices[0]); |
| 761 | |
| 762 | for (const Vector3 &vertex : vertices) { |
| 763 | new_bounds.expand_to(gt.xform(vertex)); |
| 764 | } |
| 765 | bounds = new_bounds; |
| 766 | } |