Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mesh.create_convex_shapes() to expose convex decomposition for scripts #93625

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading