navigation_region_2d.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /**************************************************************************/
  2. /* navigation_region_2d.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_region_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "scene/resources/world_2d.h"
  33. #include "servers/navigation_server_2d.h"
  34. RID NavigationRegion2D::get_rid() const {
  35. return region;
  36. }
  37. void NavigationRegion2D::set_enabled(bool p_enabled) {
  38. if (enabled == p_enabled) {
  39. return;
  40. }
  41. enabled = p_enabled;
  42. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  43. #ifdef DEBUG_ENABLED
  44. if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_navigation_enabled()) {
  45. queue_redraw();
  46. }
  47. #endif // DEBUG_ENABLED
  48. }
  49. bool NavigationRegion2D::is_enabled() const {
  50. return enabled;
  51. }
  52. void NavigationRegion2D::set_use_edge_connections(bool p_enabled) {
  53. if (use_edge_connections == p_enabled) {
  54. return;
  55. }
  56. use_edge_connections = p_enabled;
  57. NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  58. }
  59. bool NavigationRegion2D::get_use_edge_connections() const {
  60. return use_edge_connections;
  61. }
  62. void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
  63. if (navigation_layers == p_navigation_layers) {
  64. return;
  65. }
  66. navigation_layers = p_navigation_layers;
  67. NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  68. }
  69. uint32_t NavigationRegion2D::get_navigation_layers() const {
  70. return navigation_layers;
  71. }
  72. void NavigationRegion2D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  73. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  74. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  75. uint32_t _navigation_layers = get_navigation_layers();
  76. if (p_value) {
  77. _navigation_layers |= 1 << (p_layer_number - 1);
  78. } else {
  79. _navigation_layers &= ~(1 << (p_layer_number - 1));
  80. }
  81. set_navigation_layers(_navigation_layers);
  82. }
  83. bool NavigationRegion2D::get_navigation_layer_value(int p_layer_number) const {
  84. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  85. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  86. return get_navigation_layers() & (1 << (p_layer_number - 1));
  87. }
  88. void NavigationRegion2D::set_enter_cost(real_t p_enter_cost) {
  89. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  90. if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
  91. return;
  92. }
  93. enter_cost = p_enter_cost;
  94. NavigationServer2D::get_singleton()->region_set_enter_cost(region, enter_cost);
  95. }
  96. real_t NavigationRegion2D::get_enter_cost() const {
  97. return enter_cost;
  98. }
  99. void NavigationRegion2D::set_travel_cost(real_t p_travel_cost) {
  100. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  101. if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
  102. return;
  103. }
  104. travel_cost = p_travel_cost;
  105. NavigationServer2D::get_singleton()->region_set_travel_cost(region, travel_cost);
  106. }
  107. real_t NavigationRegion2D::get_travel_cost() const {
  108. return travel_cost;
  109. }
  110. RID NavigationRegion2D::get_region_rid() const {
  111. return get_rid();
  112. }
  113. #ifdef DEBUG_ENABLED
  114. Rect2 NavigationRegion2D::_edit_get_rect() const {
  115. return navigation_polygon.is_valid() ? navigation_polygon->_edit_get_rect() : Rect2();
  116. }
  117. bool NavigationRegion2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  118. return navigation_polygon.is_valid() ? navigation_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  119. }
  120. #endif // DEBUG_ENABLED
  121. void NavigationRegion2D::_notification(int p_what) {
  122. switch (p_what) {
  123. case NOTIFICATION_ENTER_TREE: {
  124. _region_enter_navigation_map();
  125. } break;
  126. case NOTIFICATION_TRANSFORM_CHANGED: {
  127. set_physics_process_internal(true);
  128. } break;
  129. case NOTIFICATION_VISIBILITY_CHANGED: {
  130. #ifdef DEBUG_ENABLED
  131. if (debug_instance_rid.is_valid()) {
  132. RS::get_singleton()->canvas_item_set_visible(debug_instance_rid, is_visible_in_tree());
  133. }
  134. #endif // DEBUG_ENABLED
  135. } break;
  136. case NOTIFICATION_EXIT_TREE: {
  137. _region_exit_navigation_map();
  138. #ifdef DEBUG_ENABLED
  139. _free_debug();
  140. #endif // DEBUG_ENABLED
  141. } break;
  142. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  143. set_physics_process_internal(false);
  144. _region_update_transform();
  145. } break;
  146. case NOTIFICATION_DRAW: {
  147. #ifdef DEBUG_ENABLED
  148. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled()) && navigation_polygon.is_valid()) {
  149. _update_debug_mesh();
  150. _update_debug_edge_connections_mesh();
  151. _update_debug_baking_rect();
  152. }
  153. #endif // DEBUG_ENABLED
  154. } break;
  155. }
  156. }
  157. void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navigation_polygon) {
  158. if (navigation_polygon.is_valid()) {
  159. navigation_polygon->disconnect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
  160. }
  161. navigation_polygon = p_navigation_polygon;
  162. #ifdef DEBUG_ENABLED
  163. debug_mesh_dirty = true;
  164. #endif // DEBUG_ENABLED
  165. NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, p_navigation_polygon);
  166. if (navigation_polygon.is_valid()) {
  167. navigation_polygon->connect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
  168. }
  169. _navigation_polygon_changed();
  170. update_configuration_warnings();
  171. }
  172. Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {
  173. return navigation_polygon;
  174. }
  175. void NavigationRegion2D::set_navigation_map(RID p_navigation_map) {
  176. if (map_override == p_navigation_map) {
  177. return;
  178. }
  179. map_override = p_navigation_map;
  180. NavigationServer2D::get_singleton()->region_set_map(region, map_override);
  181. }
  182. RID NavigationRegion2D::get_navigation_map() const {
  183. if (map_override.is_valid()) {
  184. return map_override;
  185. } else if (is_inside_tree()) {
  186. return get_world_2d()->get_navigation_map();
  187. }
  188. return RID();
  189. }
  190. void NavigationRegion2D::bake_navigation_polygon(bool p_on_thread) {
  191. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");
  192. ERR_FAIL_COND_MSG(!navigation_polygon.is_valid(), "Baking the navigation polygon requires a valid `NavigationPolygon` resource.");
  193. Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;
  194. source_geometry_data.instantiate();
  195. NavigationServer2D::get_singleton()->parse_source_geometry_data(navigation_polygon, source_geometry_data, this);
  196. if (p_on_thread) {
  197. NavigationServer2D::get_singleton()->bake_from_source_geometry_data_async(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished).bind(navigation_polygon));
  198. } else {
  199. NavigationServer2D::get_singleton()->bake_from_source_geometry_data(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished).bind(navigation_polygon));
  200. }
  201. }
  202. void NavigationRegion2D::_bake_finished(Ref<NavigationPolygon> p_navigation_polygon) {
  203. if (!Thread::is_main_thread()) {
  204. callable_mp(this, &NavigationRegion2D::_bake_finished).call_deferred(p_navigation_polygon);
  205. return;
  206. }
  207. set_navigation_polygon(p_navigation_polygon);
  208. emit_signal(SNAME("bake_finished"));
  209. }
  210. bool NavigationRegion2D::is_baking() const {
  211. return NavigationServer2D::get_singleton()->is_baking_navigation_polygon(navigation_polygon);
  212. }
  213. void NavigationRegion2D::_navigation_polygon_changed() {
  214. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
  215. queue_redraw();
  216. }
  217. if (navigation_polygon.is_valid()) {
  218. NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon);
  219. }
  220. }
  221. #ifdef DEBUG_ENABLED
  222. void NavigationRegion2D::_navigation_map_changed(RID p_map) {
  223. if (is_inside_tree() && get_world_2d()->get_navigation_map() == p_map) {
  224. queue_redraw();
  225. }
  226. }
  227. #endif // DEBUG_ENABLED
  228. #ifdef DEBUG_ENABLED
  229. void NavigationRegion2D::_navigation_debug_changed() {
  230. if (is_inside_tree()) {
  231. queue_redraw();
  232. }
  233. }
  234. #endif // DEBUG_ENABLED
  235. PackedStringArray NavigationRegion2D::get_configuration_warnings() const {
  236. PackedStringArray warnings = Node2D::get_configuration_warnings();
  237. if (is_visible_in_tree() && is_inside_tree()) {
  238. if (!navigation_polygon.is_valid()) {
  239. warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work. Please set a property or draw a polygon."));
  240. }
  241. }
  242. return warnings;
  243. }
  244. void NavigationRegion2D::_bind_methods() {
  245. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion2D::get_rid);
  246. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navigation_polygon"), &NavigationRegion2D::set_navigation_polygon);
  247. ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);
  248. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
  249. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);
  250. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion2D::set_navigation_map);
  251. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion2D::get_navigation_map);
  252. ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion2D::set_use_edge_connections);
  253. ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion2D::get_use_edge_connections);
  254. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
  255. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);
  256. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion2D::set_navigation_layer_value);
  257. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion2D::get_navigation_layer_value);
  258. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);
  259. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);
  260. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);
  261. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);
  262. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
  263. ClassDB::bind_method(D_METHOD("bake_navigation_polygon", "on_thread"), &NavigationRegion2D::bake_navigation_polygon, DEFVAL(true));
  264. ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion2D::is_baking);
  265. ClassDB::bind_method(D_METHOD("_navigation_polygon_changed"), &NavigationRegion2D::_navigation_polygon_changed);
  266. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_polygon", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
  267. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  268. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
  269. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  270. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  271. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  272. ADD_SIGNAL(MethodInfo("navigation_polygon_changed"));
  273. ADD_SIGNAL(MethodInfo("bake_finished"));
  274. }
  275. #ifndef DISABLE_DEPRECATED
  276. // Compatibility with earlier 4.0 betas.
  277. bool NavigationRegion2D::_set(const StringName &p_name, const Variant &p_value) {
  278. if (p_name == "navpoly") {
  279. set_navigation_polygon(p_value);
  280. return true;
  281. }
  282. return false;
  283. }
  284. bool NavigationRegion2D::_get(const StringName &p_name, Variant &r_ret) const {
  285. if (p_name == "navpoly") {
  286. r_ret = get_navigation_polygon();
  287. return true;
  288. }
  289. return false;
  290. }
  291. #endif // DISABLE_DEPRECATED
  292. NavigationRegion2D::NavigationRegion2D() {
  293. set_notify_transform(true);
  294. set_hide_clip_children(true);
  295. region = NavigationServer2D::get_singleton()->region_create();
  296. NavigationServer2D::get_singleton()->region_set_owner_id(region, get_instance_id());
  297. NavigationServer2D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  298. NavigationServer2D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  299. NavigationServer2D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  300. NavigationServer2D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  301. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  302. #ifdef DEBUG_ENABLED
  303. NavigationServer2D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
  304. NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_debug_changed));
  305. #endif // DEBUG_ENABLED
  306. }
  307. NavigationRegion2D::~NavigationRegion2D() {
  308. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  309. NavigationServer2D::get_singleton()->free(region);
  310. #ifdef DEBUG_ENABLED
  311. NavigationServer2D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion2D::_navigation_map_changed));
  312. NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion2D::_navigation_debug_changed));
  313. #endif // DEBUG_ENABLED
  314. }
  315. void NavigationRegion2D::_region_enter_navigation_map() {
  316. if (!is_inside_tree()) {
  317. return;
  318. }
  319. if (map_override.is_valid()) {
  320. NavigationServer2D::get_singleton()->region_set_map(region, map_override);
  321. } else {
  322. NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  323. }
  324. current_global_transform = get_global_transform();
  325. NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
  326. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  327. queue_redraw();
  328. }
  329. void NavigationRegion2D::_region_exit_navigation_map() {
  330. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  331. }
  332. void NavigationRegion2D::_region_update_transform() {
  333. if (!is_inside_tree()) {
  334. return;
  335. }
  336. Transform2D new_global_transform = get_global_transform();
  337. if (current_global_transform != new_global_transform) {
  338. current_global_transform = new_global_transform;
  339. NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
  340. }
  341. queue_redraw();
  342. }
  343. #ifdef DEBUG_ENABLED
  344. void NavigationRegion2D::_update_debug_mesh() {
  345. if (!is_inside_tree()) {
  346. _free_debug();
  347. return;
  348. }
  349. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  350. RenderingServer *rs = RenderingServer::get_singleton();
  351. if (!debug_instance_rid.is_valid()) {
  352. debug_instance_rid = rs->canvas_item_create();
  353. }
  354. if (!debug_mesh_rid.is_valid()) {
  355. debug_mesh_rid = rs->mesh_create();
  356. }
  357. const Transform2D region_gt = get_global_transform();
  358. rs->canvas_item_set_parent(debug_instance_rid, get_world_2d()->get_canvas());
  359. rs->canvas_item_set_z_index(debug_instance_rid, RS::CANVAS_ITEM_Z_MAX - 2);
  360. rs->canvas_item_set_transform(debug_instance_rid, region_gt);
  361. if (!debug_mesh_dirty) {
  362. return;
  363. }
  364. rs->canvas_item_clear(debug_instance_rid);
  365. rs->mesh_clear(debug_mesh_rid);
  366. debug_mesh_dirty = false;
  367. const Vector<Vector2> &vertices = navigation_polygon->get_vertices();
  368. if (vertices.size() < 3) {
  369. return;
  370. }
  371. int polygon_count = navigation_polygon->get_polygon_count();
  372. if (polygon_count == 0) {
  373. return;
  374. }
  375. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  376. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  377. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  378. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  379. if (!enabled) {
  380. debug_face_color = ns2d->get_debug_navigation_geometry_face_disabled_color();
  381. debug_edge_color = ns2d->get_debug_navigation_geometry_edge_disabled_color();
  382. }
  383. int vertex_count = 0;
  384. int line_count = 0;
  385. for (int i = 0; i < polygon_count; i++) {
  386. const Vector<int> &polygon = navigation_polygon->get_polygon(i);
  387. int polygon_size = polygon.size();
  388. if (polygon_size < 3) {
  389. continue;
  390. }
  391. line_count += polygon_size * 2;
  392. vertex_count += (polygon_size - 2) * 3;
  393. }
  394. Vector<Vector2> face_vertex_array;
  395. face_vertex_array.resize(vertex_count);
  396. Vector<Color> face_color_array;
  397. if (enabled_geometry_face_random_color) {
  398. face_color_array.resize(vertex_count);
  399. }
  400. Vector<Vector2> line_vertex_array;
  401. if (enabled_edge_lines) {
  402. line_vertex_array.resize(line_count);
  403. }
  404. RandomPCG rand;
  405. Color polygon_color = debug_face_color;
  406. int face_vertex_index = 0;
  407. int line_vertex_index = 0;
  408. Vector2 *face_vertex_array_ptrw = face_vertex_array.ptrw();
  409. Color *face_color_array_ptrw = face_color_array.ptrw();
  410. Vector2 *line_vertex_array_ptrw = line_vertex_array.ptrw();
  411. for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
  412. const Vector<int> &polygon_indices = navigation_polygon->get_polygon(polygon_index);
  413. int polygon_indices_size = polygon_indices.size();
  414. if (polygon_indices_size < 3) {
  415. continue;
  416. }
  417. if (enabled_geometry_face_random_color) {
  418. // Generate the polygon color, slightly randomly modified from the settings one.
  419. polygon_color.set_hsv(debug_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_face_color.get_s(), debug_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  420. polygon_color.a = debug_face_color.a;
  421. }
  422. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
  423. face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
  424. face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
  425. face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
  426. if (enabled_geometry_face_random_color) {
  427. face_color_array_ptrw[face_vertex_index] = polygon_color;
  428. face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
  429. face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
  430. }
  431. face_vertex_index += 3;
  432. }
  433. if (enabled_edge_lines) {
  434. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
  435. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
  436. line_vertex_index += 1;
  437. if (polygon_indices_index + 1 == polygon_indices_size) {
  438. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
  439. line_vertex_index += 1;
  440. } else {
  441. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
  442. line_vertex_index += 1;
  443. }
  444. }
  445. }
  446. }
  447. if (!enabled_geometry_face_random_color) {
  448. face_color_array.resize(face_vertex_array.size());
  449. face_color_array.fill(debug_face_color);
  450. }
  451. Array face_mesh_array;
  452. face_mesh_array.resize(Mesh::ARRAY_MAX);
  453. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  454. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  455. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_TRIANGLES, face_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  456. if (enabled_edge_lines) {
  457. Vector<Color> line_color_array;
  458. line_color_array.resize(line_vertex_array.size());
  459. line_color_array.fill(debug_edge_color);
  460. Array line_mesh_array;
  461. line_mesh_array.resize(Mesh::ARRAY_MAX);
  462. line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
  463. line_mesh_array[Mesh::ARRAY_COLOR] = line_color_array;
  464. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, line_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  465. }
  466. rs->canvas_item_add_mesh(debug_instance_rid, debug_mesh_rid, Transform2D());
  467. rs->canvas_item_set_visible(debug_instance_rid, is_visible_in_tree());
  468. }
  469. #endif // DEBUG_ENABLED
  470. #ifdef DEBUG_ENABLED
  471. void NavigationRegion2D::_update_debug_edge_connections_mesh() {
  472. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  473. bool enable_edge_connections = use_edge_connections && ns2d->get_debug_navigation_enable_edge_connections() && ns2d->map_get_use_edge_connections(get_world_2d()->get_navigation_map());
  474. if (enable_edge_connections) {
  475. Color debug_edge_connection_color = ns2d->get_debug_navigation_edge_connection_color();
  476. // Draw the region edge connections.
  477. Transform2D xform = get_global_transform();
  478. real_t radius = ns2d->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0;
  479. for (int i = 0; i < ns2d->region_get_connections_count(region); i++) {
  480. // Two main points
  481. Vector2 a = ns2d->region_get_connection_pathway_start(region, i);
  482. a = xform.affine_inverse().xform(a);
  483. Vector2 b = ns2d->region_get_connection_pathway_end(region, i);
  484. b = xform.affine_inverse().xform(b);
  485. draw_line(a, b, debug_edge_connection_color);
  486. // Draw a circle to illustrate the margins.
  487. real_t angle = a.angle_to_point(b);
  488. draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, debug_edge_connection_color);
  489. draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, debug_edge_connection_color);
  490. }
  491. }
  492. }
  493. #endif // DEBUG_ENABLED
  494. #ifdef DEBUG_ENABLED
  495. void NavigationRegion2D::_update_debug_baking_rect() {
  496. Rect2 baking_rect = get_navigation_polygon()->get_baking_rect();
  497. if (baking_rect.has_area()) {
  498. Vector2 baking_rect_offset = get_navigation_polygon()->get_baking_rect_offset();
  499. Rect2 debug_baking_rect = Rect2(baking_rect.position.x + baking_rect_offset.x, baking_rect.position.y + baking_rect_offset.y, baking_rect.size.x, baking_rect.size.y);
  500. Color debug_baking_rect_color = Color(0.8, 0.5, 0.7, 0.1);
  501. draw_rect(debug_baking_rect, debug_baking_rect_color);
  502. }
  503. }
  504. #endif // DEBUG_ENABLED
  505. #ifdef DEBUG_ENABLED
  506. void NavigationRegion2D::_free_debug() {
  507. RenderingServer *rs = RenderingServer::get_singleton();
  508. ERR_FAIL_NULL(rs);
  509. if (debug_instance_rid.is_valid()) {
  510. rs->canvas_item_clear(debug_instance_rid);
  511. rs->free(debug_instance_rid);
  512. debug_instance_rid = RID();
  513. }
  514. if (debug_mesh_rid.is_valid()) {
  515. rs->free(debug_mesh_rid);
  516. debug_mesh_rid = RID();
  517. }
  518. }
  519. #endif // DEBUG_ENABLED