| 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/3d/collision_shape_3d.h" | 
|---|
| 35 | #include "scene/3d/physics_body_3d.h" | 
|---|
| 36 | #include "servers/navigation_server_3d.h" | 
|---|
| 37 |  | 
|---|
| 38 | void NavigationObstacle3D::_bind_methods() { | 
|---|
| 39 | ClassDB::bind_method(D_METHOD( "get_rid"), &NavigationObstacle3D::get_rid); | 
|---|
| 40 |  | 
|---|
| 41 | ClassDB::bind_method(D_METHOD( "set_avoidance_enabled", "enabled"), &NavigationObstacle3D::set_avoidance_enabled); | 
|---|
| 42 | ClassDB::bind_method(D_METHOD( "get_avoidance_enabled"), &NavigationObstacle3D::get_avoidance_enabled); | 
|---|
| 43 |  | 
|---|
| 44 | ClassDB::bind_method(D_METHOD( "set_navigation_map", "navigation_map"), &NavigationObstacle3D::set_navigation_map); | 
|---|
| 45 | ClassDB::bind_method(D_METHOD( "get_navigation_map"), &NavigationObstacle3D::get_navigation_map); | 
|---|
| 46 |  | 
|---|
| 47 | ClassDB::bind_method(D_METHOD( "set_radius", "radius"), &NavigationObstacle3D::set_radius); | 
|---|
| 48 | ClassDB::bind_method(D_METHOD( "get_radius"), &NavigationObstacle3D::get_radius); | 
|---|
| 49 |  | 
|---|
| 50 | ClassDB::bind_method(D_METHOD( "set_height", "height"), &NavigationObstacle3D::set_height); | 
|---|
| 51 | ClassDB::bind_method(D_METHOD( "get_height"), &NavigationObstacle3D::get_height); | 
|---|
| 52 |  | 
|---|
| 53 | ClassDB::bind_method(D_METHOD( "set_velocity", "velocity"), &NavigationObstacle3D::set_velocity); | 
|---|
| 54 | ClassDB::bind_method(D_METHOD( "get_velocity"), &NavigationObstacle3D::get_velocity); | 
|---|
| 55 |  | 
|---|
| 56 | ClassDB::bind_method(D_METHOD( "set_vertices", "vertices"), &NavigationObstacle3D::set_vertices); | 
|---|
| 57 | ClassDB::bind_method(D_METHOD( "get_vertices"), &NavigationObstacle3D::get_vertices); | 
|---|
| 58 |  | 
|---|
| 59 | ClassDB::bind_method(D_METHOD( "set_avoidance_layers", "layers"), &NavigationObstacle3D::set_avoidance_layers); | 
|---|
| 60 | ClassDB::bind_method(D_METHOD( "get_avoidance_layers"), &NavigationObstacle3D::get_avoidance_layers); | 
|---|
| 61 |  | 
|---|
| 62 | ClassDB::bind_method(D_METHOD( "set_avoidance_layer_value", "layer_number", "value"), &NavigationObstacle3D::set_avoidance_layer_value); | 
|---|
| 63 | ClassDB::bind_method(D_METHOD( "get_avoidance_layer_value", "layer_number"), &NavigationObstacle3D::get_avoidance_layer_value); | 
|---|
| 64 |  | 
|---|
| 65 | ClassDB::bind_method(D_METHOD( "set_use_3d_avoidance", "enabled"), &NavigationObstacle3D::set_use_3d_avoidance); | 
|---|
| 66 | ClassDB::bind_method(D_METHOD( "get_use_3d_avoidance"), &NavigationObstacle3D::get_use_3d_avoidance); | 
|---|
| 67 |  | 
|---|
| 68 | ADD_GROUP( "Avoidance", ""); | 
|---|
| 69 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled"); | 
|---|
| 70 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity"); | 
|---|
| 71 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.0,100,0.01,suffix:m"), "set_radius", "get_radius"); | 
|---|
| 72 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.0,100,0.01,suffix:m"), "set_height", "get_height"); | 
|---|
| 73 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices"), "set_vertices", "get_vertices"); | 
|---|
| 74 | ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers"); | 
|---|
| 75 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance"); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void NavigationObstacle3D::_notification(int p_what) { | 
|---|
| 79 | switch (p_what) { | 
|---|
| 80 | case NOTIFICATION_POST_ENTER_TREE: { | 
|---|
| 81 | if (map_override.is_valid()) { | 
|---|
| 82 | _update_map(map_override); | 
|---|
| 83 | } else if (is_inside_tree()) { | 
|---|
| 84 | _update_map(get_world_3d()->get_navigation_map()); | 
|---|
| 85 | } else { | 
|---|
| 86 | _update_map(RID()); | 
|---|
| 87 | } | 
|---|
| 88 | previous_transform = get_global_transform(); | 
|---|
| 89 | // need to trigger map controlled agent assignment somehow for the fake_agent since obstacles use no callback like regular agents | 
|---|
| 90 | NavigationServer3D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled); | 
|---|
| 91 | _update_position(get_global_transform().origin); | 
|---|
| 92 | set_physics_process_internal(true); | 
|---|
| 93 | #ifdef DEBUG_ENABLED | 
|---|
| 94 | if ((NavigationServer3D::get_singleton()->get_debug_avoidance_enabled()) && | 
|---|
| 95 | (NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius())) { | 
|---|
| 96 | _update_fake_agent_radius_debug(); | 
|---|
| 97 | _update_static_obstacle_debug(); | 
|---|
| 98 | } | 
|---|
| 99 | #endif // DEBUG_ENABLED | 
|---|
| 100 | } break; | 
|---|
| 101 |  | 
|---|
| 102 | case NOTIFICATION_EXIT_TREE: { | 
|---|
| 103 | set_physics_process_internal(false); | 
|---|
| 104 | _update_map(RID()); | 
|---|
| 105 | #ifdef DEBUG_ENABLED | 
|---|
| 106 | if (fake_agent_radius_debug_instance.is_valid()) { | 
|---|
| 107 | RS::get_singleton()->instance_set_visible(fake_agent_radius_debug_instance, false); | 
|---|
| 108 | } | 
|---|
| 109 | if (static_obstacle_debug_instance.is_valid()) { | 
|---|
| 110 | RS::get_singleton()->instance_set_visible(static_obstacle_debug_instance, false); | 
|---|
| 111 | } | 
|---|
| 112 | #endif // DEBUG_ENABLED | 
|---|
| 113 | } break; | 
|---|
| 114 |  | 
|---|
| 115 | case NOTIFICATION_PAUSED: { | 
|---|
| 116 | if (!can_process()) { | 
|---|
| 117 | map_before_pause = map_current; | 
|---|
| 118 | _update_map(RID()); | 
|---|
| 119 | } else if (can_process() && !(map_before_pause == RID())) { | 
|---|
| 120 | _update_map(map_before_pause); | 
|---|
| 121 | map_before_pause = RID(); | 
|---|
| 122 | } | 
|---|
| 123 | NavigationServer3D::get_singleton()->obstacle_set_paused(obstacle, !can_process()); | 
|---|
| 124 | } break; | 
|---|
| 125 |  | 
|---|
| 126 | case NOTIFICATION_UNPAUSED: { | 
|---|
| 127 | if (!can_process()) { | 
|---|
| 128 | map_before_pause = map_current; | 
|---|
| 129 | _update_map(RID()); | 
|---|
| 130 | } else if (can_process() && !(map_before_pause == RID())) { | 
|---|
| 131 | _update_map(map_before_pause); | 
|---|
| 132 | map_before_pause = RID(); | 
|---|
| 133 | } | 
|---|
| 134 | NavigationServer3D::get_singleton()->obstacle_set_paused(obstacle, !can_process()); | 
|---|
| 135 | } break; | 
|---|
| 136 |  | 
|---|
| 137 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { | 
|---|
| 138 | if (is_inside_tree()) { | 
|---|
| 139 | _update_position(get_global_transform().origin); | 
|---|
| 140 |  | 
|---|
| 141 | if (velocity_submitted) { | 
|---|
| 142 | velocity_submitted = false; | 
|---|
| 143 | // only update if there is a noticeable change, else the rvo agent preferred velocity stays the same | 
|---|
| 144 | if (!previous_velocity.is_equal_approx(velocity)) { | 
|---|
| 145 | NavigationServer3D::get_singleton()->obstacle_set_velocity(obstacle, velocity); | 
|---|
| 146 | } | 
|---|
| 147 | previous_velocity = velocity; | 
|---|
| 148 | } | 
|---|
| 149 | #ifdef DEBUG_ENABLED | 
|---|
| 150 | if (fake_agent_radius_debug_instance.is_valid() && radius > 0.0) { | 
|---|
| 151 | RS::get_singleton()->instance_set_transform(fake_agent_radius_debug_instance, get_global_transform()); | 
|---|
| 152 | } | 
|---|
| 153 | if (static_obstacle_debug_instance.is_valid() && get_vertices().size() > 0) { | 
|---|
| 154 | RS::get_singleton()->instance_set_transform(static_obstacle_debug_instance, get_global_transform()); | 
|---|
| 155 | } | 
|---|
| 156 | #endif // DEBUG_ENABLED | 
|---|
| 157 | } | 
|---|
| 158 | } break; | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | NavigationObstacle3D::NavigationObstacle3D() { | 
|---|
| 163 | obstacle = NavigationServer3D::get_singleton()->obstacle_create(); | 
|---|
| 164 |  | 
|---|
| 165 | set_radius(radius); | 
|---|
| 166 | set_height(height); | 
|---|
| 167 | set_vertices(vertices); | 
|---|
| 168 | set_avoidance_layers(avoidance_layers); | 
|---|
| 169 | set_avoidance_enabled(avoidance_enabled); | 
|---|
| 170 | set_use_3d_avoidance(use_3d_avoidance); | 
|---|
| 171 |  | 
|---|
| 172 | #ifdef DEBUG_ENABLED | 
|---|
| 173 | NavigationServer3D::get_singleton()->connect( "avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_fake_agent_radius_debug)); | 
|---|
| 174 | NavigationServer3D::get_singleton()->connect( "avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_static_obstacle_debug)); | 
|---|
| 175 | _update_fake_agent_radius_debug(); | 
|---|
| 176 | _update_static_obstacle_debug(); | 
|---|
| 177 | #endif // DEBUG_ENABLED | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | NavigationObstacle3D::~NavigationObstacle3D() { | 
|---|
| 181 | ERR_FAIL_NULL(NavigationServer3D::get_singleton()); | 
|---|
| 182 |  | 
|---|
| 183 | NavigationServer3D::get_singleton()->free(obstacle); | 
|---|
| 184 | obstacle = RID(); | 
|---|
| 185 |  | 
|---|
| 186 | #ifdef DEBUG_ENABLED | 
|---|
| 187 | NavigationServer3D::get_singleton()->disconnect( "avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_fake_agent_radius_debug)); | 
|---|
| 188 | NavigationServer3D::get_singleton()->disconnect( "avoidance_debug_changed", callable_mp(this, &NavigationObstacle3D::_update_static_obstacle_debug)); | 
|---|
| 189 | if (fake_agent_radius_debug_instance.is_valid()) { | 
|---|
| 190 | RenderingServer::get_singleton()->free(fake_agent_radius_debug_instance); | 
|---|
| 191 | } | 
|---|
| 192 | if (fake_agent_radius_debug_mesh.is_valid()) { | 
|---|
| 193 | RenderingServer::get_singleton()->free(fake_agent_radius_debug_mesh->get_rid()); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | if (static_obstacle_debug_instance.is_valid()) { | 
|---|
| 197 | RenderingServer::get_singleton()->free(static_obstacle_debug_instance); | 
|---|
| 198 | } | 
|---|
| 199 | if (static_obstacle_debug_mesh.is_valid()) { | 
|---|
| 200 | RenderingServer::get_singleton()->free(static_obstacle_debug_mesh->get_rid()); | 
|---|
| 201 | } | 
|---|
| 202 | #endif // DEBUG_ENABLED | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | void NavigationObstacle3D::set_vertices(const Vector<Vector3> &p_vertices) { | 
|---|
| 206 | vertices = p_vertices; | 
|---|
| 207 | NavigationServer3D::get_singleton()->obstacle_set_vertices(obstacle, vertices); | 
|---|
| 208 | #ifdef DEBUG_ENABLED | 
|---|
| 209 | _update_static_obstacle_debug(); | 
|---|
| 210 | #endif // DEBUG_ENABLED | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | void NavigationObstacle3D::set_navigation_map(RID p_navigation_map) { | 
|---|
| 214 | if (map_override == p_navigation_map) { | 
|---|
| 215 | return; | 
|---|
| 216 | } | 
|---|
| 217 | map_override = p_navigation_map; | 
|---|
| 218 | _update_map(map_override); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | RID NavigationObstacle3D::get_navigation_map() const { | 
|---|
| 222 | if (map_override.is_valid()) { | 
|---|
| 223 | return map_override; | 
|---|
| 224 | } else if (is_inside_tree()) { | 
|---|
| 225 | return get_world_3d()->get_navigation_map(); | 
|---|
| 226 | } | 
|---|
| 227 | return RID(); | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | void NavigationObstacle3D::set_radius(real_t p_radius) { | 
|---|
| 231 | ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive."); | 
|---|
| 232 | if (Math::is_equal_approx(radius, p_radius)) { | 
|---|
| 233 | return; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | radius = p_radius; | 
|---|
| 237 | NavigationServer3D::get_singleton()->obstacle_set_radius(obstacle, radius); | 
|---|
| 238 |  | 
|---|
| 239 | #ifdef DEBUG_ENABLED | 
|---|
| 240 | _update_fake_agent_radius_debug(); | 
|---|
| 241 | #endif // DEBUG_ENABLED | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | void NavigationObstacle3D::set_height(real_t p_height) { | 
|---|
| 245 | ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive."); | 
|---|
| 246 | if (Math::is_equal_approx(height, p_height)) { | 
|---|
| 247 | return; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | height = p_height; | 
|---|
| 251 | NavigationServer3D::get_singleton()->obstacle_set_height(obstacle, height); | 
|---|
| 252 |  | 
|---|
| 253 | #ifdef DEBUG_ENABLED | 
|---|
| 254 | _update_static_obstacle_debug(); | 
|---|
| 255 | #endif // DEBUG_ENABLED | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | void NavigationObstacle3D::set_avoidance_layers(uint32_t p_layers) { | 
|---|
| 259 | avoidance_layers = p_layers; | 
|---|
| 260 | NavigationServer3D::get_singleton()->obstacle_set_avoidance_layers(obstacle, avoidance_layers); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | uint32_t NavigationObstacle3D::get_avoidance_layers() const { | 
|---|
| 264 | return avoidance_layers; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | void NavigationObstacle3D::set_avoidance_layer_value(int p_layer_number, bool p_value) { | 
|---|
| 268 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive."); | 
|---|
| 269 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive."); | 
|---|
| 270 | uint32_t avoidance_layers_new = get_avoidance_layers(); | 
|---|
| 271 | if (p_value) { | 
|---|
| 272 | avoidance_layers_new |= 1 << (p_layer_number - 1); | 
|---|
| 273 | } else { | 
|---|
| 274 | avoidance_layers_new &= ~(1 << (p_layer_number - 1)); | 
|---|
| 275 | } | 
|---|
| 276 | set_avoidance_layers(avoidance_layers_new); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | bool NavigationObstacle3D::get_avoidance_layer_value(int p_layer_number) const { | 
|---|
| 280 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive."); | 
|---|
| 281 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive."); | 
|---|
| 282 | return get_avoidance_layers() & (1 << (p_layer_number - 1)); | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | void NavigationObstacle3D::set_avoidance_enabled(bool p_enabled) { | 
|---|
| 286 | if (avoidance_enabled == p_enabled) { | 
|---|
| 287 | return; | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | avoidance_enabled = p_enabled; | 
|---|
| 291 | NavigationServer3D::get_singleton()->obstacle_set_avoidance_enabled(obstacle, avoidance_enabled); | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | bool NavigationObstacle3D::get_avoidance_enabled() const { | 
|---|
| 295 | return avoidance_enabled; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | void NavigationObstacle3D::set_use_3d_avoidance(bool p_use_3d_avoidance) { | 
|---|
| 299 | use_3d_avoidance = p_use_3d_avoidance; | 
|---|
| 300 | _update_use_3d_avoidance(use_3d_avoidance); | 
|---|
| 301 | notify_property_list_changed(); | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | void NavigationObstacle3D::set_velocity(const Vector3 p_velocity) { | 
|---|
| 305 | velocity = p_velocity; | 
|---|
| 306 | velocity_submitted = true; | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | void NavigationObstacle3D::_update_map(RID p_map) { | 
|---|
| 310 | NavigationServer3D::get_singleton()->obstacle_set_map(obstacle, p_map); | 
|---|
| 311 | map_current = p_map; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | void NavigationObstacle3D::_update_position(const Vector3 p_position) { | 
|---|
| 315 | NavigationServer3D::get_singleton()->obstacle_set_position(obstacle, p_position); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | void NavigationObstacle3D::_update_use_3d_avoidance(bool p_use_3d_avoidance) { | 
|---|
| 319 | NavigationServer3D::get_singleton()->obstacle_set_use_3d_avoidance(obstacle, use_3d_avoidance); | 
|---|
| 320 | _update_map(map_current); | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | #ifdef DEBUG_ENABLED | 
|---|
| 324 | void NavigationObstacle3D::_update_fake_agent_radius_debug() { | 
|---|
| 325 | bool is_debug_enabled = false; | 
|---|
| 326 | if (Engine::get_singleton()->is_editor_hint()) { | 
|---|
| 327 | is_debug_enabled = true; | 
|---|
| 328 | } else if (NavigationServer3D::get_singleton()->get_debug_enabled() && | 
|---|
| 329 | NavigationServer3D::get_singleton()->get_debug_avoidance_enabled() && | 
|---|
| 330 | NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_radius()) { | 
|---|
| 331 | is_debug_enabled = true; | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | if (is_debug_enabled == false) { | 
|---|
| 335 | if (fake_agent_radius_debug_instance.is_valid()) { | 
|---|
| 336 | RS::get_singleton()->instance_set_visible(fake_agent_radius_debug_instance, false); | 
|---|
| 337 | } | 
|---|
| 338 | return; | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | if (!fake_agent_radius_debug_instance.is_valid()) { | 
|---|
| 342 | fake_agent_radius_debug_instance = RenderingServer::get_singleton()->instance_create(); | 
|---|
| 343 | } | 
|---|
| 344 | if (!fake_agent_radius_debug_mesh.is_valid()) { | 
|---|
| 345 | fake_agent_radius_debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); | 
|---|
| 346 | } | 
|---|
| 347 | fake_agent_radius_debug_mesh->clear_surfaces(); | 
|---|
| 348 |  | 
|---|
| 349 | Vector<Vector3> face_vertex_array; | 
|---|
| 350 | Vector<int> face_indices_array; | 
|---|
| 351 |  | 
|---|
| 352 | int i, j, prevrow, thisrow, point; | 
|---|
| 353 | float x, y, z; | 
|---|
| 354 |  | 
|---|
| 355 | int rings = 16; | 
|---|
| 356 | int radial_segments = 32; | 
|---|
| 357 |  | 
|---|
| 358 | point = 0; | 
|---|
| 359 |  | 
|---|
| 360 | thisrow = 0; | 
|---|
| 361 | prevrow = 0; | 
|---|
| 362 | for (j = 0; j <= (rings + 1); j++) { | 
|---|
| 363 | float v = j; | 
|---|
| 364 | float w; | 
|---|
| 365 |  | 
|---|
| 366 | v /= (rings + 1); | 
|---|
| 367 | w = sin(Math_PI * v); | 
|---|
| 368 | y = (radius)*cos(Math_PI * v); | 
|---|
| 369 |  | 
|---|
| 370 | for (i = 0; i <= radial_segments; i++) { | 
|---|
| 371 | float u = i; | 
|---|
| 372 | u /= radial_segments; | 
|---|
| 373 |  | 
|---|
| 374 | x = sin(u * Math_TAU); | 
|---|
| 375 | z = cos(u * Math_TAU); | 
|---|
| 376 |  | 
|---|
| 377 | Vector3 p = Vector3(x * radius * w, y, z * radius * w); | 
|---|
| 378 | face_vertex_array.push_back(p); | 
|---|
| 379 |  | 
|---|
| 380 | point++; | 
|---|
| 381 |  | 
|---|
| 382 | if (i > 0 && j > 0) { | 
|---|
| 383 | face_indices_array.push_back(prevrow + i - 1); | 
|---|
| 384 | face_indices_array.push_back(prevrow + i); | 
|---|
| 385 | face_indices_array.push_back(thisrow + i - 1); | 
|---|
| 386 |  | 
|---|
| 387 | face_indices_array.push_back(prevrow + i); | 
|---|
| 388 | face_indices_array.push_back(thisrow + i); | 
|---|
| 389 | face_indices_array.push_back(thisrow + i - 1); | 
|---|
| 390 | }; | 
|---|
| 391 | }; | 
|---|
| 392 |  | 
|---|
| 393 | prevrow = thisrow; | 
|---|
| 394 | thisrow = point; | 
|---|
| 395 | }; | 
|---|
| 396 |  | 
|---|
| 397 | Array face_mesh_array; | 
|---|
| 398 | face_mesh_array.resize(Mesh::ARRAY_MAX); | 
|---|
| 399 | face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array; | 
|---|
| 400 | face_mesh_array[Mesh::ARRAY_INDEX] = face_indices_array; | 
|---|
| 401 |  | 
|---|
| 402 | fake_agent_radius_debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array); | 
|---|
| 403 | Ref<StandardMaterial3D> face_material = NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_obstacles_radius_material(); | 
|---|
| 404 | fake_agent_radius_debug_mesh->surface_set_material(0, face_material); | 
|---|
| 405 |  | 
|---|
| 406 | RS::get_singleton()->instance_set_base(fake_agent_radius_debug_instance, fake_agent_radius_debug_mesh->get_rid()); | 
|---|
| 407 | if (is_inside_tree()) { | 
|---|
| 408 | RS::get_singleton()->instance_set_scenario(fake_agent_radius_debug_instance, get_world_3d()->get_scenario()); | 
|---|
| 409 | RS::get_singleton()->instance_set_visible(fake_agent_radius_debug_instance, is_visible_in_tree()); | 
|---|
| 410 | } | 
|---|
| 411 | } | 
|---|
| 412 | #endif // DEBUG_ENABLED | 
|---|
| 413 |  | 
|---|
| 414 | #ifdef DEBUG_ENABLED | 
|---|
| 415 | void NavigationObstacle3D::_update_static_obstacle_debug() { | 
|---|
| 416 | bool is_debug_enabled = false; | 
|---|
| 417 | if (Engine::get_singleton()->is_editor_hint()) { | 
|---|
| 418 | is_debug_enabled = true; | 
|---|
| 419 | } else if (NavigationServer3D::get_singleton()->get_debug_enabled() && | 
|---|
| 420 | NavigationServer3D::get_singleton()->get_debug_avoidance_enabled() && | 
|---|
| 421 | NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_enable_obstacles_static()) { | 
|---|
| 422 | is_debug_enabled = true; | 
|---|
| 423 | } | 
|---|
| 424 |  | 
|---|
| 425 | if (is_debug_enabled == false) { | 
|---|
| 426 | if (static_obstacle_debug_instance.is_valid()) { | 
|---|
| 427 | RS::get_singleton()->instance_set_visible(static_obstacle_debug_instance, false); | 
|---|
| 428 | } | 
|---|
| 429 | return; | 
|---|
| 430 | } | 
|---|
| 431 |  | 
|---|
| 432 | if (vertices.size() < 3) { | 
|---|
| 433 | if (static_obstacle_debug_instance.is_valid()) { | 
|---|
| 434 | RS::get_singleton()->instance_set_visible(static_obstacle_debug_instance, false); | 
|---|
| 435 | } | 
|---|
| 436 | return; | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 | if (!static_obstacle_debug_instance.is_valid()) { | 
|---|
| 440 | static_obstacle_debug_instance = RenderingServer::get_singleton()->instance_create(); | 
|---|
| 441 | } | 
|---|
| 442 | if (!static_obstacle_debug_mesh.is_valid()) { | 
|---|
| 443 | static_obstacle_debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); | 
|---|
| 444 | } | 
|---|
| 445 | static_obstacle_debug_mesh->clear_surfaces(); | 
|---|
| 446 |  | 
|---|
| 447 | Vector<Vector2> polygon_2d_vertices; | 
|---|
| 448 | polygon_2d_vertices.resize(vertices.size()); | 
|---|
| 449 | Vector2 *polygon_2d_vertices_ptr = polygon_2d_vertices.ptrw(); | 
|---|
| 450 |  | 
|---|
| 451 | for (int i = 0; i < vertices.size(); ++i) { | 
|---|
| 452 | Vector3 obstacle_vertex = vertices[i]; | 
|---|
| 453 | Vector2 obstacle_vertex_2d = Vector2(obstacle_vertex.x, obstacle_vertex.z); | 
|---|
| 454 | polygon_2d_vertices_ptr[i] = obstacle_vertex_2d; | 
|---|
| 455 | } | 
|---|
| 456 |  | 
|---|
| 457 | Vector<int> triangulated_polygon_2d_indices = Geometry2D::triangulate_polygon(polygon_2d_vertices); | 
|---|
| 458 |  | 
|---|
| 459 | if (triangulated_polygon_2d_indices.is_empty()) { | 
|---|
| 460 | // failed triangulation | 
|---|
| 461 | return; | 
|---|
| 462 | } | 
|---|
| 463 |  | 
|---|
| 464 | bool obstacle_pushes_inward = Geometry2D::is_polygon_clockwise(polygon_2d_vertices); | 
|---|
| 465 |  | 
|---|
| 466 | Vector<Vector3> face_vertex_array; | 
|---|
| 467 | Vector<int> face_indices_array; | 
|---|
| 468 |  | 
|---|
| 469 | face_vertex_array.resize(polygon_2d_vertices.size()); | 
|---|
| 470 | face_indices_array.resize(triangulated_polygon_2d_indices.size()); | 
|---|
| 471 |  | 
|---|
| 472 | Vector3 *face_vertex_array_ptr = face_vertex_array.ptrw(); | 
|---|
| 473 | int *face_indices_array_ptr = face_indices_array.ptrw(); | 
|---|
| 474 |  | 
|---|
| 475 | for (int i = 0; i < triangulated_polygon_2d_indices.size(); ++i) { | 
|---|
| 476 | int vertex_index = triangulated_polygon_2d_indices[i]; | 
|---|
| 477 | const Vector2 &vertex_2d = polygon_2d_vertices[vertex_index]; | 
|---|
| 478 | Vector3 vertex_3d = Vector3(vertex_2d.x, 0.0, vertex_2d.y); | 
|---|
| 479 | face_vertex_array_ptr[vertex_index] = vertex_3d; | 
|---|
| 480 | face_indices_array_ptr[i] = vertex_index; | 
|---|
| 481 | } | 
|---|
| 482 |  | 
|---|
| 483 | Array face_mesh_array; | 
|---|
| 484 | face_mesh_array.resize(Mesh::ARRAY_MAX); | 
|---|
| 485 | face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array; | 
|---|
| 486 | face_mesh_array[Mesh::ARRAY_INDEX] = face_indices_array; | 
|---|
| 487 |  | 
|---|
| 488 | static_obstacle_debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array); | 
|---|
| 489 |  | 
|---|
| 490 | Vector<Vector3> edge_vertex_array; | 
|---|
| 491 |  | 
|---|
| 492 | for (int i = 0; i < polygon_2d_vertices.size(); ++i) { | 
|---|
| 493 | int from_index = i - 1; | 
|---|
| 494 | int to_index = i; | 
|---|
| 495 |  | 
|---|
| 496 | if (i == 0) { | 
|---|
| 497 | from_index = polygon_2d_vertices.size() - 1; | 
|---|
| 498 | } | 
|---|
| 499 |  | 
|---|
| 500 | const Vector2 &vertex_2d_from = polygon_2d_vertices[from_index]; | 
|---|
| 501 | const Vector2 &vertex_2d_to = polygon_2d_vertices[to_index]; | 
|---|
| 502 |  | 
|---|
| 503 | Vector3 vertex_3d_ground_from = Vector3(vertex_2d_from.x, 0.0, vertex_2d_from.y); | 
|---|
| 504 | Vector3 vertex_3d_ground_to = Vector3(vertex_2d_to.x, 0.0, vertex_2d_to.y); | 
|---|
| 505 |  | 
|---|
| 506 | edge_vertex_array.push_back(vertex_3d_ground_from); | 
|---|
| 507 | edge_vertex_array.push_back(vertex_3d_ground_to); | 
|---|
| 508 |  | 
|---|
| 509 | Vector3 vertex_3d_height_from = Vector3(vertex_2d_from.x, height, vertex_2d_from.y); | 
|---|
| 510 | Vector3 vertex_3d_height_to = Vector3(vertex_2d_to.x, height, vertex_2d_to.y); | 
|---|
| 511 |  | 
|---|
| 512 | edge_vertex_array.push_back(vertex_3d_height_from); | 
|---|
| 513 | edge_vertex_array.push_back(vertex_3d_height_to); | 
|---|
| 514 |  | 
|---|
| 515 | edge_vertex_array.push_back(vertex_3d_ground_from); | 
|---|
| 516 | edge_vertex_array.push_back(vertex_3d_height_from); | 
|---|
| 517 | } | 
|---|
| 518 |  | 
|---|
| 519 | Array edge_mesh_array; | 
|---|
| 520 | edge_mesh_array.resize(Mesh::ARRAY_MAX); | 
|---|
| 521 | edge_mesh_array[Mesh::ARRAY_VERTEX] = edge_vertex_array; | 
|---|
| 522 |  | 
|---|
| 523 | static_obstacle_debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, edge_mesh_array); | 
|---|
| 524 |  | 
|---|
| 525 | Ref<StandardMaterial3D> face_material; | 
|---|
| 526 | Ref<StandardMaterial3D> edge_material; | 
|---|
| 527 |  | 
|---|
| 528 | if (obstacle_pushes_inward) { | 
|---|
| 529 | face_material = NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_face_material(); | 
|---|
| 530 | edge_material = NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushin_edge_material(); | 
|---|
| 531 | } else { | 
|---|
| 532 | face_material = NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_face_material(); | 
|---|
| 533 | edge_material = NavigationServer3D::get_singleton()->get_debug_navigation_avoidance_static_obstacle_pushout_edge_material(); | 
|---|
| 534 | } | 
|---|
| 535 |  | 
|---|
| 536 | static_obstacle_debug_mesh->surface_set_material(0, face_material); | 
|---|
| 537 | static_obstacle_debug_mesh->surface_set_material(1, edge_material); | 
|---|
| 538 |  | 
|---|
| 539 | RS::get_singleton()->instance_set_base(static_obstacle_debug_instance, static_obstacle_debug_mesh->get_rid()); | 
|---|
| 540 | if (is_inside_tree()) { | 
|---|
| 541 | RS::get_singleton()->instance_set_scenario(static_obstacle_debug_instance, get_world_3d()->get_scenario()); | 
|---|
| 542 | RS::get_singleton()->instance_set_visible(static_obstacle_debug_instance, is_visible_in_tree()); | 
|---|
| 543 | } | 
|---|
| 544 | } | 
|---|
| 545 | #endif // DEBUG_ENABLED | 
|---|
| 546 |  | 
|---|