From 189c3af8b3cdeb3948ee3721dc6e5e0d6651674d Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:09:11 +0200 Subject: [PATCH] Add Mesh.create_convex_shapes() to expose convex decomposition for scripts Adds Mesh.create_convex_shapes() functions so scripts can make use of the convex decomposition with MeshConvexDecompositionSettings parameters. --- doc/classes/Mesh.xml | 7 +++++++ scene/resources/mesh.cpp | 26 ++++++++++++++++++++++++++ scene/resources/mesh.h | 1 + 3 files changed, 34 insertions(+) diff --git a/doc/classes/Mesh.xml b/doc/classes/Mesh.xml index 6b5a50d97b922..b299b4b6a650b 100644 --- a/doc/classes/Mesh.xml +++ b/doc/classes/Mesh.xml @@ -120,6 +120,13 @@ If [param simplify] is [code]true[/code], the geometry can be further simplified to reduce the number of vertices. Disabled by default. + + + + + Creates one or more [ConvexPolygonShape3D] collision shapes calculated from the mesh geometry via convex decomposition. The convex decomposition operation can be controlled with parameters from the optional [param settings]. + + diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index 8b5e438aea62a..d1e037142df56 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -811,6 +811,10 @@ void Mesh::_bind_methods() { ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material); ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material); ClassDB::bind_method(D_METHOD("create_placeholder"), &Mesh::create_placeholder); +#ifndef _3D_DISABLED + ClassDB::bind_method(D_METHOD("create_convex_shapes", "settings"), &Mesh::create_convex_shapes, DEFVAL(Ref())); + ClassDB::set_method_flags("Mesh", "create_convex_shapes", METHOD_FLAGS_DEFAULT); +#endif // _3D_DISABLED BIND_ENUM_CONSTANT(PRIMITIVE_POINTS); BIND_ENUM_CONSTANT(PRIMITIVE_LINES); @@ -901,6 +905,28 @@ void Mesh::clear_cache() const { } #ifndef _3D_DISABLED +Array Mesh::create_convex_shapes(const Ref &p_settings) const { + Ref settings; + if (p_settings.is_valid()) { + settings = p_settings; + } else { + settings.instantiate(); + settings->set_max_convex_hulls(32); + settings->set_max_concavity(0.001); + } + + Vector> decomposed_shapes = convex_decompose(settings); + + Array ret; + ret.resize(decomposed_shapes.size()); + + for (int i = 0; i < decomposed_shapes.size(); i++) { + ret[i] = static_cast>(decomposed_shapes[i]); + } + + return ret; +} + Vector> Mesh::convex_decompose(const Ref &p_settings) const { ERR_FAIL_NULL_V(convex_decomposition_function, Vector>()); diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index 13fd986e811d5..9a6c2f06a70f8 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -199,6 +199,7 @@ class Mesh : public Resource { #ifndef _3D_DISABLED Vector> convex_decompose(const Ref &p_settings) const; Ref create_convex_shape(bool p_clean = true, bool p_simplify = false) const; + Array create_convex_shapes(const Ref &p_settings) const; Ref create_trimesh_shape() const; #endif // _3D_DISABLED