Esempio n. 1
0
 public PointData(Vector3 _pos, CurveData _curve, bool _moveTo = false)
 {
     pos    = _pos;
     curve  = _curve;
     moveTo = _moveTo;
 }
Esempio n. 2
0
        public PathData GetPointsFromPath(string d)
        {
            List <string>        commands   = GetCommands(d);
            List <List <float> > _params    = GetParams(d);
            List <PointData>     points     = new List <PointData> ();
            List <Vector3>       pathPoints = new List <Vector3> ();

            Vector3   moveTo = Vector3.zero;
            CurveData curve = null;
            Vector2   diff = Vector2.zero;
            float     x, y, x1, y1, x2, y2;

            for (int i = 0, l = commands.Count; i < l; i++)
            {
                string command          = commands [i];
                string upperCaseCommand = command.ToUpper();
                int    commandLength    = commandLengths [upperCaseCommand];
                bool   relative         = IsRelative(command);

                if (commandLength > 0)
                {
                    List <float> commandParams = _params [0];
                    _params.RemoveAt(0);

                    int iterations = commandParams.Count / commandLength;

                    for (int j = 0; j < iterations; j++)
                    {
                        PointData prevPoint = (points.Count != 0) ? points [points.Count - 1] : new PointData(Vector3.zero, null);

                        switch (upperCaseCommand)
                        {
                        case "M":
                            x = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            if (j == 0)
                            {
                                moveTo = new Vector3(x, y, 0f);
                                points.Add(new PointData(moveTo, null, true));

                                pathPoints.Add(moveTo);
                            }
                            else
                            {
                                Vector3 mpoint = new Vector3(x, y, 0f);
                                points.Add(new PointData(mpoint, null));

                                pathPoints.Add(mpoint);
                            }
                            break;

                        case "L":
                            x = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            Vector3 lpoint = new Vector3(x, y, 0f);
                            points.Add(new PointData(lpoint, null));

                            pathPoints.Add(lpoint);
                            break;

                        case "H":
                            x = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            Vector3 hpoint = new Vector3(x, prevPoint.pos.y, 0f);
                            points.Add(new PointData(hpoint, null));

                            pathPoints.Add(hpoint);
                            break;

                        case "V":
                            y = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            Vector3 vpoint = new Vector3(prevPoint.pos.x, y, 0f);
                            points.Add(new PointData(vpoint, null));

                            pathPoints.Add(vpoint);
                            break;

                        case "A":
                            // TODO: try to convert arc to points
                            curve      = new CurveData();
                            curve.type = "arc";
                            curve.rx   = commandParams [0];
                            commandParams.RemoveAt(0);
                            curve.ry = commandParams [0];
                            commandParams.RemoveAt(0);
                            curve.xAxisRotation = commandParams [0];
                            commandParams.RemoveAt(0);
                            curve.largeArcFlag = (int)commandParams [0];
                            commandParams.RemoveAt(0);
                            curve.sweepFlag = (int)commandParams [0];
                            commandParams.RemoveAt(0);

                            x = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            // 最後全体を、xAxisRotationする。
                            //Debug.Log (">>>>> prev:"+new Vector3 (prevPoint.pos.x, prevPoint.pos.y, 0f));
                            //Debug.Log (">>>>> current:"+new Vector3 (x, y, 0f));

                            points.Add(new PointData(new Vector3(x, y, 0f), curve));

                            /*
                             * for (let k of optionalArcKeys) {
                             *      if (points[ points.length - 1 ][ 'curve' ][ k ] === 0) {
                             *              delete points[ points.length - 1 ][ 'curve' ][ k ]
                             *      }
                             * }
                             */

                            break;

                        case "C":
                            x1 = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y1 = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            x2 = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y2 = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            curve      = new CurveData();
                            curve.type = "cubic";
                            curve.x1   = x1;
                            curve.y1   = y1;
                            curve.x2   = x2;
                            curve.y2   = y2;

                            x = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            points.Add(new PointData(new Vector3(x, y, 0f), curve));


                            getCurve(ref pathPoints, prevPoint.pos.x, prevPoint.pos.y, x1, y1, x2, y2, x, y, "cubic");

                            break;

                        case "S":
                            float sx2 = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            float sy2 = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            float sx = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            float sy = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            diff = Vector2.zero;

                            float sx1;
                            float sy1;
                            if (prevPoint.curve != null && prevPoint.curve.type == "cubic")
                            {
                                diff.x = Mathf.Abs(prevPoint.pos.x - prevPoint.curve.x2);
                                diff.y = Mathf.Abs(prevPoint.pos.y - prevPoint.curve.y2);
                                sx1    = prevPoint.pos.x < prevPoint.curve.x2 ? prevPoint.pos.x - diff.x : prevPoint.pos.x + diff.x;
                                sy1    = prevPoint.pos.y < prevPoint.curve.y2 ? prevPoint.pos.y - diff.y : prevPoint.pos.y + diff.y;
                            }
                            else
                            {
                                diff.x = Mathf.Abs(sx - sx2);
                                diff.y = Mathf.Abs(sy - sy2);
                                sx1    = prevPoint.pos.x;
                                sy1    = prevPoint.pos.y;
                            }

                            curve      = new CurveData();
                            curve.type = "cubic";
                            curve.x1   = sx1;
                            curve.y1   = sy1;
                            curve.x2   = sx2;
                            curve.y2   = sy2;

                            points.Add(new PointData(new Vector3(sx, sy, 0f), curve));

                            getCurve(ref pathPoints, prevPoint.pos.x, prevPoint.pos.y, sx1, sy1, sx2, sy2, sx, sy, "cubic");
                            break;

                        case "Q":
                            x1 = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y1 = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            curve      = new CurveData();
                            curve.type = "quadratic";
                            curve.x1   = x1;
                            curve.y1   = y1;

                            x = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            y = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            points.Add(new PointData(new Vector3(x, y, 0f), curve));

                            getCurve(ref pathPoints, prevPoint.pos.x, prevPoint.pos.y, x1, y1, 0f, 0f, x, y, "quadratic");
                            break;

                        case "T":
                            float tx = (relative ? prevPoint.pos.x : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);
                            float ty = (relative ? prevPoint.pos.y : 0f) + commandParams [0];
                            commandParams.RemoveAt(0);

                            float tx1;
                            float ty1;

                            if (prevPoint.curve != null && prevPoint.curve.type == "quadratic")
                            {
                                diff = new Vector2(
                                    Mathf.Abs(prevPoint.pos.x - prevPoint.curve.x1),
                                    Mathf.Abs(prevPoint.pos.y - prevPoint.curve.y1)
                                    );

                                tx1 = prevPoint.pos.x < prevPoint.curve.x1 ? prevPoint.pos.x - diff.x : prevPoint.pos.x + diff.x;
                                ty1 = prevPoint.pos.y < prevPoint.curve.y1 ? prevPoint.pos.y - diff.y : prevPoint.pos.y + diff.y;
                            }
                            else
                            {
                                tx1 = prevPoint.pos.x;
                                ty1 = prevPoint.pos.y;
                            }

                            curve      = new CurveData();
                            curve.type = "quadratic";
                            curve.x1   = tx1;
                            curve.y1   = ty1;

                            points.Add(new PointData(new Vector3(tx, ty, 0f), curve));

                            getCurve(ref pathPoints, prevPoint.pos.x, prevPoint.pos.y, tx1, ty1, 0f, 0f, tx, ty, "quadratic");

                            break;
                        }
                    }
                }
                else
                {
                    PointData prevPoint = (points.Count != 0) ? points [points.Count - 1] : new PointData(Vector3.zero, null);
                    if (prevPoint.pos.x != moveTo.x || prevPoint.pos.y != moveTo.y)
                    {
                        Vector3 moveToPoint = new Vector3(moveTo.x, moveTo.y, 0f);
                        points.Add(new PointData(moveToPoint, null));
                        pathPoints.Add(moveToPoint);
                    }
                }
            }

            PathData pdata = new PathData();

            pdata.svgPoints = points;
            pdata.points    = pathPoints;

            return(pdata);
        }