Ejemplo n.º 1
0
        public static List <float[]> GetPoints(CoreSvg.Models.Path path)
        {
            var commands = GetPathCommands(path);
            var result   = new List <float[]>();

            foreach (var command in commands)
            {
                var points = GetPoints(command, result);

                result.AddRange(points);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static SvgMeshData GetMeshData(CoreSvg.Models.Path path, List <float[]> points, Style style, bool constrained = true)
        {
            var triangulation = points.Count > 4 ? (constrained ? GetConstrainedDelaunayTriangulation(points) : GetDelaunayIndexes(points)) : null;
            var data          = triangulation is null?GetVertices(points).ToList() : GetVertices(triangulation.Vertices.SelectMany(a => a).ToArray()).ToList();

            var meshIndices = triangulation is null ? (data.Count == 3) ? new List <int> {
                0, 1, 2
            } : (data.Count == 4) ? new List <int> {
                0, 1, 2, 0, 2, 3
            } : new List <int> {
            } : triangulation.Triangles;

            if (style.LinearGradient != null)
            {
                var gradient1         = new float[] { (float)style.LinearGradient.X1, (float)style.LinearGradient.Y1, 0f };
                var gradient2         = new float[] { (float)style.LinearGradient.X2, (float)style.LinearGradient.Y2, 0f };
                var gradientTransform = StyleHelper.GetTransform(style.LinearGradient.GradientTransform);
                var translate         = gradientTransform.Translate.Add(style.Transform.Translate);
                var scale             = new float[] { style.Transform.Scale.X(), style.Transform.Scale.Y(), 0f };
                gradient1 = gradient1.Add(new float[] { translate.X(), translate.Y(), 0f }).MultiplyComponents(scale);
                gradient2 = gradient2.Add(new float[] { translate.X(), translate.Y(), 0f }).MultiplyComponents(scale);

                data.ForEach(v =>
                {
                    var current = new float[] { v.PosX, v.PosY, 0f };

                    gradient1.SegmentClosestPoint(gradient2, current, out var offset);

                    var stop  = style.LinearGradient.Stops.FirstOrDefault(a => a.Offset == offset);
                    var color = Color.Black;

                    if (stop is null)
                    {
                        var stops = style.LinearGradient.Stops.OrderBy(a => Math.Abs(a.Offset - offset)).Take(2).ToList();

                        var stopStyle  = StyleHelper.GetStyle(stops[0].Style, null, null);
                        var stopColor1 = Color.FromArgb((byte)(stopStyle.StopOpacity * 255), stopStyle.StopColor);

                        stopStyle      = StyleHelper.GetStyle(stops[1].Style, null, null);
                        var stopColor2 = Color.FromArgb((byte)(stopStyle.StopOpacity * 255), stopStyle.StopColor);

                        var stopOffsetDif = stops[1].Offset - stops[0].Offset;
                        var stop1Offset   = (offset - stops[0].Offset) / stopOffsetDif;
                        var stop2Offset   = 1f - stop1Offset;

                        color = Color.FromArgb((byte)(stopColor1.A * stop1Offset) + (byte)(stopColor2.A * stop2Offset),
                                               (byte)(stopColor1.R * stop1Offset) + (byte)(stopColor2.R * stop2Offset),
                                               (byte)(stopColor1.G * stop1Offset) + (byte)(stopColor2.G * stop2Offset),
                                               (byte)(stopColor1.B * stop1Offset) + (byte)(stopColor2.B * stop2Offset));
                    }

                    v.R = color.R / 255f;
                    v.G = color.G / 255f;
                    v.B = color.B / 255f;
                    v.A = color.A / 255f;
                });
            }
            else
            {
                data.ForEach(v =>
                {
                    v.R = style.Fill.R / 255f;
                    v.G = style.Fill.G / 255f;
                    v.B = style.Fill.B / 255f;
                    v.A = style.Fill.A / 255f;
                });
            }

            return(new SvgMeshData()
            {
                Vertices = data,
                Indices = meshIndices
            });
        }