diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index 15c890c16a6..48a403ba759 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -37,11 +37,13 @@ class TriangleMesh : public Reference { GDCLASS(TriangleMesh, Reference); +public: struct Triangle { Vector3 normal; int indices[3]; }; +private: PoolVector triangles; PoolVector vertices; @@ -86,8 +88,8 @@ public: Vector3 get_area_normal(const AABB &p_aabb) const; PoolVector get_faces() const; - PoolVector get_triangles() const { return triangles; } - PoolVector get_vertices() const { return vertices; } + const PoolVector &get_triangles() const { return triangles; } + const PoolVector &get_vertices() const { return vertices; } void get_indices(PoolVector *r_triangles_indices) const; void create(const PoolVector &p_faces); diff --git a/modules/vhacd/register_types.cpp b/modules/vhacd/register_types.cpp index 3d7aaee921e..9a0391c163d 100644 --- a/modules/vhacd/register_types.cpp +++ b/modules/vhacd/register_types.cpp @@ -32,48 +32,44 @@ #include "scene/resources/mesh.h" #include "thirdparty/vhacd/public/VHACD.h" -static Vector> convex_decompose(const Vector &p_faces, int p_max_convex_hulls = -1) { - 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; - } - } - +static Vector> convex_decompose(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, int p_max_convex_hulls = -1, Vector> *r_convex_indices = nullptr) { VHACD::IVHACD::Parameters params; if (p_max_convex_hulls > 0) { params.m_maxConvexHulls = p_max_convex_hulls; } 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; + + PoolVector &points = ret.write[i]; + points.resize(hull.m_nPoints); + + PoolVector::Write w = points.write(); + 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) { + PoolVector &indices = r_convex_indices->write[i]; + indices.resize(hull.m_nTriangles * 3); + + memcpy(indices.write().ptr(), hull.m_triangles, hull.m_nTriangles * 3 * sizeof(uint32_t)); + } } decomposer->Clean(); @@ -83,9 +79,9 @@ static Vector> convex_decompose(const Vector &p_faces, int } 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 7bb1e7136d3..de64c8d1775 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -40,7 +40,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()) { @@ -170,64 +170,6 @@ PoolVector Mesh::get_faces() const { return tm->get_faces(); } return PoolVector(); - /* - for (int i=0;imesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES ) - continue; - - PoolVector indices; - PoolVector vertices; - - vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX); - - int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i); - bool has_indices; - - if (len>0) { - - indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX); - has_indices=true; - - } else { - - len=vertices.size(); - has_indices=false; - } - - if (len<=0) - continue; - - PoolVector::Read indicesr = indices.read(); - const int *indicesptr = indicesr.ptr(); - - PoolVector::Read verticesr = vertices.read(); - const Vector3 *verticesptr = verticesr.ptr(); - - int old_faces=faces.size(); - int new_faces=old_faces+(len/3); - - faces.resize(new_faces); - - PoolVector::Write facesw = faces.write(); - Face3 *facesptr=facesw.ptr(); - - - for (int i=0;i Mesh::create_convex_shape(bool p_clean, bool p_simplify) const { @@ -568,41 +510,37 @@ void Mesh::clear_cache() const { } Vector> Mesh::convex_decompose(int p_max_convex_hulls) const { - ERR_FAIL_COND_V(!convex_composition_function, Vector>()); + ERR_FAIL_COND_V(!convex_decomposition_function, Vector>()); - PoolVector faces = get_faces(); - Vector f3; - f3.resize(faces.size()); - PoolVector::Read f = faces.read(); - for (int i = 0; i < f3.size(); i++) { - f3.write[i] = f[i]; + Ref tm = generate_triangle_mesh(); + ERR_FAIL_COND_V(!tm.is_valid(), Vector>()); + + const PoolVector &triangles = tm->get_triangles(); + int triangle_count = triangles.size(); + + PoolVector indices; + { + indices.resize(triangle_count * 3); + PoolVector::Write w = indices.write(); + PoolVector::Read triangles_read = triangles.read(); + for (int i = 0; i < triangle_count; i++) { + for (int j = 0; j < 3; j++) { + w[i * 3 + j] = triangles_read[i].indices[j]; + } + } } - Vector> decomposed = convex_composition_function(f3, p_max_convex_hulls); + const PoolVector &vertices = tm->get_vertices(); + int vertex_count = vertices.size(); + + Vector> decomposed = convex_decomposition_function((real_t *)vertices.read().ptr(), vertex_count, indices.read().ptr(), triangle_count, p_max_convex_hulls, 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]); - } - - PoolVector convex_points; - convex_points.resize(points.size()); - { - PoolVector::Write w = convex_points.write(); - int idx = 0; - for (Set::Element *E = points.front(); E; E = E->next()) { - w[idx++] = E->get(); - } - } - Ref shape; shape.instance(); - 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 a5249e94d82..1798af82e35 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -148,9 +148,9 @@ public: Size2 get_lightmap_size_hint() const; void clear_cache() const; - typedef Vector> (*ConvexDecompositionFunc)(const Vector &p_faces, int p_max_convex_hulls); + typedef Vector> (*ConvexDecompositionFunc)(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, int p_max_convex_hulls, Vector> *r_convex_indices); - static ConvexDecompositionFunc convex_composition_function; + static ConvexDecompositionFunc convex_decomposition_function; Vector> convex_decompose(int p_max_convex_hulls = -1) const;