void Triangulate(List<Dictionary<int, int>> contours, Utils.Plane plane, List<Vector3>[] vertices, List<Vector3>[] normals, List<Vector2>[] uvs, List<int>[] triangles, bool uvCutMesh) { if (contours.Count == 0 || contours[0].Count < 3) { return; } // prepare plane matrix var m = plane.GetPlaneMatrix(); var mInv = m.inverse; var zShit = 0.0f; var polygons = new List<Polygon>(contours.Count); // construct polygons from contours Polygon highAreaPoly = null; foreach (var ctr in contours) { var polygonPoints = new Vector2[ctr.Count]; var j = 0; foreach (var i in ctr.Values) { var p = mInv*vertices[0][i]; polygonPoints[j++] = p; // save z-coordinate zShit = p.z; } var polygon = new Polygon(polygonPoints); polygons.Add(polygon); if (highAreaPoly == null || highAreaPoly.Area < polygon.Area) { highAreaPoly = polygon; } } MeshUtils.Assert(polygons.Count > 0, "Zero polygons!"); // test for holes if (polygons.Count > 0) { var polyToRemove = new List<Polygon>(); foreach (var polygon in polygons) { if (polygon != highAreaPoly) { if (highAreaPoly.IsPointInside(polygon.Points[0])) { highAreaPoly.AddHole(polygon); polyToRemove.Add(polygon); } } } foreach (var polygon in polyToRemove) { polygons.Remove(polygon); } } var vertCounter0 = vertices[0].Count; var vertCounter1 = vertices[1].Count; foreach (var polygon in polygons) { var indices = polygon.Triangulate(); // get polygon bounding square size var min = Mathf.Min(polygon.Min.x, polygon.Min.y); var max = Mathf.Max(polygon.Max.x, polygon.Max.y); var polygonSize = min - max; // MeshUtils.Log("PolygonSize: " + polygonSize + " " + polygon.Min + " " + polygon.Max); foreach (var polyPoint in polygon.Points) { var p = m * new Vector3(polyPoint.x, polyPoint.y, zShit); vertices[0].Add(p); vertices[1].Add(p); normals[0].Add(-plane.Normal); normals[1].Add(plane.Normal); if (uvCutMesh) { uvs[0].Add(new Vector2((polyPoint.x - min) / polygonSize, (polyPoint.y - min) / polygonSize)); uvs[1].Add(new Vector2((polyPoint.x - min) / polygonSize, (polyPoint.y - min) / polygonSize)); } else { uvs[0].Add(Vector2.zero); uvs[1].Add(Vector2.zero); } } var indicesCount = indices.Count; var j = indicesCount - 1; for (int i = 0; i < indicesCount; i++) { triangles[0].Add(vertCounter0 + indices[i]); triangles[1].Add(vertCounter1 + indices[j]); j--; } vertCounter0 += polygon.Points.Length; vertCounter1 += polygon.Points.Length; } }