navigation_server_3d.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /**************************************************************************/
  2. /* navigation_server_3d.h */
  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. #ifndef NAVIGATION_SERVER_3D_H
  31. #define NAVIGATION_SERVER_3D_H
  32. #include "core/object/class_db.h"
  33. #include "core/templates/rid.h"
  34. #include "scene/3d/navigation_region_3d.h"
  35. #include "servers/navigation/navigation_path_query_parameters_3d.h"
  36. #include "servers/navigation/navigation_path_query_result_3d.h"
  37. /// This server uses the concept of internal mutability.
  38. /// All the constant functions can be called in multithread because internally
  39. /// the server takes care to schedule the functions access.
  40. ///
  41. /// Note: All the `set` functions are commands executed during the `sync` phase,
  42. /// don't expect that a change is immediately propagated.
  43. class NavigationServer3D : public Object {
  44. GDCLASS(NavigationServer3D, Object);
  45. static NavigationServer3D *singleton;
  46. protected:
  47. static void _bind_methods();
  48. public:
  49. /// Thread safe, can be used across many threads.
  50. static NavigationServer3D *get_singleton();
  51. virtual TypedArray<RID> get_maps() const = 0;
  52. /// Create a new map.
  53. virtual RID map_create() = 0;
  54. /// Set map active.
  55. virtual void map_set_active(RID p_map, bool p_active) = 0;
  56. /// Returns true if the map is active.
  57. virtual bool map_is_active(RID p_map) const = 0;
  58. /// Set the map UP direction.
  59. virtual void map_set_up(RID p_map, Vector3 p_up) = 0;
  60. /// Returns the map UP direction.
  61. virtual Vector3 map_get_up(RID p_map) const = 0;
  62. /// Set the map cell size used to weld the navigation mesh polygons.
  63. virtual void map_set_cell_size(RID p_map, real_t p_cell_size) = 0;
  64. /// Returns the map cell size.
  65. virtual real_t map_get_cell_size(RID p_map) const = 0;
  66. /// Set the map edge connection margin used to weld the compatible region edges.
  67. virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) = 0;
  68. /// Returns the edge connection margin of this map.
  69. virtual real_t map_get_edge_connection_margin(RID p_map) const = 0;
  70. /// Set the map link connection radius used to attach links to the nav mesh.
  71. virtual void map_set_link_connection_radius(RID p_map, real_t p_connection_radius) = 0;
  72. /// Returns the link connection radius of this map.
  73. virtual real_t map_get_link_connection_radius(RID p_map) const = 0;
  74. /// Returns the navigation path to reach the destination from the origin.
  75. virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const = 0;
  76. virtual Vector3 map_get_closest_point_to_segment(RID p_map, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision = false) const = 0;
  77. virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const = 0;
  78. virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const = 0;
  79. virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const = 0;
  80. virtual TypedArray<RID> map_get_links(RID p_map) const = 0;
  81. virtual TypedArray<RID> map_get_regions(RID p_map) const = 0;
  82. virtual TypedArray<RID> map_get_agents(RID p_map) const = 0;
  83. virtual void map_force_update(RID p_map) = 0;
  84. /// Creates a new region.
  85. virtual RID region_create() = 0;
  86. /// Set the enter_cost of a region
  87. virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) = 0;
  88. virtual real_t region_get_enter_cost(RID p_region) const = 0;
  89. /// Set the travel_cost of a region
  90. virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) = 0;
  91. virtual real_t region_get_travel_cost(RID p_region) const = 0;
  92. /// Set the node which manages this region.
  93. virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) = 0;
  94. virtual ObjectID region_get_owner_id(RID p_region) const = 0;
  95. virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const = 0;
  96. /// Set the map of this region.
  97. virtual void region_set_map(RID p_region, RID p_map) = 0;
  98. virtual RID region_get_map(RID p_region) const = 0;
  99. /// Set the region's layers
  100. virtual void region_set_navigation_layers(RID p_region, uint32_t p_navigation_layers) = 0;
  101. virtual uint32_t region_get_navigation_layers(RID p_region) const = 0;
  102. /// Set the global transformation of this region.
  103. virtual void region_set_transform(RID p_region, Transform3D p_transform) = 0;
  104. /// Set the navigation mesh of this region.
  105. virtual void region_set_navigation_mesh(RID p_region, Ref<NavigationMesh> p_navigation_mesh) = 0;
  106. /// Bake the navigation mesh.
  107. virtual void region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) = 0;
  108. /// Get a list of a region's connection to other regions.
  109. virtual int region_get_connections_count(RID p_region) const = 0;
  110. virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const = 0;
  111. virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const = 0;
  112. /// Creates a new link between positions in the nav map.
  113. virtual RID link_create() = 0;
  114. /// Set the map of this link.
  115. virtual void link_set_map(RID p_link, RID p_map) = 0;
  116. virtual RID link_get_map(RID p_link) const = 0;
  117. /// Set whether this link travels in both directions.
  118. virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) = 0;
  119. virtual bool link_is_bidirectional(RID p_link) const = 0;
  120. /// Set the link's layers.
  121. virtual void link_set_navigation_layers(RID p_link, uint32_t p_navigation_layers) = 0;
  122. virtual uint32_t link_get_navigation_layers(RID p_link) const = 0;
  123. /// Set the start position of the link.
  124. virtual void link_set_start_position(RID p_link, Vector3 p_position) = 0;
  125. virtual Vector3 link_get_start_position(RID p_link) const = 0;
  126. /// Set the end position of the link.
  127. virtual void link_set_end_position(RID p_link, Vector3 p_position) = 0;
  128. virtual Vector3 link_get_end_position(RID p_link) const = 0;
  129. /// Set the enter cost of the link.
  130. virtual void link_set_enter_cost(RID p_link, real_t p_enter_cost) = 0;
  131. virtual real_t link_get_enter_cost(RID p_link) const = 0;
  132. /// Set the travel cost of the link.
  133. virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) = 0;
  134. virtual real_t link_get_travel_cost(RID p_link) const = 0;
  135. /// Set the node which manages this link.
  136. virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) = 0;
  137. virtual ObjectID link_get_owner_id(RID p_link) const = 0;
  138. /// Creates the agent.
  139. virtual RID agent_create() = 0;
  140. /// Put the agent in the map.
  141. virtual void agent_set_map(RID p_agent, RID p_map) = 0;
  142. virtual RID agent_get_map(RID p_agent) const = 0;
  143. /// The maximum distance (center point to
  144. /// center point) to other agents this agent
  145. /// takes into account in the navigation. The
  146. /// larger this number, the longer the running
  147. /// time of the simulation. If the number is too
  148. /// low, the simulation will not be safe.
  149. /// Must be non-negative.
  150. virtual void agent_set_neighbor_distance(RID p_agent, real_t p_distance) = 0;
  151. /// The maximum number of other agents this
  152. /// agent takes into account in the navigation.
  153. /// The larger this number, the longer the
  154. /// running time of the simulation. If the
  155. /// number is too low, the simulation will not
  156. /// be safe.
  157. virtual void agent_set_max_neighbors(RID p_agent, int p_count) = 0;
  158. /// The minimal amount of time for which this
  159. /// agent's velocities that are computed by the
  160. /// simulation are safe with respect to other
  161. /// agents. The larger this number, the sooner
  162. /// this agent will respond to the presence of
  163. /// other agents, but the less freedom this
  164. /// agent has in choosing its velocities.
  165. /// Must be positive.
  166. virtual void agent_set_time_horizon(RID p_agent, real_t p_time) = 0;
  167. /// The radius of this agent.
  168. /// Must be non-negative.
  169. virtual void agent_set_radius(RID p_agent, real_t p_radius) = 0;
  170. /// The maximum speed of this agent.
  171. /// Must be non-negative.
  172. virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed) = 0;
  173. /// Current velocity of the agent
  174. virtual void agent_set_velocity(RID p_agent, Vector3 p_velocity) = 0;
  175. /// The new target velocity.
  176. virtual void agent_set_target_velocity(RID p_agent, Vector3 p_velocity) = 0;
  177. /// Position of the agent in world space.
  178. virtual void agent_set_position(RID p_agent, Vector3 p_position) = 0;
  179. /// Agent ignore the Y axis and avoid collisions by moving only on the horizontal plane
  180. virtual void agent_set_ignore_y(RID p_agent, bool p_ignore) = 0;
  181. /// Returns true if the map got changed the previous frame.
  182. virtual bool agent_is_map_changed(RID p_agent) const = 0;
  183. /// Callback called at the end of the RVO process
  184. virtual void agent_set_callback(RID p_agent, Callable p_callback) = 0;
  185. /// Destroy the `RID`
  186. virtual void free(RID p_object) = 0;
  187. /// Control activation of this server.
  188. virtual void set_active(bool p_active) = 0;
  189. /// Process the collision avoidance agents.
  190. /// The result of this process is needed by the physics server,
  191. /// so this must be called in the main thread.
  192. /// Note: This function is not thread safe.
  193. virtual void process(real_t delta_time) = 0;
  194. /// Returns a customized navigation path using a query parameters object
  195. virtual void query_path(const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result) const;
  196. virtual NavigationUtilities::PathQueryResult _query_path(const NavigationUtilities::PathQueryParameters &p_parameters) const = 0;
  197. NavigationServer3D();
  198. ~NavigationServer3D() override;
  199. enum ProcessInfo {
  200. INFO_ACTIVE_MAPS,
  201. INFO_REGION_COUNT,
  202. INFO_AGENT_COUNT,
  203. INFO_LINK_COUNT,
  204. INFO_POLYGON_COUNT,
  205. INFO_EDGE_COUNT,
  206. INFO_EDGE_MERGE_COUNT,
  207. INFO_EDGE_CONNECTION_COUNT,
  208. INFO_EDGE_FREE_COUNT,
  209. };
  210. virtual int get_process_info(ProcessInfo p_info) const = 0;
  211. void set_debug_enabled(bool p_enabled);
  212. bool get_debug_enabled() const;
  213. private:
  214. bool debug_enabled = false;
  215. #ifdef DEBUG_ENABLED
  216. bool debug_dirty = true;
  217. void _emit_navigation_debug_changed_signal();
  218. Color debug_navigation_edge_connection_color = Color(1.0, 0.0, 1.0, 1.0);
  219. Color debug_navigation_geometry_edge_color = Color(0.5, 1.0, 1.0, 1.0);
  220. Color debug_navigation_geometry_face_color = Color(0.5, 1.0, 1.0, 0.4);
  221. Color debug_navigation_geometry_edge_disabled_color = Color(0.5, 0.5, 0.5, 1.0);
  222. Color debug_navigation_geometry_face_disabled_color = Color(0.5, 0.5, 0.5, 0.4);
  223. Color debug_navigation_link_connection_color = Color(1.0, 0.5, 1.0, 1.0);
  224. Color debug_navigation_link_connection_disabled_color = Color(0.5, 0.5, 0.5, 1.0);
  225. Color debug_navigation_agent_path_color = Color(1.0, 0.0, 0.0, 1.0);
  226. float debug_navigation_agent_path_point_size = 4.0;
  227. bool debug_navigation_enable_edge_connections = true;
  228. bool debug_navigation_enable_edge_connections_xray = true;
  229. bool debug_navigation_enable_edge_lines = true;
  230. bool debug_navigation_enable_edge_lines_xray = true;
  231. bool debug_navigation_enable_geometry_face_random_color = true;
  232. bool debug_navigation_enable_link_connections = true;
  233. bool debug_navigation_enable_link_connections_xray = true;
  234. bool debug_navigation_enable_agent_paths = true;
  235. bool debug_navigation_enable_agent_paths_xray = true;
  236. Ref<StandardMaterial3D> debug_navigation_geometry_edge_material;
  237. Ref<StandardMaterial3D> debug_navigation_geometry_face_material;
  238. Ref<StandardMaterial3D> debug_navigation_geometry_edge_disabled_material;
  239. Ref<StandardMaterial3D> debug_navigation_geometry_face_disabled_material;
  240. Ref<StandardMaterial3D> debug_navigation_edge_connections_material;
  241. Ref<StandardMaterial3D> debug_navigation_link_connections_material;
  242. Ref<StandardMaterial3D> debug_navigation_link_connections_disabled_material;
  243. Ref<StandardMaterial3D> debug_navigation_agent_path_line_material;
  244. Ref<StandardMaterial3D> debug_navigation_agent_path_point_material;
  245. public:
  246. void set_debug_navigation_edge_connection_color(const Color &p_color);
  247. Color get_debug_navigation_edge_connection_color() const;
  248. void set_debug_navigation_geometry_edge_color(const Color &p_color);
  249. Color get_debug_navigation_geometry_edge_color() const;
  250. void set_debug_navigation_geometry_face_color(const Color &p_color);
  251. Color get_debug_navigation_geometry_face_color() const;
  252. void set_debug_navigation_geometry_edge_disabled_color(const Color &p_color);
  253. Color get_debug_navigation_geometry_edge_disabled_color() const;
  254. void set_debug_navigation_geometry_face_disabled_color(const Color &p_color);
  255. Color get_debug_navigation_geometry_face_disabled_color() const;
  256. void set_debug_navigation_link_connection_color(const Color &p_color);
  257. Color get_debug_navigation_link_connection_color() const;
  258. void set_debug_navigation_link_connection_disabled_color(const Color &p_color);
  259. Color get_debug_navigation_link_connection_disabled_color() const;
  260. void set_debug_navigation_agent_path_color(const Color &p_color);
  261. Color get_debug_navigation_agent_path_color() const;
  262. void set_debug_navigation_enable_edge_connections(const bool p_value);
  263. bool get_debug_navigation_enable_edge_connections() const;
  264. void set_debug_navigation_enable_edge_connections_xray(const bool p_value);
  265. bool get_debug_navigation_enable_edge_connections_xray() const;
  266. void set_debug_navigation_enable_edge_lines(const bool p_value);
  267. bool get_debug_navigation_enable_edge_lines() const;
  268. void set_debug_navigation_enable_edge_lines_xray(const bool p_value);
  269. bool get_debug_navigation_enable_edge_lines_xray() const;
  270. void set_debug_navigation_enable_geometry_face_random_color(const bool p_value);
  271. bool get_debug_navigation_enable_geometry_face_random_color() const;
  272. void set_debug_navigation_enable_link_connections(const bool p_value);
  273. bool get_debug_navigation_enable_link_connections() const;
  274. void set_debug_navigation_enable_link_connections_xray(const bool p_value);
  275. bool get_debug_navigation_enable_link_connections_xray() const;
  276. void set_debug_navigation_enable_agent_paths(const bool p_value);
  277. bool get_debug_navigation_enable_agent_paths() const;
  278. void set_debug_navigation_enable_agent_paths_xray(const bool p_value);
  279. bool get_debug_navigation_enable_agent_paths_xray() const;
  280. void set_debug_navigation_agent_path_point_size(float p_point_size);
  281. float get_debug_navigation_agent_path_point_size() const;
  282. Ref<StandardMaterial3D> get_debug_navigation_geometry_face_material();
  283. Ref<StandardMaterial3D> get_debug_navigation_geometry_edge_material();
  284. Ref<StandardMaterial3D> get_debug_navigation_geometry_face_disabled_material();
  285. Ref<StandardMaterial3D> get_debug_navigation_geometry_edge_disabled_material();
  286. Ref<StandardMaterial3D> get_debug_navigation_edge_connections_material();
  287. Ref<StandardMaterial3D> get_debug_navigation_link_connections_material();
  288. Ref<StandardMaterial3D> get_debug_navigation_link_connections_disabled_material();
  289. Ref<StandardMaterial3D> get_debug_navigation_agent_path_line_material();
  290. Ref<StandardMaterial3D> get_debug_navigation_agent_path_point_material();
  291. #endif // DEBUG_ENABLED
  292. };
  293. typedef NavigationServer3D *(*NavigationServer3DCallback)();
  294. /// Manager used for the server singleton registration
  295. class NavigationServer3DManager {
  296. static NavigationServer3DCallback create_callback;
  297. public:
  298. static void set_default_server(NavigationServer3DCallback p_callback);
  299. static NavigationServer3D *new_default_server();
  300. };
  301. VARIANT_ENUM_CAST(NavigationServer3D::ProcessInfo);
  302. #endif // NAVIGATION_SERVER_3D_H