Ejemplo n.º 1
0
        public static int AddPolygon(Mesh mesh, Poly2Tri.Polygon polygon, Vector3 offset = new Vector3())
        {
            int indexFirst = mesh.Vertices.Count;
            if (polygon == null)
            {
                return indexFirst;
            }
            int vertCountPrev = mesh.Vertices.Count;
            Vertex[] verts = new Vertex[polygon.Points.Count];
            List<int> indices = new List<int>();

            for (int i = 0; i < polygon.Points.Count; i++)
            {
                TriangulationPoint p = polygon.Points[i];
                Vector3 v = new Vector3((float)p.X, (float)p.Y, 0) + offset;
                float tx = (float)((p.X - polygon.MinX) / (polygon.MaxX - polygon.MinX));
                float ty = (float)((p.Y - polygon.MinY) / (polygon.MaxY - polygon.MinY));
                Vector2 tc = new Vector2(tx, ty);
                //verts[i] = new Vertex(v, tc);
                mesh.Vertices.Add(new Vertex(v, tc));
            }

            foreach (Poly2Tri.DelaunayTriangle t in polygon.Triangles)
            {
                int index0, index1, index2;
                index0 = polygon.IndexOf(t.Points._0);
                index1 = polygon.IndexOf(t.Points._1);
                index2 = polygon.IndexOf(t.Points._2);
                //Sometimes the index is -1 and I don't know why. Ignore those points.
                if (index0 < 0 || index1 < 0 || index2 < 0)
                {
                    continue;
                }
                mesh.AddTriangle(index0 + vertCountPrev, index1 + vertCountPrev, index2 + vertCountPrev);
            }
            return indexFirst;
        }
Ejemplo n.º 2
0
 public static Model CreatePolygon(Poly2Tri.Polygon polygon, Vector3 offset = new Vector3())
 {
     Mesh mesh = new Mesh();
     AddPolygon(mesh, polygon, offset);
     Model model = new Model(mesh);
     return model;
 }
Ejemplo n.º 3
0
 private static PolygonSet FromP2T(Poly2Tri.PolygonSet pset)
 {
     var result = new PolygonSet();
     foreach (var poly in pset.Polygons)
     {
         foreach (var tri in poly.Triangles)
         {
             var rtri = new Polygon();
             rtri.Add(new PolygonPoint { X = tri.Points[0].Xf, Y = tri.Points[0].Yf });
             rtri.Add(new PolygonPoint { X = tri.Points[1].Xf, Y = tri.Points[1].Yf });
             rtri.Add(new PolygonPoint { X = tri.Points[2].Xf, Y = tri.Points[2].Yf });
             result.Add(rtri);
         }
     }
     return result;
 }