Skip to content

Commit

Permalink
NavigationPolygon: Implement get/set_polygon fast paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
libklein committed Jun 15, 2024
1 parent 71699e0 commit cd453bd
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 50 deletions.
5 changes: 1 addition & 4 deletions modules/navigation/2d/nav_mesh_generator_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,10 +1067,7 @@ void NavMeshGenerator2D::generator_bake_from_source_geometry_data(Ref<Navigation
}

p_navigation_mesh->set_vertices(new_vertices);
p_navigation_mesh->clear_polygons();
for (int i = 0; i < new_polygons.size(); i++) {
p_navigation_mesh->add_polygon(new_polygons[i]);
}
p_navigation_mesh->set_polygons(new_polygons);
}

#endif // CLIPPER2_ENABLED
4 changes: 3 additions & 1 deletion modules/navigation/3d/nav_mesh_generator_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ void NavMeshGenerator3D::generator_bake_from_source_geometry_data(Ref<Navigation
p_navigation_mesh->set_vertices(nav_vertices);
p_navigation_mesh->clear_polygons();

Vector<Vector<int>> nav_polygons;
for (int i = 0; i < detail_mesh->nmeshes; i++) {
const unsigned int *detail_mesh_m = &detail_mesh->meshes[i * 4];
const unsigned int detail_mesh_bverts = detail_mesh_m[0];
Expand All @@ -933,9 +934,10 @@ void NavMeshGenerator3D::generator_bake_from_source_geometry_data(Ref<Navigation
nav_indices.write[1] = recast_index_to_native_index[index2];
nav_indices.write[2] = recast_index_to_native_index[index3];

p_navigation_mesh->add_polygon(nav_indices);
nav_polygons.push_back(nav_indices);
}
}
p_navigation_mesh->set_polygons(nav_polygons);

bake_state = "Cleanup..."; // step #11

Expand Down
39 changes: 28 additions & 11 deletions scene/resources/2d/navigation_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ void NavigationPolygon::_set_polygons(const TypedArray<Vector<int32_t>> &p_array
}
polygons.resize(p_array.size());
for (int i = 0; i < p_array.size(); i++) {
polygons.write[i].indices = p_array[i];
polygons.write[i] = p_array[i];
}
}

