void DisplayCatmullRomSpline(int pos)
    {
        //Clamp to allow looping
        Vector3[] pnt =
        {
            points[ClampListPos(pos - 1)].position,
            points[pos].position,
            points[ClampListPos(pos + 1)].position,
            points[ClampListPos(pos + 2)].position
        };
        //Just assign a tmp value to this
        Vector3 lastPos = Vector3.zero;

        //t is always between 0 and 1 and determines the resolution of the spline
        //0 is always at p1
        for (float t = 0; t < 1; t += 0.1f)
        {
            //Find the coordinates between the control points with a Catmull-Rom spline
            CatmullRomSpline spline = new CatmullRomSpline();
            Vector3          newPos = spline.GetPoint(t, pnt);
            //Cant display anything the first iteration
            if (t == 0)
            {
                lastPos = newPos;
                continue;
            }
            Gizmos.DrawLine(lastPos, newPos);
            lastPos = newPos;
        }
        //Also draw the last line since it is always less than 1, so we will always miss it
        Gizmos.DrawLine(lastPos, pnt[2]);
    }
    // Use this for initialization
    void Start()
    {
        Mesh mesh = new Mesh();

        CatmullRomSpline spline = new CatmullRomSpline();

        Vector3[]    pos = new Vector3[points.Count];
        Quaternion[] rot = new Quaternion[points.Count];

        for (int i = 0; i < points.Count; i++)
        {
            pos[i] = points[i].transform.position;
            rot[i] = points[i].transform.rotation;
        }

        Vector3 tangent;

        for (int i = 0; i < points.Count; i++)
        {
            tangent = Vector3.Cross(spline.GetTangent(i, pos), Vector3.forward);

            if (tangent.magnitude == 0)
            {
                tangent = Vector3.Cross(spline.GetTangent(i, pos), Vector3.up);
            }
            points[i].transform.rotation = Quaternion.Euler(tangent);
        }

        OrientedPoint[] oPoints = new OrientedPoint[(int)(1 / resolution)];
        for (float i = 0f; i < 1.0f; i += resolution)
        {
            if (i != 0f || i != 1f)
            {
                oPoints[(int)i * 10].position = spline.GetPoint(i, pos);
                tangent = Vector3.Cross(spline.GetTangent(i, pos), Vector3.forward);

                if (tangent.magnitude == 0)
                {
                    tangent = Vector3.Cross(spline.GetTangent(i, pos), Vector3.up);
                }
                oPoints[(int)i * 10].rotation = Quaternion.Euler(tangent);
            }
        }

        Vector2[] verts   = { new Vector2(0, 0), new Vector2(1, 0) };
        Vector2[] normals = { new Vector2(0, 1), new Vector2(0, 1) };
        int[]     lines   = { 0, 1 };

        Shape shape = new Shape(verts, normals, lines);

        Extrude extrusao = new Extrude(ref mesh, shape, oPoints);

        this.GetComponent <MeshFilter>().sharedMesh = mesh;
    }
    private Vector3 ShowPoint(int index)
    {
        Vector3 point      = t.TransformPoint(spline.GetPoint(index));
        float   firstPoint = index == 0 ? 2f : 1f;

        if (Handles.Button(point, q, 0.2f * firstPoint, 0.6f, Handles.DotCap))
        {
            selectedIndex = index;
        }

        Handles.color = Color.gray;
        if (selectedIndex == index)
        {
            EditorGUI.BeginChangeCheck();
            point = Handles.DoPositionHandle(point, q);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(spline, "CatmullRom Moved");
                EditorUtility.SetDirty(spline);
                spline.SetPoint(index, t.InverseTransformPoint(point));
            }
        }
        return(point);
    }
Exemplo n.º 4
0
 public static Vector3 SmoothTravel(this Vector3[] points, float t)
 {
     return(CatmullRomSpline.GetPoint(points, t, false));
 }
    public void OnSceneGUI()
    {
        spline = target as CatmullRomSpline;
        t      = spline.transform;
        q      = Tools.pivotRotation == PivotRotation.Local ? t.rotation : Quaternion.identity;

        /* Draw control points for each curve */
        for (int i = 0; i < spline.Curves; i += 3)
        {
            Vector3 point0 = ShowPoint(i);
            Vector3 point1 = ShowPoint(i + 1);
            Vector3 point2 = ShowPoint(i + 2);
            Vector3 point3 = ShowPoint(i + 3);

            Handles.color = Color.gray;
            Handles.DrawLine(point0, point1);
            Handles.DrawLine(point1, point2);
            Handles.DrawLine(point2, point3);
        }

        /* Draw the curve */
        for (int i = 0; i < spline.ControlPointsCount; ++i)
        {
            if ((i == 0 || i >= spline.ControlPointsCount - 2) && !spline.Loop)
            {
                continue;
            }
            else
            {
                int iMinusOne = i - 1, iPlusOne = i + 1, iPlusTwo = i + 2, points = spline.ControlPointsCount;

                /* We only care on negative values */
                if (i == 0)
                {
                    iMinusOne = spline.ControlPointsCount - 1;
                }
                /* Handle the overflow by modding around the array */
                iPlusOne = (i + 1) % points;
                iPlusTwo = (i + 2) % points;


                Vector3 p0 = t.TransformPoint(spline.GetPoint(iMinusOne));
                Vector3 p1 = t.TransformPoint(spline.GetPoint(i));
                Vector3 p2 = t.TransformPoint(spline.GetPoint(iPlusOne));
                Vector3 p3 = t.TransformPoint(spline.GetPoint(iPlusTwo));

                Vector3 startPoint = t.TransformPoint(spline.GetPoint(i));
                Handles.color = Color.green;
                Vector3 endPoint;
                for (int j = 0; j < steps; ++j)
                {
                    float norm = j / (float)steps;
                    endPoint = spline.GetCurvePoint(norm, p0, p1, p2, p3);
                    Handles.DrawLine(startPoint, endPoint);
                    startPoint = endPoint;
                }
                endPoint = spline.GetCurvePoint(1, p0, p1, p2, p3);
                Handles.DrawLine(startPoint, endPoint);
            }
        }
    }