需要根据点位生成道路mesh网格时用到的,2D方面可以满足要求,但3D方面有起伏就不太行,换了个简单的四边形切对角的方法。耳切法先记下,以后也许在别的地方还能用到。
using System.Collections.Generic;
using UnityEngine;
public class Polygon {
NodeList m_NodeList;
List<int> m_Triangles = new List<int>();
public Polygon(List<Vector3> vertices) {
if (vertices.Count <= 2) {
throw new System.InvalidOperationException("非多边形");
}
m_NodeList = new NodeList(vertices);
}
public int[] SimpleTriangulation() {
SimpleCutPolygon();
return m_Triangles.ToArray();
}
public int[] Triangulation() {
while (m_NodeList.GetCount >= 3) {
if (!CutPolygon()) {
throw new System.InvalidOperationException("点位非逆时针排布或围成图形非简单多边形");
}
}
return m_Triangles.ToArray();
}
private void SimpleCutPolygon() {
int nodeListCount