public static MeshInfo GetRoofInfo(PolygonLoops polygonLoops, float height) { Vector2[] outerLoop = polygonLoops.OuterLoop; Vector2[][] holeLoops = polygonLoops.InnerLoops; Sebastian.Geometry.Polygon polygon2D; if (holeLoops.Length == 0) { polygon2D = new Sebastian.Geometry.Polygon(outerLoop); } else { polygon2D = new Sebastian.Geometry.Polygon(outerLoop, holeLoops); } Sebastian.Geometry.Triangulator triangulator = new Sebastian.Geometry.Triangulator(polygon2D); int[] triangles = triangulator.Triangulate(); Vector2[] vertices2D = polygon2D.points; Vector3[] vertices3D = new Vector3[vertices2D.Length * 2]; int i = 0; foreach (var vertex2D in vertices2D) { vertices3D[i] = new Vector3(vertex2D.x, height, vertex2D.y); i++; } Vector2[] uvs = new Vector2[vertices3D.Length]; for (int k = 0; k < uvs.Length; k++) { uvs[k] = new Vector3(vertices3D[k].x, vertices3D[k].z); } return(new MeshInfo(vertices3D, triangles.ToArray(), uvs)); }
public void Process() { // Generate array of valid shape data CompositeShapeData[] eligibleShapes = shapes.Select(x => new CompositeShapeData(x.points.ToArray())).Where(x => x.IsValidShape).ToArray(); // Set parents for all shapes. A parent is a shape which completely contains another shape. for (int i = 0; i < eligibleShapes.Length; i++) { for (int j = 0; j < eligibleShapes.Length; j++) { if (i == j) { continue; } if (eligibleShapes[i].IsParentOf(eligibleShapes[j])) { eligibleShapes[j].parents.Add(eligibleShapes[i]); } } } // Holes are shapes with an odd number of parents. CompositeShapeData[] holeShapes = eligibleShapes.Where(x => x.parents.Count % 2 != 0).ToArray(); foreach (CompositeShapeData holeShape in holeShapes) { // The most immediate parent (i.e the smallest parent shape) will be the one that has the highest number of parents of its own. CompositeShapeData immediateParent = holeShape.parents.OrderByDescending(x => x.parents.Count).First(); immediateParent.holes.Add(holeShape); } // Solid shapes have an even number of parents CompositeShapeData[] solidShapes = eligibleShapes.Where(x => x.parents.Count % 2 == 0).ToArray(); foreach (CompositeShapeData solidShape in solidShapes) { solidShape.ValidateHoles(); } // Create polygons from the solid shapes and their associated hole shapes Polygon[] polygons = solidShapes.Select(x => new Polygon(x.polygon.points, x.holes.Select(h => h.polygon.points).ToArray())).ToArray(); // Flatten the points arrays from all polygons into a single array, and convert the vector2s to vector3s. vertices = polygons.SelectMany(x => x.points.Select(v2 => new Vector3(v2.x, height, v2.y))).ToArray(); // Triangulate each polygon and flatten the triangle arrays into a single array. List <int> allTriangles = new List <int>(); int startVertexIndex = 0; for (int i = 0; i < polygons.Length; i++) { Triangulator triangulator = new Triangulator(polygons[i]); int[] polygonTriangles = triangulator.Triangulate(); for (int j = 0; j < polygonTriangles.Length; j++) { allTriangles.Add(polygonTriangles[j] + startVertexIndex); } startVertexIndex += polygons[i].numPoints; } triangles = allTriangles.ToArray(); }