Esempio n. 1
0
    public static Mesh TriangulateSimple(Polygon2 polygon, Vector2 UVScale, Vector2 UVOffset)
    {
        Mesh result = null;

        //result = TriangulateAdvanced(polygon, UVScale, UVOffset);

        polygon.Normalize();

        result = UnityDefaultTriangulator.Create(polygon.points);

        return(result);
    }
Esempio n. 2
0
    public static Mesh Triangulate(Polygon2D polygon, Vector2 UVScale, Vector2 UVOffset, Triangulation triangulation)
    {
        Mesh result = null;

        switch (triangulation)
        {
        case Triangulation.Advanced:
            Polygon2D newPolygon = new Polygon2D(PreparePolygon(polygon));
            if (newPolygon.pointsList.Count < 3)
            {
                Debug.LogWarning("mesh is too small for advanced triangulation, using simplified triangulations instead (size: " + polygon.GetArea() + ")");

                result = TriangulateAdvanced(polygon, UVScale, UVOffset);

                return(result);
            }

            foreach (Polygon2D hole in polygon.holesList)
            {
                newPolygon.AddHole(new Polygon2D(PreparePolygon(hole)));
            }

            result = TriangulateAdvanced(newPolygon, UVScale, UVOffset);

            break;

        case Triangulation.Legacy:

            List <Vector2> list = new List <Vector2>();
            foreach (Vector2D p in polygon.pointsList)
            {
                list.Add(p.ToVector2());
            }
            result = UnityDefaultTriangulator.Create(list.ToArray());
            return(result);
        }

        return(result);
    }
Esempio n. 3
0
    public static Mesh Triangulate3D(Polygon2D polygon, float z, Vector2 UVScale, Vector2 UVOffset, Triangulation triangulation)
    {
        Mesh result = null;

        switch (triangulation)
        {
        case Triangulation.Advanced:
            Polygon2D newPolygon = new Polygon2D(PreparePolygon(polygon));
            foreach (Polygon2D hole in polygon.holesList)
            {
                newPolygon.AddHole(new Polygon2D(PreparePolygon(hole)));
            }

            if (newPolygon.pointsList.Count < 3)
            {
                if ((int)polygon.GetArea() == 0)
                {
                    List <Vector2> l = new List <Vector2>();
                    foreach (Vector2D p in polygon.pointsList)
                    {
                        l.Add(p.ToVector2());
                    }

                    result = UnityDefaultTriangulator.Create(l.ToArray());;

                    return(result);
                }
            }

            List <Vector3> sideVertices  = new List <Vector3>();
            List <int>     sideTriangles = new List <int>();
            int            vCount        = 0;
            foreach (Pair2D pair in Pair2D.GetList(polygon.pointsList))
            {
                Vector3 pointA = new Vector3((float)pair.A.x, (float)pair.A.y, 0);
                Vector3 pointB = new Vector3((float)pair.B.x, (float)pair.B.y, 0);
                Vector3 pointC = new Vector3((float)pair.B.x, (float)pair.B.y, 1);
                Vector3 pointD = new Vector3((float)pair.A.x, (float)pair.A.y, 1);

                sideVertices.Add(pointA);
                sideVertices.Add(pointB);
                sideVertices.Add(pointC);
                sideVertices.Add(pointD);

                sideTriangles.Add(vCount + 2);
                sideTriangles.Add(vCount + 1);
                sideTriangles.Add(vCount + 0);

                sideTriangles.Add(vCount + 0);
                sideTriangles.Add(vCount + 3);
                sideTriangles.Add(vCount + 2);

                vCount += 4;
            }

            Mesh meshA = TriangulateAdvanced(newPolygon, UVScale, UVOffset);

            Mesh           meshB     = new Mesh();
            List <Vector3> verticesB = new List <Vector3>();
            foreach (Vector3 v in meshA.vertices)
            {
                verticesB.Add(new Vector3(v.x, v.y, v.z + z));
            }
            meshB.vertices  = verticesB.ToArray();
            meshB.triangles = meshA.triangles.Reverse().ToArray();


            Mesh mesh = new Mesh();
            mesh.vertices  = sideVertices.ToArray();
            mesh.triangles = sideTriangles.ToArray();

            List <Vector3> vertices = new List <Vector3>();
            foreach (Vector3 v in meshA.vertices)
            {
                vertices.Add(v);
            }
            foreach (Vector3 v in meshB.vertices)
            {
                vertices.Add(v);
            }
            foreach (Vector3 v in sideVertices)
            {
                vertices.Add(v);
            }
            mesh.vertices = vertices.ToArray();

            List <int> triangles = new List <int>();
            foreach (int p in meshA.triangles)
            {
                triangles.Add(p);
            }
            int count = meshA.vertices.Count();
            foreach (int p in meshB.triangles)
            {
                triangles.Add(p + count);
            }
            count = meshA.vertices.Count() + meshB.vertices.Count();
            foreach (int p in sideTriangles)
            {
                triangles.Add(p + count);
            }
            mesh.triangles = triangles.ToArray();

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            result = mesh;

            break;
        }

        return(result);
    }