navigation_server_3d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*************************************************************************/
  2. /* navigation_server_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifdef DEBUG_ENABLED
  32. #include "core/config/project_settings.h"
  33. #endif // DEBUG_ENABLED
  34. NavigationServer3D *NavigationServer3D::singleton = nullptr;
  35. void NavigationServer3D::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("get_maps"), &NavigationServer3D::get_maps);
  37. ClassDB::bind_method(D_METHOD("map_create"), &NavigationServer3D::map_create);
  38. ClassDB::bind_method(D_METHOD("map_set_active", "map", "active"), &NavigationServer3D::map_set_active);
  39. ClassDB::bind_method(D_METHOD("map_is_active", "nap"), &NavigationServer3D::map_is_active);
  40. ClassDB::bind_method(D_METHOD("map_set_up", "map", "up"), &NavigationServer3D::map_set_up);
  41. ClassDB::bind_method(D_METHOD("map_get_up", "map"), &NavigationServer3D::map_get_up);
  42. ClassDB::bind_method(D_METHOD("map_set_cell_size", "map", "cell_size"), &NavigationServer3D::map_set_cell_size);
  43. ClassDB::bind_method(D_METHOD("map_get_cell_size", "map"), &NavigationServer3D::map_get_cell_size);
  44. ClassDB::bind_method(D_METHOD("map_set_edge_connection_margin", "map", "margin"), &NavigationServer3D::map_set_edge_connection_margin);
  45. ClassDB::bind_method(D_METHOD("map_get_edge_connection_margin", "map"), &NavigationServer3D::map_get_edge_connection_margin);
  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_regions", "map"), &NavigationServer3D::map_get_regions);
  52. ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer3D::map_get_agents);
  53. ClassDB::bind_method(D_METHOD("map_force_update", "map"), &NavigationServer3D::map_force_update);
  54. ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer3D::region_create);
  55. ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer3D::region_set_enter_cost);
  56. ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer3D::region_get_enter_cost);
  57. ClassDB::bind_method(D_METHOD("region_set_travel_cost", "region", "travel_cost"), &NavigationServer3D::region_set_travel_cost);
  58. ClassDB::bind_method(D_METHOD("region_get_travel_cost", "region"), &NavigationServer3D::region_get_travel_cost);
  59. ClassDB::bind_method(D_METHOD("region_owns_point", "region", "point"), &NavigationServer3D::region_owns_point);
  60. ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer3D::region_set_map);
  61. ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer3D::region_get_map);
  62. ClassDB::bind_method(D_METHOD("region_set_navigation_layers", "region", "navigation_layers"), &NavigationServer3D::region_set_navigation_layers);
  63. ClassDB::bind_method(D_METHOD("region_get_navigation_layers", "region"), &NavigationServer3D::region_get_navigation_layers);
  64. ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer3D::region_set_transform);
  65. ClassDB::bind_method(D_METHOD("region_set_navmesh", "region", "nav_mesh"), &NavigationServer3D::region_set_navmesh);
  66. ClassDB::bind_method(D_METHOD("region_bake_navmesh", "mesh", "node"), &NavigationServer3D::region_bake_navmesh);
  67. ClassDB::bind_method(D_METHOD("region_get_connections_count", "region"), &NavigationServer3D::region_get_connections_count);
  68. ClassDB::bind_method(D_METHOD("region_get_connection_pathway_start", "region", "connection"), &NavigationServer3D::region_get_connection_pathway_start);
  69. ClassDB::bind_method(D_METHOD("region_get_connection_pathway_end", "region", "connection"), &NavigationServer3D::region_get_connection_pathway_end);
  70. ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer3D::agent_create);
  71. ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer3D::agent_set_map);
  72. ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer3D::agent_get_map);
  73. ClassDB::bind_method(D_METHOD("agent_set_neighbor_distance", "agent", "distance"), &NavigationServer3D::agent_set_neighbor_distance);
  74. ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer3D::agent_set_max_neighbors);
  75. ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer3D::agent_set_time_horizon);
  76. ClassDB::bind_method(D_METHOD("agent_set_radius", "agent", "radius"), &NavigationServer3D::agent_set_radius);
  77. ClassDB::bind_method(D_METHOD("agent_set_max_speed", "agent", "max_speed"), &NavigationServer3D::agent_set_max_speed);
  78. ClassDB::bind_method(D_METHOD("agent_set_velocity", "agent", "velocity"), &NavigationServer3D::agent_set_velocity);
  79. ClassDB::bind_method(D_METHOD("agent_set_target_velocity", "agent", "target_velocity"), &NavigationServer3D::agent_set_target_velocity);
  80. ClassDB::bind_method(D_METHOD("agent_set_position", "agent", "position"), &NavigationServer3D::agent_set_position);
  81. ClassDB::bind_method(D_METHOD("agent_is_map_changed", "agent"), &NavigationServer3D::agent_is_map_changed);
  82. ClassDB::bind_method(D_METHOD("agent_set_callback", "agent", "receiver", "method", "userdata"), &NavigationServer3D::agent_set_callback, DEFVAL(Variant()));
  83. ClassDB::bind_method(D_METHOD("free_rid", "rid"), &NavigationServer3D::free);
  84. ClassDB::bind_method(D_METHOD("set_active", "active"), &NavigationServer3D::set_active);
  85. ClassDB::bind_method(D_METHOD("process", "delta_time"), &NavigationServer3D::process);
  86. ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
  87. ADD_SIGNAL(MethodInfo("navigation_debug_changed"));
  88. }
  89. const NavigationServer3D *NavigationServer3D::get_singleton() {
  90. return singleton;
  91. }
  92. NavigationServer3D *NavigationServer3D::get_singleton_mut() {
  93. return singleton;
  94. }
  95. NavigationServer3D::NavigationServer3D() {
  96. ERR_FAIL_COND(singleton != nullptr);
  97. singleton = this;
  98. #ifdef DEBUG_ENABLED
  99. debug_navigation_edge_connection_color = GLOBAL_DEF("debug/shapes/navigation/edge_connection_color", Color(1.0, 0.0, 1.0, 1.0));
  100. debug_navigation_geometry_edge_color = GLOBAL_DEF("debug/shapes/navigation/geometry_edge_color", Color(0.5, 1.0, 1.0, 1.0));
  101. debug_navigation_geometry_face_color = GLOBAL_DEF("debug/shapes/navigation/geometry_face_color", Color(0.5, 1.0, 1.0, 0.4));
  102. debug_navigation_geometry_edge_disabled_color = GLOBAL_DEF("debug/shapes/navigation/geometry_edge_disabled_color", Color(0.5, 0.5, 0.5, 1.0));
  103. debug_navigation_geometry_face_disabled_color = GLOBAL_DEF("debug/shapes/navigation/geometry_face_disabled_color", Color(0.5, 0.5, 0.5, 0.4));
  104. debug_navigation_enable_edge_connections = GLOBAL_DEF("debug/shapes/navigation/enable_edge_connections", true);
  105. debug_navigation_enable_edge_connections_xray = GLOBAL_DEF("debug/shapes/navigation/enable_edge_connections_xray", true);
  106. debug_navigation_enable_edge_lines = GLOBAL_DEF("debug/shapes/navigation/enable_edge_lines", true);
  107. debug_navigation_enable_edge_lines_xray = GLOBAL_DEF("debug/shapes/navigation/enable_edge_lines_xray", true);
  108. debug_navigation_enable_geometry_face_random_color = GLOBAL_DEF("debug/shapes/navigation/enable_geometry_face_random_color", true);
  109. if (Engine::get_singleton()->is_editor_hint()) {
  110. // enable NavigationServer3D when in Editor or else navmesh edge connections are invisible
  111. // on runtime tests SceneTree has "Visible Navigation" set and main iteration takes care of this
  112. set_debug_enabled(true);
  113. }
  114. #endif // DEBUG_ENABLED
  115. }
  116. NavigationServer3D::~NavigationServer3D() {
  117. singleton = nullptr;
  118. }
  119. NavigationServer3DCallback NavigationServer3DManager::create_callback = nullptr;
  120. void NavigationServer3DManager::set_default_server(NavigationServer3DCallback p_callback) {
  121. create_callback = p_callback;
  122. }
  123. NavigationServer3D *NavigationServer3DManager::new_default_server() {
  124. ERR_FAIL_COND_V(create_callback == nullptr, nullptr);
  125. return create_callback();
  126. }
  127. #ifdef DEBUG_ENABLED
  128. void NavigationServer3D::_emit_navigation_debug_changed_signal() {
  129. if (debug_dirty) {
  130. debug_dirty = false;
  131. emit_signal(SNAME("navigation_debug_changed"));
  132. }
  133. }
  134. #endif // DEBUG_ENABLED
  135. #ifdef DEBUG_ENABLED
  136. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_face_material() {
  137. if (debug_navigation_geometry_face_material.is_valid()) {
  138. return debug_navigation_geometry_face_material;
  139. }
  140. bool enabled_geometry_face_random_color = get_debug_navigation_enable_geometry_face_random_color();
  141. Color debug_navigation_geometry_face_color = get_debug_navigation_geometry_face_color();
  142. Ref<StandardMaterial3D> face_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  143. face_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  144. face_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  145. face_material->set_albedo(debug_navigation_geometry_face_color);
  146. if (enabled_geometry_face_random_color) {
  147. face_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  148. face_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  149. }
  150. debug_navigation_geometry_face_material = face_material;
  151. return debug_navigation_geometry_face_material;
  152. }
  153. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_edge_material() {
  154. if (debug_navigation_geometry_edge_material.is_valid()) {
  155. return debug_navigation_geometry_edge_material;
  156. }
  157. bool enabled_edge_lines_xray = get_debug_navigation_enable_edge_lines_xray();
  158. Color debug_navigation_geometry_edge_color = get_debug_navigation_geometry_edge_color();
  159. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  160. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  161. line_material->set_albedo(debug_navigation_geometry_edge_color);
  162. if (enabled_edge_lines_xray) {
  163. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  164. }
  165. debug_navigation_geometry_edge_material = line_material;
  166. return debug_navigation_geometry_edge_material;
  167. }
  168. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_face_disabled_material() {
  169. if (debug_navigation_geometry_face_disabled_material.is_valid()) {
  170. return debug_navigation_geometry_face_disabled_material;
  171. }
  172. Color debug_navigation_geometry_face_disabled_color = get_debug_navigation_geometry_face_disabled_color();
  173. Ref<StandardMaterial3D> face_disabled_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  174. face_disabled_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  175. face_disabled_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  176. face_disabled_material->set_albedo(debug_navigation_geometry_face_disabled_color);
  177. debug_navigation_geometry_face_disabled_material = face_disabled_material;
  178. return debug_navigation_geometry_face_disabled_material;
  179. }
  180. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_geometry_edge_disabled_material() {
  181. if (debug_navigation_geometry_edge_disabled_material.is_valid()) {
  182. return debug_navigation_geometry_edge_disabled_material;
  183. }
  184. bool enabled_edge_lines_xray = get_debug_navigation_enable_edge_lines_xray();
  185. Color debug_navigation_geometry_edge_disabled_color = get_debug_navigation_geometry_edge_disabled_color();
  186. Ref<StandardMaterial3D> line_disabled_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  187. line_disabled_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  188. line_disabled_material->set_albedo(debug_navigation_geometry_edge_disabled_color);
  189. if (enabled_edge_lines_xray) {
  190. line_disabled_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  191. }
  192. debug_navigation_geometry_edge_disabled_material = line_disabled_material;
  193. return debug_navigation_geometry_edge_disabled_material;
  194. }
  195. Ref<StandardMaterial3D> NavigationServer3D::get_debug_navigation_edge_connections_material() {
  196. if (debug_navigation_edge_connections_material.is_valid()) {
  197. return debug_navigation_edge_connections_material;
  198. }
  199. bool enabled_edge_connections_xray = get_debug_navigation_enable_edge_connections_xray();
  200. Color debug_navigation_edge_connection_color = get_debug_navigation_edge_connection_color();
  201. Ref<StandardMaterial3D> edge_connections_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  202. edge_connections_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  203. edge_connections_material->set_albedo(debug_navigation_edge_connection_color);
  204. if (enabled_edge_connections_xray) {
  205. edge_connections_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  206. }
  207. edge_connections_material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MAX - 2);
  208. debug_navigation_edge_connections_material = edge_connections_material;
  209. return debug_navigation_edge_connections_material;
  210. }
  211. void NavigationServer3D::set_debug_navigation_edge_connection_color(const Color &p_color) {
  212. debug_navigation_edge_connection_color = p_color;
  213. if (debug_navigation_edge_connections_material.is_valid()) {
  214. debug_navigation_edge_connections_material->set_albedo(debug_navigation_edge_connection_color);
  215. }
  216. }
  217. Color NavigationServer3D::get_debug_navigation_edge_connection_color() const {
  218. return debug_navigation_edge_connection_color;
  219. }
  220. void NavigationServer3D::set_debug_navigation_geometry_edge_color(const Color &p_color) {
  221. debug_navigation_geometry_edge_color = p_color;
  222. if (debug_navigation_geometry_edge_material.is_valid()) {
  223. debug_navigation_geometry_edge_material->set_albedo(debug_navigation_geometry_edge_color);
  224. }
  225. }
  226. Color NavigationServer3D::get_debug_navigation_geometry_edge_color() const {
  227. return debug_navigation_geometry_edge_color;
  228. }
  229. void NavigationServer3D::set_debug_navigation_geometry_face_color(const Color &p_color) {
  230. debug_navigation_geometry_face_color = p_color;
  231. if (debug_navigation_geometry_face_material.is_valid()) {
  232. debug_navigation_geometry_face_material->set_albedo(debug_navigation_geometry_face_color);
  233. }
  234. }
  235. Color NavigationServer3D::get_debug_navigation_geometry_face_color() const {
  236. return debug_navigation_geometry_face_color;
  237. }
  238. void NavigationServer3D::set_debug_navigation_geometry_edge_disabled_color(const Color &p_color) {
  239. debug_navigation_geometry_edge_disabled_color = p_color;
  240. if (debug_navigation_geometry_edge_disabled_material.is_valid()) {
  241. debug_navigation_geometry_edge_disabled_material->set_albedo(debug_navigation_geometry_edge_disabled_color);
  242. }
  243. }
  244. Color NavigationServer3D::get_debug_navigation_geometry_edge_disabled_color() const {
  245. return debug_navigation_geometry_edge_disabled_color;
  246. }
  247. void NavigationServer3D::set_debug_navigation_geometry_face_disabled_color(const Color &p_color) {
  248. debug_navigation_geometry_face_disabled_color = p_color;
  249. if (debug_navigation_geometry_face_disabled_material.is_valid()) {
  250. debug_navigation_geometry_face_disabled_material->set_albedo(debug_navigation_geometry_face_disabled_color);
  251. }
  252. }
  253. Color NavigationServer3D::get_debug_navigation_geometry_face_disabled_color() const {
  254. return debug_navigation_geometry_face_disabled_color;
  255. }
  256. void NavigationServer3D::set_debug_navigation_enable_edge_connections(const bool p_value) {
  257. debug_navigation_enable_edge_connections = p_value;
  258. debug_dirty = true;
  259. call_deferred("_emit_navigation_debug_changed_signal");
  260. }
  261. bool NavigationServer3D::get_debug_navigation_enable_edge_connections() const {
  262. return debug_navigation_enable_edge_connections;
  263. }
  264. void NavigationServer3D::set_debug_navigation_enable_edge_connections_xray(const bool p_value) {
  265. debug_navigation_enable_edge_connections_xray = p_value;
  266. if (debug_navigation_edge_connections_material.is_valid()) {
  267. debug_navigation_edge_connections_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_edge_connections_xray);
  268. }
  269. }
  270. bool NavigationServer3D::get_debug_navigation_enable_edge_connections_xray() const {
  271. return debug_navigation_enable_edge_connections_xray;
  272. }
  273. void NavigationServer3D::set_debug_navigation_enable_edge_lines(const bool p_value) {
  274. debug_navigation_enable_edge_lines = p_value;
  275. debug_dirty = true;
  276. call_deferred("_emit_navigation_debug_changed_signal");
  277. }
  278. bool NavigationServer3D::get_debug_navigation_enable_edge_lines() const {
  279. return debug_navigation_enable_edge_lines;
  280. }
  281. void NavigationServer3D::set_debug_navigation_enable_edge_lines_xray(const bool p_value) {
  282. debug_navigation_enable_edge_lines_xray = p_value;
  283. if (debug_navigation_geometry_edge_material.is_valid()) {
  284. debug_navigation_geometry_edge_material->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, debug_navigation_enable_edge_lines_xray);
  285. }
  286. }
  287. bool NavigationServer3D::get_debug_navigation_enable_edge_lines_xray() const {
  288. return debug_navigation_enable_edge_lines_xray;
  289. }
  290. void NavigationServer3D::set_debug_navigation_enable_geometry_face_random_color(const bool p_value) {
  291. debug_navigation_enable_geometry_face_random_color = p_value;
  292. debug_dirty = true;
  293. call_deferred("_emit_navigation_debug_changed_signal");
  294. }
  295. bool NavigationServer3D::get_debug_navigation_enable_geometry_face_random_color() const {
  296. return debug_navigation_enable_geometry_face_random_color;
  297. }
  298. void NavigationServer3D::set_debug_enabled(bool p_enabled) {
  299. if (debug_enabled != p_enabled) {
  300. debug_dirty = true;
  301. }
  302. debug_enabled = p_enabled;
  303. if (debug_dirty) {
  304. call_deferred("_emit_navigation_debug_changed_signal");
  305. }
  306. }
  307. bool NavigationServer3D::get_debug_enabled() const {
  308. return debug_enabled;
  309. }
  310. #endif // DEBUG_ENABLED