2using System.Diagnostics;
4using System.Runtime.InteropServices;
9public class VHACD : MonoBehaviour
56 [Tooltip(
"maximum concavity")] [Range(0, 1)]
59 [Tooltip(
"controls the bias toward clipping along symmetry planes")] [Range(0, 1)]
62 [Tooltip(
"controls the bias toward clipping along revolution axes")] [Range(0, 1)]
65 [Tooltip(
"controls the adaptive sampling of the generated convex-hulls")] [Range(0, 0.01f)]
71 [Tooltip(
"maximum number of voxels generated during the voxelization stage")] [Range(10000, 64000000)]
74 [Tooltip(
"controls the maximum number of triangles per convex-hull")] [Range(4, 1024)]
77 [Tooltip(
"controls the granularity of the search for the \"best\" clipping plane")] [Range(1, 16)]
81 "controls the precision of the convex-hull generation process during the clipping plane selection stage")]
85 [Tooltip(
"enable/disable normalizing the mesh before applying the convex decomposition")] [Range(0, 1)]
88 [Tooltip(
"0: voxel-based (recommended), 1: tetrahedron-based")] [Range(0, 1)]
98 "This will project the output convex hull vertices onto the original source mesh to increase the floating point accuracy of the results")]
102 unsafe
struct ConvexHull
104 public double* m_points;
105 public uint* m_triangles;
106 public uint m_nPoints;
107 public uint m_nTriangles;
108 public double m_volume;
109 public fixed
double m_center[3];
112 [DllImport(
"libvhacd")]
113 static extern unsafe
void* CreateVHACD();
115 [DllImport(
"libvhacd")]
116 static extern unsafe
void DestroyVHACD(
void* pVHACD);
118 [DllImport(
"libvhacd")]
119 static extern unsafe
bool ComputeFloat(
125 Parameters* parameters);
127 [DllImport(
"libvhacd")]
128 static extern unsafe
bool ComputeDouble(
134 Parameters* parameters);
136 [DllImport(
"libvhacd")]
137 static extern unsafe uint GetNConvexHulls(
void* pVHACD);
139 [DllImport(
"libvhacd")]
140 static extern unsafe
void GetConvexHull(
152 [ContextMenu(
"Generate Convex Meshes")]
155 var sp =
new Stopwatch();
159 while (mesh ==
null) Thread.Sleep(10);
160 var vhacd = CreateVHACD();
163 Vector3[] verts =
null;
167 verts = mesh.vertices;
168 tris = mesh.triangles;
171 while (verts ==
null || tris ==
null) Thread.Sleep(10);
172 fixed (Vector3* pVerts = verts)
173 fixed (
int* pTris = tris)
177 (
float*) pVerts, (uint) verts.Length,
178 (uint*) pTris, (uint) tris.Length / 3,
185 var numHulls = GetNConvexHulls(vhacd);
186 foreach (var index
in Enumerable.Range(0, (
int) numHulls))
189 GetConvexHull(vhacd, (uint) index, &hull);
191 var hullMesh =
new Mesh();
192 var hullVerts =
new Vector3[hull.m_nPoints];
193 fixed (Vector3* pHullVerts = hullVerts)
195 var pComponents = hull.m_points;
196 var pVerts = pHullVerts;
198 for (var pointCount = hull.m_nPoints; pointCount != 0; --pointCount)
200 pVerts->x = (float) pComponents[0];
201 pVerts->y = (float) pComponents[1];
202 pVerts->z = (float) pComponents[2];
209 hullMesh.SetVertices(hullVerts);
211 var indices =
new int[hull.m_nTriangles * 3];
212 Marshal.Copy((IntPtr) hull.m_triangles, indices, 0, indices.Length);
213 hullMesh.SetTriangles(indices, 0);
215 var col = gameObject.AddComponent<MeshCollider>();
217 col.sharedMesh = hullMesh;
221 Debug.Log(gameObject.name +
": " + sp.Elapsed);
unsafe void GenerateConvexMeshes()
uint m_convexhullApproximation
uint m_maxNumVerticesPerCH
uint m_convexhullDownsampling
bool m_projectHullVertices