Пример #1
0
 public static List <List <Vector2> > GetPaths(SVGPathElement svgElement)
 {
     return(GetPaths(SVGMatrix.Identity(), svgElement));
 }
Пример #2
0
 public static List <Vector2> GetPath(SVGPolygonElement svgElement)
 {
     return(GetPath(SVGMatrix.Identity(), svgElement));
 }
Пример #3
0
        public static bool CreatePolygon(List <List <Vector2> > inputShapes, SVGPaintable paintable, SVGMatrix matrix, out SVGLayer layer, bool isStroke = false)
        {
            layer = new SVGLayer();
            if (inputShapes == null || inputShapes.Count == 0)
            {
                return(false);
            }

            List <List <Vector2> > simplifiedShapes = new List <List <Vector2> >();
            PolyFillType           fillType         = PolyFillType.pftNonZero;

            if (paintable.fillRule == SVGFillRule.EvenOdd)
            {
                fillType = PolyFillType.pftEvenOdd;
            }
            simplifiedShapes = SVGGeom.SimplifyPolygons(inputShapes, fillType);
            if (simplifiedShapes == null || simplifiedShapes.Count == 0)
            {
                return(false);
            }

            AddInputShape(simplifiedShapes);

            Rect bounds   = GetRect(simplifiedShapes);
            Rect viewport = paintable.viewport;

            if (!isStroke)
            {
                switch (paintable.GetPaintType())
                {
                case SVGPaintMethod.SolidFill:
                {
                    Color        color     = Color.black;
                    SVGColorType colorType = paintable.fillColor.Value.colorType;
                    if (colorType == SVGColorType.Unknown || colorType == SVGColorType.None)
                    {
                        color.a          *= paintable.fillOpacity;
                        paintable.svgFill = new SVGFill(color);
                    }
                    else
                    {
                        color             = paintable.fillColor.Value.color;
                        color.a          *= paintable.fillOpacity;
                        paintable.svgFill = new SVGFill(color);
                    }

                    paintable.svgFill.fillType = FILL_TYPE.SOLID;
                    if (color.a == 1)
                    {
                        paintable.svgFill.blend = FILL_BLEND.OPAQUE;
                    }
                    else
                    {
                        paintable.svgFill.blend = FILL_BLEND.ALPHA_BLENDED;
                    }
                }
                break;

                case SVGPaintMethod.LinearGradientFill:
                {
                    SVGLinearGradientBrush linearGradBrush = paintable.GetLinearGradientBrush(bounds, matrix, viewport);
                    paintable.svgFill = linearGradBrush.fill;
                }
                break;

                case SVGPaintMethod.RadialGradientFill:
                {
                    SVGRadialGradientBrush radialGradBrush = paintable.GetRadialGradientBrush(bounds, matrix, viewport);
                    paintable.svgFill = radialGradBrush.fill;
                }
                break;

                case SVGPaintMethod.ConicalGradientFill:
                {
                    SVGConicalGradientBrush conicalGradBrush = paintable.GetConicalGradientBrush(bounds, matrix, viewport);
                    paintable.svgFill = conicalGradBrush.fill;
                }
                break;

                case SVGPaintMethod.PathDraw:
                {
                    Color        color     = Color.black;
                    SVGColorType colorType = paintable.fillColor.Value.colorType;
                    if (colorType == SVGColorType.Unknown || colorType == SVGColorType.None)
                    {
                        color.a          *= paintable.strokeOpacity;
                        paintable.svgFill = new SVGFill(color);
                    }
                    else
                    {
                        color             = paintable.fillColor.Value.color;
                        color.a          *= paintable.strokeOpacity;
                        paintable.svgFill = new SVGFill(color);
                    }

                    paintable.svgFill.fillType = FILL_TYPE.SOLID;
                    if (color.a == 1)
                    {
                        paintable.svgFill.blend = FILL_BLEND.OPAQUE;
                    }
                    else
                    {
                        paintable.svgFill.blend = FILL_BLEND.ALPHA_BLENDED;
                    }
                }
                break;

                default:
                    break;
                }
            }
            else
            {
                Color color = paintable.strokeColor.Value.color;
                color.a          *= paintable.strokeOpacity;
                paintable.svgFill = new SVGFill(color, FILL_BLEND.OPAQUE, FILL_TYPE.SOLID);
                if (color.a != 1f)
                {
                    paintable.svgFill.blend = FILL_BLEND.ALPHA_BLENDED;
                }
                paintable.svgFill.color = color;
            }

            LibTessDotNet.Tess            tesselation = new LibTessDotNet.Tess();
            LibTessDotNet.ContourVertex[] path;
            int pathLength;

            for (int i = 0; i < simplifiedShapes.Count; i++)
            {
                if (simplifiedShapes[i] == null)
                {
                    continue;
                }

                pathLength = simplifiedShapes[i].Count;
                path       = new LibTessDotNet.ContourVertex[pathLength];
                Vector2 position;
                for (int j = 0; j < pathLength; j++)
                {
                    position         = simplifiedShapes[i][j];
                    path[j].Position = new LibTessDotNet.Vec3 {
                        X = position.x, Y = position.y, Z = 0f
                    };
                }
                tesselation.AddContour(path, SVGImporter.LibTessDotNet.ContourOrientation.Clockwise);
            }

            tesselation.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3);

            int meshVertexCount = tesselation.Vertices.Length;

            layer.vertices = new Vector2[meshVertexCount];

            for (int i = 0; i < meshVertexCount; i++)
            {
                layer.vertices[i] = new Vector2(tesselation.Vertices[i].Position.X, tesselation.Vertices[i].Position.Y) * SVGAssetImport.meshScale;
            }

            int numTriangles = tesselation.ElementCount;

            layer.triangles = new int[numTriangles * 3];
            for (int i = 0; i < numTriangles; i++)
            {
                layer.triangles[i * 3]     = tesselation.Elements[i * 3];
                layer.triangles[i * 3 + 1] = tesselation.Elements[i * 3 + 1];
                layer.triangles[i * 3 + 2] = tesselation.Elements[i * 3 + 2];
            }

            layer.fill         = paintable.svgFill;
            layer.fill.opacity = paintable.opacity;
            if (layer.fill.opacity < 1f && layer.fill.blend == FILL_BLEND.OPAQUE)
            {
                layer.fill.blend = FILL_BLEND.ALPHA_BLENDED;
            }

            if (layer.fill.fillType == FILL_TYPE.GRADIENT && layer.fill.gradientColors != null)
            {
                layer.fill.color = Color.white;
            }
            else if (layer.fill.fillType == FILL_TYPE.TEXTURE)
            {
                layer.fill.color = Color.white;
            }

            viewport.x         *= SVGAssetImport.meshScale;
            viewport.y         *= SVGAssetImport.meshScale;
            viewport.size      *= SVGAssetImport.meshScale;
            layer.fill.viewport = viewport;

            if (layer.fill.transform != null)
            {
                SVGMatrix scaleMatrix = SVGMatrix.Identity().Scale(SVGAssetImport.meshScale);
                layer.fill.transform = scaleMatrix.Multiply(layer.fill.transform);
                layer.fill.transform = layer.fill.transform.Multiply(scaleMatrix.Inverse());
            }

            Vector2 boundsMin = bounds.min * SVGAssetImport.meshScale;
            Vector2 boundsMax = bounds.max * SVGAssetImport.meshScale;

            layer.bounds = new Rect(boundsMin.x,
                                    boundsMin.y,
                                    boundsMax.x - boundsMin.x,
                                    boundsMax.y - boundsMin.y);

            return(true);
        }