navigation_region_3d.cpp 29 KB

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