Ejemplo n.º 1
0
        public static void TesselateStroke(List <List <Vector2> > inputShapes, Color32 color, out List <List <Vector2> > simplifiedShapes, out Vector3[] vertices, out int[] triangles, out Color32[] colors32)
        {
            simplifiedShapes = null;
            vertices         = null;
            triangles        = null;
            colors32         = null;

            if (inputShapes == null || inputShapes.Count == 0)
            {
                return;
            }

            int i, j;

            simplifiedShapes = new List <List <Vector2> >();

            PolyFillType fillType = PolyFillType.pftNonZero;

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

                List <List <Vector2> > output = SVGGeom.SimplifyPolygon(inputShapes[i], fillType);
                if (output == null || output.Count == 0)
                {
                    simplifiedShapes.Add(inputShapes[i]);
                }
                else
                {
                    simplifiedShapes.AddRange(output);
                }
            }

            LibTessDotNet.Tess tesselation = new LibTessDotNet.Tess();

            LibTessDotNet.ContourVertex[] path;
            for (i = 0; i < simplifiedShapes.Count; i++)
            {
                if (simplifiedShapes[i] == null || simplifiedShapes[i].Count < 2)
                {
                    continue;
                }

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

            tesselation.Tessellate(LibTessDotNet.WindingRule.Positive, LibTessDotNet.ElementType.Polygons, 3);
            if (tesselation.Vertices == null || tesselation.Vertices.Length == 0)
            {
                return;
            }

            int numVertices  = tesselation.Vertices.Length;
            int numTriangles = tesselation.ElementCount * 3;

            triangles = new int[numTriangles];
            vertices  = new Vector3[numVertices];
            colors32  = new Color32[numVertices];

            for (i = 0; i < numVertices; i++)
            {
                vertices[i] = new Vector3(tesselation.Vertices[i].Position.X, tesselation.Vertices[i].Position.Y, 0f);
                colors32[i] = color;
            }
            for (i = 0; i < numTriangles; i += 3)
            {
                triangles[i]     = tesselation.Elements[i];
                triangles[i + 1] = tesselation.Elements[i + 1];
                triangles[i + 2] = tesselation.Elements[i + 2];
            }
        }
Ejemplo n.º 2
0
        public static List <List <Vector2> > StrokeShape(List <StrokeSegment[]> segments, float thickness, Color32 color, StrokeLineJoin lineJoin, StrokeLineCap lineCap, float miterLimit = 4f, float[] dashArray = null, float dashOffset = 0f, ClosePathRule closeLine = ClosePathRule.NEVER, float roundQuality = 10f)
        {
            if (segments == null || segments.Count == 0)
            {
                return(null);
            }

            float totalCurveLength = 0f;
            int   i, j;

            for (i = 0; i < segments.Count; i++)
            {
                if (segments[i] == null)
                {
                    continue;
                }
                for (j = 0; j < segments[i].Length; j++)
                {
                    totalCurveLength += segments[i][j].length;
                }
            }

            if (totalCurveLength == 0f)
            {
                return(null);
            }

            bool useDash;

            ProcessDashArray(ref dashArray, out useDash);

            ClosePathRule          closeSegments = closeLine;
            List <StrokeSegment[]> finalSegments = new List <StrokeSegment[]>();

            for (i = 0; i < segments.Count; i++)
            {
                if (segments[i] == null || segments[i].Length == 0)
                {
                    continue;
                }

                if (!useDash)
                {
                    finalSegments.Add(segments[i]);
                }
                else
                {
                    finalSegments.AddRange(CreateDashedStroke(segments[i], dashArray, dashOffset, ref closeSegments));
                }
            }

            if (finalSegments.Count > 0)
            {
                List <List <Vector2> > finalPoints = new List <List <Vector2> >();
                for (i = 0; i < finalSegments.Count; i++)
                {
                    List <Vector2> points = Stroke(finalSegments[i], thickness, color, lineJoin, lineCap, miterLimit, closeSegments, roundQuality);
                    if (points == null || points.Count < 2)
                    {
                        continue;
                    }

                    List <List <Vector2> > simplifiedShape = SVGGeom.SimplifyPolygon(points);
                    for (j = 0; j < simplifiedShape.Count; j++)
                    {
                        if (simplifiedShape[j] == null || simplifiedShape[j].Count == 0)
                        {
                            continue;
                        }
                        finalPoints.Add(simplifiedShape[j]);
                    }
                }
                return(finalPoints);
            }
            else
            {
                return(null);
            }
        }