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