navigation_region_2d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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/random_pcg.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. _set_debug_visible(is_visible_in_tree());
  132. #endif // DEBUG_ENABLED
  133. } break;
  134. case NOTIFICATION_EXIT_TREE: {
  135. _region_exit_navigation_map();
  136. #ifdef DEBUG_ENABLED
  137. _set_debug_visible(false);
  138. #endif // DEBUG_ENABLED
  139. } break;
  140. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  141. set_physics_process_internal(false);
  142. _region_update_transform();
  143. } break;
  144. case NOTIFICATION_DRAW: {
  145. #ifdef DEBUG_ENABLED
  146. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || (NavigationServer2D::get_singleton()->get_debug_enabled() && NavigationServer2D::get_singleton()->get_debug_navigation_enabled())) && navigation_polygon.is_valid()) {
  147. _update_debug_mesh();
  148. _update_debug_edge_connections_mesh();
  149. _update_debug_baking_rect();
  150. }
  151. #endif // DEBUG_ENABLED
  152. } break;
  153. }
  154. }
  155. void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navigation_polygon) {
  156. if (navigation_polygon.is_valid()) {
  157. navigation_polygon->disconnect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
  158. }
  159. navigation_polygon = p_navigation_polygon;
  160. if (navigation_polygon.is_valid()) {
  161. navigation_polygon->connect_changed(callable_mp(this, &NavigationRegion2D::_navigation_polygon_changed));
  162. }
  163. _navigation_polygon_changed();
  164. }
  165. Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {
  166. return navigation_polygon;
  167. }
  168. void NavigationRegion2D::set_navigation_map(RID p_navigation_map) {
  169. if (map_override == p_navigation_map) {
  170. return;
  171. }
  172. map_override = p_navigation_map;
  173. NavigationServer2D::get_singleton()->region_set_map(region, map_override);
  174. }
  175. RID NavigationRegion2D::get_navigation_map() const {
  176. if (map_override.is_valid()) {
  177. return map_override;
  178. } else if (is_inside_tree()) {
  179. return get_world_2d()->get_navigation_map();
  180. }
  181. return RID();
  182. }
  183. void NavigationRegion2D::bake_navigation_polygon(bool p_on_thread) {
  184. 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().");
  185. ERR_FAIL_COND_MSG(navigation_polygon.is_null(), "Baking the navigation polygon requires a valid `NavigationPolygon` resource.");
  186. Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;
  187. source_geometry_data.instantiate();
  188. NavigationServer2D::get_singleton()->parse_source_geometry_data(navigation_polygon, source_geometry_data, this);
  189. if (p_on_thread) {
  190. NavigationServer2D::get_singleton()->bake_from_source_geometry_data_async(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished));
  191. } else {
  192. NavigationServer2D::get_singleton()->bake_from_source_geometry_data(navigation_polygon, source_geometry_data, callable_mp(this, &NavigationRegion2D::_bake_finished));
  193. }
  194. }
  195. void NavigationRegion2D::_bake_finished() {
  196. if (!Thread::is_main_thread()) {
  197. callable_mp(this, &NavigationRegion2D::_bake_finished).call_deferred();
  198. return;
  199. }
  200. emit_signal(SNAME("bake_finished"));
  201. }
  202. bool NavigationRegion2D::is_baking() const {
  203. return NavigationServer2D::get_singleton()->is_baking_navigation_polygon(navigation_polygon);
  204. }
  205. void NavigationRegion2D::_navigation_polygon_changed() {
  206. _update_bounds();
  207. NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon);
  208. #ifdef DEBUG_ENABLED
  209. debug_mesh_dirty = true;
  210. if (navigation_polygon.is_null()) {
  211. _set_debug_visible(false);
  212. }
  213. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
  214. queue_redraw();
  215. }
  216. #endif // DEBUG_ENABLED
  217. emit_signal(SNAME("navigation_polygon_changed"));
  218. update_configuration_warnings();
  219. }
  220. #ifdef DEBUG_ENABLED
  221. void NavigationRegion2D::_navigation_map_changed(RID p_map) {
  222. if (is_inside_tree() && get_world_2d()->get_navigation_map() == p_map) {
  223. queue_redraw();
  224. }
  225. }
  226. #endif // DEBUG_ENABLED
  227. #ifdef DEBUG_ENABLED
  228. void NavigationRegion2D::_navigation_debug_changed() {
  229. if (is_inside_tree()) {
  230. queue_redraw();
  231. }
  232. }
  233. #endif // DEBUG_ENABLED
  234. PackedStringArray NavigationRegion2D::get_configuration_warnings() const {
  235. PackedStringArray warnings = Node2D::get_configuration_warnings();
  236. if (is_visible_in_tree() && is_inside_tree()) {
  237. if (navigation_polygon.is_null()) {
  238. 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."));
  239. }
  240. }
  241. return warnings;
  242. }
  243. void NavigationRegion2D::_bind_methods() {
  244. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion2D::get_rid);
  245. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navigation_polygon"), &NavigationRegion2D::set_navigation_polygon);
  246. ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);
  247. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
  248. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);
  249. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion2D::set_navigation_map);
  250. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion2D::get_navigation_map);
  251. ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion2D::set_use_edge_connections);
  252. ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion2D::get_use_edge_connections);
  253. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
  254. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);
  255. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion2D::set_navigation_layer_value);
  256. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion2D::get_navigation_layer_value);
  257. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);
  258. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);
  259. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);
  260. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);
  261. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
  262. ClassDB::bind_method(D_METHOD("bake_navigation_polygon", "on_thread"), &NavigationRegion2D::bake_navigation_polygon, DEFVAL(true));
  263. ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion2D::is_baking);
  264. ClassDB::bind_method(D_METHOD("_navigation_polygon_changed"), &NavigationRegion2D::_navigation_polygon_changed);
  265. ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationRegion2D::get_bounds);
  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. if (debug_instance_rid.is_valid()) {
  314. RS::get_singleton()->free(debug_instance_rid);
  315. }
  316. if (debug_mesh_rid.is_valid()) {
  317. RS::get_singleton()->free(debug_mesh_rid);
  318. }
  319. #endif // DEBUG_ENABLED
  320. }
  321. void NavigationRegion2D::_region_enter_navigation_map() {
  322. if (!is_inside_tree()) {
  323. return;
  324. }
  325. if (map_override.is_valid()) {
  326. NavigationServer2D::get_singleton()->region_set_map(region, map_override);
  327. } else {
  328. NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  329. }
  330. current_global_transform = get_global_transform();
  331. NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
  332. NavigationServer2D::get_singleton()->region_set_enabled(region, enabled);
  333. queue_redraw();
  334. }
  335. void NavigationRegion2D::_region_exit_navigation_map() {
  336. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  337. }
  338. void NavigationRegion2D::_region_update_transform() {
  339. if (!is_inside_tree()) {
  340. return;
  341. }
  342. Transform2D new_global_transform = get_global_transform();
  343. if (current_global_transform != new_global_transform) {
  344. current_global_transform = new_global_transform;
  345. NavigationServer2D::get_singleton()->region_set_transform(region, current_global_transform);
  346. }
  347. queue_redraw();
  348. }
  349. #ifdef DEBUG_ENABLED
  350. void NavigationRegion2D::_update_debug_mesh() {
  351. if (!is_inside_tree()) {
  352. _set_debug_visible(false);
  353. return;
  354. }
  355. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  356. RenderingServer *rs = RenderingServer::get_singleton();
  357. if (!debug_instance_rid.is_valid()) {
  358. debug_instance_rid = rs->canvas_item_create();
  359. }
  360. if (!debug_mesh_rid.is_valid()) {
  361. debug_mesh_rid = rs->mesh_create();
  362. }
  363. const Transform2D region_gt = get_global_transform();
  364. rs->canvas_item_set_parent(debug_instance_rid, get_world_2d()->get_canvas());
  365. rs->canvas_item_set_z_index(debug_instance_rid, RS::CANVAS_ITEM_Z_MAX - 2);
  366. rs->canvas_item_set_transform(debug_instance_rid, region_gt);
  367. if (!debug_mesh_dirty) {
  368. return;
  369. }
  370. rs->canvas_item_clear(debug_instance_rid);
  371. rs->mesh_clear(debug_mesh_rid);
  372. debug_mesh_dirty = false;
  373. const Vector<Vector2> &vertices = navigation_polygon->get_vertices();
  374. if (vertices.size() < 3) {
  375. return;
  376. }
  377. int polygon_count = navigation_polygon->get_polygon_count();
  378. if (polygon_count == 0) {
  379. return;
  380. }
  381. bool enabled_geometry_face_random_color = ns2d->get_debug_navigation_enable_geometry_face_random_color();
  382. bool enabled_edge_lines = ns2d->get_debug_navigation_enable_edge_lines();
  383. Color debug_face_color = ns2d->get_debug_navigation_geometry_face_color();
  384. Color debug_edge_color = ns2d->get_debug_navigation_geometry_edge_color();
  385. if (!enabled) {
  386. debug_face_color = ns2d->get_debug_navigation_geometry_face_disabled_color();
  387. debug_edge_color = ns2d->get_debug_navigation_geometry_edge_disabled_color();
  388. }
  389. int vertex_count = 0;
  390. int line_count = 0;
  391. for (int i = 0; i < polygon_count; i++) {
  392. const Vector<int> &polygon = navigation_polygon->get_polygon(i);
  393. int polygon_size = polygon.size();
  394. if (polygon_size < 3) {
  395. continue;
  396. }
  397. line_count += polygon_size * 2;
  398. vertex_count += (polygon_size - 2) * 3;
  399. }
  400. Vector<Vector2> face_vertex_array;
  401. face_vertex_array.resize(vertex_count);
  402. Vector<Color> face_color_array;
  403. if (enabled_geometry_face_random_color) {
  404. face_color_array.resize(vertex_count);
  405. }
  406. Vector<Vector2> line_vertex_array;
  407. if (enabled_edge_lines) {
  408. line_vertex_array.resize(line_count);
  409. }
  410. RandomPCG rand;
  411. Color polygon_color = debug_face_color;
  412. int face_vertex_index = 0;
  413. int line_vertex_index = 0;
  414. Vector2 *face_vertex_array_ptrw = face_vertex_array.ptrw();
  415. Color *face_color_array_ptrw = face_color_array.ptrw();
  416. Vector2 *line_vertex_array_ptrw = line_vertex_array.ptrw();
  417. for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
  418. const Vector<int> &polygon_indices = navigation_polygon->get_polygon(polygon_index);
  419. int polygon_indices_size = polygon_indices.size();
  420. if (polygon_indices_size < 3) {
  421. continue;
  422. }
  423. if (enabled_geometry_face_random_color) {
  424. // Generate the polygon color, slightly randomly modified from the settings one.
  425. 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);
  426. polygon_color.a = debug_face_color.a;
  427. }
  428. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
  429. face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
  430. face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
  431. face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
  432. if (enabled_geometry_face_random_color) {
  433. face_color_array_ptrw[face_vertex_index] = polygon_color;
  434. face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
  435. face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
  436. }
  437. face_vertex_index += 3;
  438. }
  439. if (enabled_edge_lines) {
  440. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
  441. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
  442. line_vertex_index += 1;
  443. if (polygon_indices_index + 1 == polygon_indices_size) {
  444. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
  445. line_vertex_index += 1;
  446. } else {
  447. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
  448. line_vertex_index += 1;
  449. }
  450. }
  451. }
  452. }
  453. if (!enabled_geometry_face_random_color) {
  454. face_color_array.resize(face_vertex_array.size());
  455. face_color_array.fill(debug_face_color);
  456. }
  457. Array face_mesh_array;
  458. face_mesh_array.resize(Mesh::ARRAY_MAX);
  459. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  460. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  461. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_TRIANGLES, face_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  462. if (enabled_edge_lines) {
  463. Vector<Color> line_color_array;
  464. line_color_array.resize(line_vertex_array.size());
  465. line_color_array.fill(debug_edge_color);
  466. Array line_mesh_array;
  467. line_mesh_array.resize(Mesh::ARRAY_MAX);
  468. line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
  469. line_mesh_array[Mesh::ARRAY_COLOR] = line_color_array;
  470. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, line_mesh_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  471. }
  472. rs->canvas_item_add_mesh(debug_instance_rid, debug_mesh_rid, Transform2D());
  473. rs->canvas_item_set_visible(debug_instance_rid, is_visible_in_tree());
  474. }
  475. #endif // DEBUG_ENABLED
  476. #ifdef DEBUG_ENABLED
  477. void NavigationRegion2D::_update_debug_edge_connections_mesh() {
  478. const NavigationServer2D *ns2d = NavigationServer2D::get_singleton();
  479. 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());
  480. if (enable_edge_connections) {
  481. Color debug_edge_connection_color = ns2d->get_debug_navigation_edge_connection_color();
  482. // Draw the region edge connections.
  483. Transform2D xform = get_global_transform();
  484. real_t radius = ns2d->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0;
  485. for (int i = 0; i < ns2d->region_get_connections_count(region); i++) {
  486. // Two main points
  487. Vector2 a = ns2d->region_get_connection_pathway_start(region, i);
  488. a = xform.affine_inverse().xform(a);
  489. Vector2 b = ns2d->region_get_connection_pathway_end(region, i);
  490. b = xform.affine_inverse().xform(b);
  491. draw_line(a, b, debug_edge_connection_color);
  492. // Draw a circle to illustrate the margins.
  493. real_t angle = a.angle_to_point(b);
  494. draw_arc(a, radius, angle + Math::PI / 2.0, angle - Math::PI / 2.0 + Math::TAU, 10, debug_edge_connection_color);
  495. draw_arc(b, radius, angle - Math::PI / 2.0, angle + Math::PI / 2.0, 10, debug_edge_connection_color);
  496. }
  497. }
  498. }
  499. #endif // DEBUG_ENABLED
  500. #ifdef DEBUG_ENABLED
  501. void NavigationRegion2D::_update_debug_baking_rect() {
  502. Rect2 baking_rect = get_navigation_polygon()->get_baking_rect();
  503. if (baking_rect.has_area()) {
  504. Vector2 baking_rect_offset = get_navigation_polygon()->get_baking_rect_offset();
  505. 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);
  506. Color debug_baking_rect_color = Color(0.8, 0.5, 0.7, 0.1);
  507. draw_rect(debug_baking_rect, debug_baking_rect_color);
  508. }
  509. }
  510. #endif // DEBUG_ENABLED
  511. #ifdef DEBUG_ENABLED
  512. void NavigationRegion2D::_set_debug_visible(bool p_visible) {
  513. RenderingServer *rs = RenderingServer::get_singleton();
  514. ERR_FAIL_NULL(rs);
  515. if (debug_instance_rid.is_valid()) {
  516. RS::get_singleton()->canvas_item_set_visible(debug_instance_rid, p_visible);
  517. }
  518. }
  519. #endif // DEBUG_ENABLED
  520. void NavigationRegion2D::_update_bounds() {
  521. if (navigation_polygon.is_null()) {
  522. bounds = Rect2();
  523. return;
  524. }
  525. const Vector<Vector2> &vertices = navigation_polygon->get_vertices();
  526. if (vertices.is_empty()) {
  527. bounds = Rect2();
  528. return;
  529. }
  530. const Transform2D gt = is_inside_tree() ? get_global_transform() : get_transform();
  531. Rect2 new_bounds;
  532. new_bounds.position = gt.xform(vertices[0]);
  533. for (const Vector2 &vertex : vertices) {
  534. new_bounds.expand_to(gt.xform(vertex));
  535. }
  536. bounds = new_bounds;
  537. }