navigation_region_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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/3d/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. PackedStringArray NavigationRegion3D::get_configuration_warnings() const {
  218. PackedStringArray warnings = Node3D::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. #ifdef DEBUG_ENABLED
  288. void NavigationRegion3D::_navigation_debug_changed() {
  289. if (is_inside_tree()) {
  290. _update_debug_mesh();
  291. _update_debug_edge_connections_mesh();
  292. }
  293. }
  294. #endif // DEBUG_ENABLED
  295. void NavigationRegion3D::_region_enter_navigation_map() {
  296. if (!is_inside_tree()) {
  297. return;
  298. }
  299. if (map_override.is_valid()) {
  300. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  301. } else {
  302. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  303. }
  304. current_global_transform = get_global_transform();
  305. NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
  306. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  307. #ifdef DEBUG_ENABLED
  308. if (NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  309. _update_debug_mesh();
  310. }
  311. #endif // DEBUG_ENABLED
  312. }
  313. void NavigationRegion3D::_region_exit_navigation_map() {
  314. NavigationServer3D::get_singleton()->region_set_map(region, RID());
  315. #ifdef DEBUG_ENABLED
  316. if (debug_instance.is_valid()) {
  317. RS::get_singleton()->instance_set_visible(debug_instance, false);
  318. }
  319. if (debug_edge_connections_instance.is_valid()) {
  320. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  321. }
  322. #endif // DEBUG_ENABLED
  323. }
  324. void NavigationRegion3D::_region_update_transform() {
  325. if (!is_inside_tree()) {
  326. return;
  327. }
  328. Transform3D new_global_transform = get_global_transform();
  329. if (current_global_transform != new_global_transform) {
  330. current_global_transform = new_global_transform;
  331. NavigationServer3D::get_singleton()->region_set_transform(region, current_global_transform);
  332. #ifdef DEBUG_ENABLED
  333. if (debug_instance.is_valid()) {
  334. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  335. }
  336. #endif // DEBUG_ENABLED
  337. }
  338. }
  339. NavigationRegion3D::NavigationRegion3D() {
  340. set_notify_transform(true);
  341. region = NavigationServer3D::get_singleton()->region_create();
  342. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  343. NavigationServer3D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  344. NavigationServer3D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  345. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  346. NavigationServer3D::get_singleton()->region_set_use_edge_connections(region, use_edge_connections);
  347. NavigationServer3D::get_singleton()->region_set_enabled(region, enabled);
  348. #ifdef DEBUG_ENABLED
  349. NavigationServer3D::get_singleton()->connect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
  350. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_navigation_debug_changed));
  351. #endif // DEBUG_ENABLED
  352. }
  353. NavigationRegion3D::~NavigationRegion3D() {
  354. if (navigation_mesh.is_valid()) {
  355. navigation_mesh->disconnect_changed(callable_mp(this, &NavigationRegion3D::_navigation_mesh_changed));
  356. }
  357. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  358. NavigationServer3D::get_singleton()->free(region);
  359. #ifdef DEBUG_ENABLED
  360. NavigationServer3D::get_singleton()->disconnect(SNAME("map_changed"), callable_mp(this, &NavigationRegion3D::_navigation_map_changed));
  361. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationRegion3D::_navigation_debug_changed));
  362. ERR_FAIL_NULL(RenderingServer::get_singleton());
  363. if (debug_instance.is_valid()) {
  364. RenderingServer::get_singleton()->free(debug_instance);
  365. }
  366. if (debug_mesh.is_valid()) {
  367. RenderingServer::get_singleton()->free(debug_mesh->get_rid());
  368. }
  369. if (debug_edge_connections_instance.is_valid()) {
  370. RenderingServer::get_singleton()->free(debug_edge_connections_instance);
  371. }
  372. if (debug_edge_connections_mesh.is_valid()) {
  373. RenderingServer::get_singleton()->free(debug_edge_connections_mesh->get_rid());
  374. }
  375. #endif // DEBUG_ENABLED
  376. }
  377. #ifdef DEBUG_ENABLED
  378. void NavigationRegion3D::_update_debug_mesh() {
  379. if (Engine::get_singleton()->is_editor_hint()) {
  380. // don't update inside Editor as node 3d gizmo takes care of this
  381. // as collisions and selections for Editor Viewport need to be updated
  382. return;
  383. }
  384. if (!NavigationServer3D::get_singleton()->get_debug_enabled() || !NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  385. if (debug_instance.is_valid()) {
  386. RS::get_singleton()->instance_set_visible(debug_instance, false);
  387. }
  388. return;
  389. }
  390. if (!navigation_mesh.is_valid()) {
  391. if (debug_instance.is_valid()) {
  392. RS::get_singleton()->instance_set_visible(debug_instance, false);
  393. }
  394. return;
  395. }
  396. if (!debug_instance.is_valid()) {
  397. debug_instance = RenderingServer::get_singleton()->instance_create();
  398. }
  399. if (debug_mesh.is_null()) {
  400. debug_mesh.instantiate();
  401. }
  402. debug_mesh->clear_surfaces();
  403. Vector<Vector3> vertices = navigation_mesh->get_vertices();
  404. if (vertices.size() == 0) {
  405. return;
  406. }
  407. int polygon_count = navigation_mesh->get_polygon_count();
  408. if (polygon_count == 0) {
  409. return;
  410. }
  411. bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
  412. bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
  413. int vertex_count = 0;
  414. int line_count = 0;
  415. for (int i = 0; i < polygon_count; i++) {
  416. const Vector<int> &polygon = navigation_mesh->get_polygon(i);
  417. int polygon_size = polygon.size();
  418. if (polygon_size < 3) {
  419. continue;
  420. }
  421. line_count += polygon_size * 2;
  422. vertex_count += (polygon_size - 2) * 3;
  423. }
  424. Vector<Vector3> face_vertex_array;
  425. face_vertex_array.resize(vertex_count);
  426. Vector<Color> face_color_array;
  427. if (enabled_geometry_face_random_color) {
  428. face_color_array.resize(vertex_count);
  429. }
  430. Vector<Vector3> line_vertex_array;
  431. if (enabled_edge_lines) {
  432. line_vertex_array.resize(line_count);
  433. }
  434. Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  435. RandomPCG rand;
  436. Color polygon_color = debug_navigation_geometry_face_color;
  437. int face_vertex_index = 0;
  438. int line_vertex_index = 0;
  439. Vector3 *face_vertex_array_ptrw = face_vertex_array.ptrw();
  440. Color *face_color_array_ptrw = face_color_array.ptrw();
  441. Vector3 *line_vertex_array_ptrw = line_vertex_array.ptrw();
  442. for (int polygon_index = 0; polygon_index < polygon_count; polygon_index++) {
  443. const Vector<int> &polygon_indices = navigation_mesh->get_polygon(polygon_index);
  444. int polygon_indices_size = polygon_indices.size();
  445. if (polygon_indices_size < 3) {
  446. continue;
  447. }
  448. if (enabled_geometry_face_random_color) {
  449. // Generate the polygon color, slightly randomly modified from the settings one.
  450. 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);
  451. polygon_color.a = debug_navigation_geometry_face_color.a;
  452. }
  453. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size - 2; polygon_indices_index++) {
  454. face_vertex_array_ptrw[face_vertex_index] = vertices[polygon_indices[0]];
  455. face_vertex_array_ptrw[face_vertex_index + 1] = vertices[polygon_indices[polygon_indices_index + 1]];
  456. face_vertex_array_ptrw[face_vertex_index + 2] = vertices[polygon_indices[polygon_indices_index + 2]];
  457. if (enabled_geometry_face_random_color) {
  458. face_color_array_ptrw[face_vertex_index] = polygon_color;
  459. face_color_array_ptrw[face_vertex_index + 1] = polygon_color;
  460. face_color_array_ptrw[face_vertex_index + 2] = polygon_color;
  461. }
  462. face_vertex_index += 3;
  463. }
  464. if (enabled_edge_lines) {
  465. for (int polygon_indices_index = 0; polygon_indices_index < polygon_indices_size; polygon_indices_index++) {
  466. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index]];
  467. line_vertex_index += 1;
  468. if (polygon_indices_index + 1 == polygon_indices_size) {
  469. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[0]];
  470. line_vertex_index += 1;
  471. } else {
  472. line_vertex_array_ptrw[line_vertex_index] = vertices[polygon_indices[polygon_indices_index + 1]];
  473. line_vertex_index += 1;
  474. }
  475. }
  476. }
  477. }
  478. Ref<StandardMaterial3D> face_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_material();
  479. Array face_mesh_array;
  480. face_mesh_array.resize(Mesh::ARRAY_MAX);
  481. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  482. if (enabled_geometry_face_random_color) {
  483. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  484. }
  485. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
  486. debug_mesh->surface_set_material(0, face_material);
  487. if (enabled_edge_lines) {
  488. Ref<StandardMaterial3D> line_material = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_material();
  489. Array line_mesh_array;
  490. line_mesh_array.resize(Mesh::ARRAY_MAX);
  491. line_mesh_array[Mesh::ARRAY_VERTEX] = line_vertex_array;
  492. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, line_mesh_array);
  493. debug_mesh->surface_set_material(1, line_material);
  494. }
  495. RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
  496. if (is_inside_tree()) {
  497. RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
  498. RS::get_singleton()->instance_set_transform(debug_instance, current_global_transform);
  499. RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
  500. }
  501. if (!is_enabled()) {
  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, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_material()->get_rid());
  505. }
  506. if (debug_mesh->get_surface_count() > 1) {
  507. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_material()->get_rid());
  508. }
  509. }
  510. } else {
  511. if (debug_mesh.is_valid()) {
  512. if (debug_mesh->get_surface_count() > 0) {
  513. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 0, RID());
  514. }
  515. if (debug_mesh->get_surface_count() > 1) {
  516. RS::get_singleton()->instance_set_surface_override_material(debug_instance, 1, RID());
  517. }
  518. }
  519. }
  520. }
  521. #endif // DEBUG_ENABLED
  522. #ifdef DEBUG_ENABLED
  523. void NavigationRegion3D::_update_debug_edge_connections_mesh() {
  524. if (!NavigationServer3D::get_singleton()->get_debug_enabled() || !NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  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 (!is_inside_tree()) {
  531. return;
  532. }
  533. if (!use_edge_connections || !NavigationServer3D::get_singleton()->map_get_use_edge_connections(get_world_3d()->get_navigation_map())) {
  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 (!navigation_mesh.is_valid()) {
  540. if (debug_edge_connections_instance.is_valid()) {
  541. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  542. }
  543. return;
  544. }
  545. if (!debug_edge_connections_instance.is_valid()) {
  546. debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  547. }
  548. if (debug_edge_connections_mesh.is_null()) {
  549. debug_edge_connections_mesh.instantiate();
  550. }
  551. debug_edge_connections_mesh->clear_surfaces();
  552. float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
  553. float half_edge_connection_margin = edge_connection_margin * 0.5;
  554. int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(region);
  555. if (connections_count == 0) {
  556. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  557. return;
  558. }
  559. Vector<Vector3> vertex_array;
  560. vertex_array.resize(connections_count * 6);
  561. Vector3 *vertex_array_ptrw = vertex_array.ptrw();
  562. int vertex_array_index = 0;
  563. for (int i = 0; i < connections_count; i++) {
  564. Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(region, i);
  565. Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(region, i);
  566. Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
  567. Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
  568. Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
  569. Vector3 start_left_dir = -start_right_dir;
  570. Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
  571. Vector3 end_left_dir = -end_right_dir;
  572. Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
  573. Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
  574. Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
  575. Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
  576. vertex_array_ptrw[vertex_array_index++] = connection_pathway_start;
  577. vertex_array_ptrw[vertex_array_index++] = connection_pathway_end;
  578. vertex_array_ptrw[vertex_array_index++] = left_start_pos;
  579. vertex_array_ptrw[vertex_array_index++] = right_start_pos;
  580. vertex_array_ptrw[vertex_array_index++] = left_end_pos;
  581. vertex_array_ptrw[vertex_array_index++] = right_end_pos;
  582. }
  583. if (vertex_array.size() == 0) {
  584. return;
  585. }
  586. Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material();
  587. Array mesh_array;
  588. mesh_array.resize(Mesh::ARRAY_MAX);
  589. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  590. debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array);
  591. debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
  592. RS::get_singleton()->instance_set_base(debug_edge_connections_instance, debug_edge_connections_mesh->get_rid());
  593. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, is_visible_in_tree());
  594. if (is_inside_tree()) {
  595. RS::get_singleton()->instance_set_scenario(debug_edge_connections_instance, get_world_3d()->get_scenario());
  596. }
  597. bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
  598. if (!enable_edge_connections) {
  599. RS::get_singleton()->instance_set_visible(debug_edge_connections_instance, false);
  600. }
  601. }
  602. #endif // DEBUG_ENABLED