navigation_region_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. callable_mp(this, &NavigationRegion3D::_bake_finished).call_deferred(p_navigation_mesh);
  209. return;
  210. }
  211. set_navigation_mesh(p_navigation_mesh);
  212. emit_signal(SNAME("bake_finished"));
  213. }
  214. bool NavigationRegion3D::is_baking() const {
  215. return NavigationServer3D::get_singleton()->is_baking_navigation_mesh(navigation_mesh);
  216. }
  217. Array NavigationRegion3D::get_configuration_warnings() const {
  218. Array warnings = Node::get_configuration_warnings();
  219. if (is_visible_in_tree() && is_inside_tree()) {
  220. if (!navigation_mesh.is_valid()) {
  221. warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work."));
  222. }
  223. }
  224. return warnings;
  225. }
  226. void NavigationRegion3D::_bind_methods() {
  227. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationRegion3D::get_rid);
  228. ClassDB::bind_method(D_METHOD("set_navigation_mesh", "navigation_mesh"), &NavigationRegion3D::set_navigation_mesh);
  229. ClassDB::bind_method(D_METHOD("get_navigation_mesh"), &NavigationRegion3D::get_navigation_mesh);
  230. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion3D::set_enabled);
  231. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion3D::is_enabled);
  232. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationRegion3D::set_navigation_map);
  233. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationRegion3D::get_navigation_map);
  234. ClassDB::bind_method(D_METHOD("set_use_edge_connections", "enabled"), &NavigationRegion3D::set_use_edge_connections);
  235. ClassDB::bind_method(D_METHOD("get_use_edge_connections"), &NavigationRegion3D::get_use_edge_connections);
  236. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion3D::set_navigation_layers);
  237. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion3D::get_navigation_layers);
  238. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion3D::set_navigation_layer_value);
  239. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion3D::get_navigation_layer_value);
  240. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion3D::get_region_rid);
  241. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion3D::set_enter_cost);
  242. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion3D::get_enter_cost);
  243. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion3D::set_travel_cost);
  244. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion3D::get_travel_cost);
  245. ClassDB::bind_method(D_METHOD("bake_navigation_mesh", "on_thread"), &NavigationRegion3D::bake_navigation_mesh, DEFVAL(true));
  246. ClassDB::bind_method(D_METHOD("is_baking"), &NavigationRegion3D::is_baking);
  247. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navigation_mesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"), "set_navigation_mesh", "get_navigation_mesh");
  248. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  249. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_edge_connections"), "set_use_edge_connections", "get_use_edge_connections");
  250. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  251. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  252. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  253. ADD_SIGNAL(MethodInfo("navigation_mesh_changed"));
  254. ADD_SIGNAL(MethodInfo("bake_finished"));
  255. }
  256. #ifndef DISABLE_DEPRECATED
  257. // Compatibility with earlier 4.0 betas.
  258. bool NavigationRegion3D::_set(const StringName &p_name, const Variant &p_value) {
  259. if (p_name == "navmesh") {
  260. set_navigation_mesh(p_value);
  261. return true;
  262. }
  263. return false;
  264. }
  265. bool NavigationRegion3D::_get(const StringName &p_name, Variant &r_ret) const {
  266. if (p_name == "navmesh") {
  267. r_ret = get_navigation_mesh();
  268. return true;
  269. }
  270. return false;
  271. }
  272. #endif // DISABLE_DEPRECATED
  273. void NavigationRegion3D::_navigation_mesh_changed() {
  274. update_gizmos();
  275. update_configuration_warnings();
  276. #ifdef DEBUG_ENABLED
  277. _update_debug_edge_connections_mesh();
  278. #endif // DEBUG_ENABLED
  279. }
  280. #ifdef DEBUG_ENABLED
  281. void NavigationRegion3D::_navigation_map_changed(RID p_map) {
  282. if (is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) {
  283. _update_debug_edge_connections_mesh();
  284. }
  285. }
  286. #endif // DEBUG_ENABLED
  287. void NavigationRegion3D::_region_enter_navigation_map() {
  288. if (!is_inside_tree()) {
  289. return;
  290. }
  291. if (map_override.is_valid()) {
  292. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  293. } else {
  294. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  295. }
  296. current_global_transform = get_global_transform();
  297. NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
  298. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  299. #ifdef DEBUG_ENABLED
  300. if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  301. _update_debug_mesh();
  302. }
  303. #endif // DEBUG_ENABLED
  304. }
  305. void NavigationRegion3D::_region_exit_navigation_map() {
  306. NavigationServer3D::get_singleton()->region_set_map(region, RID());
  307. #ifdef DEBUG_ENABLED
  308. if (debug_instance.is_valid()) {
  309. RS::get_singleton()->instance_set_visible(debug_instance, false);
  310. }
  311. if (debug_edge_connections_instance.is_valid()) {
  312. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  313. }
  314. #endif // DEBUG_ENABLED
  315. }
  316. void NavigationRegion3D::_region_update_transform() {
  317. if (!is_inside_tree()) {
  318. return;
  319. }
  320. Transform3D new_global_transform = get_global_transform();
  321. if (current_global_transform != new_global_transform) {
  322. current_global_transform = new_global_transform;
  323. NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
  324. #ifdef DEBUG_ENABLED
  325. if (debug_instance.is_valid()) {
  326. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  327. }
  328. #endif // DEBUG_ENABLED
  329. }
  330. }
  331. NavigationRegion3D::NavigationRegion3D() {
  332. set_notify_transform(true);
  333. region = NavigationServer3D::get_singleton()->region_create();
  334. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  335. NavigationServer3D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  336. NavigationServer3D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  337. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  338. NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  339. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  340. #ifdef DEBUG_ENABLED
  341. NavigationServer3D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
  342. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_mesh));
  343. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_edge_connections_mesh));
  344. #endif // DEBUG_ENABLED
  345. }
  346. NavigationRegion3D::~NavigationRegion3D() {
  347. if (navigation_mesh.is_valid()) {
  348. navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
  349. }
  350. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  351. NavigationServer3D::get_singleton()->free(region);
  352. #ifdef DEBUG_ENABLED
  353. NavigationServer3D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
  354. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_mesh));
  355. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_update_debug_edge_connections_mesh));
  356. ERR_FAIL_NULL(RenderingServer::get_singleton());
  357. if (debug_instance.is_valid()) {
  358. RenderingServer::get_singleton()->free(debug_instance);
  359. }
  360. if (debug_mesh.is_valid()) {
  361. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  362. }
  363. if (debug_edge_connections_instance.is_valid()) {
  364. RenderingServer::get_singleton()->free(debug_edge_connections_instance);
  365. }
  366. if (debug_edge_connections_mesh.is_valid()) {
  367. RenderingServer::get_singleton()->free(debug_edge_connections_mesh->get_rid());
  368. }
  369. #endif // DEBUG_ENABLED
  370. }
  371. #ifdef DEBUG_ENABLED
  372. void NavigationRegion3D::_update_debug_mesh() {
  373. if (Engine::get_singleton()->is_editor_hint()) {
  374. // don't update inside Editor as node 3d gizmo takes care of this
  375. // as collisions and selections for Editor Viewport need to be updated
  376. return;
  377. }
  378. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  379. if (debug_instance.is_valid()) {
  380. RS::get_singleton()->instance_set_visible(debug_instance, false);
  381. }
  382. return;
  383. }
  384. if (!navigation_mesh.is_valid()) {
  385. if (debug_instance.is_valid()) {
  386. RS::get_singleton()->instance_set_visible(debug_instance, false);
  387. }
  388. return;
  389. }
  390. if (!debug_instance.is_valid()) {
  391. debug_instance = RenderingServer::get_singleton()->instance_create();
  392. }
  393. if (!debug_mesh.is_valid()) {
  394. debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  395. }
  396. debug_mesh->clear_surfaces();
  397. Vector<Vector3> vertices = navigation_mesh->get_vertices();
  398. if (vertices.size() == 0) {
  399. return;
  400. }
  401. int polygon_count = navigation_mesh->get_polygon_count();
  402. if (polygon_count == 0) {
  403. return;
  404. }
  405. bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
  406. bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
  407. int vertex_count = 0;
  408. int line_count = 0;
  409. for (int i = 0; i < polygon_count; i++) {
  410. const Vector<int> &polygon = navigation_mesh->get_polygon(i);
  411. int polygon_size = polygon.size();
  412. if (polygon_size < 3) {
  413. continue;
  414. }
  415. line_count += polygon_size * 2;
  416. vertex_count += (polygon_size - 2) * 3;
  417. }
  418. Vector<Vector3> face_vertex_array;
  419. face_vertex_array.resize(vertex_count);
  420. Vector<Color> face_color_array;
  421. if (enabled_geometry_face_random_color) {
  422. face_color_array.resize(vertex_count);
  423. }
  424. Vector<Vector3> line_vertex_array;
  425. if (enabled_edge_lines) {
  426. line_vertex_array.resize(line_count);
  427. }
  428. Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  429. RandomPCG rand;
  430. Color polygon_color = debug_navigation_geometry_face_color;
  431. int face_vertex_index = 0;
  432. int line_vertex_index = 0;
  433. Vector3 *face_vertex_array_ptrw = face_vertex_array.ptrw();
  434. Color *face_color_array_ptrw = face_color_array.ptrw();
  435. Vector3 *line_vertex_array_ptrw = line_vertex_array.ptrw();
  436. for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
  437. const Vector<int> &polygon_indices = navigation_mesh->get_polygon(polygon_index);
  438. int polygon_indices_size = polygon_indices.size();
  439. if (polygon_indices_size < 3) {
  440. continue;
  441. }
  442. if (enabled_geometry_face_random_color) {
  443. // Generate the polygon color, slightly randomly modified from the settings one.
  444. 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);
  445. polygon_color.a = debug_navigation_geometry_face_color.a;
  446. }
  447. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
  448. face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
  449. face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
  450. face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
  451. if (enabled_geometry_face_random_color) {
  452. face_color_array_ptrw[face_vertex_index] = polygon_color;
  453. face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
  454. face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
  455. }
  456. face_vertex_index += 3;
  457. }
  458. if (enabled_edge_lines) {
  459. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
  460. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
  461. line_vertex_index += 1;
  462. if (polygon_indices_index + 1 == polygon_indices_size) {
  463. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
  464. line_vertex_index += 1;
  465. } else {
  466. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
  467. line_vertex_index += 1;
  468. }
  469. }
  470. }
  471. }
  472. Ref<StandardMaterial3D> face_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_material();
  473. Array face_mesh_array;
  474. face_mesh_array.resize(Mesh::ARRAY_MAX);
  475. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  476. if (enabled_geometry_face_random_color) {
  477. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  478. }
  479. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
  480. debug_mesh->surface_set_material(0, face_material);
  481. if (enabled_edge_lines) {
  482. Ref<StandardMaterial3D> line_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_material();
  483. Array line_mesh_array;
  484. line_mesh_array.resize(Mesh::ARRAY_MAX);
  485. line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
  486. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, line_mesh_array);
  487. debug_mesh->surface_set_material(1, line_material);
  488. }
  489. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  490. if (is_inside_tree()) {
  491. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  492. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  493. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  494. }
  495. if (!is_enabled()) {
  496. if (debug_mesh.is_valid()) {
  497. if (debug_mesh->get_surface_count() > 0) {
  498. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
  499. }
  500. if (debug_mesh->get_surface_count() > 1) {
  501. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
  502. }
  503. }
  504. } else {
  505. if (debug_mesh.is_valid()) {
  506. if (debug_mesh->get_surface_count() > 0) {
  507. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID());
  508. }
  509. if (debug_mesh->get_surface_count() > 1) {
  510. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID());
  511. }
  512. }
  513. }
  514. }
  515. #endif // DEBUG_ENABLED
  516. #ifdef DEBUG_ENABLED
  517. void NavigationRegion3D::_update_debug_edge_connections_mesh() {
  518. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  519. if (debug_edge_connections_instance.is_valid()) {
  520. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  521. }
  522. return;
  523. }
  524. if (!is_inside_tree()) {
  525. return;
  526. }
  527. if (!use_edge_connections || !NavigationServer3D::get_singleton()->map_get_use_edge_connections(get_world_3d()->get_navigation_map())) {
  528. if (debug_edge_connections_instance.is_valid()) {
  529. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  530. }
  531. return;
  532. }
  533. if (!navigation_mesh.is_valid()) {
  534. if (debug_edge_connections_instance.is_valid()) {
  535. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  536. }
  537. return;
  538. }
  539. if (!debug_edge_connections_instance.is_valid()) {
  540. debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  541. }
  542. if (!debug_edge_connections_mesh.is_valid()) {
  543. debug_edge_connections_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  544. }
  545. debug_edge_connections_mesh->clear_surfaces();
  546. float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
  547. float half_edge_connection_margin = edge_connection_margin * 0.5;
  548. int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(region);
  549. if (connections_count == 0) {
  550. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  551. return;
  552. }
  553. Vector<Vector3> vertex_array;
  554. vertex_array.resize(connections_count * 6);
  555. for (int i = 0; i < connections_count; i++) {
  556. Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(region, i);
  557. Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(region, i);
  558. Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
  559. Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
  560. Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
  561. Vector3 start_left_dir = -start_right_dir;
  562. Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
  563. Vector3 end_left_dir = -end_right_dir;
  564. Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
  565. Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
  566. Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
  567. Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
  568. vertex_array.push_back(right_end_pos);
  569. vertex_array.push_back(left_start_pos);
  570. vertex_array.push_back(right_start_pos);
  571. vertex_array.push_back(left_end_pos);
  572. vertex_array.push_back(right_end_pos);
  573. vertex_array.push_back(right_start_pos);
  574. }
  575. if (vertex_array.size() == 0) {
  576. return;
  577. }
  578. Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material();
  579. Array mesh_array;
  580. mesh_array.resize(Mesh::ARRAY_MAX);
  581. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  582. debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
  583. debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
  584. RS::get_singleton()->instance_set_base(debug_edge_connections_instance, debug_edge_connections_mesh->get_rid());
  585. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, is_visible_in_tree());
  586. if (is_inside_tree()) {
  587. RS::get_singleton()->instance_set_scenario(debug_edge_connections_instance, get_world_3d()->get_scenario());
  588. }
  589. bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
  590. if (!enable_edge_connections) {
  591. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  592. }
  593. }
  594. #endif // DEBUG_ENABLED