Unity - Draw a regular pentagon grid

This paper describes the basic method Unity regular pentagon drawn mesh: calculating vertex information, information is provided to cover the triangle, arranged to create mesh

Rendering

  • The basic idea: calculated pentagon vertex coordinate information as an array, disposed triangles surround , and create a new configuration of the mesh vertices, triangle parameters , the final assignment to the current mesh
  • Project Implementation:
    • Create DrawPentagon.cs, linked with the object wherein the mesh (in this case Quad
    • Write code as follows:
    • View mesh information created
public class DrawPentagon : MonoBehaviour
{
    private Vector3[] newVertices;      //五边形顶点数组
    private int[] newTriangles;         //五边形网格内的三角形网格信息

    void Start()
    {
        //1. 创建五边形顶点坐标数组:顶点编号0~4
        newVertices = new Vector3[5] {
            Vector3.zero,
            new Vector3(Mathf.Cos(Mathf.Deg2Rad * 36), 0, Mathf.Sin(Mathf.Deg2Rad * 36)),
            new Vector3(2 * Mathf.Cos(Mathf.Deg2Rad * 36), 0, 0),
            new Vector3(Mathf.Cos(Mathf.Deg2Rad * 72) + 1, 0, -Mathf.Sin(Mathf.Deg2Rad * 72)),
            new Vector3(Mathf.Cos(Mathf.Deg2Rad * 72), 0, -Mathf.Sin(Mathf.Deg2Rad * 72))
            /*
             newVertices[0] = (0.0, 0.0, 0.0)
             newVertices[1] = (0.8, 0.0, 0.6)
             newVertices[2] = (1.6, 0.0, 0.0)
             newVertices[3] = (1.3, 0.0, -1.0)
             newVertices[4] = (0.3, 0.0, -1.0)          
             */
        };

        //2. 设根据已有的顶点编号设置三角形包围顺序,例如0,1,2顶点围成一个三角形;0,2,3顶点围成另一三角形
        newTriangles = new int[9] { 0, 1, 2, 0, 2, 3, 0, 3, 4 };


        //错误情况:三角形数量不足
        //newTriangles = new int[6] { 0, 1, 2, 0, 2, 3 };

        //错误情况:三角形覆盖面不全
        //newTriangles = new int[9] { 0, 1, 2, 1, 2, 3, 0, 3, 4 };


        //3. 创建mesh信息:顶点数据、三角形
        Mesh mesh = new Mesh
        {
            name = "Pentagon",
            vertices = newVertices,
            triangles = newTriangles           
        };
        GetComponent<MeshFilter>().mesh = mesh;
    }
}

And a schematic example of an error:

reference

Guess you like

Origin www.cnblogs.com/SouthBegonia/p/11788070.html