CRSpline.InterpConstantSpeed C# (CSharp) Method

InterpConstantSpeed() public static method

public static InterpConstantSpeed ( Vector3 pts, float t ) : Vector3
pts Vector3
t float
return Vector3
    public static Vector3 InterpConstantSpeed(Vector3[] pts, float t)
    {
        int numSections = pts.Length - 3;
        float mag = 0;
        float [] sizes = new float[pts.Length-1];
        for(var i = 0; i < pts.Length-1; i++)
        {
            var m = (pts[i+1] - pts[i]).magnitude;
            sizes[i] = m;
            mag += m;
        }

        int currPt=1;
        float s=0;
        double u = 0;
        do
        {
            double tAtBeginning = s/mag;
            double tAtEnd = (s + sizes[currPt+0])/mag;
            u = (t-tAtBeginning) / (tAtEnd - tAtBeginning);
            if(u<0 || u>1)
            {
                s+=sizes[currPt];
                currPt++;
            }
            else
                break;
        } while(currPt < numSections+1);
        u = Mathf.Clamp01((float)u);
        Vector3 a = pts[currPt-1];
        Vector3 b = pts[currPt + 0];
        Vector3 c = pts[currPt + 1];
        Vector3 d = pts[currPt + 2];

        return .5f * (
            (-a + 3f * b - 3f * c + d) * (float)(u * u * u)
            + (2f * a - 5f * b + 4f * c - d) * (float)(u * u)
            + (-a + c) * (float)u
            + 2f * b
        );
    }

Usage Example

Example #1
0
    public static Vector3 InterpConstantSpeed(Path pts, float t, Easing.EaseType easeType = Easing.EaseType.linear)
    {
        t = Easing.ease(easeType, t);

        if (pts.Length == 0)
        {
            return(Vector3.zero);
        }
        else if (pts.Length == 1)
        {
            return(pts[0]);
        }
        else if (pts.Length == 2)
        {
            return(Vector3.Lerp(pts[0], pts[1], t));
        }
        else if (pts.Length == 3)
        {
            return(QuadBez.Interp(pts[0], pts[2], pts[1], t));
        }
        else if (pts.Length == 4)
        {
            return(CubicBez.Interp(pts[0], pts[3], pts[1], pts[2], t));
        }
        else
        {
            return(CRSpline.InterpConstantSpeed(Wrap(pts), t));
        }
    }
All Usage Examples Of CRSpline::InterpConstantSpeed