navigation_server_3d.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /**************************************************************************/
  2. /* navigation_server_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_server_3d.h"
  31. #include "core/config/project_settings.h"
  32. NavigationServer3D *NavigationServer3D::singleton = nullptr;
  33. void NavigationServer3D::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("get_maps"), &NavigationServer3D::get_maps);
  35. ClassDB::bind_method(D_METHOD("map_create"), &NavigationServer3D::map_create);
  36. ClassDB::bind_method(D_METHOD("map_set_active", "map", "active"), &NavigationServer3D::map_set_active);
  37. ClassDB::bind_method(D_METHOD("map_is_active", "map"), &NavigationServer3D::map_is_active);
  38. ClassDB::bind_method(D_METHOD("map_set_up", "map", "up"), &NavigationServer3D::map_set_up);
  39. ClassDB::bind_method(D_METHOD("map_get_up", "map"), &NavigationServer3D::map_get_up);
  40. ClassDB::bind_method(D_METHOD("map_set_cell_size", "map", "cell_size"), &NavigationServer3D::map_set_cell_size);
  41. ClassDB::bind_method(D_METHOD("map_get_cell_size", "map"), &NavigationServer3D::map_get_cell_size);
  42. ClassDB::bind_method(D_METHOD("map_set_edge_connection_margin", "map", "margin"), &NavigationServer3D::map_set_edge_connection_margin);
  43. ClassDB::bind_method(D_METHOD("map_get_edge_connection_margin", "map"), &NavigationServer3D::map_get_edge_connection_margin);
  44. ClassDB::bind_method(D_METHOD("map_set_link_connection_radius", "map", "radius"), &NavigationServer3D::map_set_link_connection_radius);
  45. ClassDB::bind_method(D_METHOD("map_get_link_connection_radius", "map"), &NavigationServer3D::map_get_link_connection_radius);
  46. ClassDB::bind_method(D_METHOD("map_get_path", "map", "origin", "destination", "optimize", "navigation_layers"), &NavigationServer3D::map_get_path, DEFVAL(1));
  47. ClassDB::bind_method(D_METHOD("map_get_closest_point_to_segment", "map", "start", "end", "use_collision"), &NavigationServer3D::map_get_closest_point_to_segment, DEFVAL(false));
  48. ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &NavigationServer3D::map_get_closest_point);
  49. ClassDB::bind_method(D_METHOD("map_get_closest_point_normal", "map", "to_point"), &NavigationServer3D::map_get_closest_point_normal);
  50. ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer3D::map_get_closest_point_owner);
  51. ClassDB::bind_method(D_METHOD("map_get_links", "map"), &NavigationServer3D::map_get_links);
  52. ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer3D::map_get_regions);
  53. ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer3D::map_get_agents);
  54. ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer3D::map_force_update);
  55. ClassDB::bind_method(D_METHOD("query_path", "parameters", "result"), &NavigationServer3D::query_path);
  56. ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer3D::region_create);
  57. ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer3D::region_set_enter_cost);
  58. ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer3D::region_get_enter_cost);
  59. ClassDB::bind_method(D_METHOD("region_set_travel_cost", "region", "travel_cost"), &NavigationServer3D::region_set_travel_cost);
  60. ClassDB::bind_method(D_METHOD("region_get_travel_cost", "region"), &NavigationServer3D::region_get_travel_cost);
  61. ClassDB::bind_method(D_METHOD("region_set_owner_id", "region", "owner_id"), &NavigationServer3D::region_set_owner_id);
  62. ClassDB::bind_method(D_METHOD("region_get_owner_id", "region"), &NavigationServer3D::region_get_owner_id);
  63. ClassDB::bind_method(D_METHOD("region_owns_point", "region", "point"), &NavigationServer3D::region_owns_point);
  64. ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer3D::region_set_map);
  65. ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer3D::region_get_map);
  66. ClassDB::bind_method(D_METHOD("region_set_navigation_layers", "region", "navigation_layers"), &NavigationServer3D::region_set_navigation_layers);
  67. ClassDB::bind_method(D_METHOD("region_get_navigation_layers", "region"), &NavigationServer3D::region_get_navigation_layers);
  68. ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer3D::region_set_transform);
  69. ClassDB::bind_method(D_METHOD("region_set_navigation_mesh", "region", "navigation_mesh"), &NavigationServer3D::region_set_navigation_mesh);
  70. ClassDB::bind_method(D_METHOD("region_bake_navigation_mesh", "navigation_mesh", "root_node"), &NavigationServer3D::region_bake_navigation_mesh);
  71. ClassDB::bind_method(D_METHOD("region_get_connections_count", "region"), &NavigationServer3D::region_get_connections_count);
  72. ClassDB::bind_method(D_METHOD("region_get_connection_pathway_start", "region", "connection"), &NavigationServer3D::region_get_connection_pathway_start);
  73. ClassDB::bind_method(D_METHOD("region_get_connection_pathway_end", "region", "connection"), &NavigationServer3D::region_get_connection_pathway_end);
  74. ClassDB::bind_method(D_METHOD("link_create"), &NavigationServer3D::link_create);
  75. ClassDB::bind_method(D_METHOD("link_set_map", "link", "map"), &NavigationServer3D::link_set_map);
  76. ClassDB::bind_method(D_METHOD("link_get_map", "link"), &NavigationServer3D::link_get_map);
  77. ClassDB::bind_method(D_METHOD("link_set_bidirectional", "link", "bidirectional"), &NavigationServer3D::link_set_bidirectional);
  78. ClassDB::bind_method(D_METHOD("link_is_bidirectional", "link"), &NavigationServer3D::link_is_bidirectional);
  79. ClassDB::bind_method(D_METHOD("link_set_navigation_layers", "link", "navigation_layers"), &NavigationServer3D::link_set_navigation_layers);
  80. ClassDB::bind_method(D_METHOD("link_get_navigation_layers", "link"), &NavigationServer3D::link_get_navigation_layers);
  81. ClassDB::bind_method(D_METHOD("link_set_start_position", "link", "position"), &NavigationServer3D::link_set_start_position);
  82. ClassDB::bind_method(D_METHOD("link_get_start_position", "link"), &NavigationServer3D::link_get_start_position);
  83. ClassDB::bind_method(D_METHOD("link_set_end_position", "link", "position"), &NavigationServer3D::link_set_end_position);
  84. ClassDB::bind_method(D_METHOD("link_get_end_position", "link"), &NavigationServer3D::link_get_end_position);
  85. ClassDB::bind_method(D_METHOD("link_set_enter_cost", "link", "enter_cost"), &NavigationServer3D::link_set_enter_cost);
  86. ClassDB::bind_method(D_METHOD("link_get_enter_cost", "link"), &NavigationServer3D::link_get_enter_cost);
  87. ClassDB::bind_method(D_METHOD("link_set_travel_cost", "link", "travel_cost"), &NavigationServer3D::link_set_travel_cost);
  88. ClassDB::bind_method(D_METHOD("link_get_travel_cost", "link"), &NavigationServer3D::link_get_travel_cost);
  89. ClassDB::bind_method(D_METHOD("link_set_owner_id", "link", "owner_id"), &NavigationServer3D::link_set_owner_id);
  90. ClassDB::bind_method(D_METHOD("link_get_owner_id", "link"), &NavigationServer3D::link_get_owner_id);
  91. ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer3D::agent_create);
  92. ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer3D::agent_set_map);
  93. ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer3D::agent_get_map);
  94. ClassDB::bind_method(D_METHOD("agent_set_neighbor_distance", "agent", "distance"), &NavigationServer3D::agent_set_neighbor_distance);
  95. ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer3D::agent_set_max_neighbors);
  96. ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer3D::agent_set_time_horizon);
  97. ClassDB::bind_method(D_METHOD("agent_set_radius", "agent", "radius"), &NavigationServer3D::agent_set_radius);
  98. ClassDB::bind_method(D_METHOD("agent_set_max_speed", "agent", "max_speed"), &NavigationServer3D::agent_set_max_speed);
  99. ClassDB::bind_method(D_METHOD("agent_set_velocity", "agent", "velocity"), &NavigationServer3D::agent_set_velocity);
  100. ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer3D::agent_set_target_velocity);
  101. ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer3D::agent_set_position);
  102. ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer3D::agent_is_map_changed);
  103. ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "callback"), &NavigationServer3D::agent_set_callback);
  104. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer3D::free);
  105. ClassDB::bind_method(D_METHOD("set_active", "active"), &NavigationServer3D::set_active);
  106. ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationServer3D::set_debug_enabled);
  107. ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationServer3D::get_debug_enabled);
  108. ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
  109. ADD_SIGNAL(MethodInfo("navigation_debug_changed"));
  110. ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &NavigationServer3D::get_process_info);
  111. BIND_ENUM_CONSTANT(INFO_ACTIVE_MAPS);
  112. BIND_ENUM_CONSTANT(INFO_REGION_COUNT);
  113. BIND_ENUM_CONSTANT(INFO_AGENT_COUNT);
  114. BIND_ENUM_CONSTANT(INFO_LINK_COUNT);
  115. BIND_ENUM_CONSTANT(INFO_POLYGON_COUNT);
  116. BIND_ENUM_CONSTANT(INFO_EDGE_COUNT);
  117. BIND_ENUM_CONSTANT(INFO_EDGE_MERGE_COUNT);
  118. BIND_ENUM_CONSTANT(INFO_EDGE_CONNECTION_COUNT);
  119. BIND_ENUM_CONSTANT(INFO_EDGE_FREE_COUNT);
  120. }
  121. NavigationServer3D *NavigationServer3D::get_singleton() {
  122. return singleton;
  123. }
  124. NavigationServer3D::NavigationServer3D() {
  125. ERR_FAIL_COND(singleton != nullptr);
  126. singleton = this;
  127. GLOBAL_DEF("navigation/2d/default_cell_size", 1);
  128. GLOBAL_DEF("navigation/2d/default_edge_connection_margin", 1);
  129. GLOBAL_DEF("navigation/2d/default_link_connection_radius", 4);
  130. GLOBAL_DEF("navigation/3d/default_cell_size", 0.25);
  131. GLOBAL_DEF("navigation/3d/default_edge_connection_margin", 0.25);
  132. GLOBAL_DEF("navigation/3d/default_link_connection_radius", 1.0);
  133. #ifdef DEBUG_ENABLED
  134. debug_navigation_edge_connection_color = GLOBAL_DEF("debug/shapes/navigation/edge_connection_color", Color(1.0, 0.0, 1.0, 1.0));
  135. debug_navigation_geometry_edge_color = GLOBAL_DEF("debug/shapes/navigation/geometry_edge_color", Color(0.5, 1.0, 1.0, 1.0));
  136. debug_navigation_geometry_face_color = GLOBAL_DEF("debug/shapes/navigation/geometry_face_color", Color(0.5, 1.0, 1.0, 0.4));
  137. debug_navigation_geometry_edge_disabled_color = GLOBAL_DEF("debug/shapes/navigation/geometry_edge_disabled_color", Color(0.5, 0.5, 0.5, 1.0));
  138. debug_navigation_geometry_face_disabled_color = GLOBAL_DEF("debug/shapes/navigation/geometry_face_disabled_color", Color(0.5, 0.5, 0.5, 0.4));
  139. debug_navigation_link_connection_color = GLOBAL_DEF("debug/shapes/navigation/link_connection_color", Color(1.0, 0.5, 1.0, 1.0));
  140. debug_navigation_link_connection_disabled_color = GLOBAL_DEF("debug/shapes/navigation/link_connection_disabled_color", Color(0.5, 0.5, 0.5, 1.0));
  141. debug_navigation_agent_path_color = GLOBAL_DEF("debug/shapes/navigation/agent_path_color", Color(1.0, 0.0, 0.0, 1.0));
  142. debug_navigation_enable_edge_connections = GLOBAL_DEF("debug/shapes/navigation/enable_edge_connections", true);
  143. debug_navigation_enable_edge_connections_xray = GLOBAL_DEF("debug/shapes/navigation/enable_edge_connections_xray", true);
  144. debug_navigation_enable_edge_lines = GLOBAL_DEF("debug/shapes/navigation/enable_edge_lines", true);
  145. debug_navigation_enable_edge_lines_xray = GLOBAL_DEF("debug/shapes/navigation/enable_edge_lines_xray", true);
  146. debug_navigation_enable_geometry_face_random_color = GLOBAL_DEF("debug/shapes/navigation/enable_geometry_face_random_color", true);
  147. debug_navigation_enable_link_connections = GLOBAL_DEF("debug/shapes/navigation/enable_link_connections", true);
  148. debug_navigation_enable_link_connections_xray = GLOBAL_DEF("debug/shapes/navigation/enable_link_connections_xray", true);
  149. debug_navigation_enable_agent_paths = GLOBAL_DEF("debug/shapes/navigation/enable_agent_paths", true);
  150. debug_navigation_enable_agent_paths_xray = GLOBAL_DEF("debug/shapes/navigation/enable_agent_paths_xray", true);
  151. debug_navigation_agent_path_point_size = GLOBAL_DEF("debug/shapes/navigation/agent_path_point_size", 4.0);
  152. if (Engine::get_singleton()->is_editor_hint()) {
  153. // enable NavigationServer3D when in Editor or else navigation mesh edge connections are invisible
  154. // on runtime tests SceneTree has "Visible Navigation" set and main iteration takes care of this
  155. set_debug_enabled(true);
  156. }
  157. #endif // DEBUG_ENABLED
  158. }
  159. NavigationServer3D::~NavigationServer3D() {
  160. singleton = nullptr;
  161. }
  162. void NavigationServer3D::set_debug_enabled(bool p_enabled) {
  163. #ifdef DEBUG_ENABLED
  164. if (debug_enabled != p_enabled) {
  165. debug_dirty = true;
  166. }
  167. debug_enabled = p_enabled;
  168. if (debug_dirty) {
  169. call_deferred("_emit_navigation_debug_changed_signal");
  170. }
  171. #endif // DEBUG_ENABLED
  172. }
  173. bool NavigationServer3D::get_debug_enabled() const {
  174. return debug_enabled;
  175. }
  176. #ifdef DEBUG_ENABLED
  177. void NavigationServer3D::_emit_navigation_debug_changed_signal() {
  178. if (debug_dirty) {
  179. debug_dirty = false;
  180. emit_signal(SNAME("navigation_debug_changed"));
  181. }
  182. }
  183. #endif // DEBUG_ENABLED
  184. #ifdef DEBUG_ENABLED
  185. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_face_material() {
  186. if (debug_navigation_geometry_face_material.is_valid()) {
  187. return debug_navigation_geometry_face_material;
  188. }
  189. bool enabled_geometry_face_random_color = get_debug_navigation_enable_geometry_face_random_color();
  190. Ref<StandardMaterial3D> face_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  191. face_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  192. face_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  193. face_material->set_albedo(get_debug_navigation_geometry_face_color());
  194. if (enabled_geometry_face_random_color) {
  195. face_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  196. face_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  197. }
  198. debug_navigation_geometry_face_material = face_material;
  199. return debug_navigation_geometry_face_material;
  200. }
  201. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_edge_material() {
  202. if (debug_navigation_geometry_edge_material.is_valid()) {
  203. return debug_navigation_geometry_edge_material;
  204. }
  205. bool enabled_edge_lines_xray = get_debug_navigation_enable_edge_lines_xray();
  206. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  207. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  208. line_material->set_albedo(get_debug_navigation_geometry_edge_color());
  209. if (enabled_edge_lines_xray) {
  210. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  211. }
  212. debug_navigation_geometry_edge_material = line_material;
  213. return debug_navigation_geometry_edge_material;
  214. }
  215. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_face_disabled_material() {
  216. if (debug_navigation_geometry_face_disabled_material.is_valid()) {
  217. return debug_navigation_geometry_face_disabled_material;
  218. }
  219. Ref<StandardMaterial3D> face_disabled_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  220. face_disabled_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  221. face_disabled_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  222. face_disabled_material->set_albedo(get_debug_navigation_geometry_face_disabled_color());
  223. debug_navigation_geometry_face_disabled_material = face_disabled_material;
  224. return debug_navigation_geometry_face_disabled_material;
  225. }
  226. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_edge_disabled_material() {
  227. if (debug_navigation_geometry_edge_disabled_material.is_valid()) {
  228. return debug_navigation_geometry_edge_disabled_material;
  229. }
  230. bool enabled_edge_lines_xray = get_debug_navigation_enable_edge_lines_xray();
  231. Ref<StandardMaterial3D> line_disabled_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  232. line_disabled_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  233. line_disabled_material->set_albedo(get_debug_navigation_geometry_edge_disabled_color());
  234. if (enabled_edge_lines_xray) {
  235. line_disabled_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  236. }
  237. debug_navigation_geometry_edge_disabled_material = line_disabled_material;
  238. return debug_navigation_geometry_edge_disabled_material;
  239. }
  240. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_edge_connections_material() {
  241. if (debug_navigation_edge_connections_material.is_valid()) {
  242. return debug_navigation_edge_connections_material;
  243. }
  244. bool enabled_edge_connections_xray = get_debug_navigation_enable_edge_connections_xray();
  245. Ref<StandardMaterial3D> edge_connections_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  246. edge_connections_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  247. edge_connections_material->set_albedo(get_debug_navigation_edge_connection_color());
  248. if (enabled_edge_connections_xray) {
  249. edge_connections_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  250. }
  251. edge_connections_material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MAX - 2);
  252. debug_navigation_edge_connections_material = edge_connections_material;
  253. return debug_navigation_edge_connections_material;
  254. }
  255. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_link_connections_material() {
  256. if (debug_navigation_link_connections_material.is_valid()) {
  257. return debug_navigation_link_connections_material;
  258. }
  259. Ref<StandardMaterial3D> material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  260. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  261. material->set_albedo(debug_navigation_link_connection_color);
  262. if (debug_navigation_enable_link_connections_xray) {
  263. material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  264. }
  265. material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MAX - 2);
  266. debug_navigation_link_connections_material = material;
  267. return debug_navigation_link_connections_material;
  268. }
  269. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_link_connections_disabled_material() {
  270. if (debug_navigation_link_connections_disabled_material.is_valid()) {
  271. return debug_navigation_link_connections_disabled_material;
  272. }
  273. Ref<StandardMaterial3D> material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  274. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  275. material->set_albedo(debug_navigation_link_connection_disabled_color);
  276. if (debug_navigation_enable_link_connections_xray) {
  277. material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  278. }
  279. material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MAX - 2);
  280. debug_navigation_link_connections_disabled_material = material;
  281. return debug_navigation_link_connections_disabled_material;
  282. }
  283. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_agent_path_line_material() {
  284. if (debug_navigation_agent_path_line_material.is_valid()) {
  285. return debug_navigation_agent_path_line_material;
  286. }
  287. Ref<StandardMaterial3D> material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  288. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  289. material->set_albedo(debug_navigation_agent_path_color);
  290. if (debug_navigation_enable_agent_paths_xray) {
  291. material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  292. }
  293. material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MAX - 2);
  294. debug_navigation_agent_path_line_material = material;
  295. return debug_navigation_agent_path_line_material;
  296. }
  297. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_agent_path_point_material() {
  298. if (debug_navigation_agent_path_point_material.is_valid()) {
  299. return debug_navigation_agent_path_point_material;
  300. }
  301. Ref<StandardMaterial3D> material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  302. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  303. material->set_albedo(debug_navigation_agent_path_color);
  304. material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  305. material->set_point_size(debug_navigation_agent_path_point_size);
  306. if (debug_navigation_enable_agent_paths_xray) {
  307. material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  308. }
  309. material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MAX - 2);
  310. debug_navigation_agent_path_point_material = material;
  311. return debug_navigation_agent_path_point_material;
  312. }
  313. void NavigationServer3D::set_debug_navigation_edge_connection_color(const Color &p_color) {
  314. debug_navigation_edge_connection_color = p_color;
  315. if (debug_navigation_edge_connections_material.is_valid()) {
  316. debug_navigation_edge_connections_material->set_albedo(debug_navigation_edge_connection_color);
  317. }
  318. }
  319. Color NavigationServer3D::get_debug_navigation_edge_connection_color() const {
  320. return debug_navigation_edge_connection_color;
  321. }
  322. void NavigationServer3D::set_debug_navigation_geometry_edge_color(const Color &p_color) {
  323. debug_navigation_geometry_edge_color = p_color;
  324. if (debug_navigation_geometry_edge_material.is_valid()) {
  325. debug_navigation_geometry_edge_material->set_albedo(debug_navigation_geometry_edge_color);
  326. }
  327. }
  328. Color NavigationServer3D::get_debug_navigation_geometry_edge_color() const {
  329. return debug_navigation_geometry_edge_color;
  330. }
  331. void NavigationServer3D::set_debug_navigation_geometry_face_color(const Color &p_color) {
  332. debug_navigation_geometry_face_color = p_color;
  333. if (debug_navigation_geometry_face_material.is_valid()) {
  334. debug_navigation_geometry_face_material->set_albedo(debug_navigation_geometry_face_color);
  335. }
  336. }
  337. Color NavigationServer3D::get_debug_navigation_geometry_face_color() const {
  338. return debug_navigation_geometry_face_color;
  339. }
  340. void NavigationServer3D::set_debug_navigation_geometry_edge_disabled_color(const Color &p_color) {
  341. debug_navigation_geometry_edge_disabled_color = p_color;
  342. if (debug_navigation_geometry_edge_disabled_material.is_valid()) {
  343. debug_navigation_geometry_edge_disabled_material->set_albedo(debug_navigation_geometry_edge_disabled_color);
  344. }
  345. }
  346. Color NavigationServer3D::get_debug_navigation_geometry_edge_disabled_color() const {
  347. return debug_navigation_geometry_edge_disabled_color;
  348. }
  349. void NavigationServer3D::set_debug_navigation_geometry_face_disabled_color(const Color &p_color) {
  350. debug_navigation_geometry_face_disabled_color = p_color;
  351. if (debug_navigation_geometry_face_disabled_material.is_valid()) {
  352. debug_navigation_geometry_face_disabled_material->set_albedo(debug_navigation_geometry_face_disabled_color);
  353. }
  354. }
  355. Color NavigationServer3D::get_debug_navigation_geometry_face_disabled_color() const {
  356. return debug_navigation_geometry_face_disabled_color;
  357. }
  358. void NavigationServer3D::set_debug_navigation_link_connection_color(const Color &p_color) {
  359. debug_navigation_link_connection_color = p_color;
  360. if (debug_navigation_link_connections_material.is_valid()) {
  361. debug_navigation_link_connections_material->set_albedo(debug_navigation_link_connection_color);
  362. }
  363. }
  364. Color NavigationServer3D::get_debug_navigation_link_connection_color() const {
  365. return debug_navigation_link_connection_color;
  366. }
  367. void NavigationServer3D::set_debug_navigation_link_connection_disabled_color(const Color &p_color) {
  368. debug_navigation_link_connection_disabled_color = p_color;
  369. if (debug_navigation_link_connections_disabled_material.is_valid()) {
  370. debug_navigation_link_connections_disabled_material->set_albedo(debug_navigation_link_connection_disabled_color);
  371. }
  372. }
  373. Color NavigationServer3D::get_debug_navigation_link_connection_disabled_color() const {
  374. return debug_navigation_link_connection_disabled_color;
  375. }
  376. void NavigationServer3D::set_debug_navigation_agent_path_point_size(float p_point_size) {
  377. debug_navigation_agent_path_point_size = MAX(0.1, p_point_size);
  378. if (debug_navigation_agent_path_point_material.is_valid()) {
  379. debug_navigation_agent_path_point_material->set_point_size(debug_navigation_agent_path_point_size);
  380. }
  381. }
  382. float NavigationServer3D::get_debug_navigation_agent_path_point_size() const {
  383. return debug_navigation_agent_path_point_size;
  384. }
  385. void NavigationServer3D::set_debug_navigation_agent_path_color(const Color &p_color) {
  386. debug_navigation_agent_path_color = p_color;
  387. if (debug_navigation_agent_path_line_material.is_valid()) {
  388. debug_navigation_agent_path_line_material->set_albedo(debug_navigation_agent_path_color);
  389. }
  390. if (debug_navigation_agent_path_point_material.is_valid()) {
  391. debug_navigation_agent_path_point_material->set_albedo(debug_navigation_agent_path_color);
  392. }
  393. }
  394. Color NavigationServer3D::get_debug_navigation_agent_path_color() const {
  395. return debug_navigation_agent_path_color;
  396. }
  397. void NavigationServer3D::set_debug_navigation_enable_edge_connections(const bool p_value) {
  398. debug_navigation_enable_edge_connections = p_value;
  399. debug_dirty = true;
  400. call_deferred("_emit_navigation_debug_changed_signal");
  401. }
  402. bool NavigationServer3D::get_debug_navigation_enable_edge_connections() const {
  403. return debug_navigation_enable_edge_connections;
  404. }
  405. void NavigationServer3D::set_debug_navigation_enable_edge_connections_xray(const bool p_value) {
  406. debug_navigation_enable_edge_connections_xray = p_value;
  407. if (debug_navigation_edge_connections_material.is_valid()) {
  408. debug_navigation_edge_connections_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_edge_connections_xray);
  409. }
  410. }
  411. bool NavigationServer3D::get_debug_navigation_enable_edge_connections_xray() const {
  412. return debug_navigation_enable_edge_connections_xray;
  413. }
  414. void NavigationServer3D::set_debug_navigation_enable_edge_lines(const bool p_value) {
  415. debug_navigation_enable_edge_lines = p_value;
  416. debug_dirty = true;
  417. call_deferred("_emit_navigation_debug_changed_signal");
  418. }
  419. bool NavigationServer3D::get_debug_navigation_enable_edge_lines() const {
  420. return debug_navigation_enable_edge_lines;
  421. }
  422. void NavigationServer3D::set_debug_navigation_enable_edge_lines_xray(const bool p_value) {
  423. debug_navigation_enable_edge_lines_xray = p_value;
  424. if (debug_navigation_geometry_edge_material.is_valid()) {
  425. debug_navigation_geometry_edge_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_edge_lines_xray);
  426. }
  427. }
  428. bool NavigationServer3D::get_debug_navigation_enable_edge_lines_xray() const {
  429. return debug_navigation_enable_edge_lines_xray;
  430. }
  431. void NavigationServer3D::set_debug_navigation_enable_geometry_face_random_color(const bool p_value) {
  432. debug_navigation_enable_geometry_face_random_color = p_value;
  433. debug_dirty = true;
  434. call_deferred("_emit_navigation_debug_changed_signal");
  435. }
  436. bool NavigationServer3D::get_debug_navigation_enable_geometry_face_random_color() const {
  437. return debug_navigation_enable_geometry_face_random_color;
  438. }
  439. void NavigationServer3D::set_debug_navigation_enable_link_connections(const bool p_value) {
  440. debug_navigation_enable_link_connections = p_value;
  441. debug_dirty = true;
  442. call_deferred("_emit_navigation_debug_changed_signal");
  443. }
  444. bool NavigationServer3D::get_debug_navigation_enable_link_connections() const {
  445. return debug_navigation_enable_link_connections;
  446. }
  447. void NavigationServer3D::set_debug_navigation_enable_link_connections_xray(const bool p_value) {
  448. debug_navigation_enable_link_connections_xray = p_value;
  449. if (debug_navigation_link_connections_material.is_valid()) {
  450. debug_navigation_link_connections_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_link_connections_xray);
  451. }
  452. }
  453. bool NavigationServer3D::get_debug_navigation_enable_link_connections_xray() const {
  454. return debug_navigation_enable_link_connections_xray;
  455. }
  456. void NavigationServer3D::set_debug_navigation_enable_agent_paths(const bool p_value) {
  457. if (debug_navigation_enable_agent_paths != p_value) {
  458. debug_dirty = true;
  459. }
  460. debug_navigation_enable_agent_paths = p_value;
  461. if (debug_dirty) {
  462. call_deferred("_emit_navigation_debug_changed_signal");
  463. }
  464. }
  465. bool NavigationServer3D::get_debug_navigation_enable_agent_paths() const {
  466. return debug_navigation_enable_agent_paths;
  467. }
  468. void NavigationServer3D::set_debug_navigation_enable_agent_paths_xray(const bool p_value) {
  469. debug_navigation_enable_agent_paths_xray = p_value;
  470. if (debug_navigation_agent_path_line_material.is_valid()) {
  471. debug_navigation_agent_path_line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_agent_paths_xray);
  472. }
  473. if (debug_navigation_agent_path_point_material.is_valid()) {
  474. debug_navigation_agent_path_point_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_agent_paths_xray);
  475. }
  476. }
  477. bool NavigationServer3D::get_debug_navigation_enable_agent_paths_xray() const {
  478. return debug_navigation_enable_agent_paths_xray;
  479. }
  480. #endif // DEBUG_ENABLED
  481. void NavigationServer3D::query_path(const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result) const {
  482. ERR_FAIL_COND(!p_query_parameters.is_valid());
  483. ERR_FAIL_COND(!p_query_result.is_valid());
  484. const NavigationUtilities::PathQueryResult _query_result = _query_path(p_query_parameters->get_parameters());
  485. p_query_result->set_path(_query_result.path);
  486. p_query_result->set_path_types(_query_result.path_types);
  487. p_query_result->set_path_rids(_query_result.path_rids);
  488. p_query_result->set_path_owner_ids(_query_result.path_owner_ids);
  489. }
  490. ///////////////////////////////////////////////////////
  491. NavigationServer3DCallback NavigationServer3DManager::create_callback = nullptr;
  492. void NavigationServer3DManager::set_default_server(NavigationServer3DCallback p_callback) {
  493. create_callback = p_callback;
  494. }
  495. NavigationServer3D *NavigationServer3DManager::new_default_server() {
  496. if (create_callback == nullptr) {
  497. return nullptr;
  498. }
  499. return create_callback();
  500. }