Esempio n. 1
0
    public DragLines(Vector3[] pts, float offset, bool doBeizer = false)
    {
        List <Vector3> newDragline = new List <Vector3>();

        if (pts.Length == 2)
        {
            Vector2 o1 = pts[0].to2DwoY();
            Vector2 o2 = pts[1].to2DwoY();

            Vector2 o1o2 = o2 - o1;

            Vector2 po1o2 = o1o2.PerpClockWise();
            po1o2.Normalize();

            Vector2 n1 = o1 - po1o2 * offset;
            Vector2 n2 = o2 - po1o2 * offset;

            List <int> vertexRelation = new List <int>();

            newDragline.Add(n1.to3DwY(pts[0].y));
            vertexRelation.Add(0);
            newDragline.Add(n2.to3DwY(pts[1].y));
            vertexRelation.Add(1);
        }
        else
        {
            Vector2 o1 = pts[0].to2DwoY();
            Vector2 o2 = pts[1].to2DwoY();

            Vector2 o1o2 = o2 - o1;

            Vector2 po1o2 = o1o2.PerpClockWise();
            po1o2.Normalize();

            Vector2 n1 = o1 - po1o2 * offset;
            Vector2 n2 = o2 - po1o2 * offset;

            List <int> vertexRelation = new List <int>();

            newDragline.Add(n1.to3DwY(pts[0].y));
            vertexRelation.Add(0);

            for (int i = 1; i < pts.Length - 1; i++)
            {
                o2 = pts[i].to2DwoY();
                Vector2 o3 = pts[i + 1].to2DwoY();

                Vector2 o2o3 = o3 - o2;

                Vector2 po2o3 = o2o3.PerpClockWise();

                po2o3.Normalize();

                Vector2 n3 = o2 - po2o3 * offset;
                Vector2 n4 = o3 - po2o3 * offset;

                Vector output;
                if (MiscFunc.Intersects(n1.toV(), n2.toV(), n3.toV(), n4.toV(), out output))
                {
                    Vector2 isc = output.toV2();
                    Vector2 n5  = new Ray2D(n1, isc - n1).GetPoint((isc - n1).magnitude - (n2 - isc).magnitude);
                    Vector2 n6  = new Ray2D(n4, isc - n4).GetPoint((isc - n4).magnitude - (n3 - isc).magnitude);

                    if (doBeizer)
                    {
                        Vector2[] res = MiscFunc.DoBezier(new Vector2[] { n5, isc, n6 }, 2, 40);

                        for (int p = 0; p < res.Length; p++)
                        {
                            newDragline.Add(res[p].to3DwY(pts[i].y));
                            vertexRelation.Add(i);
                        }

                        n1 = res[res.Length - 1]; //This one is correct for sure
                        n2 = n4;                  //This one might get fixed next iteration
                    }
                    else
                    {
                        newDragline.Add(isc.to3DwY(pts[i].y));
                        vertexRelation.Add(i);
                        n1 = isc;   //This one is correct for sure
                        n2 = n4;    //This one might get fixed next iteration
                    }
                }
                else
                {
                    Vector2 isc;
                    if (!MiscFunc.IntersectRay2D(n1, n2 - n1, n4, n3 - n4, out isc))
                    {
                        Debug.LogError("Did not intersect");
                    }

                    if (doBeizer)
                    {
                        Vector2[] res = MiscFunc.DoBezier(new Vector2[] { n2, isc, n3 }, 2, 20);

                        for (int p = 0; p < res.Length; p++)
                        {
                            newDragline.Add(res[p].to3DwY(pts[i].y));
                            vertexRelation.Add(i);
                        }

                        n1 = res[res.Length - 1]; //This one is correct for sure
                        n2 = n4;                  //This one might get fixed next iteration
                    }
                    else
                    {
                        newDragline.Add(isc.to3DwY(pts[i].y));
                        vertexRelation.Add(i);

                        n1 = isc;   //This one is correct for sure
                        n2 = n4;    //This one might get fixed next iteration
                    }
                }


                if (i == pts.Length - 2)
                {
                    newDragline.Add(n4.to3DwY(pts[i + 1].y));
                    vertexRelation.Add(i + 1);
                }
            }
        }
        points = newDragline.ToArray();
    }