Implemented thread safe GenerateConvexMeshes function.

This commit is contained in:
2024-12-08 07:07:30 +01:00
parent d95a6b5817
commit bc6b95441e
3 changed files with 73 additions and 0 deletions

View File

@@ -186,6 +186,58 @@ namespace MeshProcess
hullMesh.SetTriangles(indices, 0);
convexMesh.Add(hullMesh);
}
return convexMesh;
}
public unsafe List<MeshSafe> GenerateConvexMeshes(Vector3[] verts, int[] tris)
{
var vhacd = CreateVHACD();
var parameters = m_parameters;
fixed (Vector3* pVerts = verts)
fixed (int* pTris = tris)
{
ComputeFloat(
vhacd,
(float*)pVerts, (uint)verts.Length,
(uint*)pTris, (uint)tris.Length / 3,
&parameters);
}
var numHulls = GetNConvexHulls(vhacd);
List<MeshSafe> convexMesh = new List<MeshSafe>((int)numHulls);
foreach (var index in Enumerable.Range(0, (int)numHulls))
{
ConvexHull hull;
GetConvexHull(vhacd, (uint)index, &hull);
var hullMesh = new MeshSafe();
var hullVerts = new Vector3[hull.m_nPoints];
fixed (Vector3* pHullVerts = hullVerts)
{
var pComponents = hull.m_points;
var pVerts = pHullVerts;
for (var pointCount = hull.m_nPoints; pointCount != 0; --pointCount)
{
pVerts->x = (float)pComponents[0];
pVerts->y = (float)pComponents[1];
pVerts->z = (float)pComponents[2];
pVerts += 1;
pComponents += 3;
}
}
hullMesh.Vertices = hullVerts;
var indices = new int[hull.m_nTriangles * 3];
Marshal.Copy((System.IntPtr)hull.m_triangles, indices, 0, indices.Length);
hullMesh.Triangles = indices;
convexMesh.Add(hullMesh);
}
return convexMesh;