navigation_region_2d.cpp 25 KB

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