Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_3d/3d/nav_map_builder_3d.cpp
11353 views
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
31
#include "nav_map_builder_3d.h"
32
33
#include "../nav_link_3d.h"
34
#include "../nav_map_3d.h"
35
#include "../nav_region_3d.h"
36
#include "nav_map_iteration_3d.h"
37
#include "nav_region_iteration_3d.h"
38
39
#include "core/config/project_settings.h"
40
41
using namespace Nav3D;
42
43
PointKey NavMapBuilder3D::get_point_key(const Vector3 &p_pos, const Vector3 &p_cell_size) {
44
const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
45
const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
46
const int z = static_cast<int>(Math::floor(p_pos.z / p_cell_size.z));
47
48
PointKey p;
49
p.key = 0;
50
p.x = x;
51
p.y = y;
52
p.z = z;
53
return p;
54
}
55
56
void NavMapBuilder3D::build_navmap_iteration(NavMapIterationBuild3D &r_build) {
57
PerformanceData &performance_data = r_build.performance_data;
58
59
performance_data.pm_polygon_count = 0;
60
performance_data.pm_edge_count = 0;
61
performance_data.pm_edge_merge_count = 0;
62
performance_data.pm_edge_connection_count = 0;
63
performance_data.pm_edge_free_count = 0;
64
65
_build_step_gather_region_polygons(r_build);
66
67
_build_step_find_edge_connection_pairs(r_build);
68
69
_build_step_merge_edge_connection_pairs(r_build);
70
71
_build_step_edge_connection_margin_connections(r_build);
72
73
_build_step_navlink_connections(r_build);
74
75
_build_update_map_iteration(r_build);
76
}
77
78
void NavMapBuilder3D::_build_step_gather_region_polygons(NavMapIterationBuild3D &r_build) {
79
PerformanceData &performance_data = r_build.performance_data;
80
NavMapIteration3D *map_iteration = r_build.map_iteration;
81
82
const LocalVector<Ref<NavRegionIteration3D>> &regions = map_iteration->region_iterations;
83
HashMap<const NavBaseIteration3D *, LocalVector<Connection>> &region_external_connections = map_iteration->external_region_connections;
84
85
map_iteration->navbases_polygons_external_connections.clear();
86
87
// Remove regions connections.
88
region_external_connections.clear();
89
90
// Copy all region polygons in the map.
91
int polygon_count = 0;
92
for (const Ref<NavRegionIteration3D> &region : regions) {
93
const uint32_t polygons_size = region->navmesh_polygons.size();
94
polygon_count += polygons_size;
95
96
region_external_connections[region.ptr()] = LocalVector<Connection>();
97
map_iteration->navbases_polygons_external_connections[region.ptr()] = LocalVector<LocalVector<Connection>>();
98
map_iteration->navbases_polygons_external_connections[region.ptr()].resize(polygons_size);
99
}
100
101
performance_data.pm_polygon_count = polygon_count;
102
r_build.polygon_count = polygon_count;
103
}
104
105
void NavMapBuilder3D::_build_step_find_edge_connection_pairs(NavMapIterationBuild3D &r_build) {
106
PerformanceData &performance_data = r_build.performance_data;
107
NavMapIteration3D *map_iteration = r_build.map_iteration;
108
int polygon_count = r_build.polygon_count;
109
110
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
111
112
// Group all edges per key.
113
connection_pairs_map.clear();
114
connection_pairs_map.reserve(polygon_count);
115
int free_edges_count = 0; // How many ConnectionPairs have only one Connection.
116
int edge_merge_error_count = 0;
117
118
for (const Ref<NavRegionIteration3D> &region : map_iteration->region_iterations) {
119
for (const ConnectableEdge &connectable_edge : region->get_external_edges()) {
120
const EdgeKey &ek = connectable_edge.ek;
121
122
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
123
if (!pair_it) {
124
pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());
125
performance_data.pm_edge_count += 1;
126
++free_edges_count;
127
}
128
EdgeConnectionPair &pair = pair_it->value;
129
if (pair.size < 2) {
130
// Add the polygon/edge tuple to this key.
131
Connection new_connection;
132
new_connection.polygon = &region->navmesh_polygons[connectable_edge.polygon_index];
133
new_connection.edge = connectable_edge.edge;
134
new_connection.pathway_start = connectable_edge.pathway_start;
135
new_connection.pathway_end = connectable_edge.pathway_end;
136
137
pair.connections[pair.size] = new_connection;
138
++pair.size;
139
if (pair.size == 2) {
140
--free_edges_count;
141
}
142
143
} else {
144
// The edge is already connected with another edge, skip.
145
edge_merge_error_count++;
146
}
147
}
148
}
149
150
if (edge_merge_error_count > 0 && GLOBAL_GET_CACHED(bool, "navigation/3d/warnings/navmesh_edge_merge_errors")) {
151
WARN_PRINT("Navigation map synchronization had " + itos(edge_merge_error_count) + " edge error(s).\nMore than 2 edges tried to occupy the same map rasterization space.\nThis causes a logical error in the navigation mesh geometry and is commonly caused by overlap or too densely placed edges.\nConsider baking with a higher 'cell_size', greater geometry margin, and less detailed bake objects to cause fewer edges.\nConsider lowering the 'navigation/3d/merge_rasterizer_cell_scale' in the project settings.\nThis warning can be toggled under 'navigation/3d/warnings/navmesh_edge_merge_errors' in the project settings.");
152
}
153
154
r_build.free_edge_count = free_edges_count;
155
}
156
157
void NavMapBuilder3D::_build_step_merge_edge_connection_pairs(NavMapIterationBuild3D &r_build) {
158
PerformanceData &performance_data = r_build.performance_data;
159
160
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
161
LocalVector<Connection> &free_edges = r_build.iter_free_edges;
162
int free_edges_count = r_build.free_edge_count;
163
bool use_edge_connections = r_build.use_edge_connections;
164
165
free_edges.clear();
166
free_edges.reserve(free_edges_count);
167
168
NavMapIteration3D *map_iteration = r_build.map_iteration;
169
170
HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
171
172
for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {
173
const EdgeConnectionPair &pair = pair_it.value;
174
if (pair.size == 2) {
175
// Connect edge that are shared in different polygons.
176
const Connection &c1 = pair.connections[0];
177
const Connection &c2 = pair.connections[1];
178
179
navbases_polygons_external_connections[c1.polygon->owner][c1.polygon->id].push_back(c2);
180
navbases_polygons_external_connections[c2.polygon->owner][c2.polygon->id].push_back(c1);
181
performance_data.pm_edge_connection_count += 1;
182
183
} else {
184
CRASH_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
185
if (use_edge_connections && pair.connections[0].polygon->owner->get_use_edge_connections()) {
186
free_edges.push_back(pair.connections[0]);
187
}
188
}
189
}
190
}
191
192
void NavMapBuilder3D::_build_step_edge_connection_margin_connections(NavMapIterationBuild3D &r_build) {
193
PerformanceData &performance_data = r_build.performance_data;
194
NavMapIteration3D *map_iteration = r_build.map_iteration;
195
196
real_t edge_connection_margin = r_build.edge_connection_margin;
197
198
LocalVector<Connection> &free_edges = r_build.iter_free_edges;
199
HashMap<const NavBaseIteration3D *, LocalVector<Connection>> &region_external_connections = map_iteration->external_region_connections;
200
201
HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
202
203
// Find the compatible near edges.
204
//
205
// Note:
206
// Considering that the edges must be compatible (for obvious reasons)
207
// to be connected, create new polygons to remove that small gap is
208
// not really useful and would result in wasteful computation during
209
// connection, integration and path finding.
210
performance_data.pm_edge_free_count = free_edges.size();
211
212
const real_t edge_connection_margin_squared = edge_connection_margin * edge_connection_margin;
213
214
for (uint32_t i = 0; i < free_edges.size(); i++) {
215
const Connection &free_edge = free_edges[i];
216
const Vector3 &edge_p1 = free_edge.pathway_start;
217
const Vector3 &edge_p2 = free_edge.pathway_end;
218
219
for (uint32_t j = 0; j < free_edges.size(); j++) {
220
const Connection &other_edge = free_edges[j];
221
if (i == j || free_edge.polygon->owner == other_edge.polygon->owner) {
222
continue;
223
}
224
225
const Vector3 &other_edge_p1 = other_edge.pathway_start;
226
const Vector3 &other_edge_p2 = other_edge.pathway_end;
227
228
// Compute the projection of the opposite edge on the current one
229
Vector3 edge_vector = edge_p2 - edge_p1;
230
real_t projected_p1_ratio = edge_vector.dot(other_edge_p1 - edge_p1) / (edge_vector.length_squared());
231
real_t projected_p2_ratio = edge_vector.dot(other_edge_p2 - edge_p1) / (edge_vector.length_squared());
232
if ((projected_p1_ratio < 0.0 && projected_p2_ratio < 0.0) || (projected_p1_ratio > 1.0 && projected_p2_ratio > 1.0)) {
233
continue;
234
}
235
236
// Check if the two edges are close to each other enough and compute a pathway between the two regions.
237
Vector3 self1 = edge_vector * CLAMP(projected_p1_ratio, 0.0, 1.0) + edge_p1;
238
Vector3 other1;
239
if (projected_p1_ratio >= 0.0 && projected_p1_ratio <= 1.0) {
240
other1 = other_edge_p1;
241
} else {
242
other1 = other_edge_p1.lerp(other_edge_p2, (1.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
243
}
244
if (other1.distance_squared_to(self1) > edge_connection_margin_squared) {
245
continue;
246
}
247
248
Vector3 self2 = edge_vector * CLAMP(projected_p2_ratio, 0.0, 1.0) + edge_p1;
249
Vector3 other2;
250
if (projected_p2_ratio >= 0.0 && projected_p2_ratio <= 1.0) {
251
other2 = other_edge_p2;
252
} else {
253
other2 = other_edge_p1.lerp(other_edge_p2, (0.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
254
}
255
if (other2.distance_squared_to(self2) > edge_connection_margin_squared) {
256
continue;
257
}
258
259
// The edges can now be connected.
260
Connection new_connection = other_edge;
261
new_connection.pathway_start = (self1 + other1) / 2.0;
262
new_connection.pathway_end = (self2 + other2) / 2.0;
263
//free_edge.polygon->connections.push_back(new_connection);
264
265
// Add the connection to the region_connection map.
266
region_external_connections[free_edge.polygon->owner].push_back(new_connection);
267
navbases_polygons_external_connections[free_edge.polygon->owner][free_edge.polygon->id].push_back(new_connection);
268
performance_data.pm_edge_connection_count += 1;
269
}
270
}
271
}
272
273
void NavMapBuilder3D::_build_step_navlink_connections(NavMapIterationBuild3D &r_build) {
274
NavMapIteration3D *map_iteration = r_build.map_iteration;
275
276
real_t link_connection_radius = r_build.link_connection_radius;
277
278
const LocalVector<Ref<NavLinkIteration3D>> &links = map_iteration->link_iterations;
279
280
int polygon_count = r_build.polygon_count;
281
282
real_t link_connection_radius_sqr = link_connection_radius * link_connection_radius;
283
284
HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
285
LocalVector<Nav3D::Polygon> &navlink_polygons = map_iteration->navlink_polygons;
286
navlink_polygons.clear();
287
navlink_polygons.resize(links.size());
288
uint32_t navlink_index = 0;
289
290
// Search for polygons within range of a nav link.
291
for (const Ref<NavLinkIteration3D> &link : links) {
292
polygon_count++;
293
Polygon &new_polygon = navlink_polygons[navlink_index++];
294
295
new_polygon.id = 0;
296
new_polygon.owner = link.ptr();
297
298
const Vector3 link_start_pos = link->get_start_position();
299
const Vector3 link_end_pos = link->get_end_position();
300
301
Polygon *closest_start_polygon = nullptr;
302
real_t closest_start_sqr_dist = link_connection_radius_sqr;
303
Vector3 closest_start_point;
304
305
Polygon *closest_end_polygon = nullptr;
306
real_t closest_end_sqr_dist = link_connection_radius_sqr;
307
Vector3 closest_end_point;
308
309
for (const Ref<NavRegionIteration3D> &region : map_iteration->region_iterations) {
310
AABB region_bounds = region->get_bounds().grow(link_connection_radius);
311
if (!region_bounds.has_point(link_start_pos) && !region_bounds.has_point(link_end_pos)) {
312
continue;
313
}
314
315
for (Polygon &polyon : region->navmesh_polygons) {
316
for (uint32_t point_id = 2; point_id < polyon.vertices.size(); point_id += 1) {
317
const Face3 face(polyon.vertices[0], polyon.vertices[point_id - 1], polyon.vertices[point_id]);
318
319
{
320
const Vector3 start_point = face.get_closest_point_to(link_start_pos);
321
const real_t sqr_dist = start_point.distance_squared_to(link_start_pos);
322
323
// Pick the polygon that is within our radius and is closer than anything we've seen yet.
324
if (sqr_dist < closest_start_sqr_dist) {
325
closest_start_sqr_dist = sqr_dist;
326
closest_start_point = start_point;
327
closest_start_polygon = &polyon;
328
}
329
}
330
331
{
332
const Vector3 end_point = face.get_closest_point_to(link_end_pos);
333
const real_t sqr_dist = end_point.distance_squared_to(link_end_pos);
334
335
// Pick the polygon that is within our radius and is closer than anything we've seen yet.
336
if (sqr_dist < closest_end_sqr_dist) {
337
closest_end_sqr_dist = sqr_dist;
338
closest_end_point = end_point;
339
closest_end_polygon = &polyon;
340
}
341
}
342
}
343
}
344
}
345
346
// If we have both a start and end point, then create a synthetic polygon to route through.
347
if (closest_start_polygon && closest_end_polygon) {
348
new_polygon.vertices.resize(4);
349
350
// Build a set of vertices that create a thin polygon going from the start to the end point.
351
new_polygon.vertices[0] = closest_start_point;
352
new_polygon.vertices[1] = closest_start_point;
353
new_polygon.vertices[2] = closest_end_point;
354
new_polygon.vertices[3] = closest_end_point;
355
356
// Setup connections to go forward in the link.
357
{
358
Connection entry_connection;
359
entry_connection.polygon = &new_polygon;
360
entry_connection.edge = -1;
361
entry_connection.pathway_start = new_polygon.vertices[0];
362
entry_connection.pathway_end = new_polygon.vertices[1];
363
navbases_polygons_external_connections[closest_start_polygon->owner][closest_start_polygon->id].push_back(entry_connection);
364
365
Connection exit_connection;
366
exit_connection.polygon = closest_end_polygon;
367
exit_connection.edge = -1;
368
exit_connection.pathway_start = new_polygon.vertices[2];
369
exit_connection.pathway_end = new_polygon.vertices[3];
370
navbases_polygons_external_connections[link.ptr()].push_back(LocalVector<Nav3D::Connection>());
371
navbases_polygons_external_connections[link.ptr()][new_polygon.id].push_back(exit_connection);
372
}
373
374
// If the link is bi-directional, create connections from the end to the start.
375
if (link->is_bidirectional()) {
376
Connection entry_connection;
377
entry_connection.polygon = &new_polygon;
378
entry_connection.edge = -1;
379
entry_connection.pathway_start = new_polygon.vertices[2];
380
entry_connection.pathway_end = new_polygon.vertices[3];
381
navbases_polygons_external_connections[closest_end_polygon->owner][closest_end_polygon->id].push_back(entry_connection);
382
383
Connection exit_connection;
384
exit_connection.polygon = closest_start_polygon;
385
exit_connection.edge = -1;
386
exit_connection.pathway_start = new_polygon.vertices[0];
387
exit_connection.pathway_end = new_polygon.vertices[1];
388
navbases_polygons_external_connections[link.ptr()].push_back(LocalVector<Nav3D::Connection>());
389
navbases_polygons_external_connections[link.ptr()][new_polygon.id].push_back(exit_connection);
390
}
391
}
392
}
393
394
r_build.polygon_count = polygon_count;
395
}
396
397
void NavMapBuilder3D::_build_update_map_iteration(NavMapIterationBuild3D &r_build) {
398
NavMapIteration3D *map_iteration = r_build.map_iteration;
399
400
map_iteration->navmesh_polygon_count = r_build.polygon_count;
401
402
uint32_t navmesh_polygon_count = r_build.polygon_count;
403
uint32_t total_polygon_count = navmesh_polygon_count;
404
405
map_iteration->path_query_slots_mutex.lock();
406
for (NavMeshQueries3D::PathQuerySlot &p_path_query_slot : map_iteration->path_query_slots) {
407
p_path_query_slot.traversable_polys.clear();
408
p_path_query_slot.traversable_polys.reserve(navmesh_polygon_count * 0.25);
409
p_path_query_slot.path_corridor.clear();
410
411
p_path_query_slot.path_corridor.resize(total_polygon_count);
412
413
p_path_query_slot.poly_to_id.clear();
414
p_path_query_slot.poly_to_id.reserve(total_polygon_count);
415
416
int polygon_id = 0;
417
for (Ref<NavRegionIteration3D> &region : map_iteration->region_iterations) {
418
for (const Polygon &polygon : region->navmesh_polygons) {
419
p_path_query_slot.poly_to_id[&polygon] = polygon_id;
420
polygon_id++;
421
}
422
}
423
424
for (const Polygon &polygon : map_iteration->navlink_polygons) {
425
p_path_query_slot.poly_to_id[&polygon] = polygon_id;
426
polygon_id++;
427
}
428
429
DEV_ASSERT(p_path_query_slot.path_corridor.size() == p_path_query_slot.poly_to_id.size());
430
}
431
432
map_iteration->path_query_slots_mutex.unlock();
433
}
434
435