项目有一个对任意形状截面生成3维物体的需求,需要用到三角化的算法。如果截面是凸多边形,那么非常简单,以任意一点为起点按顺序切分即可:
/// <summary>
/// 局限性三角剖分
/// </summary>
/// <returns></returns>
public static int[] LimitedTriangleIndex(Vector3[] verts)
{
int len = verts.Length;
//若是闭环去除最后一点
if (len > 1 && MathUtil.IsEqual(verts[0], verts[len - 1]))
{
len--;
}
int triangleNum = len - 2;
int[] triangles = new int[triangleNum * 3];
for (int i = 0; i < triangleNum; i++)
{
int start = i * 3;
triangles[start] = 0;
triangles[start + 1] = i + 1;
triangles[start + 2] = i + 2;
}
return triangles;
}
而对于凹多边形这种方法就无能为力了: