From 2ca94e51e4d4477709b83b487a4f5b938f24f56b Mon Sep 17 00:00:00 2001 From: PouleyKetchoupp Date: Tue, 14 Sep 2021 17:14:06 -0700 Subject: [PATCH] Clean convex hull decomposition code Remove unnecessary conversion between triangle data and vertex data whenever possible. --- core/math/triangle_mesh.h | 6 +- editor/import/scene_importer_mesh.cpp | 51 ++++++++------ modules/vhacd/register_types.cpp | 52 +++++++------- scene/resources/mesh.cpp | 98 +++++++-------------------- scene/resources/mesh.h | 4 +- 5 files changed, 84 insertions(+), 127 deletions(-) diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index 463b0dd5c8..2d3b4db4bb 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -37,11 +37,13 @@ class TriangleMesh : public RefCounted { GDCLASS(TriangleMesh, RefCounted); +public: struct Triangle { Vector3 normal; int indices[3]; }; +private: Vector triangles; Vector vertices; @@ -86,8 +88,8 @@ public: Vector3 get_area_normal(const AABB &p_aabb) const; Vector get_faces() const; - Vector get_triangles() const { return triangles; } - Vector get_vertices() const { return vertices; } + const Vector &get_triangles() const { return triangles; } + const Vector &get_vertices() const { return vertices; } void get_indices(Vector *r_triangles_indices) const; void create(const Vector &p_faces); diff --git a/editor/import/scene_importer_mesh.cpp b/editor/import/scene_importer_mesh.cpp index 55bea50432..5e6dd08e79 100644 --- a/editor/import/scene_importer_mesh.cpp +++ b/editor/import/scene_importer_mesh.cpp @@ -525,35 +525,46 @@ Vector EditorSceneImporterMesh::get_faces() const { } Vector> EditorSceneImporterMesh::convex_decompose(const Mesh::ConvexDecompositionSettings &p_settings) const { - ERR_FAIL_COND_V(!Mesh::convex_composition_function, Vector>()); + ERR_FAIL_COND_V(!Mesh::convex_decomposition_function, Vector>()); const Vector faces = get_faces(); + int face_count = faces.size(); - Vector> decomposed = Mesh::convex_composition_function(faces, p_settings); + Vector vertices; + uint32_t vertex_count = 0; + vertices.resize(face_count * 3); + Vector indices; + indices.resize(face_count * 3); + { + Map vertex_map; + Vector3 *vertex_w = vertices.ptrw(); + uint32_t *index_w = indices.ptrw(); + for (int i = 0; i < face_count; i++) { + for (int j = 0; j < 3; j++) { + const Vector3 &vertex = faces[i].vertex[j]; + Map::Element *found_vertex = vertex_map.find(vertex); + uint32_t index; + if (found_vertex) { + index = found_vertex->get(); + } else { + index = ++vertex_count; + vertex_map[vertex] = index; + vertex_w[index] = vertex; + } + index_w[i * 3 + j] = index; + } + } + } + vertices.resize(vertex_count); + + Vector> decomposed = Mesh::convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), face_count, p_settings, nullptr); Vector> ret; for (int i = 0; i < decomposed.size(); i++) { - Set points; - for (int j = 0; j < decomposed[i].size(); j++) { - points.insert(decomposed[i][j].vertex[0]); - points.insert(decomposed[i][j].vertex[1]); - points.insert(decomposed[i][j].vertex[2]); - } - - Vector convex_points; - convex_points.resize(points.size()); - { - Vector3 *w = convex_points.ptrw(); - int idx = 0; - for (Set::Element *E = points.front(); E; E = E->next()) { - w[idx++] = E->get(); - } - } - Ref shape; shape.instantiate(); - shape->set_points(convex_points); + shape->set_points(decomposed[i]); ret.push_back(shape); } diff --git a/modules/vhacd/register_types.cpp b/modules/vhacd/register_types.cpp index 88b2a568ea..54240e66fc 100644 --- a/modules/vhacd/register_types.cpp +++ b/modules/vhacd/register_types.cpp @@ -32,7 +32,7 @@ #include "scene/resources/mesh.h" #include "thirdparty/vhacd/public/VHACD.h" -static Vector> convex_decompose(const Vector &p_faces, const Mesh::ConvexDecompositionSettings &p_settings) { +static Vector> convex_decompose(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, const Mesh::ConvexDecompositionSettings &p_settings, Vector> *r_convex_indices) { VHACD::IVHACD::Parameters params; params.m_concavity = p_settings.max_concavity; params.m_alpha = p_settings.symmetry_planes_clipping_bias; @@ -49,42 +49,38 @@ static Vector> convex_decompose(const Vector &p_faces, cons params.m_maxConvexHulls = p_settings.max_convex_hulls; params.m_projectHullVertices = p_settings.project_hull_vertices; - Vector vertices; - vertices.resize(p_faces.size() * 9); - Vector indices; - indices.resize(p_faces.size() * 3); - - for (int i = 0; i < p_faces.size(); i++) { - for (int j = 0; j < 3; j++) { - vertices.write[i * 9 + j * 3 + 0] = p_faces[i].vertex[j].x; - vertices.write[i * 9 + j * 3 + 1] = p_faces[i].vertex[j].y; - vertices.write[i * 9 + j * 3 + 2] = p_faces[i].vertex[j].z; - indices.write[i * 3 + j] = i * 3 + j; - } - } - VHACD::IVHACD *decomposer = VHACD::CreateVHACD(); - decomposer->Compute(vertices.ptr(), vertices.size() / 3, indices.ptr(), indices.size() / 3, params); + decomposer->Compute(p_vertices, p_vertex_count, p_triangles, p_triangle_count, params); int hull_count = decomposer->GetNConvexHulls(); - Vector> ret; + Vector> ret; + ret.resize(hull_count); + + if (r_convex_indices) { + r_convex_indices->resize(hull_count); + } for (int i = 0; i < hull_count; i++) { - Vector triangles; VHACD::IVHACD::ConvexHull hull; decomposer->GetConvexHull(i, hull); - triangles.resize(hull.m_nTriangles); - for (uint32_t j = 0; j < hull.m_nTriangles; j++) { - Face3 f; + + Vector &points = ret.write[i]; + points.resize(hull.m_nPoints); + + Vector3 *w = points.ptrw(); + for (uint32_t j = 0; j < hull.m_nPoints; ++j) { for (int k = 0; k < 3; k++) { - for (int l = 0; l < 3; l++) { - f.vertex[k][l] = hull.m_points[hull.m_triangles[j * 3 + k] * 3 + l]; - } + w[j][k] = hull.m_points[j * 3 + k]; } - triangles.write[j] = f; } - ret.push_back(triangles); + + if (r_convex_indices) { + Vector &indices = r_convex_indices->write[i]; + indices.resize(hull.m_nTriangles * 3); + + memcpy(indices.ptrw(), hull.m_triangles, hull.m_nTriangles * 3 * sizeof(uint32_t)); + } } decomposer->Clean(); @@ -94,9 +90,9 @@ static Vector> convex_decompose(const Vector &p_faces, cons } void register_vhacd_types() { - Mesh::convex_composition_function = convex_decompose; + Mesh::convex_decomposition_function = convex_decompose; } void unregister_vhacd_types() { - Mesh::convex_composition_function = nullptr; + Mesh::convex_decomposition_function = nullptr; } diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index 1edc3ff38b..8f3f25f104 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -38,7 +38,7 @@ #include -Mesh::ConvexDecompositionFunc Mesh::convex_composition_function = nullptr; +Mesh::ConvexDecompositionFunc Mesh::convex_decomposition_function = nullptr; Ref Mesh::generate_triangle_mesh() const { if (triangle_mesh.is_valid()) { @@ -167,59 +167,6 @@ Vector Mesh::get_faces() const { return tm->get_faces(); } return Vector(); - /* - for (int i=0;imesh_surface_get_primitive_type( mesh, i ) != RenderingServer::PRIMITIVE_TRIANGLES ) - continue; - - Vector indices; - Vector vertices; - - vertices=RenderingServer::get_singleton()->mesh_surface_get_array(mesh, i,RenderingServer::ARRAY_VERTEX); - - int len=RenderingServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i); - bool has_indices; - - if (len>0) { - indices=RenderingServer::get_singleton()->mesh_surface_get_array(mesh, i,RenderingServer::ARRAY_INDEX); - has_indices=true; - - } else { - len=vertices.size(); - has_indices=false; - } - - if (len<=0) - continue; - - const int* indicesr = indices.ptr(); - const int *indicesptr = indicesr.ptr(); - - const Vector3* verticesr = vertices.ptr(); - const Vector3 *verticesptr = verticesr.ptr(); - - int old_faces=faces.size(); - int new_faces=old_faces+(len/3); - - faces.resize(new_faces); - - Face3* facesw = faces.ptrw(); - Face3 *facesptr=facesw.ptr(); - - - for (int i=0;i Mesh::create_convex_shape(bool p_clean, bool p_simplify) const { @@ -568,35 +515,36 @@ void Mesh::clear_cache() const { } Vector> Mesh::convex_decompose(const ConvexDecompositionSettings &p_settings) const { - ERR_FAIL_COND_V(!convex_composition_function, Vector>()); + ERR_FAIL_COND_V(!convex_decomposition_function, Vector>()); - const Vector faces = get_faces(); + Ref tm = generate_triangle_mesh(); + ERR_FAIL_COND_V(!tm.is_valid(), Vector>()); - const Vector> decomposed = convex_composition_function(faces, p_settings); + const Vector &triangles = tm->get_triangles(); + int triangle_count = triangles.size(); + + Vector indices; + { + indices.resize(triangle_count * 3); + uint32_t *w = indices.ptrw(); + for (int i = 0; i < triangle_count; i++) { + for (int j = 0; j < 3; j++) { + w[i * 3 + j] = triangles[i].indices[j]; + } + } + } + + const Vector &vertices = tm->get_vertices(); + int vertex_count = vertices.size(); + + Vector> decomposed = convex_decomposition_function((real_t *)vertices.ptr(), vertex_count, indices.ptr(), triangle_count, p_settings, nullptr); Vector> ret; for (int i = 0; i < decomposed.size(); i++) { - Set points; - for (int j = 0; j < decomposed[i].size(); j++) { - points.insert(decomposed[i][j].vertex[0]); - points.insert(decomposed[i][j].vertex[1]); - points.insert(decomposed[i][j].vertex[2]); - } - - Vector convex_points; - convex_points.resize(points.size()); - { - Vector3 *w = convex_points.ptrw(); - int idx = 0; - for (Set::Element *E = points.front(); E; E = E->next()) { - w[idx++] = E->get(); - } - } - Ref shape; shape.instantiate(); - shape->set_points(convex_points); + shape->set_points(decomposed[i]); ret.push_back(shape); } diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index 4d0ee0f247..0776585a11 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -191,9 +191,9 @@ public: uint32_t max_convex_hulls = 1; bool project_hull_vertices = true; }; - typedef Vector> (*ConvexDecompositionFunc)(const Vector &p_faces, const ConvexDecompositionSettings &p_settings); + typedef Vector> (*ConvexDecompositionFunc)(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, const ConvexDecompositionSettings &p_settings, Vector> *r_convex_indices); - static ConvexDecompositionFunc convex_composition_function; + static ConvexDecompositionFunc convex_decomposition_function; Vector> convex_decompose(const ConvexDecompositionSettings &p_settings) const;