예제 #1
0
    public List <Vector3> MakeSpline(List <Vector3> controlPoints, bool closedLoop)
    {
        ControlPoints = controlPoints;

        List <Vector3> points = new List <Vector3>();       //All points of the spline

        int closedAdjustment = closedLoop ? 0 : 1;

        // First for loop goes through each individual control point and connects it to the next, so 0-1, 1-2, 2-3 and so on
        for (int i = 0; i < controlPoints.Count - closedAdjustment; i++)
        {
            //The 4 points on my catmull spline
            Vector3 point1, point2, point3, point4;

            //The two points to interpolate between
            point2 = controlPoints[i];
            point3 = (closedLoop == true && i == controlPoints.Count - 1) ? controlPoints[0] : controlPoints[i + 1];

            //The first handle/anchor thingy
            if (i == 0 && !closedLoop)
            {
                //If its the first point, make the point up
                point1 = point3 - point2;
            }
            else
            {
                //This will loop back round
                point1 = controlPoints[((i - 1) + controlPoints.Count) % controlPoints.Count];
            }

            //The second handle/anchor thingy
            if (i >= controlPoints.Count - 2 && !closedLoop)
            {
                //If we're on the last point, make it up
                point4 = point3 - point2;
            }
            else
            {
                point4 = controlPoints[(i + 2) % controlPoints.Count];
            }

            float pointStep = 1.0f / CurveResolution;

            if (i == controlPoints.Count - 1 && closedLoop)
            {
                pointStep = 1.0f / (CurveResolution - 1);
            }

            // Second loop actually creates the spline for this particular segment
            for (int j = 0; j < CurveResolution; j++)
            {
                float   t        = j * pointStep;
                Vector3 position = CatmullRom.GetCatmullRomPosition(point1, point2, point3, point4, t, out var tangent, alpha);
                points.Add(position);
            }
        }

        return(SplitCurveEvenly(points));
    }