navigation_obstacle_3d.cpp 25 KB

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