| 1 | /**************************************************************************/ |
| 2 | /* nav_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 "nav_region_3d.h" |
| 32 | |
| 33 | #include "nav_map_3d.h" |
| 34 | |
| 35 | #include "3d/nav_mesh_queries_3d.h" |
| 36 | #include "3d/nav_region_builder_3d.h" |
| 37 | #include "3d/nav_region_iteration_3d.h" |
| 38 | #include "core/config/project_settings.h" |
| 39 | |
| 40 | using namespace Nav3D; |
| 41 | |
| 42 | void NavRegion3D::set_map(NavMap3D *p_map) { |
| 43 | if (map == p_map) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | cancel_async_thread_join(); |
| 48 | cancel_sync_request(); |
| 49 | |
| 50 | if (map) { |
| 51 | map->remove_region(this); |
| 52 | } |
| 53 | |
| 54 | map = p_map; |
| 55 | iteration_dirty = true; |
| 56 | |
| 57 | if (map) { |
| 58 | map->add_region(this); |
| 59 | request_sync(); |
| 60 | if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) { |
| 61 | request_async_thread_join(); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void NavRegion3D::set_enabled(bool p_enabled) { |
| 67 | if (enabled == p_enabled) { |
| 68 | return; |
| 69 | } |
| 70 | enabled = p_enabled; |
| 71 | iteration_dirty = true; |
| 72 | |
| 73 | request_sync(); |
| 74 | } |
| 75 | |
| 76 | void NavRegion3D::set_use_edge_connections(bool p_enabled) { |
| 77 | if (use_edge_connections != p_enabled) { |
| 78 | use_edge_connections = p_enabled; |
| 79 | iteration_dirty = true; |
| 80 | } |
| 81 | |
| 82 | request_sync(); |
| 83 | } |
| 84 | |
| 85 | void NavRegion3D::set_transform(Transform3D p_transform) { |
| 86 | if (transform == p_transform) { |
| 87 | return; |
| 88 | } |
| 89 | transform = p_transform; |
| 90 | iteration_dirty = true; |
| 91 | |
| 92 | request_sync(); |
| 93 | |
| 94 | #ifdef DEBUG_ENABLED |
| 95 | if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) { |
| 96 | ERR_PRINT_ONCE("Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation."); |
| 97 | } |
| 98 | #endif // DEBUG_ENABLED |
| 99 | } |
| 100 | |
| 101 | void NavRegion3D::set_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) { |
| 102 | #ifdef DEBUG_ENABLED |
| 103 | if (map && p_navigation_mesh.is_valid() && GLOBAL_GET_CACHED(bool, "navigation/3d/warnings/navmesh_cell_size_mismatch")) { |
| 104 | const double map_cell_size = double(map->get_cell_size()); |
| 105 | const double map_cell_height = double(map->get_cell_height()); |
| 106 | const double navmesh_cell_size = double(p_navigation_mesh->get_cell_size()); |
| 107 | const double navmesh_cell_height = double(p_navigation_mesh->get_cell_height()); |
| 108 | |
| 109 | if (map_cell_size > navmesh_cell_size) { |
| 110 | WARN_PRINT(vformat("A navigation mesh that uses a `cell_size` of %s was assigned to a navigation map set to a larger `cell_size` of %s.\nThis mismatch in cell size can cause rasterization errors with navigation mesh edges on the navigation map.\nThe cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function.\nThe cell size for default navigation maps can also be changed in the project settings.\nThis warning can be toggled under 'navigation/3d/warnings/navmesh_cell_size_mismatch' in the project settings.", navmesh_cell_size, map_cell_size)); |
| 111 | } |
| 112 | if (map_cell_height > navmesh_cell_height) { |
| 113 | WARN_PRINT(vformat("A navigation mesh that uses a `cell_height` of %s was assigned to a navigation map set to a larger `cell_height` of %s.\nThis mismatch in cell height can cause rasterization errors with navigation mesh edges on the navigation map.\nThe cell height for navigation maps can be changed by using the NavigationServer map_set_cell_height() function.\nThe cell height for default navigation maps can also be changed in the project settings.\nThis warning can be toggled under 'navigation/3d/warnings/navmesh_cell_size_mismatch' in the project settings.", navmesh_cell_height, map_cell_height)); |
| 114 | } |
| 115 | } |
| 116 | #endif // DEBUG_ENABLED |
| 117 | |
| 118 | navmesh = p_navigation_mesh; |
| 119 | |
| 120 | iteration_dirty = true; |
| 121 | |
| 122 | request_sync(); |
| 123 | } |
| 124 | |
| 125 | Vector3 NavRegion3D::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision) const { |
| 126 | RWLockRead read_lock(region_rwlock); |
| 127 | |
| 128 | return NavMeshQueries3D::polygons_get_closest_point_to_segment( |
| 129 | get_polygons(), p_from, p_to, p_use_collision); |
| 130 | } |
| 131 | |
| 132 | ClosestPointQueryResult NavRegion3D::get_closest_point_info(const Vector3 &p_point) const { |
| 133 | RWLockRead read_lock(region_rwlock); |
| 134 | |
| 135 | return NavMeshQueries3D::polygons_get_closest_point_info(get_polygons(), p_point); |
| 136 | } |
| 137 | |
| 138 | Vector3 NavRegion3D::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const { |
| 139 | RWLockRead read_lock(region_rwlock); |
| 140 | |
| 141 | if (!get_enabled()) { |
| 142 | return Vector3(); |
| 143 | } |
| 144 | |
| 145 | return NavMeshQueries3D::polygons_get_random_point(get_polygons(), p_navigation_layers, p_uniformly); |
| 146 | } |
| 147 | |
| 148 | void NavRegion3D::set_navigation_layers(uint32_t p_navigation_layers) { |
| 149 | if (navigation_layers == p_navigation_layers) { |
| 150 | return; |
| 151 | } |
| 152 | navigation_layers = p_navigation_layers; |
| 153 | iteration_dirty = true; |
| 154 | |
| 155 | request_sync(); |
| 156 | } |
| 157 | |
| 158 | void NavRegion3D::set_enter_cost(real_t p_enter_cost) { |
| 159 | real_t new_enter_cost = MAX(p_enter_cost, 0.0); |
| 160 | if (enter_cost == new_enter_cost) { |
| 161 | return; |
| 162 | } |
| 163 | enter_cost = new_enter_cost; |
| 164 | iteration_dirty = true; |
| 165 | |
| 166 | request_sync(); |
| 167 | } |
| 168 | |
| 169 | void NavRegion3D::set_travel_cost(real_t p_travel_cost) { |
| 170 | real_t new_travel_cost = MAX(p_travel_cost, 0.0); |
| 171 | if (travel_cost == new_travel_cost) { |
| 172 | return; |
| 173 | } |
| 174 | travel_cost = new_travel_cost; |
| 175 | iteration_dirty = true; |
| 176 | |
| 177 | request_sync(); |
| 178 | } |
| 179 | |
| 180 | void NavRegion3D::set_owner_id(ObjectID p_owner_id) { |
| 181 | if (owner_id == p_owner_id) { |
| 182 | return; |
| 183 | } |
| 184 | owner_id = p_owner_id; |
| 185 | iteration_dirty = true; |
| 186 | |
| 187 | request_sync(); |
| 188 | } |
| 189 | |
| 190 | void NavRegion3D::scratch_polygons() { |
| 191 | iteration_dirty = true; |
| 192 | |
| 193 | request_sync(); |
| 194 | } |
| 195 | |
| 196 | real_t NavRegion3D::get_surface_area() const { |
| 197 | RWLockRead read_lock(iteration_rwlock); |
| 198 | return iteration->get_surface_area(); |
| 199 | } |
| 200 | |
| 201 | AABB NavRegion3D::get_bounds() const { |
| 202 | RWLockRead read_lock(iteration_rwlock); |
| 203 | return iteration->get_bounds(); |
| 204 | } |
| 205 | |
| 206 | LocalVector<Nav3D::Polygon> const &NavRegion3D::get_polygons() const { |
| 207 | RWLockRead read_lock(iteration_rwlock); |
| 208 | return iteration->get_navmesh_polygons(); |
| 209 | } |
| 210 | |
| 211 | bool NavRegion3D::sync() { |
| 212 | bool requires_map_update = false; |
| 213 | if (!map) { |
| 214 | return requires_map_update; |
| 215 | } |
| 216 | |
| 217 | if (iteration_dirty && !iteration_building && !iteration_ready) { |
| 218 | _build_iteration(); |
| 219 | } |
| 220 | |
| 221 | if (iteration_ready) { |
| 222 | _sync_iteration(); |
| 223 | requires_map_update = true; |
| 224 | } |
| 225 | |
| 226 | return requires_map_update; |
| 227 | } |
| 228 | |
| 229 | void NavRegion3D::sync_async_tasks() { |
| 230 | if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) { |
| 231 | if (WorkerThreadPool::get_singleton()->is_task_completed(iteration_build_thread_task_id)) { |
| 232 | WorkerThreadPool::get_singleton()->wait_for_task_completion(iteration_build_thread_task_id); |
| 233 | |
| 234 | iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID; |
| 235 | iteration_building = false; |
| 236 | iteration_ready = true; |
| 237 | request_sync(); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void NavRegion3D::_build_iteration() { |
| 243 | if (!iteration_dirty || iteration_building || iteration_ready) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | iteration_dirty = false; |
| 248 | iteration_building = true; |
| 249 | iteration_ready = false; |
| 250 | |
| 251 | iteration_build.reset(); |
| 252 | |
| 253 | if (navmesh.is_valid()) { |
| 254 | navmesh->get_data(iteration_build.navmesh_data.vertices, iteration_build.navmesh_data.polygons); |
| 255 | } |
| 256 | |
| 257 | iteration_build.map_cell_size = map->get_merge_rasterizer_cell_size(); |
| 258 | |
| 259 | Ref<NavRegionIteration3D> new_iteration; |
| 260 | new_iteration.instantiate(); |
| 261 | |
| 262 | new_iteration->navigation_layers = get_navigation_layers(); |
| 263 | new_iteration->enter_cost = get_enter_cost(); |
| 264 | new_iteration->travel_cost = get_travel_cost(); |
| 265 | new_iteration->owner_object_id = get_owner_id(); |
| 266 | new_iteration->owner_type = get_type(); |
| 267 | new_iteration->owner_rid = get_self(); |
| 268 | new_iteration->enabled = get_enabled(); |
| 269 | new_iteration->transform = get_transform(); |
| 270 | new_iteration->owner_use_edge_connections = get_use_edge_connections(); |
| 271 | |
| 272 | iteration_build.region_iteration = new_iteration; |
| 273 | |
| 274 | if (use_async_iterations) { |
| 275 | iteration_build_thread_task_id = WorkerThreadPool::get_singleton()->add_native_task(&NavRegion3D::_build_iteration_threaded, &iteration_build, true, SNAME("NavRegionBuilder3D")); |
| 276 | request_async_thread_join(); |
| 277 | } else { |
| 278 | NavRegionBuilder3D::build_iteration(iteration_build); |
| 279 | |
| 280 | iteration_building = false; |
| 281 | iteration_ready = true; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void NavRegion3D::_build_iteration_threaded(void *p_arg) { |
| 286 | NavRegionIterationBuild3D *_iteration_build = static_cast<NavRegionIterationBuild3D *>(p_arg); |
| 287 | |
| 288 | NavRegionBuilder3D::build_iteration(*_iteration_build); |
| 289 | } |
| 290 | |
| 291 | void NavRegion3D::_sync_iteration() { |
| 292 | if (iteration_building || !iteration_ready) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | performance_data.pm_polygon_count = iteration_build.performance_data.pm_polygon_count; |
| 297 | performance_data.pm_edge_count = iteration_build.performance_data.pm_edge_count; |
| 298 | performance_data.pm_edge_merge_count = iteration_build.performance_data.pm_edge_merge_count; |
| 299 | |
| 300 | RWLockWrite write_lock(iteration_rwlock); |
| 301 | ERR_FAIL_COND(iteration.is_null()); |
| 302 | iteration = Ref<NavRegionIteration3D>(); |
| 303 | DEV_ASSERT(iteration.is_null()); |
| 304 | iteration = iteration_build.region_iteration; |
| 305 | iteration_build.region_iteration = Ref<NavRegionIteration3D>(); |
| 306 | DEV_ASSERT(iteration_build.region_iteration.is_null()); |
| 307 | iteration_id = iteration_id % UINT32_MAX + 1; |
| 308 | |
| 309 | iteration_ready = false; |
| 310 | |
| 311 | cancel_async_thread_join(); |
| 312 | } |
| 313 | |
| 314 | Ref<NavRegionIteration3D> NavRegion3D::get_iteration() { |
| 315 | RWLockRead read_lock(iteration_rwlock); |
| 316 | return iteration; |
| 317 | } |
| 318 | |
| 319 | void NavRegion3D::request_async_thread_join() { |
| 320 | DEV_ASSERT(map); |
| 321 | if (map && !async_list_element.in_list()) { |
| 322 | map->add_region_async_thread_join_request(&async_list_element); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void NavRegion3D::cancel_async_thread_join() { |
| 327 | if (map && async_list_element.in_list()) { |
| 328 | map->remove_region_async_thread_join_request(&async_list_element); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void NavRegion3D::request_sync() { |
| 333 | if (map && !sync_dirty_request_list_element.in_list()) { |
| 334 | map->add_region_sync_dirty_request(&sync_dirty_request_list_element); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void NavRegion3D::cancel_sync_request() { |
| 339 | if (map && sync_dirty_request_list_element.in_list()) { |
| 340 | map->remove_region_sync_dirty_request(&sync_dirty_request_list_element); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void NavRegion3D::set_use_async_iterations(bool p_enabled) { |
| 345 | if (use_async_iterations == p_enabled) { |
| 346 | return; |
| 347 | } |
| 348 | #ifdef THREADS_ENABLED |
| 349 | use_async_iterations = p_enabled; |
| 350 | #endif |
| 351 | } |
| 352 | |
| 353 | bool NavRegion3D::get_use_async_iterations() const { |
| 354 | return use_async_iterations; |
| 355 | } |
| 356 | |
| 357 | NavRegion3D::NavRegion3D() : |
| 358 | sync_dirty_request_list_element(this), async_list_element(this) { |
| 359 | type = NavigationEnums3D::PathSegmentType::PATH_SEGMENT_TYPE_REGION; |
| 360 | iteration_build.region = this; |
| 361 | iteration.instantiate(); |
| 362 | |
| 363 | #ifdef THREADS_ENABLED |
| 364 | use_async_iterations = GLOBAL_GET("navigation/world/region_use_async_iterations"); |
| 365 | #else |
| 366 | use_async_iterations = false; |
| 367 | #endif |
| 368 | } |
| 369 | |
| 370 | NavRegion3D::~NavRegion3D() { |
| 371 | cancel_async_thread_join(); |
| 372 | cancel_sync_request(); |
| 373 | |
| 374 | if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) { |
| 375 | WorkerThreadPool::get_singleton()->wait_for_task_completion(iteration_build_thread_task_id); |
| 376 | iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID; |
| 377 | } |
| 378 | |
| 379 | iteration_build.region = nullptr; |
| 380 | iteration_build.region_iteration = Ref<NavRegionIteration3D>(); |
| 381 | iteration = Ref<NavRegionIteration3D>(); |
| 382 | } |