navigation_region_2d.cpp 24 KB

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