TypedArray<Vector<int32_t>> NavigationPolygon::_get_polygons() const {
TypedArray<Vector<int32_t>> ret;
ret.resize(polygons.size());
for (int i = 0; i < ret.size(); i++) {
ret[i] = polygons[i].indices;
ret[i] = polygons[i];
}

return ret;
Expand All @@ -132,9 +132,7 @@ TypedArray<Vector<Vector2>> NavigationPolygon::_get_outlines() const {
}

void NavigationPolygon::add_polygon(const Vector<int> &p_polygon) {
Polygon polygon;
polygon.indices = p_polygon;
polygons.push_back(polygon);
polygons.push_back(p_polygon);
{
MutexLock lock(navigation_mesh_generation);
navigation_mesh.unref();
Expand All @@ -152,7 +150,7 @@ int NavigationPolygon::get_polygon_count() const {

Vector<int> NavigationPolygon::get_polygon(int p_idx) {
ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
return polygons[p_idx].indices;
return polygons[p_idx];
}

void NavigationPolygon::clear_polygons() {
Expand Down Expand Up @@ -190,15 +188,34 @@ Ref<NavigationMesh> NavigationPolygon::get_navigation_mesh() {
}
navigation_mesh->set_vertices(verts);

for (int i(0); i < get_polygon_count(); i++) {
navigation_mesh->add_polygon(get_polygon(i));
}
navigation_mesh->set_polygons(polygons);
navigation_mesh->set_cell_size(cell_size); // Needed to not fail the cell size check on the server
}

return navigation_mesh;
}

void NavigationPolygon::set_outlines(const Vector<Vector<Vector2>> &p_outlines) {
outlines = p_outlines;
rect_cache_dirty = true;
}

Vector<Vector<Vector2>> NavigationPolygon::get_outlines() const {
return outlines;
}

void NavigationPolygon::set_polygons(const Vector<Vector<int>> &p_polygons) {
polygons = p_polygons;
{
MutexLock lock(navigation_mesh_generation);
navigation_mesh.unref();
}
}

Vector<Vector<int>> NavigationPolygon::get_polygons() const {
return polygons;
}

void NavigationPolygon::add_outline(const Vector<Vector2> &p_outline) {
outlines.push_back(p_outline);
rect_cache_dirty = true;
Expand Down Expand Up @@ -319,15 +336,15 @@ void NavigationPolygon::make_polygons_from_outlines() {
for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
TPPLPoly &tp = I->get();

struct Polygon p;
Vector<int> p;

for (int64_t i = 0; i < tp.GetNumPoints(); i++) {
HashMap<Vector2, int>::Iterator E = points.find(tp[i]);
if (!E) {
E = points.insert(tp[i], vertices.size());
vertices.push_back(tp[i]);
}
p.indices.push_back(E->value);
p.push_back(E->value);
}

polygons.push_back(p);
Expand Down
9 changes: 4 additions & 5 deletions scene/resources/2d/navigation_polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ class NavigationPolygon : public Resource {
GDCLASS(NavigationPolygon, Resource);

Vector<Vector2> vertices;
struct Polygon {
Vector<int> indices;
};
Vector<Polygon> polygons;
Vector<Vector<int>> polygons;
Vector<Vector<Vector2>> outlines;
Vector<Vector<Vector2>> baked_outlines;

Expand Down Expand Up @@ -108,14 +105,16 @@ class NavigationPolygon : public Resource {
Vector<Vector2> get_outline(int p_idx) const;
void remove_outline(int p_idx);
int get_outline_count() const;
void set_outlines(const Vector<Vector<Vector2>> &p_outlines);
Vector<Vector<Vector2>> get_outlines() const;

void clear_outlines();
#ifndef DISABLE_DEPRECATED
void make_polygons_from_outlines();
#endif // DISABLE_DEPRECATED

void set_polygons(const Vector<Vector<int>> &p_polygons);
const Vector<Vector<int>> &get_polygons() const;
Vector<Vector<int>> get_polygons() const;
Vector<int> get_polygon(int p_idx);
void clear_polygons();

Expand Down
14 changes: 7 additions & 7 deletions scene/resources/2d/tile_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6489,15 +6489,15 @@ Ref<NavigationPolygon> TileData::get_navigation_polygon(int p_layer_id, bool p_f
PackedVector2Array new_points = get_transformed_vertices(layer_tile_data.navigation_polygon->get_vertices(), p_flip_h, p_flip_v, p_transpose);
transformed_polygon->set_vertices(new_points);

int num_polygons = layer_tile_data.navigation_polygon->get_polygon_count();
for (int i = 0; i < num_polygons; ++i) {
transformed_polygon->add_polygon(layer_tile_data.navigation_polygon->get_polygon(i));
}
transformed_polygon->set_polygons(layer_tile_data.navigation_polygon->get_polygons());

for (int i = 0; i < layer_tile_data.navigation_polygon->get_outline_count(); i++) {
PackedVector2Array new_outline = get_transformed_vertices(layer_tile_data.navigation_polygon->get_outline(i), p_flip_h, p_flip_v, p_transpose);
transformed_polygon->add_outline(new_outline);
int outline_count = layer_tile_data.navigation_polygon->get_outline_count();
Vector<Vector<Vector2>> new_outlines;
new_outlines.resize(outline_count);
for (int i = 0; i < outline_count; i++) {
new_outlines.write[i] = get_transformed_vertices(layer_tile_data.navigation_polygon->get_outline(i), p_flip_h, p_flip_v, p_transpose);
}
transformed_polygon->set_outlines(new_outlines);

layer_tile_data.transformed_navigation_polygon[key] = transformed_polygon;
return transformed_polygon;
Expand Down
18 changes: 7 additions & 11 deletions scene/resources/3d/importer_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,12 @@ Ref<NavigationMesh> ImporterMesh::create_navigation_mesh() {
}

HashMap<Vector3, int> unique_vertices;
LocalVector<int> face_indices;
Vector<Vector<int>> face_polygons;
face_polygons.resize(faces.size());

for (int i = 0; i < faces.size(); i++) {
Vector<int> face_indices;
face_indices.resize(3);
for (int j = 0; j < 3; j++) {
Vector3 v = faces[i].vertex[j];
int idx;
Expand All @@ -1071,8 +1074,9 @@ Ref<NavigationMesh> ImporterMesh::create_navigation_mesh() {
idx = unique_vertices.size();
unique_vertices[v] = idx;
}
face_indices.push_back(idx);
face_indices.write[j] = idx;
}
face_polygons.write[i] = face_indices;
}

Vector<Vector3> vertices;
Expand All @@ -1084,15 +1088,7 @@ Ref<NavigationMesh> ImporterMesh::create_navigation_mesh() {
Ref<NavigationMesh> nm;
nm.instantiate();
nm->set_vertices(vertices);

Vector<int> v3;
v3.resize(3);
for (uint32_t i = 0; i < face_indices.size(); i += 3) {
v3.write[0] = face_indices[i + 0];
v3.write[1] = face_indices[i + 1];
v3.write[2] = face_indices[i + 2];
nm->add_polygon(v3);
}
nm->set_polygons(face_polygons);

return nm;
}
Expand Down
24 changes: 17 additions & 7 deletions scene/resources/navigation_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ void NavigationMesh::create_from_mesh(const Ref<Mesh> &p_mesh) {
int rlen = iarr.size();
const int *r = iarr.ptr();

Vector<Vector<int>> new_polygons;
new_polygons.resize(rlen / 3);
for (int j = 0; j < rlen; j += 3) {
Vector<int> vi;
vi.resize(3);
vi.write[0] = r[j + 0] + from;
vi.write[1] = r[j + 1] + from;
vi.write[2] = r[j + 2] + from;

add_polygon(vi);
polygons.write[j / 3] = vi;
}
set_polygons(polygons);
}
}

Expand Down Expand Up @@ -315,7 +318,7 @@ Vector<Vector3> NavigationMesh::get_vertices() const {
void NavigationMesh::_set_polygons(const Array &p_array) {
polygons.resize(p_array.size());
for (int i = 0; i < p_array.size(); i++) {
polygons.write[i].indices = p_array[i];
polygons.write[i] = p_array[i];
}
notify_property_list_changed();
}
Expand All @@ -324,16 +327,23 @@ Array NavigationMesh::_get_polygons() const {
Array ret;
ret.resize(polygons.size());
for (int i = 0; i < ret.size(); i++) {
ret[i] = polygons[i].indices;
ret[i] = polygons[i];
}

return ret;
}

void NavigationMesh::set_polygons(const Vector<Vector<int>> &p_polygons) {
polygons = p_polygons;
notify_property_list_changed();
}

Vector<Vector<int>> NavigationMesh::get_polygons() const {
return polygons;
}

void NavigationMesh::add_polygon(const Vector<int> &p_polygon) {
Polygon polygon;
polygon.indices = p_polygon;
polygons.push_back(polygon);
polygons.push_back(p_polygon);
notify_property_list_changed();
}

Expand All @@ -343,7 +353,7 @@ int NavigationMesh::get_polygon_count() const {

Vector<int> NavigationMesh::get_polygon(int p_idx) {
ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
return polygons[p_idx].indices;
return polygons[p_idx];
}

void NavigationMesh::clear_polygons() {
Expand Down
7 changes: 3 additions & 4 deletions scene/resources/navigation_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class NavigationMesh : public Resource {
GDCLASS(NavigationMesh, Resource);

Vector<Vector3> vertices;
struct Polygon {
Vector<int> indices;
};
Vector<Polygon> polygons;
Vector<Vector<int>> polygons;
Ref<ArrayMesh> debug_mesh;

protected:
Expand Down Expand Up @@ -192,6 +189,8 @@ class NavigationMesh : public Resource {
int get_polygon_count() const;
Vector<int> get_polygon(int p_idx);
void clear_polygons();
void set_polygons(const Vector<Vector<int>> &p_polygons);
Vector<Vector<int>> get_polygons() const;

void clear();

Expand Down

0 comments on commit cd453bd

Please sign in to comment.