Reference article:
MESH profile
Create mesh
Simple polygon triangle (violence)(cut ears)
Occasionally need to create a plane Mesh based on the polygon, so you browse a lot of information online, and then repair the supplement to make up a piece of code.
[ContextMenu("Create_Mesh_For_PolygonCollider2D")]
void Create_Mesh_For_PolygonCollider2D()
{
PolygonCollider2D pc = GetComponent<PolygonCollider2D>();
// Get the vertex of the polygon collision body
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);
}
// ***** Create mesh
void DoCreatPloygonMesh(Vector3[] s_Vertives)
{
// Create an empty object for drawing custom polygon
GameObject tPolygon = new GameObject("tPolygon");
tPolygon.transform.parent = transform;
// Two components necessary for drawing
tPolygon.AddComponent<MeshFilter>();
tPolygon.AddComponent<MeshRenderer>();
// Apply for a Mesh grid
Mesh tMesh = new Mesh();
// Storage all vertices
Vector3[] tVertices = s_Vertives;
// Storage painting all triangle sort
List<int> tTriangles = Calculate_Triangles_CutEar_Method(tVertices);
// Assignment of polygonal apex
tMesh.vertices = tVertices;
// Sort the triangle point of assignment
tMesh.triangles = tTriangles.ToArray();
// Set UV, French line
tMesh.RecalculateBounds();
tMesh.RecalculateNormals();
// Assign the drawn MESH
tPolygon.GetComponent<MeshFilter>().mesh = tMesh;
tPolygon.transform.localPosition = Vector3.zero;
}
// ************* calculate and get triangular array-cutting ears, please refer to the article in detail
List<int> Calculate_Triangles_CutEar_Method(Vector3[] s_Vertives)
{
List<Vector3> v_vertives = new List<Vector3>(); // The Z of v_vertives here is the serial number of 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;
}
// ************ determine whether it is in polygonal, the ray method
// ************ 2 launches a ray from the checkpoint. If the number of edges encountered in the rays is a strange number, the point is inside the polygon
bool Check_Point_InPolygon(Vector2 v_check, List<Vector2> v_points)
{
// Make line to the right from the checkpoint.
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;
}
Note:
The number of codes is not many times, so there may be errors
When drawing Polygon Collider 2D, the node must be arranged counterclockwise