diff --git a/doc/classes/Mesh.xml b/doc/classes/Mesh.xml index 6b5a50d97b9229..b299b4b6a650be 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 8b5e438aea62ad..aa9667b2d0e329 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,26 @@ 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(); + } + + 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 13fd986e811d56..9a6c2f06a70f83 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