Beispiel #1
0
        /// <summary>
        /// Constructs a mesh from the given polygon via <see cref="Polygon.Triangulate()"/>. <para/>
        /// UV coordinates are the normalized polygon within its own bounds.
        /// </summary>
        /// <param name="polygon">Some polygon.</param>
        /// <returns>A new mesh representign the 'filled' space of the polygon.</returns>
        public static Mesh CreateFromPolygon(IReadOnlyList <Vector> polygon)
        {
            if (polygon is null)
            {
                throw new ArgumentNullException(nameof(polygon));
            }

            var mesh = new Mesh();

            // Compute polygon bounds
            var bounds = Rectangle.FromPoints(polygon);

            // Add polygon vertices
            foreach (var pt in polygon)
            {
                var uv = (pt - bounds.Min) / (Vector)bounds.Size;
                mesh._vertices.Add(new Vertex(pt, uv));
            }

            // Add triangle indices
            foreach (var(a, b, c) in PolygonTools.TriangulateIndices(polygon))
            {
                mesh._indices.Add((ushort)a);
                mesh._indices.Add((ushort)b);
                mesh._indices.Add((ushort)c);
            }

            return(mesh);
        }