| 1 | /**************************************************************************/ |
| 2 | /* navigation_obstacle_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_obstacle_3d.h" |
| 32 | |
| 33 | #include "core/math/geometry_2d.h" |
| 34 | #include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h" |
| 35 | #include "scene/resources/navigation_mesh.h" |
| 36 | #include "servers/navigation_3d/navigation_server_3d.h" |
| 37 | |
| 38 | Callable NavigationObstacle3D::_navmesh_source_geometry_parsing_callback; |
| 39 | RID NavigationObstacle3D::_navmesh_source_geometry_parser; |
| 40 | |
| 41 | void NavigationObstacle3D::_bind_methods() { |
| 42 | ClassDB::bind_method(D_METHOD("get_rid"), &NavigationObstacle3D::get_rid); |
| 43 | |
| 44 | ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationObstacle3D::set_avoidance_enabled); |
| 45 | ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationObstacle3D::get_avoidance_enabled); |
| 46 | |
| 47 | ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationObstacle3D::set_navigation_map); |
| 48 | ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationObstacle3D::get_navigation_map); |
| 49 | |
| 50 | ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationObstacle3D::set_radius); |
| 51 | ClassDB::bind_method(D_METHOD("get_radius"), &NavigationObstacle3D::get_radius); |
| 52 | |
| 53 | ClassDB::bind_method(D_METHOD("set_height", "height"), &NavigationObstacle3D::set_height); |
| 54 | ClassDB::bind_method(D_METHOD("get_height"), &NavigationObstacle3D::get_height); |
| 55 | |
| 56 | ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationObstacle3D::set_velocity); |
| 57 | ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationObstacle3D::get_velocity); |
| 58 | |
| 59 | ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationObstacle3D::set_vertices); |
| 60 | ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationObstacle3D::get_vertices); |
| 61 | |
| 62 | ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationObstacle3D::set_avoidance_layers); |
| 63 | ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationObstacle3D::get_avoidance_layers); |
| 64 | |
| 65 | ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationObstacle3D::set_avoidance_layer_value); |
| 66 | ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationObstacle3D::get_avoidance_layer_value); |
| 67 | |
| 68 | ClassDB::bind_method(D_METHOD("set_use_3d_avoidance", "enabled"), &NavigationObstacle3D::set_use_3d_avoidance); |
| 69 | ClassDB::bind_method(D_METHOD("get_use_3d_avoidance"), &NavigationObstacle3D::get_use_3d_avoidance); |
| 70 | |
| 71 | ClassDB::bind_method(D_METHOD("set_affect_navigation_mesh", "enabled"), &NavigationObstacle3D::set_affect_navigation_mesh); |
| 72 | ClassDB::bind_method(D_METHOD("get_affect_navigation_mesh"), &NavigationObstacle3D::get_affect_navigation_mesh); |
| 73 | |
| 74 | ClassDB::bind_method(D_METHOD("set_carve_navigation_mesh", "enabled"), &NavigationObstacle3D::set_carve_navigation_mesh); |
| 75 | ClassDB::bind_method(D_METHOD("get_carve_navigation_mesh"), &NavigationObstacle3D::get_carve_navigation_mesh); |
| 76 | |
| 77 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.0,100,0.01,suffix:m"), "set_radius", "get_radius"); |
| 78 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.0,100,0.01,suffix:m"), "set_height", "get_height"); |
| 79 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices"), "set_vertices", "get_vertices"); |
| 80 | ADD_GROUP("NavigationMesh", ""); |
| 81 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "affect_navigation_mesh"), "set_affect_navigation_mesh", "get_affect_navigation_mesh"); |
| 82 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "carve_navigation_mesh"), "set_carve_navigation_mesh", "get_carve_navigation_mesh"); |
| 83 | ADD_GROUP("Avoidance", ""); |
| 84 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_avoidance_enabled", "get_avoidance_enabled"); |
| 85 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity"); |
| 86 | ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers"); |
| 87 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance"); |
| 88 | } |
| 89 | |
| 90 | void NavigationObstacle3D::_notification(int p_what) { |
| 91 | switch (p_what) { |
| 92 | case NOTIFICATION_POST_ENTER_TREE: { |
| 93 | if (map_override.is_valid()) { |
| 94 | _update_map(map_override); |
| 95 | } else if (is_inside_tree()) { |
| 96 | _update_map(get_world_3d()->get_navigation_map()); |
| 97 | } else { |
| 98 | _update_map(RID()); |
| 99 | } |
| 100 | // need to trigger map controlled agent assignment somehow for the fake_agent since obstacles use no callback like regular agents |
| 101 | NavigationServer3D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled); |
| 102 | _update_transform(); |
| 103 | set_physics_process_internal(true); |
| 104 | #ifdef DEBUG_ENABLED |
| 105 | _update_debug(); |
| 106 | #endif // DEBUG_ENABLED |
| 107 | } break; |
| 108 | |
| 109 | #ifdef TOOLS_ENABLED |
| 110 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 111 | update_gizmos(); |
| 112 | } break; |
| 113 | #endif // TOOLS_ENABLED |
| 114 | |
| 115 | case NOTIFICATION_EXIT_TREE: { |
| 116 | set_physics_process_internal(false); |
| 117 | _update_map(RID()); |
| 118 | #ifdef DEBUG_ENABLED |
| 119 | _clear_debug(); |
| 120 | #endif // DEBUG_ENABLED |
| 121 | } break; |
| 122 | |
| 123 | case NOTIFICATION_SUSPENDED: |
| 124 | case NOTIFICATION_PAUSED: { |
| 125 | if (!can_process()) { |
| 126 | map_before_pause = map_current; |
| 127 | _update_map(RID()); |
| 128 | } else if (can_process() && !(map_before_pause == RID())) { |
| 129 | _update_map(map_before_pause); |
| 130 | map_before_pause = RID(); |
| 131 | } |
| 132 | NavigationServer3D::get_singleton()->obstacle_set_paused(obstacle, !can_process()); |
| 133 | } break; |
| 134 | |
| 135 | case NOTIFICATION_UNSUSPENDED: { |
| 136 | if (get_tree()->is_paused()) { |
| 137 | break; |
| 138 | } |
| 139 | [[fallthrough]]; |
| 140 | } |
| 141 | |
| 142 | case NOTIFICATION_UNPAUSED: { |
| 143 | if (!can_process()) { |
| 144 | map_before_pause = map_current; |
| 145 | _update_map(RID()); |
| 146 | } else if (can_process() && !(map_before_pause == RID())) { |
| 147 | _update_map(map_before_pause); |
| 148 | map_before_pause = RID(); |
| 149 | } |
| 150 | NavigationServer3D::get_singleton()->obstacle_set_paused(obstacle, !can_process()); |
| 151 | } break; |
| 152 | |
| 153 | #ifdef DEBUG_ENABLED |
| 154 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 155 | _update_debug(); |
| 156 | } break; |
| 157 | #endif // DEBUG_ENABLED |
| 158 | |
| 159 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { |
| 160 | if (is_inside_tree()) { |
| 161 | _update_transform(); |
| 162 | |
| 163 | if (velocity_submitted) { |
| 164 | velocity_submitted = false; |
| 165 | // only update if there is a noticeable change, else the rvo agent preferred velocity stays the same |
| 166 | if (!previous_velocity.is_equal_approx(velocity)) { |
| 167 | NavigationServer3D::get_singleton()->obstacle_set_velocity(obstacle, velocity); |
| 168 | } |
| 169 | previous_velocity = velocity; |
| 170 | } |
| 171 | #ifdef DEBUG_ENABLED |
| 172 | if (fake_agent_radius_debug_instance_rid.is_valid() && radius > 0.0) { |
| 173 | // Prevent non-positive scaling. |
| 174 | const Vector3 safe_scale = get_global_basis().get_scale().abs().maxf(0.001); |
| 175 | // Agent radius is a scalar value and does not support non-uniform scaling, choose the largest axis. |
| 176 | const float scaling_max_value = safe_scale[safe_scale.max_axis_index()]; |
| 177 | const Vector3 uniform_max_scale = Vector3(scaling_max_value, scaling_max_value, scaling_max_value); |
| 178 | const Transform3D debug_transform = Transform3D(Basis().scaled(uniform_max_scale), get_global_position()); |
| 179 | |
| 180 | RS::get_singleton()->instance_set_transform(fake_agent_radius_debug_instance_rid, debug_transform); |
| 181 | } |
| 182 | if (static_obstacle_debug_instance_rid.is_valid() && get_vertices().size() > 0) { |
| 183 | // Prevent non-positive scaling. |
| 184 | const Vector3 safe_scale = get_global_basis().get_scale().abs().maxf(0.001); |
| 185 | // Obstacles are projected to the xz-plane, so only rotation around the y-axis can be taken into account. |
| 186 | const Transform3D debug_transform = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), get_global_rotation().y), get_global_position()); |
| 187 | |
| 188 | RS::get_singleton()->instance_set_transform(static_obstacle_debug_instance_rid, debug_transform); |
| 189 | } |
| 190 | #endif // DEBUG_ENABLED |
| 191 | } |
| 192 | } break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | NavigationObstacle3D::NavigationObstacle3D() { |
| 197 | NavigationServer3D *ns3d = NavigationServer3D::get_singleton(); |
| 198 | |
| 199 | obstacle = ns3d->obstacle_create(); |
| 200 | |
| 201 | ns3d->obstacle_set_height(obstacle, height); |
| 202 | ns3d->obstacle_set_radius(obstacle, radius); |
| 203 | ns3d->obstacle_set_vertices(obstacle, vertices); |
| 204 | ns3d->obstacle_set_avoidance_layers(obstacle, avoidance_layers); |
| 205 | ns3d->obstacle_set_use_3d_avoidance(obstacle, use_3d_avoidance); |
| 206 | ns3d->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled); |
| 207 | |
| 208 | #ifdef DEBUG_ENABLED |
| 209 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 210 | |
| 211 | fake_agent_radius_debug_mesh_rid = rs->mesh_create(); |
| 212 | static_obstacle_debug_mesh_rid = rs->mesh_create(); |
| 213 | |
| 214 | fake_agent_radius_debug_instance_rid = rs->instance_create(); |
| 215 | static_obstacle_debug_instance_rid = rs->instance_create(); |
| 216 | |
| 217 | rs->instance_set_base(fake_agent_radius_debug_instance_rid, fake_agent_radius_debug_mesh_rid); |
| 218 | rs->instance_set_base(static_obstacle_debug_instance_rid, static_obstacle_debug_mesh_rid); |
| 219 | |
| 220 | ns3d->connect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_fake_agent_radius_debug)); |
| 221 | ns3d->connect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_static_obstacle_debug)); |
| 222 | _update_fake_agent_radius_debug(); |
| 223 | _update_static_obstacle_debug(); |
| 224 | #endif // DEBUG_ENABLED |
| 225 | |
| 226 | #ifdef TOOLS_ENABLED |
| 227 | set_notify_transform(true); |
| 228 | #endif // TOOLS_ENABLED |
| 229 | } |
| 230 | |
| 231 | NavigationObstacle3D::~NavigationObstacle3D() { |
| 232 | NavigationServer3D *ns3d = NavigationServer3D::get_singleton(); |
| 233 | ERR_FAIL_NULL(ns3d); |
| 234 | |
| 235 | ns3d->free_rid(obstacle); |
| 236 | obstacle = RID(); |
| 237 | |
| 238 | #ifdef DEBUG_ENABLED |
| 239 | ns3d->disconnect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_fake_agent_radius_debug)); |
| 240 | ns3d->disconnect("avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_static_obstacle_debug)); |
| 241 | |
| 242 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 243 | ERR_FAIL_NULL(rs); |
| 244 | if (fake_agent_radius_debug_instance_rid.is_valid()) { |
| 245 | rs->free_rid(fake_agent_radius_debug_instance_rid); |
| 246 | fake_agent_radius_debug_instance_rid = RID(); |
| 247 | } |
| 248 | if (fake_agent_radius_debug_mesh_rid.is_valid()) { |
| 249 | rs->free_rid(fake_agent_radius_debug_mesh_rid); |
| 250 | fake_agent_radius_debug_mesh_rid = RID(); |
| 251 | } |
| 252 | if (static_obstacle_debug_instance_rid.is_valid()) { |
| 253 | rs->free_rid(static_obstacle_debug_instance_rid); |
| 254 | static_obstacle_debug_instance_rid = RID(); |
| 255 | } |
| 256 | if (static_obstacle_debug_mesh_rid.is_valid()) { |
| 257 | rs->free_rid(static_obstacle_debug_mesh_rid); |
| 258 | static_obstacle_debug_mesh_rid = RID(); |
| 259 | } |
| 260 | #endif // DEBUG_ENABLED |
| 261 | } |
| 262 | |
| 263 | void NavigationObstacle3D::set_vertices(const Vector<Vector3> &p_vertices) { |
| 264 | vertices = p_vertices; |
| 265 | |
| 266 | Vector<Vector2> vertices_2d; |
| 267 | vertices_2d.resize(vertices.size()); |
| 268 | |
| 269 | const Vector3 *vertices_ptr = vertices.ptr(); |
| 270 | Vector2 *vertices_2d_ptrw = vertices_2d.ptrw(); |
| 271 | |
| 272 | for (int i = 0; i < vertices.size(); i++) { |
| 273 | vertices_2d_ptrw[i] = Vector2(vertices_ptr[i].x, vertices_ptr[i].z); |
| 274 | } |
| 275 | |
| 276 | vertices_are_clockwise = !Geometry2D::is_polygon_clockwise(vertices_2d); // Geometry2D is inverted. |
| 277 | vertices_are_valid = !Geometry2D::triangulate_polygon(vertices_2d).is_empty(); |
| 278 | |
| 279 | const Basis basis = is_inside_tree() ? get_global_basis() : get_basis(); |
| 280 | const float rotation_y = is_inside_tree() ? get_global_rotation().y : get_rotation().y; |
| 281 | const Vector3 safe_scale = basis.get_scale().abs().maxf(0.001); |
| 282 | const Transform3D safe_transform = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), rotation_y), Vector3()); |
| 283 | NavigationServer3D::get_singleton()->obstacle_set_vertices(obstacle, safe_transform.xform(vertices)); |
| 284 | #ifdef DEBUG_ENABLED |
| 285 | _update_static_obstacle_debug(); |
| 286 | update_gizmos(); |
| 287 | #endif // DEBUG_ENABLED |
| 288 | } |
| 289 | |
| 290 | void NavigationObstacle3D::set_navigation_map(RID p_navigation_map) { |
| 291 | if (map_override == p_navigation_map) { |
| 292 | return; |
| 293 | } |
| 294 | map_override = p_navigation_map; |
| 295 | _update_map(map_override); |
| 296 | } |
| 297 | |
| 298 | RID NavigationObstacle3D::get_navigation_map() const { |
| 299 | if (map_override.is_valid()) { |
| 300 | return map_override; |
| 301 | } else if (is_inside_tree()) { |
| 302 | return get_world_3d()->get_navigation_map(); |
| 303 | } |
| 304 | return RID(); |
| 305 | } |
| 306 | |
| 307 | void NavigationObstacle3D::set_radius(real_t p_radius) { |
| 308 | ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive."); |
| 309 | if (Math::is_equal_approx(radius, p_radius)) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | radius = p_radius; |
| 314 | |
| 315 | // Prevent non-positive or non-uniform scaling of dynamic obstacle radius. |
| 316 | const Vector3 safe_scale = (is_inside_tree() ? get_global_basis() : get_basis()).get_scale().abs().maxf(0.001); |
| 317 | NavigationServer3D::get_singleton()->obstacle_set_radius(obstacle, safe_scale[safe_scale.max_axis_index()] * radius); |
| 318 | |
| 319 | #ifdef DEBUG_ENABLED |
| 320 | _update_fake_agent_radius_debug(); |
| 321 | update_gizmos(); |
| 322 | #endif // DEBUG_ENABLED |
| 323 | } |
| 324 | |
| 325 | void NavigationObstacle3D::set_height(real_t p_height) { |
| 326 | ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive."); |
| 327 | if (Math::is_equal_approx(height, p_height)) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | height = p_height; |
| 332 | const float scale_factor = MAX(Math::abs((is_inside_tree() ? get_global_basis() : get_basis()).get_scale().y), 0.001); |
| 333 | NavigationServer3D::get_singleton()->obstacle_set_height(obstacle, scale_factor * height); |
| 334 | |
| 335 | #ifdef DEBUG_ENABLED |
| 336 | _update_static_obstacle_debug(); |
| 337 | update_gizmos(); |
| 338 | #endif // DEBUG_ENABLED |
| 339 | } |
| 340 | |
| 341 | void NavigationObstacle3D::set_avoidance_layers(uint32_t p_layers) { |
| 342 | avoidance_layers = p_layers; |
| 343 | NavigationServer3D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers); |
| 344 | } |
| 345 | |
| 346 | uint32_t NavigationObstacle3D::get_avoidance_layers() const { |
| 347 | return avoidance_layers; |
| 348 | } |
| 349 | |
| 350 | void NavigationObstacle3D::set_avoidance_layer_value(int p_layer_number, bool p_value) { |
| 351 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive."); |
| 352 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive."); |
| 353 | uint32_t avoidance_layers_new = get_avoidance_layers(); |
| 354 | if (p_value) { |
| 355 | avoidance_layers_new |= 1 << (p_layer_number - 1); |
| 356 | } else { |
| 357 | avoidance_layers_new &= ~(1 << (p_layer_number - 1)); |
| 358 | } |
| 359 | set_avoidance_layers(avoidance_layers_new); |
| 360 | } |
| 361 | |
| 362 | bool NavigationObstacle3D::get_avoidance_layer_value(int p_layer_number) const { |
| 363 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive."); |
| 364 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive."); |
| 365 | return get_avoidance_layers() & (1 << (p_layer_number - 1)); |
| 366 | } |
| 367 | |
| 368 | void NavigationObstacle3D::set_avoidance_enabled(bool p_enabled) { |
| 369 | if (avoidance_enabled == p_enabled) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | avoidance_enabled = p_enabled; |
| 374 | NavigationServer3D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled); |
| 375 | } |
| 376 | |
| 377 | bool NavigationObstacle3D::get_avoidance_enabled() const { |
| 378 | return avoidance_enabled; |
| 379 | } |
| 380 | |
| 381 | void NavigationObstacle3D::set_use_3d_avoidance(bool p_use_3d_avoidance) { |
| 382 | use_3d_avoidance = p_use_3d_avoidance; |
| 383 | _update_use_3d_avoidance(use_3d_avoidance); |
| 384 | notify_property_list_changed(); |
| 385 | } |
| 386 | |
| 387 | void NavigationObstacle3D::set_velocity(const Vector3 p_velocity) { |
| 388 | velocity = p_velocity; |
| 389 | velocity_submitted = true; |
| 390 | } |
| 391 | |
| 392 | void NavigationObstacle3D::set_affect_navigation_mesh(bool p_enabled) { |
| 393 | affect_navigation_mesh = p_enabled; |
| 394 | } |
| 395 | |
| 396 | bool NavigationObstacle3D::get_affect_navigation_mesh() const { |
| 397 | return affect_navigation_mesh; |
| 398 | } |
| 399 | |
| 400 | void NavigationObstacle3D::set_carve_navigation_mesh(bool p_enabled) { |
| 401 | carve_navigation_mesh = p_enabled; |
| 402 | } |
| 403 | |
| 404 | bool NavigationObstacle3D::get_carve_navigation_mesh() const { |
| 405 | return carve_navigation_mesh; |
| 406 | } |
| 407 | |
| 408 | PackedStringArray NavigationObstacle3D::get_configuration_warnings() const { |
| 409 | PackedStringArray warnings = Node3D::get_configuration_warnings(); |
| 410 | |
| 411 | if (get_global_rotation().x != 0.0 || get_global_rotation().z != 0.0) { |
| 412 | warnings.push_back(RTR("NavigationObstacle3D only takes global rotation around the y-axis into account. Rotations around the x-axis or z-axis might lead to unexpected results.")); |
| 413 | } |
| 414 | |
| 415 | const Vector3 global_scale = get_global_basis().get_scale(); |
| 416 | if (global_scale.x < 0.001 || global_scale.y < 0.001 || global_scale.z < 0.001) { |
| 417 | warnings.push_back(RTR("NavigationObstacle3D does not support negative or zero scaling.")); |
| 418 | } |
| 419 | |
| 420 | if (radius > 0.0 && !get_global_basis().is_conformal()) { |
| 421 | warnings.push_back(RTR("The agent radius can only be scaled uniformly. The largest scale value along the three axes will be used.")); |
| 422 | } |
| 423 | |
| 424 | return warnings; |
| 425 | } |
| 426 | |
| 427 | void NavigationObstacle3D::navmesh_parse_init() { |
| 428 | ERR_FAIL_NULL(NavigationServer3D::get_singleton()); |
| 429 | if (!_navmesh_source_geometry_parser.is_valid()) { |
| 430 | _navmesh_source_geometry_parsing_callback = callable_mp_static(&NavigationObstacle3D::navmesh_parse_source_geometry); |
| 431 | _navmesh_source_geometry_parser = NavigationServer3D::get_singleton()->source_geometry_parser_create(); |
| 432 | NavigationServer3D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | void NavigationObstacle3D::navmesh_parse_source_geometry(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node) { |
| 437 | NavigationObstacle3D *obstacle = Object::cast_to<NavigationObstacle3D>(p_node); |
| 438 | |
| 439 | if (obstacle == nullptr) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | if (!obstacle->get_affect_navigation_mesh()) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | const float elevation = obstacle->get_global_position().y + p_source_geometry_data->root_node_transform.origin.y; |
| 448 | // Prevent non-positive scaling. |
| 449 | const Vector3 safe_scale = obstacle->get_global_basis().get_scale().abs().maxf(0.001); |
| 450 | const float obstacle_radius = obstacle->get_radius(); |
| 451 | |
| 452 | if (obstacle_radius > 0.0) { |
| 453 | // Radius defined obstacle should be uniformly scaled from obstacle basis max scale axis. |
| 454 | const float scaling_max_value = safe_scale[safe_scale.max_axis_index()]; |
| 455 | const Vector3 uniform_max_scale = Vector3(scaling_max_value, scaling_max_value, scaling_max_value); |
| 456 | const Transform3D obstacle_circle_transform = p_source_geometry_data->root_node_transform * Transform3D(Basis().scaled(uniform_max_scale), obstacle->get_global_position()); |
| 457 | |
| 458 | Vector<Vector3> obstruction_circle_vertices; |
| 459 | |
| 460 | // The point of this is that the moving obstacle can make a simple hole in the navigation mesh and affect the pathfinding. |
| 461 | // Without, navigation paths can go directly through the middle of the obstacle and conflict with the avoidance to get agents stuck. |
| 462 | // No place for excessive "round" detail here. Every additional edge adds a high cost for something that needs to be quick, not pretty. |
| 463 | static const int circle_points = 12; |
| 464 | |
| 465 | obstruction_circle_vertices.resize(circle_points); |
| 466 | Vector3 *circle_vertices_ptrw = obstruction_circle_vertices.ptrw(); |
| 467 | const real_t circle_point_step = Math::TAU / circle_points; |
| 468 | |
| 469 | for (int i = 0; i < circle_points; i++) { |
| 470 | const float angle = i * circle_point_step; |
| 471 | circle_vertices_ptrw[i] = obstacle_circle_transform.xform(Vector3(Math::cos(angle) * obstacle_radius, 0.0, Math::sin(angle) * obstacle_radius)); |
| 472 | } |
| 473 | |
| 474 | p_source_geometry_data->add_projected_obstruction(obstruction_circle_vertices, elevation - obstacle_radius, scaling_max_value * obstacle_radius, obstacle->get_carve_navigation_mesh()); |
| 475 | } |
| 476 | |
| 477 | // Obstacles are projected to the xz-plane, so only rotation around the y-axis can be taken into account. |
| 478 | const Transform3D node_xform = p_source_geometry_data->root_node_transform * Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), obstacle->get_global_rotation().y), obstacle->get_global_position()); |
| 479 | |
| 480 | const Vector<Vector3> &obstacle_vertices = obstacle->get_vertices(); |
| 481 | |
| 482 | if (obstacle_vertices.is_empty()) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | Vector<Vector3> obstruction_shape_vertices; |
| 487 | obstruction_shape_vertices.resize(obstacle_vertices.size()); |
| 488 | |
| 489 | const Vector3 *obstacle_vertices_ptr = obstacle_vertices.ptr(); |
| 490 | Vector3 *obstruction_shape_vertices_ptrw = obstruction_shape_vertices.ptrw(); |
| 491 | |
| 492 | for (int i = 0; i < obstacle_vertices.size(); i++) { |
| 493 | obstruction_shape_vertices_ptrw[i] = node_xform.xform(obstacle_vertices_ptr[i]); |
| 494 | obstruction_shape_vertices_ptrw[i].y = 0.0; |
| 495 | } |
| 496 | p_source_geometry_data->add_projected_obstruction(obstruction_shape_vertices, elevation, safe_scale.y * obstacle->get_height(), obstacle->get_carve_navigation_mesh()); |
| 497 | } |
| 498 | |
| 499 | void NavigationObstacle3D::_update_map(RID p_map) { |
| 500 | NavigationServer3D::get_singleton()->obstacle_set_map(obstacle, p_map); |
| 501 | map_current = p_map; |
| 502 | } |
| 503 | |
| 504 | void NavigationObstacle3D::_update_position(const Vector3 p_position) { |
| 505 | NavigationServer3D::get_singleton()->obstacle_set_position(obstacle, p_position); |
| 506 | } |
| 507 | |
| 508 | void NavigationObstacle3D::_update_transform() { |
| 509 | _update_position(get_global_position()); |
| 510 | |
| 511 | // Prevent non-positive or non-uniform scaling of dynamic obstacle radius. |
| 512 | const Vector3 safe_scale = get_global_basis().get_scale().abs().maxf(0.001); |
| 513 | const float scaling_max_value = safe_scale[safe_scale.max_axis_index()]; |
| 514 | NavigationServer3D::get_singleton()->obstacle_set_radius(obstacle, scaling_max_value * radius); |
| 515 | |
| 516 | // Apply modified node transform which only takes y-axis rotation into account to vertices. |
| 517 | const Transform3D safe_transform = Transform3D(Basis().scaled(safe_scale).rotated(Vector3(0.0, 1.0, 0.0), get_global_rotation().y), Vector3()); |
| 518 | NavigationServer3D::get_singleton()->obstacle_set_vertices(obstacle, safe_transform.xform(vertices)); |
| 519 | NavigationServer3D::get_singleton()->obstacle_set_height(obstacle, safe_scale.y * height); |
| 520 | } |
| 521 | |
| 522 | void NavigationObstacle3D::_update_use_3d_avoidance(bool p_use_3d_avoidance) { |
| 523 | NavigationServer3D::get_singleton()->obstacle_set_use_3d_avoidance(obstacle, use_3d_avoidance); |
| 524 | _update_map(map_current); |
| 525 | } |
| 526 | |
| 527 | #ifdef DEBUG_ENABLED |
| 528 | void NavigationObstacle3D::_update_debug() { |
| 529 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 530 | if (is_inside_tree()) { |
| 531 | rs->instance_set_visible(fake_agent_radius_debug_instance_rid, is_visible_in_tree()); |
| 532 | rs->instance_set_visible(static_obstacle_debug_instance_rid, is_visible_in_tree()); |
| 533 | rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, get_world_3d()->get_scenario()); |
| 534 | rs->instance_set_scenario(static_obstacle_debug_instance_rid, get_world_3d()->get_scenario()); |
| 535 | rs->instance_set_transform(fake_agent_radius_debug_instance_rid, Transform3D(Basis(), get_global_position())); |
| 536 | rs->instance_set_transform(static_obstacle_debug_instance_rid, Transform3D(Basis(), get_global_position())); |
| 537 | _update_fake_agent_radius_debug(); |
| 538 | _update_static_obstacle_debug(); |
| 539 | } else { |
| 540 | rs->mesh_clear(fake_agent_radius_debug_mesh_rid); |
| 541 | rs->mesh_clear(static_obstacle_debug_mesh_rid); |
| 542 | rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, RID()); |
| 543 | rs->instance_set_scenario(static_obstacle_debug_instance_rid, RID()); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | void NavigationObstacle3D::_update_fake_agent_radius_debug() { |
| 548 | NavigationServer3D *ns3d = NavigationServer3D::get_singleton(); |
| 549 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 550 | |
| 551 | bool is_debug_enabled = false; |
| 552 | if (Engine::get_singleton()->is_editor_hint()) { |
| 553 | is_debug_enabled = true; |
| 554 | } else if (ns3d->get_debug_enabled() && |
| 555 | ns3d->get_debug_avoidance_enabled() && |
| 556 | ns3d->get_debug_navigation_avoidance_enable_obstacles_radius()) { |
| 557 | is_debug_enabled = true; |
| 558 | } |
| 559 | |
| 560 | rs->mesh_clear(fake_agent_radius_debug_mesh_rid); |
| 561 | |
| 562 | if (!is_debug_enabled) { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | Vector<Vector3> face_vertex_array; |
| 567 | Vector<int> face_indices_array; |
| 568 | |
| 569 | int i, j, prevrow, thisrow, point; |
| 570 | float x, y, z; |
| 571 | |
| 572 | int rings = 16; |
| 573 | int radial_segments = 32; |
| 574 | |
| 575 | point = 0; |
| 576 | |
| 577 | thisrow = 0; |
| 578 | prevrow = 0; |
| 579 | for (j = 0; j <= (rings + 1); j++) { |
| 580 | float v = j; |
| 581 | float w; |
| 582 | |
| 583 | v /= (rings + 1); |
| 584 | w = std::sin(Math::PI * v); |
| 585 | y = (radius)*std::cos(Math::PI * v); |
| 586 | |
| 587 | for (i = 0; i <= radial_segments; i++) { |
| 588 | float u = i; |
| 589 | u /= radial_segments; |
| 590 | |
| 591 | x = std::sin(u * Math::TAU); |
| 592 | z = std::cos(u * Math::TAU); |
| 593 | |
| 594 | Vector3 p = Vector3(x * radius * w, y, z * radius * w); |
| 595 | face_vertex_array.push_back(p); |
| 596 | |
| 597 | point++; |
| 598 | |
| 599 | if (i > 0 && j > 0) { |
| 600 | face_indices_array.push_back(prevrow + i - 1); |
| 601 | face_indices_array.push_back(prevrow + i); |
| 602 | face_indices_array.push_back(thisrow + i - 1); |
| 603 | |
| 604 | face_indices_array.push_back(prevrow + i); |
| 605 | face_indices_array.push_back(thisrow + i); |
| 606 | face_indices_array.push_back(thisrow + i - 1); |
| 607 | }; |
| 608 | }; |
| 609 | |
| 610 | prevrow = thisrow; |
| 611 | thisrow = point; |
| 612 | }; |
| 613 | |
| 614 | Array face_mesh_array; |
| 615 | face_mesh_array.resize(Mesh::ARRAY_MAX); |
| 616 | face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array; |
| 617 | face_mesh_array[Mesh::ARRAY_INDEX] = face_indices_array; |
| 618 | |
| 619 | rs->mesh_add_surface_from_arrays(fake_agent_radius_debug_mesh_rid, RS::PRIMITIVE_TRIANGLES, face_mesh_array); |
| 620 | |
| 621 | Ref<StandardMaterial3D> face_material = ns3d->get_debug_navigation_avoidance_obstacles_radius_material(); |
| 622 | rs->instance_set_surface_override_material(fake_agent_radius_debug_instance_rid, 0, face_material->get_rid()); |
| 623 | |
| 624 | if (is_inside_tree()) { |
| 625 | rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, get_world_3d()->get_scenario()); |
| 626 | rs->instance_set_visible(fake_agent_radius_debug_instance_rid, is_visible_in_tree()); |
| 627 | } |
| 628 | } |
| 629 | #endif // DEBUG_ENABLED |
| 630 | |
| 631 | #ifdef DEBUG_ENABLED |
| 632 | void NavigationObstacle3D::_update_static_obstacle_debug() { |
| 633 | if (Engine::get_singleton()->is_editor_hint()) { |
| 634 | // Don't update inside Editor as Node3D gizmo takes care of this. |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | NavigationServer3D *ns3d = NavigationServer3D::get_singleton(); |
| 639 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 640 | |
| 641 | bool is_debug_enabled = false; |
| 642 | if (ns3d->get_debug_enabled() && |
| 643 | ns3d->get_debug_avoidance_enabled() && |
| 644 | ns3d->get_debug_navigation_avoidance_enable_obstacles_static()) { |
| 645 | is_debug_enabled = true; |
| 646 | } |
| 647 | |
| 648 | rs->mesh_clear(static_obstacle_debug_mesh_rid); |
| 649 | |
| 650 | if (!is_debug_enabled) { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | const int vertex_count = vertices.size(); |
| 655 | |
| 656 | if (vertex_count < 3) { |
| 657 | if (static_obstacle_debug_instance_rid.is_valid()) { |
| 658 | rs->instance_set_visible(static_obstacle_debug_instance_rid, false); |
| 659 | } |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | Vector<Vector3> edge_vertex_array; |
| 664 | edge_vertex_array.resize(vertex_count * 8); |
| 665 | |
| 666 | Vector3 *edge_vertex_array_ptrw = edge_vertex_array.ptrw(); |
| 667 | |
| 668 | int vertex_index = 0; |
| 669 | |
| 670 | for (int i = 0; i < vertex_count; i++) { |
| 671 | Vector3 point = vertices[i]; |
| 672 | Vector3 next_point = vertices[(i + 1) % vertex_count]; |
| 673 | |
| 674 | Vector3 direction = next_point.direction_to(point); |
| 675 | Vector3 arrow_dir = direction.cross(Vector3(0.0, 1.0, 0.0)); |
| 676 | Vector3 edge_middle = point + ((next_point - point) * 0.5); |
| 677 | |
| 678 | edge_vertex_array_ptrw[vertex_index++] = edge_middle; |
| 679 | edge_vertex_array_ptrw[vertex_index++] = edge_middle + (arrow_dir * 0.5); |
| 680 | |
| 681 | edge_vertex_array_ptrw[vertex_index++] = point; |
| 682 | edge_vertex_array_ptrw[vertex_index++] = next_point; |
| 683 | |
| 684 | edge_vertex_array_ptrw[vertex_index++] = Vector3(point.x, height, point.z); |
| 685 | edge_vertex_array_ptrw[vertex_index++] = Vector3(next_point.x, height, next_point.z); |
| 686 | |
| 687 | edge_vertex_array_ptrw[vertex_index++] = point; |
| 688 | edge_vertex_array_ptrw[vertex_index++] = Vector3(point.x, height, point.z); |
| 689 | } |
| 690 | |
| 691 | Array edge_mesh_array; |
| 692 | edge_mesh_array.resize(Mesh::ARRAY_MAX); |
| 693 | edge_mesh_array[Mesh::ARRAY_VERTEX] = edge_vertex_array; |
| 694 | |
| 695 | rs->mesh_add_surface_from_arrays(static_obstacle_debug_mesh_rid, RS::PRIMITIVE_LINES, edge_mesh_array); |
| 696 | |
| 697 | Ref<StandardMaterial3D> edge_material; |
| 698 | |
| 699 | if (are_vertices_valid()) { |
| 700 | edge_material = ns3d->get_debug_navigation_avoidance_static_obstacle_pushout_edge_material(); |
| 701 | } else { |
| 702 | edge_material = ns3d->get_debug_navigation_avoidance_static_obstacle_pushin_edge_material(); |
| 703 | } |
| 704 | |
| 705 | rs->instance_set_surface_override_material(static_obstacle_debug_instance_rid, 0, edge_material->get_rid()); |
| 706 | |
| 707 | if (is_inside_tree()) { |
| 708 | rs->instance_set_scenario(static_obstacle_debug_instance_rid, get_world_3d()->get_scenario()); |
| 709 | rs->instance_set_visible(static_obstacle_debug_instance_rid, is_visible_in_tree()); |
| 710 | } |
| 711 | } |
| 712 | #endif // DEBUG_ENABLED |
| 713 | |
| 714 | #ifdef DEBUG_ENABLED |
| 715 | void NavigationObstacle3D::_clear_debug() { |
| 716 | RenderingServer *rs = RenderingServer::get_singleton(); |
| 717 | ERR_FAIL_NULL(rs); |
| 718 | rs->mesh_clear(fake_agent_radius_debug_mesh_rid); |
| 719 | rs->mesh_clear(static_obstacle_debug_mesh_rid); |
| 720 | rs->instance_set_scenario(fake_agent_radius_debug_instance_rid, RID()); |
| 721 | rs->instance_set_scenario(static_obstacle_debug_instance_rid, RID()); |
| 722 | } |
| 723 | #endif // DEBUG_ENABLED |