参考文章: mesh的简介 创建mesh 简单多边形三角化(暴力)(削耳朵)
偶尔需要依据多边形创建平面mesh,所以在网上浏览了很多资料,再修修补补才凑出一段代码。
[ContextMenu("Create_Mesh_For_PolygonCollider2D")]
void Create_Mesh_For_PolygonCollider2D()
{
PolygonCollider2D pc = GetComponent<PolygonCollider2D>();
Vector3[] v_vertices = new Vector3[pc.points.Length];
for (int i = 0; i < pc.points.Length; i++)
{
v_vertices[i] = new Vector3(pc.points[i].x, pc.points[i].y, 0);
}
DoCreatPloygonMesh(v_vertices);
}
void DoCreatPloygonMesh(Vector3[] s_Vertives)
{
GameObject tPolygon = new GameObject("tPolygon");
tPolygon.transform.parent = transform;
tPolygon.AddComponent<MeshFilter>();
tPolygon.AddComponent<MeshRenderer>();
Mesh tMesh = new Mesh();
Vector3[] tVertices = s_Vertives;
List<int> tTriangles = Calculate_Triangles_CutEar_Method(tVertices);
tMesh.vertices = tVertices;
tMesh.triangles = tTriangles.ToArray();
tMesh.RecalculateBounds();
tMesh.RecalculateNormals();
tPolygon.GetComponent<MeshFilter>().mesh = tMesh;
tPolygon.transform.localPosition = Vector3.zero;
}
List<int> Calculate_Triangles_CutEar_Method(Vector3[] s_Vertives)
{
List<Vector3> v_vertives = new List<Vector3>();
for (int i = 0; i < s_Vertives.Length; i++)
{
v_vertives.Add(new Vector3(s_Vertives[i].x, s_Vertives[i].y, i));
}
int sn = 0;
List<int> tTriangles = new List<int>();
int layer = 0;
while (v_vertives.Count > 3 && layer < 1000)
{
List<Vector2> v_points = new List<Vector2>();
for (int i = 0; i < v_vertives.Count; i++)
{
v_points.Add(v_vertives[i]);
}
int sna = sn - 1;
if (sna < 0)
sna = v_points.Count - 1;
int snb = sn;
int snc = sn + 1;
if (snc >= v_points.Count)
snc = 0;
List<Vector2> v_triangle = new List<Vector2>();
v_triangle.Add(v_points[sna]);
v_triangle.Add(v_points[snb]);
v_triangle.Add(v_points[snc]);
bool bl_haveOtherPointInTriangle = false;
for (int i = 0; i < v_points.Count; i++)
{
if (i != sna && i != snb && i != snc)
{
if (Check_Point_InPolygon(v_points[i], v_triangle) == true)
{
bl_haveOtherPointInTriangle = true;
break;
}
}
}
bool bl_cutEar = false;
if (bl_haveOtherPointInTriangle == false)
{
Vector2 v_center = (v_triangle[0] + v_triangle[1] + v_triangle[2]) / 3;
if (Check_Point_InPolygon(v_center, v_points) == true)
{
int ta = (int)v_vertives[sna].z;
int tb = (int)v_vertives[snb].z;
int tc = (int)v_vertives[snc].z;
tTriangles.Add(tc);
tTriangles.Add(tb);
tTriangles.Add(ta);
v_vertives.RemoveAt(snb);
bl_cutEar = true;
}
}
if (bl_cutEar == false)
{
sn++;
}
if (sn >= v_vertives.Count)
sn = 0;
layer++;
}
if (v_vertives.Count == 3)
{
int ta = (int)v_vertives[0].z;
int tb = (int)v_vertives[1].z;
int tc = (int)v_vertives[2].z;
tTriangles.Add(tc);
tTriangles.Add(tb);
tTriangles.Add(ta);
}
Debug.Log("tTriangles.Count : " + tTriangles.Count + " layer : " + layer);
return tTriangles;
}
bool Check_Point_InPolygon(Vector2 v_check, List<Vector2> v_points)
{
int num_across = 0;
int count = 0;
int i = 0;
while (count < v_points.Count)
{
Vector2 v1 = v_points[i];
if (i == v_points.Count - 1)
i = -1;
Vector2 v2 = v_points[i + 1];
if (v1.y == v2.y)
{
}
else if (v1.y == v_check.y && v1.x >= v_check.x)
{
num_across++;
}
else if (v2.y == v_check.y && v2.x >= v_check.x)
{
num_across++;
}
else if (Mathf.Sign(v1.y - v_check.y) == Mathf.Sign(v2.y - v_check.y))
{
}
else
{
float x_line = v1.x + (v2.x - v1.x) * (v_check.y - v1.y) / (v2.y - v1.y);
if (x_line >= v_check.x)
{
num_across++;
}
}
i++;
count++;
}
if (num_across % 2 == 1)
return true;
return false;
}
注: 代码尝试次数不多,所以可能会出错 在绘制Polygon Collider 2D时,节点要逆时针排列
|