Assets/SplineTool/Scripts/CorgiSpline/Spline.cs
/// <summary>
/// Projects a world position onto this Spline, which can be used for fetching an interpolated SplinePoint with GetPoint(t); 
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
Projects a world position onto this Spline, which can be used for fetching an interpolated SplinePoint with GetPoint(t).
Projects a world position onto this Spline which can be used for fetching an interpolated SplinePoint with GetPoint ( t ) .
public float ProjectOnSpline_t(Vector3 position)
        {
            if (SplineSpace == Space.Self)
            {
                position = transform.InverseTransformPoint(position);
            }
            if (Points.Length == 0)
            {
                return 0f;
            }
            if (Points.Length == 1)
            {
                return 0f;
            }
            var length = Points.Length;
            if (Mode == SplineMode.Linear)
            {
                var closestDistance = float.MaxValue;
                var closestIndex = -1;
                for (var i = 0; i < length; ++i)
                {
                    var point = Points[i];
                    var toPoint = point.position - position;
                    var toPointDistance = toPoint.magnitude;
                    if (toPointDistance < closestDistance)
                    {
                        closestDistance = toPointDistance;
                        closestIndex = i;
                    }
                }
                SplinePoint point0;
                SplinePoint point1;
                if (closestIndex <= 0)
                {
                    var index_a = closestIndex;
                    var index_b = closestIndex + 1;
                    point0 = Points[index_a];
                    point1 = Points[index_b];
                }
                else if (closestIndex == Points.Length - 1)
                {
                    var index_a = closestIndex;
                    var index_b = closestIndex - 1;
                    point0 = Points[index_a];
                    point1 = Points[index_b];
                }
                else
                {
                    var index_a = closestIndex;
                    var index_b = closestIndex - 1;
                    var index_c = closestIndex + 1;
                    var point_a = Points[index_a];
                    var point_b = Points[index_b];
                    var point_c = Points[index_c];
                    var projected_ab = ProjectLinear(point_a, point_b, position);
                    var projected_ac = ProjectLinear(point_a, point_c, position);
                    var distance_ab = Vector3.Distance(position, projected_ab);
                    var distance_ac = Vector3.Distance(position, projected_ac);
                    if (distance_ab < distance_ac)
                    {
                        point0 = point_b;
                        point1 = point_a;
                    }
                    else
                    {
                        point0 = point_a;
                        point1 = point_c;
                    }
                }
                var projectedPosition = ProjectLinear(point0, point1, position);
                var percentageBetweenPoints = GetPercentageLinear(point0, point1, projectedPosition);
                return (float)closestIndex / Points.Length + percentageBetweenPoints * (1f / Points.Length);
            }
            else if (Mode == SplineMode.Bezier)
            {
                var closestDistance = float.MaxValue;
                var best_t = 0f;
                var best_i = -1;
                for (var i = 0; i < length - 3; i += 3)
                {
                    var p0 = Points[i + 0];
                    var p1 = Points[i + 1];
                    var p2 = Points[i + 2];
                    var p3 = Points[i + 3];
                    var t = QuadraticProject(p0.position, p1.position, p2.position, p3.position, position);
                    var projected = QuadraticInterpolate(p0.position, p1.position, p2.position, p3.position, t);
                    var distance = Vector3.Distance(projected, position);
                    if (distance < closestDistance)
                    {
                        closestDistance = distance;
                        best_i = i;
                        best_t = t;
                    }
                }
                return (float)best_i / Points.Length + best_t * (3f / Points.Length);
            }
            else if (Mode == SplineMode.BSpline)
            {
                var closestDistance = float.MaxValue;
                var best_t = 0f;
                var best_i = -1;
                for (var i = 0; i < length; i += 1)
                {
                    var index = i;
                    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 p0 = Points[index0];
                    var p1 = Points[index1];
                    var p2 = Points[index2];
                    var p3 = Points[index3];
                    var t = BSplineProject(p0.position, p1.position, p2.position, p3.position, position);
                    var projected = BSplineInterpolate(p0.position, p1.position, p2.position, p3.position, t);
                    var distance = Vector3.Distance(projected, position);
                    if (distance < closestDistance)
                    {
                        closestDistance = distance;
                        best_i = i;
                        best_t = t;
                    }
                }
                return (float)best_i / Points.Length + best_t * (1f / Points.Length);
            }
            return 0f;
        }
