public static void AddLines(Mesh mesh, IList<LineF> lines) { for (int i = 0; i < lines.Count; i++) { Vector3 v; v = new Vector3(lines[i][0].X, lines[i][0].Y, 0); int index0 = mesh.AddVertex(new Vertex(v, new Vector2())); v = new Vector3(lines[i][1].X, lines[i][1].Y, 0); int index1 = mesh.AddVertex(new Vertex(v, new Vector2())); mesh.AddTriangle(index0, index1, index1); } }
private static void AddLineWidth(Mesh mesh, Vector2 v0, Vector2 v1, float width) { Vector2[] vectors = PolygonFactory.CreateLineWidth(new LineF(v0, v1), width); Vertex[] vertices = new Vertex[vectors.Length]; for (int i = 0; i < vectors.Length; i++) { vertices[i] = new Vertex(vectors[i]); } int index = mesh.AddVertexRange(vertices); mesh.AddTriangle(index + 2, index + 1, index); mesh.AddTriangle(index, index + 3, index + 2); }
public static Model CreateCircle(Vector3 origin, float radius, int detail) { Debug.Assert(detail >= 3, "Detail must be greater or equal to 3."); Mesh mesh = new Mesh(); for (int i = 0; i < detail; i++) { double rad = Math.PI * 2 * i / detail; Vector3 pos = new Vector3((float)Math.Cos(rad), (float)Math.Sin(rad), 0) * radius + origin; Vector2 textureCoord = new Vector2((float)(1 + Math.Cos(rad) / 2), (float)(1 + Math.Sin(rad) / 2)); mesh.Vertices.Add(new Vertex(pos, textureCoord)); } for (int i = 0; i < detail - 1; i++) { if (i == detail - 1 - i || i + 1 == detail - 1 - i) { continue; } mesh.AddTriangle(i, i + 1, detail - 1 - i); } Model model = new Model(mesh); return model; }
public static Model CreateLineStrip(Vector2[] vertices, Vector3[] colors) { Debug.Assert(vertices.Length >= 2); Mesh mesh = new Mesh(); for (int i = 0; i < vertices.Length - 1; i++) { Vector3 v, color = new Vector3(); if (colors != null) { color = colors[i]; } v = new Vector3(vertices[i].X, vertices[i].Y, 0); int index0 = mesh.AddVertex(new Vertex(v, new Vector2(), color)); v = new Vector3(vertices[i + 1].X, vertices[i + 1].Y, 0); int index1 = mesh.AddVertex(new Vertex(v, new Vector2(), color)); mesh.AddTriangle(index0, index1, index1); } Model model = new Model(mesh); model.Wireframe = true; return model; }
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; }