summaryrefslogtreecommitdiff
path: root/scene/resources/mesh.cpp
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2019-04-10 17:46:04 -0300
committerJuan Linietsky <reduzio@gmail.com>2019-04-10 17:47:28 -0300
commit5823b5d77d47339c3c981953ca8178e7ef14b6df (patch)
tree608310c810903163292b9cd8133374699aa9ae6f /scene/resources/mesh.cpp
parent07b76c0376eef34fcbf52ecc21074c5084f980f5 (diff)
Bundled VHACD library for convex decomposition.
Modified both MeshInstance tools as well as importer to use it instead of QuickHull.
Diffstat (limited to 'scene/resources/mesh.cpp')
-rw-r--r--scene/resources/mesh.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp
index 85018f38d7..67639858ce 100644
--- a/scene/resources/mesh.cpp
+++ b/scene/resources/mesh.cpp
@@ -37,6 +37,8 @@
#include <stdlib.h>
+Mesh::ConvexDecompositionFunc Mesh::convex_composition_function = NULL;
+
Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
if (triangle_mesh.is_valid())
@@ -543,6 +545,49 @@ void Mesh::clear_cache() const {
debug_lines.clear();
}
+Vector<Ref<Shape> > Mesh::convex_decompose() const {
+
+ ERR_FAIL_COND_V(!convex_composition_function, Vector<Ref<Shape> >());
+
+ PoolVector<Face3> faces = get_faces();
+ Vector<Face3> f3;
+ f3.resize(faces.size());
+ PoolVector<Face3>::Read f = faces.read();
+ for (int i = 0; i < f3.size(); i++) {
+ f3.write[i] = f[i];
+ }
+
+ Vector<Vector<Face3> > decomposed = convex_composition_function(f3);
+
+ Vector<Ref<Shape> > ret;
+
+ for (int i = 0; i < decomposed.size(); i++) {
+ Set<Vector3> 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<Vector3> convex_points;
+ convex_points.resize(points.size());
+ {
+ PoolVector<Vector3>::Write w = convex_points.write();
+ int idx = 0;
+ for (Set<Vector3>::Element *E = points.front(); E; E = E->next()) {
+ w[idx++] = E->get();
+ }
+ }
+
+ Ref<ConvexPolygonShape> shape;
+ shape.instance();
+ shape->set_points(convex_points);
+ ret.push_back(shape);
+ }
+
+ return ret;
+}
+
Mesh::Mesh() {
}