navigation_region_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /**************************************************************************/
  2. /* navigation_region_3d.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_3d.h"
  31. #include "scene/resources/navigation_mesh_source_geometry_data_3d.h"
  32. #include "servers/navigation_server_3d.h"
  33. RID NavigationRegion3D::get_rid() const {
  34. return region;
  35. }
  36. void NavigationRegion3D::set_enabled(bool p_enabled) {
  37. if (enabled == p_enabled) {
  38. return;
  39. }
  40. enabled = p_enabled;
  41. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  42. #ifdef DEBUG_ENABLED
  43. if (debug_instance.is_valid()) {
  44. if (!is_enabled()) {
  45. if (debug_mesh.is_valid()) {
  46. if (debug_mesh->get_surface_count() > 0) {
  47. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
  48. }
  49. if (debug_mesh->get_surface_count() > 1) {
  50. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
  51. }
  52. }
  53. } else {
  54. if (debug_mesh.is_valid()) {
  55. if (debug_mesh->get_surface_count() > 0) {
  56. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID());
  57. }
  58. if (debug_mesh->get_surface_count() > 1) {
  59. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID());
  60. }
  61. }
  62. }
  63. }
  64. #endif // DEBUG_ENABLED
  65. update_gizmos();
  66. }
  67. bool NavigationRegion3D::is_enabled() const {
  68. return enabled;
  69. }
  70. void NavigationRegion3D::set_use_edge_connections(bool p_enabled) {
  71. if (use_edge_connections == p_enabled) {
  72. return;
  73. }
  74. use_edge_connections = p_enabled;
  75. NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  76. }
  77. bool NavigationRegion3D::get_use_edge_connections() const {
  78. return use_edge_connections;
  79. }
  80. void NavigationRegion3D::set_navigation_layers(uint32_t p_navigation_layers) {
  81. if (navigation_layers == p_navigation_layers) {
  82. return;
  83. }
  84. navigation_layers = p_navigation_layers;
  85. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  86. }
  87. uint32_t NavigationRegion3D::get_navigation_layers() const {
  88. return navigation_layers;
  89. }
  90. void NavigationRegion3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  91. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  92. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  93. uint32_t _navigation_layers = get_navigation_layers();
  94. if (p_value) {
  95. _navigation_layers |= 1 << (p_layer_number - 1);
  96. } else {
  97. _navigation_layers &= ~(1 << (p_layer_number - 1));
  98. }
  99. set_navigation_layers(_navigation_layers);
  100. }
  101. bool NavigationRegion3D::get_navigation_layer_value(int p_layer_number) const {
  102. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  103. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  104. return get_navigation_layers() & (1 << (p_layer_number - 1));
  105. }
  106. void NavigationRegion3D::set_enter_cost(real_t p_enter_cost) {
  107. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  108. if (Math::is_equal_approx(enter_cost, p_enter_cost)) {
  109. return;
  110. }
  111. enter_cost = p_enter_cost;
  112. NavigationServer3D::get_singleton()->region_set_enter_cost(region, enter_cost);
  113. }
  114. real_t NavigationRegion3D::get_enter_cost() const {
  115. return enter_cost;
  116. }
  117. void NavigationRegion3D::set_travel_cost(real_t p_travel_cost) {
  118. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  119. if (Math::is_equal_approx(travel_cost, p_travel_cost)) {
  120. return;
  121. }
  122. travel_cost = p_travel_cost;
  123. NavigationServer3D::get_singleton()->region_set_travel_cost(region, travel_cost);
  124. }
  125. real_t NavigationRegion3D::get_travel_cost() const {
  126. return travel_cost;
  127. }
  128. RID NavigationRegion3D::get_region_rid() const {
  129. return get_rid();
  130. }
  131. void NavigationRegion3D::_notification(int p_what) {
  132. switch (p_what) {
  133. case NOTIFICATION_ENTER_TREE: {
  134. _region_enter_navigation_map();
  135. } break;
  136. case NOTIFICATION_TRANSFORM_CHANGED: {
  137. set_physics_process_internal(true);
  138. } break;
  139. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  140. set_physics_process_internal(false);
  141. _region_update_transform();
  142. } break;
  143. case NOTIFICATION_EXIT_TREE: {
  144. _region_exit_navigation_map();
  145. } break;
  146. }
  147. }
  148. void NavigationRegion3D::set_navigation_mesh(const Ref<NavigationMesh> &p_navigation_mesh) {
  149. if (navigation_mesh.is_valid()) {
  150. navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
  151. }
  152. navigation_mesh = p_navigation_mesh;
  153. if (navigation_mesh.is_valid()) {
  154. navigation_mesh->connect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
  155. }
  156. NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, p_navigation_mesh);
  157. #ifdef DEBUG_ENABLED
  158. if (is_inside_tree() && NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  159. if (navigation_mesh.is_valid()) {
  160. _update_debug_mesh();
  161. _update_debug_edge_connections_mesh();
  162. } else {
  163. if (debug_instance.is_valid()) {
  164. RS::get_singleton()->instance_set_visible(debug_instance, false);
  165. }
  166. if (debug_edge_connections_instance.is_valid()) {
  167. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  168. }
  169. }
  170. }
  171. #endif // DEBUG_ENABLED
  172. emit_signal(SNAME("navigation_mesh_changed"));
  173. update_gizmos();
  174. update_configuration_warnings();
  175. }
  176. Ref<NavigationMesh> NavigationRegion3D::get_navigation_mesh() const {
  177. return navigation_mesh;
  178. }
  179. void NavigationRegion3D::set_navigation_map(RID p_navigation_map) {
  180. if (map_override == p_navigation_map) {
  181. return;
  182. }
  183. map_override = p_navigation_map;
  184. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  185. }
  186. RID NavigationRegion3D::get_navigation_map() const {
  187. if (map_override.is_valid()) {
  188. return map_override;
  189. } else if (is_inside_tree()) {
  190. return get_world_3d()->get_navigation_map();
  191. }
  192. return RID();
  193. }
  194. void NavigationRegion3D::bake_navigation_mesh(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_mesh.is_valid(), "Baking the navigation mesh requires a valid `NavigationMesh` resource.");
  197. Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
  198. source_geometry_data.instantiate();
  199. NavigationServer3D::get_singleton()->parse_source_geometry_data(navigation_mesh, source_geometry_data, this);
  200. if (p_on_thread) {
  201. NavigationServer3D::get_singleton()->bake_from_source_geometry_data_async(navigation_mesh, source_geometry_data, callable_mp(this, &NavigationRegion3D::_bake_finished).bind(navigation_mesh));
  202. } else {
  203. NavigationServer3D::get_singleton()->bake_from_source_geometry_data(navigation_mesh, source_geometry_data, callable_mp(this, &NavigationRegion3D::_bake_finished).bind(navigation_mesh));
  204. }
  205. }
  206. void NavigationRegion3D::_bake_finished(Ref<NavigationMesh> p_navigation_mesh) {
  207. if (!Thread::is_main_thread()) {
  208. call_deferred(SNAME("_bake_finished"), p_navigation_mesh);
  209. return;
  210. }
  211. set_navigation_mesh(p_navigation_mesh);
  212. emit_signal(SNAME("bake_finished"));
  213. }
  214. PackedStringArray NavigationRegion3D::get_configuration_warnings() const {
  215. PackedStringArray warnings = Node::get_configuration_warnings();
  216. if (is_visible_in_tree() && is_inside_tree()) {
  217. if (!navigation_mesh.is_valid()) {
  218. warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work."));
  219. }
  220. }
  221. return warnings;
  222. }
  223. void NavigationRegion3D::_bind_methods() {
  224. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion3D::get_rid);
  225. ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navigation_mesh"), &NavigationRegion3D::set_navigation_mesh);
  226. ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationRegion3D::get_navigation_mesh);
  227. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion3D::set_enabled);
  228. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion3D::is_enabled);
  229. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion3D::set_navigation_map);
  230. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion3D::get_navigation_map);
  231. ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion3D::set_use_edge_connections);
  232. ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion3D::get_use_edge_connections);
  233. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion3D::set_navigation_layers);
  234. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion3D::get_navigation_layers);
  235. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion3D::set_navigation_layer_value);
  236. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion3D::get_navigation_layer_value);
  237. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion3D::get_region_rid);
  238. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion3D::set_enter_cost);
  239. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion3D::get_enter_cost);
  240. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion3D::set_travel_cost);
  241. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion3D::get_travel_cost);
  242. ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationRegion3D::bake_navigation_mesh, DEFVAL(true));
  243. ClassDB::bind_method(D_METHOD("_bake_finished", "navigation_mesh"), &NavigationRegion3D::_bake_finished);
  244. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
  245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  246. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
  247. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  248. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  249. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  250. ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
  251. ADD_SIGNAL(MethodInfo("bake_finished"));
  252. }
  253. #ifndef DISABLE_DEPRECATED
  254. // Compatibility with earlier 4.0 betas.
  255. bool NavigationRegion3D::_set(const StringName &p_name, const Variant &p_value) {
  256. if (p_name == "navmesh") {
  257. set_navigation_mesh(p_value);
  258. return true;
  259. }
  260. return false;
  261. }
  262. bool NavigationRegion3D::_get(const StringName &p_name, Variant &r_ret) const {
  263. if (p_name == "navmesh") {
  264. r_ret = get_navigation_mesh();
  265. return true;
  266. }
  267. return false;
  268. }
  269. #endif // DISABLE_DEPRECATED
  270. void NavigationRegion3D::_navigation_mesh_changed() {
  271. update_gizmos();
  272. update_configuration_warnings();
  273. #ifdef DEBUG_ENABLED
  274. _update_debug_edge_connections_mesh();
  275. #endif // DEBUG_ENABLED
  276. }
  277. #ifdef DEBUG_ENABLED
  278. void NavigationRegion3D::_navigation_map_changed(RID p_map) {
  279. if (is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) {
  280. _update_debug_edge_connections_mesh();
  281. }
  282. }
  283. #endif // DEBUG_ENABLED
  284. void NavigationRegion3D::_region_enter_navigation_map() {
  285. if (!is_inside_tree()) {
  286. return;
  287. }
  288. if (map_override.is_valid()) {
  289. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  290. } else {
  291. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  292. }
  293. current_global_transform = get_global_transform();
  294. NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
  295. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  296. #ifdef DEBUG_ENABLED
  297. if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  298. _update_debug_mesh();
  299. }
  300. #endif // DEBUG_ENABLED
  301. }
  302. void NavigationRegion3D::_region_exit_navigation_map() {
  303. NavigationServer3D::get_singleton()->region_set_map(region, RID());
  304. #ifdef DEBUG_ENABLED
  305. if (debug_instance.is_valid()) {
  306. RS::get_singleton()->instance_set_visible(debug_instance, false);
  307. }
  308. if (debug_edge_connections_instance.is_valid()) {
  309. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  310. }
  311. #endif // DEBUG_ENABLED
  312. }
  313. void NavigationRegion3D::_region_update_transform() {
  314. if (!is_inside_tree()) {
  315. return;
  316. }
  317. Transform3D new_global_transform = get_global_transform();
  318. if (current_global_transform != new_global_transform) {
  319. current_global_transform = new_global_transform;
  320. NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
  321. #ifdef DEBUG_ENABLED
  322. if (debug_instance.is_valid()) {
  323. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  324. }
  325. #endif // DEBUG_ENABLED
  326. }
  327. }
  328. NavigationRegion3D::NavigationRegion3D() {
  329. set_notify_transform(true);
  330. region = NavigationServer3D::get_singleton()->region_create();
  331. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  332. NavigationServer3D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  333. NavigationServer3D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  334. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  335. NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  336. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  337. #ifdef DEBUG_ENABLED
  338. NavigationServer3D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
  339. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_mesh));
  340. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_edge_connections_mesh));
  341. #endif // DEBUG_ENABLED
  342. }
  343. NavigationRegion3D::~NavigationRegion3D() {
  344. if (navigation_mesh.is_valid()) {
  345. navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
  346. }
  347. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  348. NavigationServer3D::get_singleton()->free(region);
  349. #ifdef DEBUG_ENABLED
  350. NavigationServer3D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
  351. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_mesh));
  352. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_edge_connections_mesh));
  353. ERR_FAIL_NULL(RenderingServer::get_singleton());
  354. if (debug_instance.is_valid()) {
  355. RenderingServer::get_singleton()->free(debug_instance);
  356. }
  357. if (debug_mesh.is_valid()) {
  358. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  359. }
  360. if (debug_edge_connections_instance.is_valid()) {
  361. RenderingServer::get_singleton()->free(debug_edge_connections_instance);
  362. }
  363. if (debug_edge_connections_mesh.is_valid()) {
  364. RenderingServer::get_singleton()->free(debug_edge_connections_mesh->get_rid());
  365. }
  366. #endif // DEBUG_ENABLED
  367. }
  368. #ifdef DEBUG_ENABLED
  369. void NavigationRegion3D::_update_debug_mesh() {
  370. if (Engine::get_singleton()->is_editor_hint()) {
  371. // don't update inside Editor as node 3d gizmo takes care of this
  372. // as collisions and selections for Editor Viewport need to be updated
  373. return;
  374. }
  375. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  376. if (debug_instance.is_valid()) {
  377. RS::get_singleton()->instance_set_visible(debug_instance, false);
  378. }
  379. return;
  380. }
  381. if (!navigation_mesh.is_valid()) {
  382. if (debug_instance.is_valid()) {
  383. RS::get_singleton()->instance_set_visible(debug_instance, false);
  384. }
  385. return;
  386. }
  387. if (!debug_instance.is_valid()) {
  388. debug_instance = RenderingServer::get_singleton()->instance_create();
  389. }
  390. if (!debug_mesh.is_valid()) {
  391. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  392. }
  393. debug_mesh->clear_surfaces();
  394. Vector<Vector3> vertices = navigation_mesh->get_vertices();
  395. if (vertices.size() == 0) {
  396. return;
  397. }
  398. int polygon_count = navigation_mesh->get_polygon_count();
  399. if (polygon_count == 0) {
  400. return;
  401. }
  402. bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
  403. bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
  404. int vertex_count = 0;
  405. int line_count = 0;
  406. for (int i = 0; i < polygon_count; i++) {
  407. const Vector<int> &polygon = navigation_mesh->get_polygon(i);
  408. int polygon_size = polygon.size();
  409. if (polygon_size < 3) {
  410. continue;
  411. }
  412. line_count += polygon_size * 2;
  413. vertex_count += (polygon_size - 2) * 3;
  414. }
  415. Vector<Vector3> face_vertex_array;
  416. face_vertex_array.resize(vertex_count);
  417. Vector<Color> face_color_array;
  418. if (enabled_geometry_face_random_color) {
  419. face_color_array.resize(vertex_count);
  420. }
  421. Vector<Vector3> line_vertex_array;
  422. if (enabled_edge_lines) {
  423. line_vertex_array.resize(line_count);
  424. }
  425. Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  426. RandomPCG rand;
  427. Color polygon_color = debug_navigation_geometry_face_color;
  428. int face_vertex_index = 0;
  429. int line_vertex_index = 0;
  430. Vector3 *face_vertex_array_ptrw = face_vertex_array.ptrw();
  431. Color *face_color_array_ptrw = face_color_array.ptrw();
  432. Vector3 *line_vertex_array_ptrw = line_vertex_array.ptrw();
  433. for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
  434. const Vector<int> &polygon_indices = navigation_mesh->get_polygon(polygon_index);
  435. int polygon_indices_size = polygon_indices.size();
  436. if (polygon_indices_size < 3) {
  437. continue;
  438. }
  439. if (enabled_geometry_face_random_color) {
  440. // Generate the polygon color, slightly randomly modified from the settings one.
  441. polygon_color.set_hsv(debug_navigation_geometry_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_navigation_geometry_face_color.get_s(), debug_navigation_geometry_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  442. polygon_color.a = debug_navigation_geometry_face_color.a;
  443. }
  444. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
  445. face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
  446. face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
  447. face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
  448. if (enabled_geometry_face_random_color) {
  449. face_color_array_ptrw[face_vertex_index] = polygon_color;
  450. face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
  451. face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
  452. }
  453. face_vertex_index += 3;
  454. }
  455. if (enabled_edge_lines) {
  456. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
  457. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
  458. line_vertex_index += 1;
  459. if (polygon_indices_index + 1 == polygon_indices_size) {
  460. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
  461. line_vertex_index += 1;
  462. } else {
  463. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
  464. line_vertex_index += 1;
  465. }
  466. }
  467. }
  468. }
  469. Ref<StandardMaterial3D> face_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_material();
  470. Array face_mesh_array;
  471. face_mesh_array.resize(Mesh::ARRAY_MAX);
  472. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  473. if (enabled_geometry_face_random_color) {
  474. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  475. }
  476. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
  477. debug_mesh->surface_set_material(0, face_material);
  478. if (enabled_edge_lines) {
  479. Ref<StandardMaterial3D> line_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_material();
  480. Array line_mesh_array;
  481. line_mesh_array.resize(Mesh::ARRAY_MAX);
  482. line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
  483. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, line_mesh_array);
  484. debug_mesh->surface_set_material(1, line_material);
  485. }
  486. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  487. if (is_inside_tree()) {
  488. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  489. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  490. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  491. }
  492. if (!is_enabled()) {
  493. if (debug_mesh.is_valid()) {
  494. if (debug_mesh->get_surface_count() > 0) {
  495. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
  496. }
  497. if (debug_mesh->get_surface_count() > 1) {
  498. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
  499. }
  500. }
  501. } else {
  502. if (debug_mesh.is_valid()) {
  503. if (debug_mesh->get_surface_count() > 0) {
  504. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID());
  505. }
  506. if (debug_mesh->get_surface_count() > 1) {
  507. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID());
  508. }
  509. }
  510. }
  511. }
  512. #endif // DEBUG_ENABLED
  513. #ifdef DEBUG_ENABLED
  514. void NavigationRegion3D::_update_debug_edge_connections_mesh() {
  515. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  516. if (debug_edge_connections_instance.is_valid()) {
  517. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  518. }
  519. return;
  520. }
  521. if (!is_inside_tree()) {
  522. return;
  523. }
  524. if (!use_edge_connections || !NavigationServer3D::get_singleton()->map_get_use_edge_connections(get_world_3d()->get_navigation_map())) {
  525. if (debug_edge_connections_instance.is_valid()) {
  526. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  527. }
  528. return;
  529. }
  530. if (!navigation_mesh.is_valid()) {
  531. if (debug_edge_connections_instance.is_valid()) {
  532. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  533. }
  534. return;
  535. }
  536. if (!debug_edge_connections_instance.is_valid()) {
  537. debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  538. }
  539. if (!debug_edge_connections_mesh.is_valid()) {
  540. debug_edge_connections_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  541. }
  542. debug_edge_connections_mesh->clear_surfaces();
  543. float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
  544. float half_edge_connection_margin = edge_connection_margin * 0.5;
  545. int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(region);
  546. if (connections_count == 0) {
  547. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  548. return;
  549. }
  550. Vector<Vector3> vertex_array;
  551. vertex_array.resize(connections_count * 6);
  552. for (int i = 0; i < connections_count; i++) {
  553. Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(region, i);
  554. Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(region, i);
  555. Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
  556. Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
  557. Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
  558. Vector3 start_left_dir = -start_right_dir;
  559. Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
  560. Vector3 end_left_dir = -end_right_dir;
  561. Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
  562. Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
  563. Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
  564. Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
  565. vertex_array.push_back(right_end_pos);
  566. vertex_array.push_back(left_start_pos);
  567. vertex_array.push_back(right_start_pos);
  568. vertex_array.push_back(left_end_pos);
  569. vertex_array.push_back(right_end_pos);
  570. vertex_array.push_back(right_start_pos);
  571. }
  572. if (vertex_array.size() == 0) {
  573. return;
  574. }
  575. Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material();
  576. Array mesh_array;
  577. mesh_array.resize(Mesh::ARRAY_MAX);
  578. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  579. debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
  580. debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
  581. RS::get_singleton()->instance_set_base(debug_edge_connections_instance, debug_edge_connections_mesh->get_rid());
  582. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, is_visible_in_tree());
  583. if (is_inside_tree()) {
  584. RS::get_singleton()->instance_set_scenario(debug_edge_connections_instance, get_world_3d()->get_scenario());
  585. }
  586. bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
  587. if (!enable_edge_connections) {
  588. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  589. }
  590. }
  591. #endif // DEBUG_ENABLED