navigation_obstacle_3d.cpp 22 KB

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