From 2d2d24a538bf958acd44c30e9291fa54c8d77164 Mon Sep 17 00:00:00 2001 From: AndreaCatania Date: Sun, 22 Aug 2021 18:19:13 +0200 Subject: Improve collision generation usability in the new 3D scene import workflow. With this PR it's possible to add a collision during the Mesh import, directly in editor. To generate the shape is possible to chose between the following options: - Decompose Convex: The Mesh is decomposed in one or many Convex Shapes (Using the VHACD library). - Simple Convex: Is generated a convex shape that enclose the entire mesh. - Trimesh: Generate a trimesh shape using the Mesh faces. - Box: Add a primitive box shape, where you can tweak the `size`, `position`, `rotation`. - Sphere: Add a primitive sphere shape, where you can tweak the `radius`, `position`, `rotation`. - Cylinder: Add a primitive cylinder shape, where you can tweak the `height`, `radius`, `position`, `rotation`. - Capsule: Add a primitive capsule shape, where you can tweak the `height`, `radius`, `position`, `rotation`. It's also possible to chose the generated body, so you can create: - Rigid Body. - Static Body. - Area. --- scene/3d/mesh_instance_3d.cpp | 3 ++- scene/resources/mesh.cpp | 8 +++++--- scene/resources/mesh.h | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) (limited to 'scene') diff --git a/scene/3d/mesh_instance_3d.cpp b/scene/3d/mesh_instance_3d.cpp index de6925244a..7e7db57af3 100644 --- a/scene/3d/mesh_instance_3d.cpp +++ b/scene/3d/mesh_instance_3d.cpp @@ -274,7 +274,8 @@ Node *MeshInstance3D::create_multiple_convex_collisions_node() { return nullptr; } - Vector> shapes = mesh->convex_decompose(); + Mesh::ConvexDecompositionSettings settings; + Vector> shapes = mesh->convex_decompose(settings); if (!shapes.size()) { return nullptr; } diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index ad589a605e..4f301fca95 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -224,7 +224,9 @@ Vector Mesh::get_faces() const { Ref Mesh::create_convex_shape(bool p_clean, bool p_simplify) const { if (p_simplify) { - Vector> decomposed = convex_decompose(1); + ConvexDecompositionSettings settings; + settings.max_convex_hulls = 1; + Vector> decomposed = convex_decompose(settings); if (decomposed.size() == 1) { return decomposed[0]; } else { @@ -564,12 +566,12 @@ void Mesh::clear_cache() const { debug_lines.clear(); } -Vector> Mesh::convex_decompose(int p_max_convex_hulls) const { +Vector> Mesh::convex_decompose(const ConvexDecompositionSettings &p_settings) const { ERR_FAIL_COND_V(!convex_composition_function, Vector>()); const Vector faces = get_faces(); - Vector> decomposed = convex_composition_function(faces, p_max_convex_hulls); + const Vector> decomposed = convex_composition_function(faces, p_settings); Vector> ret; diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index 27b0eb098b..240182361f 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -159,11 +159,42 @@ public: Size2i get_lightmap_size_hint() const; void clear_cache() const; - typedef Vector> (*ConvexDecompositionFunc)(const Vector &p_faces, int p_max_convex_hulls); + struct ConvexDecompositionSettings { + enum Mode : int { + CONVEX_DECOMPOSITION_MODE_VOXEL = 0, + CONVEX_DECOMPOSITION_MODE_TETRAHEDRON + }; + + /// Maximum concavity. [Range: 0.0 -> 1.0] + real_t max_concavity = 1.0; + /// Controls the bias toward clipping along symmetry planes. [Range: 0.0 -> 1.0] + real_t symmetry_planes_clipping_bias = 0.05; + /// Controls the bias toward clipping along revolution axes. [Range: 0.0 -> 1.0] + real_t revolution_axes_clipping_bias = 0.05; + real_t min_volume_per_convex_hull = 0.0001; + /// Maximum number of voxels generated during the voxelization stage. + uint32_t resolution = 10'000; + uint32_t max_num_vertices_per_convex_hull = 32; + /// Controls the granularity of the search for the "best" clipping plane. + /// [Range: 1 -> 16] + uint32_t plane_downsampling = 4; + /// Controls the precision of the convex-hull generation process during the + /// clipping plane selection stage. + /// [Range: 1 -> 16] + uint32_t convexhull_downsampling = 4; + /// enable/disable normalizing the mesh before applying the convex decomposition. + bool normalize_mesh = false; + Mode mode = CONVEX_DECOMPOSITION_MODE_VOXEL; + bool convexhull_approximation = true; + /// This is the maximum number of convex hulls to produce from the merge operation. + uint32_t max_convex_hulls = 1; + bool project_hull_vertices = true; + }; + typedef Vector> (*ConvexDecompositionFunc)(const Vector &p_faces, const ConvexDecompositionSettings &p_settings); static ConvexDecompositionFunc convex_composition_function; - Vector> convex_decompose(int p_max_convex_hulls = -1) const; + Vector> convex_decompose(const ConvexDecompositionSettings &p_settings) const; virtual int get_builtin_bind_pose_count() const; virtual Transform3D get_builtin_bind_pose(int p_index) const; -- cgit v1.2.3