unity 点位绘制面片

2021-03-23  本文已影响0人  WOTTOW

效果:


image.png
image.png
image.png
image.png image.png
 public void CreatePlane(GameObject obj, List<Vector2> vertices2D)
    {
        Triangulator tr = new Triangulator(vertices2D);
        int[] indices = tr.Triangulate();

        //创建顶点
        Vector3[] vertices = new Vector3[vertices2D.Count];
        Vector2[] uvs= new Vector2[vertices2D.Count];

        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(vertices2D[i].x,0, vertices2D[i].y);
            uvs[i] = new Vector2(vertices2D[i].x / 1f, vertices2D[i].y / 1f);
        }
        // 设置游戏对象;   
        // 创建mesh
        Mesh msh = new Mesh();
        msh.vertices = vertices;
        msh.triangles = indices;
        msh.uv = uvs;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        obj.AddComponent<MeshRenderer>();
        MeshFilter filter = obj.AddComponent<MeshFilter>();
        filter.mesh = msh;

        obj.AddComponent<MeshCollider>();
        obj.GetComponent<MeshRenderer>().material = Resources.Load("Materials/Custom_Floor") as Material;
        obj.GetComponent<MeshRenderer>().material.color = Color.white;
    }

这里没有附加对三角面得排序

上一篇下一篇

猜你喜欢

热点阅读