Esempio n. 1
0
        public ISVGElement Reverse()
        {
            SVGLine result = new SVGLine(_closed);

            result._points = new List <Vector2>(_points);
            result._points.Reverse();

            return(result);
        }
        public static ISVGElement CreateRect(Rect r)
        {
            SVGLine line = new SVGLine(true);

            line.Add(new Vector2(r.xMin, r.yMin));
            line.Add(new Vector2(r.xMax, r.yMin));
            line.Add(new Vector2(r.xMax, r.yMax));
            line.Add(new Vector2(r.xMin, r.yMax));
            return(line);
        }
        public List <ISVGElement> RenderDashed(float aStepResolution, AnimationCurve aDashStrength, Vector2 aGapSize, Vector2 aDashSize)
        {
            List <ISVGElement> result = new List <ISVGElement>();
            SVGLine            curr   = new SVGLine(false);
            float   dist       = 0;
            float   nextSwitch = 0;
            bool    on         = true;
            Vector2 prevPt     = _points[0].point;

            int count = _closed ? _points.Count : _points.Count - 1;

            for (int i = 0; i < count; i++)
            {
                Vector2 p1     = _points[i].point;
                Vector2 p2     = _points[WrapI(i + 1)].point;
                Vector2 c1     = GetControl(i, true);
                Vector2 c2     = GetControl(WrapI(i + 1), false);
                float   length = BezierLength(p1, c1, c2, p2);
                float   str1   = aDashStrength.Evaluate(i / (count - 1f));
                float   str2   = aDashStrength.Evaluate(WrapI(i + 1) / (count - 1f));

                int steps = (int)(length / aStepResolution);
                for (int s = 0; s < steps; s++)
                {
                    float   pct = s / (float)steps;
                    float   str = Mathf.Lerp(str1, str2, pct);
                    Vector2 pt  = BezierPoint(p1, c1, c2, p2, s / (float)steps);
                    float   d   = (pt - prevPt).magnitude;
                    dist += d;

                    if (dist >= nextSwitch && str < 1)
                    {
                        if (on)
                        {
                            on         = false;
                            nextSwitch = dist + Mathf.Lerp(aGapSize.x, aGapSize.y, 1 - str);
                            curr.Add(pt);
                        }
                        else
                        {
                            on         = true;
                            nextSwitch = dist + Mathf.Lerp(aDashSize.x, aDashSize.y, 1 - str);
                            if (curr.Count > 1)
                            {
                                result.Add(curr);
                            }
                            curr = new SVGLine();
                        }
                    }

                    if (on)
                    {
                        curr.Add(pt);
                    }

                    prevPt = pt;
                }
            }
            if (curr.Count > 1)
            {
                result.Add(curr);
            }

            return(result);
        }