Skip to content

Commit

Permalink
Add Mesh.create_convex_shapes() to expose convex decomposition for sc…
Browse files Browse the repository at this point in the history
…ripts

Adds Mesh.create_convex_shapes() functions so scripts can make use of the convex decomposition with MeshConvexDecompositionSettings parameters.
  • Loading branch information
smix8 committed Jun 26, 2024
1 parent 95110dd commit 189c3af
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/classes/Mesh.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</description>
</method>
<method name="create_convex_shapes" qualifiers="const">
<return type="Array" />
<param index="0" name="settings" type="MeshConvexDecompositionSettings" default="null" />
<description>
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].
</description>
</method>
<method name="create_outline" qualifiers="const">
<return type="Mesh" />
<param index="0" name="margin" type="float" />
Expand Down
26 changes: 26 additions & 0 deletions scene/resources/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshConvexDecompositionSettings>()));
ClassDB::set_method_flags("Mesh", "create_convex_shapes", METHOD_FLAGS_DEFAULT);
#endif // _3D_DISABLED

BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
Expand Down Expand Up @@ -901,6 +905,28 @@ void Mesh::clear_cache() const {
}

#ifndef _3D_DISABLED
Array Mesh::create_convex_shapes(const Ref<MeshConvexDecompositionSettings> &p_settings) const {
Ref<MeshConvexDecompositionSettings> 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<Ref<Shape3D>> 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<Ref<ConvexPolygonShape3D>>(decomposed_shapes[i]);
}

return ret;
}

Vector<Ref<Shape3D>> Mesh::convex_decompose(const Ref<MeshConvexDecompositionSettings> &p_settings) const {
ERR_FAIL_NULL_V(convex_decomposition_function, Vector<Ref<Shape3D>>());

Expand Down
1 change: 1 addition & 0 deletions scene/resources/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class Mesh : public Resource {
#ifndef _3D_DISABLED
Vector<Ref<Shape3D>> convex_decompose(const Ref<MeshConvexDecompositionSettings> &p_settings) const;
Ref<ConvexPolygonShape3D> create_convex_shape(bool p_clean = true, bool p_simplify = false) const;
Array create_convex_shapes(const Ref<MeshConvexDecompositionSettings> &p_settings) const;
Ref<ConcavePolygonShape3D> create_trimesh_shape() const;
#endif // _3D_DISABLED

Expand Down

0 comments on commit 189c3af

Please sign in to comment.