Assets/SplineTool/Scripts/CorgiSpline/Spline.cs
/// <summary>
/// Returns a world space SplinePoint, given an input t. t is valid from 0 to 1. 
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
Returns a world space SplinePoint, given an input t. t is valid from 0 to 1.
Returns a world space SplinePoint given an input t . t is valid from 0 to 1 .
public SplinePoint GetPoint(float t)
        {
            if (Points.Length == 0)
            {
                return new SplinePoint();
            }
            t = Mathf.Clamp01(t);
            if (Mode == SplineMode.Linear)
            {
                var delta_t = 1f / Points.Length;
                var mod_t = Mathf.Repeat(t, delta_t);
                var inner_t = mod_t / delta_t;
                var index0 = Mathf.FloorToInt(t * Points.Length);
                var index1 = index0 + 1;
                index0 = Mathf.Clamp(index0, 0, Points.Length - 1);
                index1 = Mathf.Clamp(index1, 0, Points.Length - 1);
                if (index0 == Points.Length - 1)
                {
                    var firstPoint = Points[index0];
                    if (SplineSpace == Space.Self)
                    {
                        firstPoint = TransformSplinePoint(firstPoint);
                    }
                    return firstPoint;
                }
                var point0 = Points[index0];
                var point1 = Points[index1];
                var result = new SplinePoint();
                result.position = Vector3.Lerp(point0.position, point1.position, inner_t);
                result.rotation = Quaternion.Slerp(point0.rotation, point1.rotation, inner_t);
                result.scale = Vector3.Lerp(point0.scale, point1.scale, inner_t);
                if (SplineSpace == Space.Self)
                {
                    return TransformSplinePoint(result);
                }
                else
                {
                    return result;
                }
            }
            else if (Mode == SplineMode.Bezier)
            {
                var delta_t = 3f / Points.Length;
                var mod_t = Mathf.Repeat(t, delta_t);
                var inner_t = mod_t / delta_t;
                var index0 = Mathf.FloorToInt(t * Points.Length);
                index0 = Mathf.Clamp(index0, 0, Points.Length - 1);
                index0 = index0 - index0 % 3;
                var index1 = index0 + 1;
                var index2 = index0 + 2;
                var index3 = index0 + 3;
                if (index0 > Points.Length - 4)
                {
                    var lastPoint = Points[Points.Length - 1]; ;
                    if (SplineSpace == Space.Self)
                    {
                        lastPoint = TransformSplinePoint(lastPoint);
                    }
                    return lastPoint;
                }
                var point0 = Points[index0];
                var point1 = Points[index1];
                var point2 = Points[index2];
                var point3 = Points[index3];
                var result = CalculateBezierPoint(point0, point1, point2, point3, inner_t);
                if (SplineSpace == Space.Self)
                {
                    return TransformSplinePoint(result);
                }
                else
                {
                    return result;
                }
            }
            else if(Mode == SplineMode.BSpline)
            {
                var delta_t = 1f / Points.Length;
                var mod_t = Mathf.Repeat(t, delta_t);
                var inner_t = mod_t / delta_t;
                var index = Mathf.FloorToInt(t * Points.Length);
                    index = Mathf.Clamp(index, 0, Points.Length) - 1; 
                int index0;
                int index1;
                int index2;
                int index3;
                if(ClosedSpline)
                {
                    int mod_count = Points.Length - 1; 
                    index0 = ((index + 0) % (mod_count) + mod_count) % mod_count;
                    index1 = ((index + 1) % (mod_count) + mod_count) % mod_count;
                    index2 = ((index + 2) % (mod_count) + mod_count) % mod_count;
                    index3 = ((index + 3) % (mod_count) + mod_count) % mod_count;
                }
                else
                {
                    index0 = index + 0;
                    index1 = index + 1;
                    index2 = index + 2;
                    index3 = index + 3;
                    index0 = Mathf.Clamp(index0, 0, Points.Length - 1);
                    index1 = Mathf.Clamp(index1, 0, Points.Length - 1);
                    index2 = Mathf.Clamp(index2, 0, Points.Length - 1);
                    index3 = Mathf.Clamp(index3, 0, Points.Length - 1);
                }
                var point0 = Points[index0];
                var point1 = Points[index1];
                var point2 = Points[index2];
                var point3 = Points[index3];
                var result = CalculateBSplinePoint(point0, point1, point2, point3, inner_t);
                if (SplineSpace == Space.Self)
                {
                    return TransformSplinePoint(result);
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return new SplinePoint();
            }
        }
public SplinePoint GetPoint ( float t ) { if ( Points . Length == 0 ) { return new SplinePoint ( ) ; } t = Mathf . Clamp01 ( t ) ; if ( Mode == SplineMode . Linear ) { var delta_t = 1f / Points . Length ; var mod_t = Mathf . Repeat ( t , delta_t ) ; var inner_t = mod_t / delta_t ; var index0 = Mathf . FloorToInt ( t * Points . Length ) ; var index1 = index0 + 1 ; index0 = Mathf . Clamp ( index0 , 0 , Points . Length - 1 ) ; index1 = Mathf . Clamp ( index1 , 0 , Points . Length - 1 ) ; if ( index0 == Points . Length - 1 ) { var firstPoint = Points [ index0 ] ; if ( SplineSpace == Space . Self ) { firstPoint = TransformSplinePoint ( firstPoint ) ; } return firstPoint ; } var point0 = Points [ index0 ] ; var point1 = Points [ index1 ] ; var result = new SplinePoint ( ) ; result . position = Vector3 . Lerp ( point0 . position , point1 . position , inner_t ) ; result . rotation = Quaternion . Slerp ( point0 . rotation , point1 . rotation , inner_t ) ; result . scale = Vector3 . Lerp ( point0 . scale , point1 . scale , inner_t ) ; if ( SplineSpace == Space . Self ) { return TransformSplinePoint ( result ) ; } else { return result ; } } else if ( Mode == SplineMode . Bezier ) { var delta_t = 3f / Points . Length ; var mod_t = Mathf . Repeat ( t , delta_t ) ; var inner_t = mod_t / delta_t ; var index0 = Mathf . FloorToInt ( t * Points . Length ) ; index0 = Mathf . Clamp ( index0 , 0 , Points . Length - 1 ) ; index0 = index0 - index0 % 3 ; var index1 = index0 + 1 ; var index2 = index0 + 2 ; var index3 = index0 + 3 ; if ( index0 > Points . Length - 4 ) { var lastPoint = Points [ Points . Length - 1 ] ; ; if ( SplineSpace == Space . Self ) { lastPoint = TransformSplinePoint ( lastPoint ) ; } return lastPoint ; } var point0 = Points [ index0 ] ; var point1 = Points [ index1 ] ; var point2 = Points [ index2 ] ; var point3 = Points [ index3 ] ; var result = CalculateBezierPoint ( point0 , point1 , point2 , point3 , inner_t ) ; if ( SplineSpace == Space . Self ) { return TransformSplinePoint ( result ) ; } else { return result ; } } else if ( Mode == SplineMode . BSpline ) { var delta_t = 1f / Points . Length ; var mod_t = Mathf . Repeat ( t , delta_t ) ; var inner_t = mod_t / delta_t ; var index = Mathf . FloorToInt ( t * Points . Length ) ; index = Mathf . Clamp ( index , 0 , Points . Length ) - 1 ; int index0 ; int index1 ; int index2 ; int index3 ; if ( ClosedSpline ) { int mod_count = Points . Length - 1 ; index0 = ( ( index + 0 ) % ( mod_count ) + mod_count ) % mod_count ; index1 = ( ( index + 1 ) % ( mod_count ) + mod_count ) % mod_count ; index2 = ( ( index + 2 ) % ( mod_count ) + mod_count ) % mod_count ; index3 = ( ( index + 3 ) % ( mod_count ) + mod_count ) % mod_count ; } else { index0 = index + 0 ; index1 = index + 1 ; index2 = index + 2 ; index3 = index + 3 ; index0 = Mathf . Clamp ( index0 , 0 , Points . Length - 1 ) ; index1 = Mathf . Clamp ( index1 , 0 , Points . Length - 1 ) ; index2 = Mathf . Clamp ( index2 , 0 , Points . Length - 1 ) ; index3 = Mathf . Clamp ( index3 , 0 , Points . Length - 1 ) ; } var point0 = Points [ index0 ] ; var point1 = Points [ index1 ] ; var point2 = Points [ index2 ] ; var point3 = Points [ index3 ] ; var result = CalculateBSplinePoint ( point0 , point1 , point2 , point3 , inner_t ) ; if ( SplineSpace == Space . Self ) { return TransformSplinePoint ( result ) ; } else { return result ; } } else { return new SplinePoint ( ) ; } }
Returns a world space SplinePoint, given an input t. t is valid from 0 to 1.
Returns a world space SplinePoint given an input t . t is valid from 0 to 1 .
// index1 = Mathf.Clamp(index1, 0, Points.Length - 1); // index2 = Mathf.Clamp(index2, 0, Points.Length - 1); // index3 = Mathf.Clamp(index3, 0, Points.Length - 1); // note, offsetting by -1 so index0 starts behind current point  // -1 to ignore duplicate final point  // not implemented 
None
t
float
None