Implemented thread safe GenerateConvexMeshes function.
This commit is contained in:
19
Runtime/MeshSafe.cs
Normal file
19
Runtime/MeshSafe.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace MeshProcess
|
||||
{
|
||||
public struct MeshSafe
|
||||
{
|
||||
public Vector3[] Vertices;
|
||||
public int[] Triangles;
|
||||
|
||||
public Mesh ToMesh()
|
||||
{
|
||||
return new Mesh
|
||||
{
|
||||
vertices = Vertices,
|
||||
triangles = Triangles
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/MeshSafe.cs.meta
Normal file
2
Runtime/MeshSafe.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 182d52cf7acb8c2409432384bf11f643
|
||||
@@ -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,
|
||||
¶meters);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user