2
0

nav_map_builder_3d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**************************************************************************/
  2. /* nav_map_builder_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. #ifndef _3D_DISABLED
  31. #include "nav_map_builder_3d.h"
  32. #include "../nav_link.h"
  33. #include "../nav_map.h"
  34. #include "../nav_region.h"
  35. #include "nav_map_iteration_3d.h"
  36. #include "nav_region_iteration_3d.h"
  37. gd::PointKey NavMapBuilder3D::get_point_key(const Vector3 &p_pos, const Vector3 &p_cell_size) {
  38. const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
  39. const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
  40. const int z = static_cast<int>(Math::floor(p_pos.z / p_cell_size.z));
  41. gd::PointKey p;
  42. p.key = 0;
  43. p.x = x;
  44. p.y = y;
  45. p.z = z;
  46. return p;
  47. }
  48. void NavMapBuilder3D::build_navmap_iteration(NavMapIterationBuild &r_build) {
  49. gd::PerformanceData &performance_data = r_build.performance_data;
  50. performance_data.pm_polygon_count = 0;
  51. performance_data.pm_edge_count = 0;
  52. performance_data.pm_edge_merge_count = 0;
  53. performance_data.pm_edge_connection_count = 0;
  54. performance_data.pm_edge_free_count = 0;
  55. _build_step_gather_region_polygons(r_build);
  56. _build_step_find_edge_connection_pairs(r_build);
  57. _build_step_merge_edge_connection_pairs(r_build);
  58. _build_step_edge_connection_margin_connections(r_build);
  59. _build_step_navlink_connections(r_build);
  60. _build_update_map_iteration(r_build);
  61. }
  62. void NavMapBuilder3D::_build_step_gather_region_polygons(NavMapIterationBuild &r_build) {
  63. gd::PerformanceData &performance_data = r_build.performance_data;
  64. NavMapIteration *map_iteration = r_build.map_iteration;
  65. LocalVector<NavRegionIteration> &regions = map_iteration->region_iterations;
  66. HashMap<uint32_t, LocalVector<gd::Edge::Connection>> &region_external_connections = map_iteration->external_region_connections;
  67. // Remove regions connections.
  68. region_external_connections.clear();
  69. for (const NavRegionIteration &region : regions) {
  70. region_external_connections[region.id] = LocalVector<gd::Edge::Connection>();
  71. }
  72. // Copy all region polygons in the map.
  73. int polygon_count = 0;
  74. for (NavRegionIteration &region : regions) {
  75. if (!region.get_enabled()) {
  76. continue;
  77. }
  78. LocalVector<gd::Polygon> &polygons_source = region.navmesh_polygons;
  79. for (uint32_t n = 0; n < polygons_source.size(); n++) {
  80. polygons_source[n].id = polygon_count;
  81. polygon_count++;
  82. }
  83. }
  84. performance_data.pm_polygon_count = polygon_count;
  85. r_build.polygon_count = polygon_count;
  86. }
  87. void NavMapBuilder3D::_build_step_find_edge_connection_pairs(NavMapIterationBuild &r_build) {
  88. gd::PerformanceData &performance_data = r_build.performance_data;
  89. NavMapIteration *map_iteration = r_build.map_iteration;
  90. int polygon_count = r_build.polygon_count;
  91. HashMap<gd::EdgeKey, gd::EdgeConnectionPair, gd::EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
  92. // Group all edges per key.
  93. connection_pairs_map.clear();
  94. connection_pairs_map.reserve(polygon_count);
  95. int free_edges_count = 0; // How many ConnectionPairs have only one Connection.
  96. for (NavRegionIteration &region : map_iteration->region_iterations) {
  97. if (!region.get_enabled()) {
  98. continue;
  99. }
  100. for (gd::Polygon &poly : region.navmesh_polygons) {
  101. for (uint32_t p = 0; p < poly.points.size(); p++) {
  102. const int next_point = (p + 1) % poly.points.size();
  103. const gd::EdgeKey ek(poly.points[p].key, poly.points[next_point].key);
  104. HashMap<gd::EdgeKey, gd::EdgeConnectionPair, gd::EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
  105. if (!pair_it) {
  106. pair_it = connection_pairs_map.insert(ek, gd::EdgeConnectionPair());
  107. performance_data.pm_edge_count += 1;
  108. ++free_edges_count;
  109. }
  110. gd::EdgeConnectionPair &pair = pair_it->value;
  111. if (pair.size < 2) {
  112. // Add the polygon/edge tuple to this key.
  113. gd::Edge::Connection new_connection;
  114. new_connection.polygon = &poly;
  115. new_connection.edge = p;
  116. new_connection.pathway_start = poly.points[p].pos;
  117. new_connection.pathway_end = poly.points[next_point].pos;
  118. pair.connections[pair.size] = new_connection;
  119. ++pair.size;
  120. if (pair.size == 2) {
  121. --free_edges_count;
  122. }
  123. } else {
  124. // The edge is already connected with another edge, skip.
  125. ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to merge a navigation mesh polygon edge with another already-merged edge. This is usually caused by crossing edges, overlapping polygons, or a mismatch of the NavigationMesh / NavigationPolygon baked 'cell_size' and navigation map 'cell_size'. If you're certain none of above is the case, change 'navigation/3d/merge_rasterizer_cell_scale' to 0.001.");
  126. }
  127. }
  128. }
  129. }
  130. r_build.free_edge_count = free_edges_count;
  131. }
  132. void NavMapBuilder3D::_build_step_merge_edge_connection_pairs(NavMapIterationBuild &r_build) {
  133. gd::PerformanceData &performance_data = r_build.performance_data;
  134. HashMap<gd::EdgeKey, gd::EdgeConnectionPair, gd::EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
  135. LocalVector<gd::Edge::Connection> &free_edges = r_build.iter_free_edges;
  136. int free_edges_count = r_build.free_edge_count;
  137. bool use_edge_connections = r_build.use_edge_connections;
  138. free_edges.clear();
  139. free_edges.reserve(free_edges_count);
  140. for (const KeyValue<gd::EdgeKey, gd::EdgeConnectionPair> &pair_it : connection_pairs_map) {
  141. const gd::EdgeConnectionPair &pair = pair_it.value;
  142. if (pair.size == 2) {
  143. // Connect edge that are shared in different polygons.
  144. const gd::Edge::Connection &c1 = pair.connections[0];
  145. const gd::Edge::Connection &c2 = pair.connections[1];
  146. c1.polygon->edges[c1.edge].connections.push_back(c2);
  147. c2.polygon->edges[c2.edge].connections.push_back(c1);
  148. // Note: The pathway_start/end are full for those connection and do not need to be modified.
  149. performance_data.pm_edge_merge_count += 1;
  150. } else {
  151. CRASH_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
  152. if (use_edge_connections && pair.connections[0].polygon->owner->get_use_edge_connections()) {
  153. free_edges.push_back(pair.connections[0]);
  154. }
  155. }
  156. }
  157. }
  158. void NavMapBuilder3D::_build_step_edge_connection_margin_connections(NavMapIterationBuild &r_build) {
  159. gd::PerformanceData &performance_data = r_build.performance_data;
  160. NavMapIteration *map_iteration = r_build.map_iteration;
  161. real_t edge_connection_margin = r_build.edge_connection_margin;
  162. LocalVector<gd::Edge::Connection> &free_edges = r_build.iter_free_edges;
  163. HashMap<uint32_t, LocalVector<gd::Edge::Connection>> &region_external_connections = map_iteration->external_region_connections;
  164. // Find the compatible near edges.
  165. //
  166. // Note:
  167. // Considering that the edges must be compatible (for obvious reasons)
  168. // to be connected, create new polygons to remove that small gap is
  169. // not really useful and would result in wasteful computation during
  170. // connection, integration and path finding.
  171. performance_data.pm_edge_free_count = free_edges.size();
  172. const real_t edge_connection_margin_squared = edge_connection_margin * edge_connection_margin;
  173. for (uint32_t i = 0; i < free_edges.size(); i++) {
  174. const gd::Edge::Connection &free_edge = free_edges[i];
  175. Vector3 edge_p1 = free_edge.polygon->points[free_edge.edge].pos;
  176. Vector3 edge_p2 = free_edge.polygon->points[(free_edge.edge + 1) % free_edge.polygon->points.size()].pos;
  177. for (uint32_t j = 0; j < free_edges.size(); j++) {
  178. const gd::Edge::Connection &other_edge = free_edges[j];
  179. if (i == j || free_edge.polygon->owner == other_edge.polygon->owner) {
  180. continue;
  181. }
  182. Vector3 other_edge_p1 = other_edge.polygon->points[other_edge.edge].pos;
  183. Vector3 other_edge_p2 = other_edge.polygon->points[(other_edge.edge + 1) % other_edge.polygon->points.size()].pos;
  184. // Compute the projection of the opposite edge on the current one
  185. Vector3 edge_vector = edge_p2 - edge_p1;
  186. real_t projected_p1_ratio = edge_vector.dot(other_edge_p1 - edge_p1) / (edge_vector.length_squared());
  187. real_t projected_p2_ratio = edge_vector.dot(other_edge_p2 - edge_p1) / (edge_vector.length_squared());
  188. if ((projected_p1_ratio < 0.0 && projected_p2_ratio < 0.0) || (projected_p1_ratio > 1.0 && projected_p2_ratio > 1.0)) {
  189. continue;
  190. }
  191. // Check if the two edges are close to each other enough and compute a pathway between the two regions.
  192. Vector3 self1 = edge_vector * CLAMP(projected_p1_ratio, 0.0, 1.0) + edge_p1;
  193. Vector3 other1;
  194. if (projected_p1_ratio >= 0.0 && projected_p1_ratio <= 1.0) {
  195. other1 = other_edge_p1;
  196. } else {
  197. other1 = other_edge_p1.lerp(other_edge_p2, (1.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
  198. }
  199. if (other1.distance_squared_to(self1) > edge_connection_margin_squared) {
  200. continue;
  201. }
  202. Vector3 self2 = edge_vector * CLAMP(projected_p2_ratio, 0.0, 1.0) + edge_p1;
  203. Vector3 other2;
  204. if (projected_p2_ratio >= 0.0 && projected_p2_ratio <= 1.0) {
  205. other2 = other_edge_p2;
  206. } else {
  207. other2 = other_edge_p1.lerp(other_edge_p2, (0.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
  208. }
  209. if (other2.distance_squared_to(self2) > edge_connection_margin_squared) {
  210. continue;
  211. }
  212. // The edges can now be connected.
  213. gd::Edge::Connection new_connection = other_edge;
  214. new_connection.pathway_start = (self1 + other1) / 2.0;
  215. new_connection.pathway_end = (self2 + other2) / 2.0;
  216. free_edge.polygon->edges[free_edge.edge].connections.push_back(new_connection);
  217. // Add the connection to the region_connection map.
  218. region_external_connections[(uint32_t)free_edge.polygon->owner->id].push_back(new_connection);
  219. performance_data.pm_edge_connection_count += 1;
  220. }
  221. }
  222. }
  223. void NavMapBuilder3D::_build_step_navlink_connections(NavMapIterationBuild &r_build) {
  224. NavMapIteration *map_iteration = r_build.map_iteration;
  225. real_t link_connection_radius = r_build.link_connection_radius;
  226. Vector3 merge_rasterizer_cell_size = r_build.merge_rasterizer_cell_size;
  227. LocalVector<gd::Polygon> &link_polygons = map_iteration->link_polygons;
  228. LocalVector<NavLinkIteration> &links = map_iteration->link_iterations;
  229. int polygon_count = r_build.polygon_count;
  230. real_t link_connection_radius_sqr = link_connection_radius * link_connection_radius;
  231. uint32_t link_poly_idx = 0;
  232. link_polygons.resize(links.size());
  233. // Search for polygons within range of a nav link.
  234. for (const NavLinkIteration &link : links) {
  235. if (!link.get_enabled()) {
  236. continue;
  237. }
  238. const Vector3 link_start_pos = link.get_start_position();
  239. const Vector3 link_end_pos = link.get_end_position();
  240. gd::Polygon *closest_start_polygon = nullptr;
  241. real_t closest_start_sqr_dist = link_connection_radius_sqr;
  242. Vector3 closest_start_point;
  243. gd::Polygon *closest_end_polygon = nullptr;
  244. real_t closest_end_sqr_dist = link_connection_radius_sqr;
  245. Vector3 closest_end_point;
  246. for (NavRegionIteration &region : map_iteration->region_iterations) {
  247. if (!region.get_enabled()) {
  248. continue;
  249. }
  250. AABB region_bounds = region.get_bounds().grow(link_connection_radius);
  251. if (!region_bounds.has_point(link_start_pos) && !region_bounds.has_point(link_end_pos)) {
  252. continue;
  253. }
  254. for (gd::Polygon &polyon : region.navmesh_polygons) {
  255. //for (gd::Polygon &polyon : polygons) {
  256. for (uint32_t point_id = 2; point_id < polyon.points.size(); point_id += 1) {
  257. const Face3 face(polyon.points[0].pos, polyon.points[point_id - 1].pos, polyon.points[point_id].pos);
  258. {
  259. const Vector3 start_point = face.get_closest_point_to(link_start_pos);
  260. const real_t sqr_dist = start_point.distance_squared_to(link_start_pos);
  261. // Pick the polygon that is within our radius and is closer than anything we've seen yet.
  262. if (sqr_dist < closest_start_sqr_dist) {
  263. closest_start_sqr_dist = sqr_dist;
  264. closest_start_point = start_point;
  265. closest_start_polygon = &polyon;
  266. }
  267. }
  268. {
  269. const Vector3 end_point = face.get_closest_point_to(link_end_pos);
  270. const real_t sqr_dist = end_point.distance_squared_to(link_end_pos);
  271. // Pick the polygon that is within our radius and is closer than anything we've seen yet.
  272. if (sqr_dist < closest_end_sqr_dist) {
  273. closest_end_sqr_dist = sqr_dist;
  274. closest_end_point = end_point;
  275. closest_end_polygon = &polyon;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. // If we have both a start and end point, then create a synthetic polygon to route through.
  282. if (closest_start_polygon && closest_end_polygon) {
  283. gd::Polygon &new_polygon = link_polygons[link_poly_idx++];
  284. new_polygon.id = polygon_count++;
  285. new_polygon.owner = &link;
  286. new_polygon.edges.clear();
  287. new_polygon.edges.resize(4);
  288. new_polygon.points.resize(4);
  289. // Build a set of vertices that create a thin polygon going from the start to the end point.
  290. new_polygon.points[0] = { closest_start_point, get_point_key(closest_start_point, merge_rasterizer_cell_size) };
  291. new_polygon.points[1] = { closest_start_point, get_point_key(closest_start_point, merge_rasterizer_cell_size) };
  292. new_polygon.points[2] = { closest_end_point, get_point_key(closest_end_point, merge_rasterizer_cell_size) };
  293. new_polygon.points[3] = { closest_end_point, get_point_key(closest_end_point, merge_rasterizer_cell_size) };
  294. // Setup connections to go forward in the link.
  295. {
  296. gd::Edge::Connection entry_connection;
  297. entry_connection.polygon = &new_polygon;
  298. entry_connection.edge = -1;
  299. entry_connection.pathway_start = new_polygon.points[0].pos;
  300. entry_connection.pathway_end = new_polygon.points[1].pos;
  301. closest_start_polygon->edges[0].connections.push_back(entry_connection);
  302. gd::Edge::Connection exit_connection;
  303. exit_connection.polygon = closest_end_polygon;
  304. exit_connection.edge = -1;
  305. exit_connection.pathway_start = new_polygon.points[2].pos;
  306. exit_connection.pathway_end = new_polygon.points[3].pos;
  307. new_polygon.edges[2].connections.push_back(exit_connection);
  308. }
  309. // If the link is bi-directional, create connections from the end to the start.
  310. if (link.is_bidirectional()) {
  311. gd::Edge::Connection entry_connection;
  312. entry_connection.polygon = &new_polygon;
  313. entry_connection.edge = -1;
  314. entry_connection.pathway_start = new_polygon.points[2].pos;
  315. entry_connection.pathway_end = new_polygon.points[3].pos;
  316. closest_end_polygon->edges[0].connections.push_back(entry_connection);
  317. gd::Edge::Connection exit_connection;
  318. exit_connection.polygon = closest_start_polygon;
  319. exit_connection.edge = -1;
  320. exit_connection.pathway_start = new_polygon.points[0].pos;
  321. exit_connection.pathway_end = new_polygon.points[1].pos;
  322. new_polygon.edges[0].connections.push_back(exit_connection);
  323. }
  324. }
  325. }
  326. }
  327. void NavMapBuilder3D::_build_update_map_iteration(NavMapIterationBuild &r_build) {
  328. NavMapIteration *map_iteration = r_build.map_iteration;
  329. LocalVector<gd::Polygon> &link_polygons = map_iteration->link_polygons;
  330. map_iteration->navmesh_polygon_count = r_build.polygon_count;
  331. map_iteration->link_polygon_count = link_polygons.size();
  332. map_iteration->path_query_slots_mutex.lock();
  333. for (NavMeshQueries3D::PathQuerySlot &p_path_query_slot : map_iteration->path_query_slots) {
  334. p_path_query_slot.traversable_polys.clear();
  335. p_path_query_slot.traversable_polys.reserve(map_iteration->navmesh_polygon_count * 0.25);
  336. p_path_query_slot.path_corridor.clear();
  337. p_path_query_slot.path_corridor.resize(map_iteration->navmesh_polygon_count + map_iteration->link_polygon_count);
  338. }
  339. map_iteration->path_query_slots_mutex.unlock();
  340. }
  341. #endif // _3D_DISABLED