private void Start()
    {
        Vector3[]  vertices = new Vector3[pointsParent.childCount];
        List <int> indices  = new List <int>();

        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = pointsParent.GetChild(i).position;
            indices.Add(i);
        }

        Plane plane = new Plane(Vector3.back, 0);

        ConvexHullAlgorithm.Execute(ref indices, vertices, plane.normal);

        indices.Reverse();//反转列表,逆时针变顺时针

        /*Vector3[] vertices2=new Vector3[indices.Count];
         *      for(int i=0;i<indices.Count;i++){
         *              Vector3 vertex=vertices[indices[i]];
         *              vertex.z=0;
         *              vertices2[i]=vertex;
         *      }
         *      indices=TriangulationAlgorithm.WidelyTriangleIndex(vertices2);*/

        TriangulationAlgorithm.WidelyTriangleIndex(vertices, ref indices, plane);

        for (int i = 0; i < indices.Count; i++)
        {
            int index = indices[i];
            s_points.Add(vertices[index]);
        }
    }
Ejemplo n.º 2
0
    private void DrawPolygon()
    {
        mesh          = new Mesh();
        mesh.name     = "Polygon";
        mesh.vertices = points.ToArray();

        tris = TriangulationAlgorithm.WidelyTriangleIndex(new List <Vector3>(points), indexes).ToArray();

        mesh.triangles = tris;

        normals = new Vector3[mesh.vertices.Length];
        for (int i = 0; i < mesh.vertices.Length; ++i)
        {
            normals[i] = Vector3.back;
        }

        mesh.normals = normals;

        mesh.RecalculateBounds();
        mesh.RecalculateTangents();

        targetFilter.mesh = mesh;
    }