navigation_obstacle_3d.cpp 29 KB

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