public float ProjectOnSpline_t ( Vector3 position ) { if ( SplineSpace == Space . Self ) { position = transform . InverseTransformPoint ( position ) ; } if ( Points . Length == 0 ) { return 0f ; } if ( Points . Length == 1 ) { return 0f ; } var length = Points . Length ; if ( Mode == SplineMode . Linear ) { var closestDistance = float . MaxValue ; var closestIndex = - 1 ; for ( var i = 0 ; i < length ; ++ i ) { var point = Points [ i ] ; var toPoint = point . position - position ; var toPointDistance = toPoint . magnitude ; if ( toPointDistance < closestDistance ) { closestDistance = toPointDistance ; closestIndex = i ; } } SplinePoint point0 ; SplinePoint point1 ; if ( closestIndex <= 0 ) { var index_a = closestIndex ; var index_b = closestIndex + 1 ; point0 = Points [ index_a ] ; point1 = Points [ index_b ] ; } else if ( closestIndex == Points . Length - 1 ) { var index_a = closestIndex ; var index_b = closestIndex - 1 ; point0 = Points [ index_a ] ; point1 = Points [ index_b ] ; } else { var index_a = closestIndex ; var index_b = closestIndex - 1 ; var index_c = closestIndex + 1 ; var point_a = Points [ index_a ] ; var point_b = Points [ index_b ] ; var point_c = Points [ index_c ] ; var projected_ab = ProjectLinear ( point_a , point_b , position ) ; var projected_ac = ProjectLinear ( point_a , point_c , position ) ; var distance_ab = Vector3 . Distance ( position , projected_ab ) ; var distance_ac = Vector3 . Distance ( position , projected_ac ) ; if ( distance_ab < distance_ac ) { point0 = point_b ; point1 = point_a ; } else { point0 = point_a ; point1 = point_c ; } } var projectedPosition = ProjectLinear ( point0 , point1 , position ) ; var percentageBetweenPoints = GetPercentageLinear ( point0 , point1 , projectedPosition ) ; return ( float ) closestIndex / Points . Length + percentageBetweenPoints * ( 1f / Points . Length ) ; } else if ( Mode == SplineMode . Bezier ) { var closestDistance = float . MaxValue ; var best_t = 0f ; var best_i = - 1 ; for ( var i = 0 ; i < length - 3 ; i += 3 ) { var p0 = Points [ i + 0 ] ; var p1 = Points [ i + 1 ] ; var p2 = Points [ i + 2 ] ; var p3 = Points [ i + 3 ] ; var t = QuadraticProject ( p0 . position , p1 . position , p2 . position , p3 . position , position ) ; var projected = QuadraticInterpolate ( p0 . position , p1 . position , p2 . position , p3 . position , t ) ; var distance = Vector3 . Distance ( projected , position ) ; if ( distance < closestDistance ) { closestDistance = distance ; best_i = i ; best_t = t ; } } return ( float ) best_i / Points . Length + best_t * ( 3f / Points . Length ) ; } else if ( Mode == SplineMode . BSpline ) { var closestDistance = float . MaxValue ; var best_t = 0f ; var best_i = - 1 ; for ( var i = 0 ; i < length ; i += 1 ) { var index = i ; 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 p0 = Points [ index0 ] ; var p1 = Points [ index1 ] ; var p2 = Points [ index2 ] ; var p3 = Points [ index3 ] ; var t = BSplineProject ( p0 . position , p1 . position , p2 . position , p3 . position , position ) ; var projected = BSplineInterpolate ( p0 . position , p1 . position , p2 . position , p3 . position , t ) ; var distance = Vector3 . Distance ( projected , position ) ; if ( distance < closestDistance ) { closestDistance = distance ; best_i = i ; best_t = t ; } } return ( float ) best_i / Points . Length + best_t * ( 1f / Points . Length ) ; } return 0f ; }
Projects a world position onto this Spline, which can be used for fetching an interpolated SplinePoint with GetPoint(t);
Projects a world position onto this Spline which can be used for fetching an interpolated SplinePoint with GetPoint ( t ) ;
// find closest point  // find closest point  // find closest point  // note, offsetting by -1 so index0 starts behind current point 
None
position
Vector3
None