参考文章:
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);
}
//***** 创建mesh
void DoCreatPloygonMesh(Vector3[] s_Vertives)
{
//新建一个空物体进行进行绘制自定义多边形
GameObject tPolygon = new GameObject("tPolygon");
tPolygon.transform.parent = transform;
//绘制所必须的两个组件
tPolygon.AddComponent<MeshFilter>();
tPolygon.AddComponent<MeshRenderer>();
//新申请一个Mesh网格
Mesh tMesh = new Mesh();
//存储所有的顶点
Vector3[] tVertices = s_Vertives;
//存储画所有三角形的点排序
List<int> tTriangles = Calculate_Triangles_CutEar_Method(tVertices);
//赋值多边形顶点
tMesh.vertices = tVertices;
//赋值三角形点排序
tMesh.triangles = tTriangles.ToArray();
//重新设置UV,法线
tMesh.RecalculateBounds();
tMesh.RecalculateNormals();
//将绘制好的Mesh赋值
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>(); //此处的v_vertives的z为s_Vertives的序号
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)
{
//从检查点向右做线,若碰到两点之间的线段,则num_across++
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时,节点要逆时针排列