Class SplineUtility
A collection of methods for extracting information about Spline types.
Inherited Members
Namespace: UnityEngine.Splines
Syntax
public static class SplineUtility
Fields
DrawResolutionDefault
The default resolution used when unrolling a curve to draw a preview in the Scene View.
Pick resolution is used when determining how many segments are required to unroll a curve. Unrolling is the process of calculating a series of line segments to approximate a curve. Some functions in SplineUtility allow you to specify a resolution. Lower resolution means fewer segments, while higher resolutions result in more segments. Use lower resolutions where performance is critical and accuracy is not paramount. Use higher resolution where a fine degree of accuracy is necessary and performance is less important.
Declaration
public const int DrawResolutionDefault = 10
Field Value
Type | Description |
---|---|
Int32 |
PickResolutionDefault
The default resolution used when unrolling a curve to hit test while picking (selecting a spline with a cursor).
Pick resolution is used when determining how many segments are required to unroll a curve. Unrolling is the process of calculating a series of line segments to approximate a curve. Some functions in SplineUtility allow you to specify a resolution. Lower resolution means fewer segments, while higher resolutions result in more segments. Use lower resolutions where performance is critical and accuracy is not paramount. Use higher resolution where a fine degree of accuracy is necessary and performance is less important.
Declaration
public const int PickResolutionDefault = 4
Field Value
Type | Description |
---|---|
Int32 |
PickResolutionMax
The maximum resolution allowed when unrolling a curve to hit test while picking (selecting a spline with a cursor).
Pick resolution is used when determining how many segments are required to unroll a curve. Unrolling is the process of calculating a series of line segments to approximate a curve. Some functions in SplineUtility allow you to specify a resolution. Lower resolution means fewer segments, while higher resolutions result in more segments. Use lower resolutions where performance is critical and accuracy is not paramount. Use higher resolution where a fine degree of accuracy is necessary and performance is less important.
Declaration
public const int PickResolutionMax = 64
Field Value
Type | Description |
---|---|
Int32 |
PickResolutionMin
The minimum resolution allowable when unrolling a curve to hit test while picking (selecting a spline with a cursor).
Pick resolution is used when determining how many segments are required to unroll a curve. Unrolling is the process of calculating a series of line segments to approximate a curve. Some functions in SplineUtility allow you to specify a resolution. Lower resolution means fewer segments, while higher resolutions result in more segments. Use lower resolutions where performance is critical and accuracy is not paramount. Use higher resolution where a fine degree of accuracy is necessary and performance is less important.
Declaration
public const int PickResolutionMin = 2
Field Value
Type | Description |
---|---|
Int32 |
Methods
AddSpline<T>(T)
Creates a new spline and adds it to the ISplineContainer.
Declaration
public static Spline AddSpline<T>(this T container)
where T : ISplineContainer
Parameters
Type | Name | Description |
---|---|---|
T | container | The target container. |
Returns
Type | Description |
---|---|
Spline | Returns the spline that was created and added to the container. |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISplineContainer. |
CalculateLength<T>(T, float4x4)
Calculate the length of a spline when transformed by a matrix.
Declaration
public static float CalculateLength<T>(this T spline, float4x4 transform)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | |
float4x4 | transform |
Returns
Type | Description |
---|---|
Single |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
ConvertIndexUnit<T>(T, Single, PathIndexUnit)
Given a normalized interpolation ratio, calculate the associated interpolation value in another targetPathUnit regarding a specific spline.
Declaration
public static float ConvertIndexUnit<T>(this T spline, float t, PathIndexUnit targetPathUnit)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The Spline to use for the conversion, this is necessary to compute Normalized and Distance PathIndexUnits. |
Single | t | Normalized interpolation ratio (0 to 1). |
PathIndexUnit | targetPathUnit | The PathIndexUnit to which 't' should be converted. |
Returns
Type | Description |
---|---|
Single | The interpolation value converted to targetPathUnit. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
ConvertIndexUnit<T>(T, Single, PathIndexUnit, PathIndexUnit)
Given an interpolation value using a certain PathIndexUnit type, calculate the associated interpolation value in another targetPathUnit regarding a specific spline.
Declaration
public static float ConvertIndexUnit<T>(this T spline, float t, PathIndexUnit fromPathUnit, PathIndexUnit targetPathUnit)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The Spline to use for the conversion, this is necessary to compute Normalized and Distance PathIndexUnits. |
Single | t | Interpolation in the original PathIndexUnit. |
PathIndexUnit | fromPathUnit | The PathIndexUnit for the original interpolation value. |
PathIndexUnit | targetPathUnit | The PathIndexUnit to which 't' should be converted. |
Returns
Type | Description |
---|---|
Single | The interpolation value converted to targetPathUnit. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
CurveToSplineT<T>(T, Single)
Given an interpolation value for a curve, calculate the relative normalized spline interpolation.
Declaration
public static float CurveToSplineT<T>(this T spline, float curve)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The target spline. |
Single | curve | A curve index and normalized interpolation. The curve index is represented by the integer part of the float, and interpolation is the fractional part. This is the format used by Knot. |
Returns
Type | Description |
---|---|
Single | An interpolation value relative to normalized Spline length (0 to 1). |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
See Also
Evaluate<T>(T, Single, out float3, out float3, out float3)
Compute interpolated position, direction and upDirection at ratio t. Calling this method to get the 3 vectors is faster than calling independently EvaluatePosition, EvaluateDirection and EvaluateUpVector for the same time t as it reduces some redundant computation.
Declaration
public static bool Evaluate<T>(this T spline, float t, out float3 position, out float3 tangent, out float3 upVector)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to interpolate. |
Single | t | A value between 0 and 1 representing the ratio along the curve. |
float3 | position | Output variable for the float3 position at t. |
float3 | tangent | Output variable for the float3 tangent at t. |
float3 | upVector | Output variable for the float3 up direction at t. |
Returns
Type | Description |
---|---|
Boolean | Boolean value, true if a valid set of output variables as been computed. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
EvaluateAcceleration<T>(T, Single)
Return an interpolated acceleration at ratio t.
Declaration
public static float3 EvaluateAcceleration<T>(this T spline, float t)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to interpolate. |
Single | t | A value between 0 and 1 representing the ratio along the curve. |
Returns
Type | Description |
---|---|
float3 | An acceleration on the spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
EvaluateCurvature<T>(T, Single)
Return an interpolated curvature at ratio t.
Declaration
public static float EvaluateCurvature<T>(this T spline, float t)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to interpolate. |
Single | t | A value between 0 and 1 representing the ratio along the curve. |
Returns
Type | Description |
---|---|
Single | A curvature on the spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
EvaluateCurvatureCenter<T>(T, Single)
Return the curvature center at ratio t. The curvature center represents the center of the circle that is tangent to the curve at t. This circle is in the plane defined by the curve velocity (tangent) and the curve acceleration at that point.
Declaration
public static float3 EvaluateCurvatureCenter<T>(this T spline, float t)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to interpolate. |
Single | t | A value between 0 and 1 representing the ratio along the curve. |
Returns
Type | Description |
---|---|
float3 | A point representing the curvature center associated to the position at t on the spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
EvaluatePosition<T>(T, Single)
Return an interpolated position at ratio t.
Declaration
public static float3 EvaluatePosition<T>(this T spline, float t)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to interpolate. |
Single | t | A value between 0 and 1 representing the ratio along the curve. |
Returns
Type | Description |
---|---|
float3 | A position on the spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
EvaluateTangent<T>(T, Single)
Return an interpolated direction at ratio t.
Declaration
public static float3 EvaluateTangent<T>(this T spline, float t)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to interpolate. |
Single | t | A value between 0 and 1 representing the ratio along the curve. |
Returns
Type | Description |
---|---|
float3 | A direction on the spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
EvaluateUpVector<T>(T, Single)
Evaluate the normal (up) vector of a spline.
Declaration
public static float3 EvaluateUpVector<T>(this T spline, float t)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The NativeSpline to evaluate. |
Single | t | A value between 0 and 1 representing a percentage of the curve. |
Returns
Type | Description |
---|---|
float3 | An up vector |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetAutoSmoothKnot(float3, float3, float3)
Gets a BezierKnot with its tangents and rotation calculated using the previous and next knot positions.
Declaration
public static BezierKnot GetAutoSmoothKnot(float3 position, float3 previous, float3 next)
Parameters
Type | Name | Description |
---|---|---|
float3 | position | The position of the knot. |
float3 | previous | The knot that immediately precedes the requested knot. |
float3 | next | The knot that immediately follows the requested knot. |
Returns
Type | Description |
---|---|
BezierKnot | A BezierKnot with tangent and rotation values calculated from the previous and next knot positions. |
GetAutoSmoothKnot(float3, float3, float3, float3)
Gets a BezierKnot with its tangents and rotation calculated using the previous and next knot positions.
Declaration
public static BezierKnot GetAutoSmoothKnot(float3 position, float3 previous, float3 next, float3 normal)
Parameters
Type | Name | Description |
---|---|---|
float3 | position | The position of the knot. |
float3 | previous | The knot that immediately precedes the requested knot. |
float3 | next | The knot that immediately follows the requested knot. |
float3 | normal | The normal vector of the knot. |
Returns
Type | Description |
---|---|
BezierKnot | A BezierKnot with tangent and rotation values calculated from the previous and next knot positions. |
GetBounds<T>(T)
Calculate the bounding box of a Spline.
Declaration
public static Bounds GetBounds<T>(this T spline)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline for which to calculate bounds. |
Returns
Type | Description |
---|---|
Bounds | The bounds of a spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetBounds<T>(T, float4x4)
Creates a bounding box for a spline.
Declaration
public static Bounds GetBounds<T>(this T spline, float4x4 transform)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to calculate bounds for. |
float4x4 | transform | The matrix to transform the spline's elements with. |
Returns
Type | Description |
---|---|
Bounds | The bounds of a spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetCatmullRomTangent(float3, float3)
Calculates a tangent from the previous and next knot positions.
Declaration
public static float3 GetCatmullRomTangent(float3 previous, float3 next)
Parameters
Type | Name | Description |
---|---|---|
float3 | previous | The position of the previous BezierKnot. |
float3 | next | The position of the next BezierKnot. |
Returns
Type | Description |
---|---|
float3 | Returns a tangent calculated from the previous and next knot positions. |
GetCurveCount<T>(T)
Calculates the number of curves in a spline.
Declaration
public static int GetCurveCount<T>(this T spline)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline |
Returns
Type | Description |
---|---|
Int32 | The number of curves in a spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetNearestPoint<T>(T, float3, out float3, out Single, Int32, Int32)
Calculate the point on a spline nearest to a point.
Declaration
public static float GetNearestPoint<T>(T spline, float3 point, out float3 nearest, out float t, int resolution = 4, int iterations = 2)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The input spline to search for nearest point. |
float3 | point | The input point to compare. |
float3 | nearest | The point on a spline nearest to the input point. The accuracy of this value is
affected by the |
Single | t | The normalized interpolation ratio corresponding to the nearest point. |
Int32 | resolution | Affects how many segments to split a spline into when calculating the nearest point.
Higher values mean smaller and more segments, which increases accuracy at the cost of processing time.
The minimum resolution is defined by PickResolutionMin, and the maximum is defined by
PickResolutionMax.
In most cases, the default resolution is appropriate. Use with |
Int32 | iterations | The nearest point is calculated by finding the nearest point on the entire length
of the spline using |
Returns
Type | Description |
---|---|
Single | The distance from input point to nearest point on spline. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetNearestPoint<T>(T, Ray, out float3, out Single, Int32, Int32)
Calculate the point on a spline nearest to a ray.
Declaration
public static float GetNearestPoint<T>(T spline, Ray ray, out float3 nearest, out float t, int resolution = 4, int iterations = 2)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The input spline to search for nearest point. |
Ray | ray | The input ray to search against. |
float3 | nearest | The point on a spline nearest to the input ray. The accuracy of this value is
affected by the |
Single | t | The normalized time value to the nearest point. |
Int32 | resolution | Affects how many segments to split a spline into when calculating the nearest point.
Higher values mean smaller and more segments, which increases accuracy at the cost of processing time.
The minimum resolution is defined by PickResolutionMin, and the maximum is defined by
PickResolutionMax.
In most cases, the default resolution is appropriate. Use with |
Int32 | iterations | The nearest point is calculated by finding the nearest point on the entire length
of the spline using |
Returns
Type | Description |
---|---|
Single | The distance from ray to nearest point. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetNormalizedInterpolation<T>(T, Single, PathIndexUnit)
Given an interpolation value in any PathIndexUnit type, calculate the normalized interpolation ratio value relative to a Spline.
Declaration
public static float GetNormalizedInterpolation<T>(T spline, float t, PathIndexUnit originalPathUnit)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The Spline to use for the conversion, this is necessary to compute Normalized and Distance PathIndexUnits. |
Single | t | The 't' value to normalize in the original PathIndexUnit. |
PathIndexUnit | originalPathUnit | The PathIndexUnit from the original 't'. |
Returns
Type | Description |
---|---|
Single | The normalized interpolation ratio (0 to 1). |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetPointAtLinearDistance<T>(T, Single, Single, out Single)
Given a Spline and interpolation ratio, calculate the 3d point at a linear distance from point at spline.EvaluatePosition(t). Returns the corresponding time associated to this 3d position on the Spline.
Declaration
public static float3 GetPointAtLinearDistance<T>(this T spline, float fromT, float relativeDistance, out float resultPointT)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The Spline on which to compute the point. |
Single | fromT | The Spline interpolation ratio 't' (normalized) from which the next position need to be computed. |
Single | relativeDistance | The relative distance at which the new point should be placed. A negative value will compute a point at a 'resultPointTime' previous to 'fromT' (backward search). |
Single | resultPointT | The normalized interpolation ratio of the resulting point. |
Returns
Type | Description |
---|---|
float3 | The 3d point from the spline located at a linear distance from the point at t. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
GetSubdivisionCount(Single, Int32)
Gets the number of subdivisions for a spline length and resolution.
Declaration
public static int GetSubdivisionCount(float length, int resolution)
Parameters
Type | Name | Description |
---|---|---|
Single | length | The length of the spline to consider. |
Int32 | resolution | The resolution to consider. Higher resolutions result in more precise representations. However, higher resolutions have higher performance requirements. |
Returns
Type | Description |
---|---|
Int32 | The number of subdivisions as calculated for given length and resolution. |
LinkKnots<T>(T, SplineKnotIndex, SplineKnotIndex)
Links two knots in an ISplineContainer. The two knots can be on different splines, but both must be in the referenced SplineContainer. If these knots are linked to other knots, all existing links are kept and updated.
Declaration
public static void LinkKnots<T>(this T container, SplineKnotIndex knotA, SplineKnotIndex knotB)
where T : ISplineContainer
Parameters
Type | Name | Description |
---|---|---|
T | container | The target SplineContainer. |
SplineKnotIndex | knotA | The first knot to link. |
SplineKnotIndex | knotB | The second knot to link. |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISplineContainer. |
Next<T>(T, Int32)
Gets the BezierKnot after spline[index]. This method uses the Count and Closed properties to ensure that it returns the correct knot.
Declaration
public static BezierKnot Next<T>(this T spline, int index)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to consider. |
Int32 | index | The current index to consider. |
Returns
Type | Description |
---|---|
BezierKnot | The knot after the knot at spline[index]. |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISpline. |
NextIndex<T>(T, Int32)
Gets the index of a knot that follows a spline index. This method uses the Count and Closed properties to ensure that it returns the correct index of the knot.
Declaration
public static int NextIndex<T>(this T spline, int index)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to consider. |
Int32 | index | The current index to consider. |
Returns
Type | Description |
---|---|
Int32 | The knot index after |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISpline. |
Previous<T>(T, Int32)
Gets the BezierKnot before spline[index]. This method uses the Count and Closed properties to ensure that it returns the correct knot.
Declaration
public static BezierKnot Previous<T>(this T spline, int index)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to consider. |
Int32 | index | The current index to consider. |
Returns
Type | Description |
---|---|
BezierKnot | The knot before the knot at spline[index]. |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISpline. |
PreviousIndex<T>(T, Int32)
Gets the index of a knot that precedes a spline index. This method uses the Count and Closed properties to ensure that it returns the correct index of the knot.
Declaration
public static int PreviousIndex<T>(this T spline, int index)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The spline to consider. |
Int32 | index | The current index to consider. |
Returns
Type | Description |
---|---|
Int32 | Returns a knot index that precedes the |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISpline. |
RemoveSpline<T>(T, Spline)
Removes a spline from a ISplineContainer.
Declaration
public static bool RemoveSpline<T>(this T container, Spline spline)
where T : ISplineContainer
Parameters
Type | Name | Description |
---|---|---|
T | container | The target SplineContainer. |
Spline | spline | The spline to remove from the SplineContainer. |
Returns
Type | Description |
---|---|
Boolean | Returns true if the spline was removed from the container. |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISplineContainer. |
RemoveSplineAt<T>(T, Int32)
Removes a spline from a ISplineContainer.
Declaration
public static bool RemoveSplineAt<T>(this T container, int splineIndex)
where T : ISplineContainer
Parameters
Type | Name | Description |
---|---|---|
T | container | The target container. |
Int32 | splineIndex | The index of the spline to remove from the SplineContainer. |
Returns
Type | Description |
---|---|
Boolean | Returns true if the spline was removed from the container. |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISplineContainer. |
SetLinkedKnotPosition<T>(T, SplineKnotIndex)
Sets the position of all knots linked to the knot at index
in an ISplineContainer to the same position.
Declaration
public static void SetLinkedKnotPosition<T>(this T container, SplineKnotIndex index)
where T : ISplineContainer
Parameters
Type | Name | Description |
---|---|---|
T | container | The target container. |
SplineKnotIndex | index | The |
Type Parameters
Name | Description |
---|---|
T | A type that implements ISplineContainer. |
SetPivot(SplineContainer, Vector3)
Reset a transform position to a position while keeping knot positions in the same place. This modifies both knot positions and transform position.
Declaration
public static void SetPivot(SplineContainer container, Vector3 position)
Parameters
Type | Name | Description |
---|---|---|
SplineContainer | container | The target spline. |
Vector3 | position | The point in world space to move the pivot to. |
SplineToCurveT<T>(T, Single, out Single)
Given a normalized interpolation (t) for a spline, calculate the curve index and curve-relative normalized interpolation.
Declaration
public static int SplineToCurveT<T>(this T spline, float splineT, out float curveT)
where T : ISpline
Parameters
Type | Name | Description |
---|---|---|
T | spline | The target spline. |
Single | splineT | A normalized spline interpolation value to be converted into curve space. |
Single | curveT | A normalized curve interpolation value. |
Returns
Type | Description |
---|---|
Int32 | The curve index. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISpline. |
UnlinkKnots<T>(T, IReadOnlyList<SplineKnotIndex>)
Unlinks several knots from an ISplineContainer. A knot in knots
disconnects from other knots it was linked to.
Declaration
public static void UnlinkKnots<T>(this T container, IReadOnlyList<SplineKnotIndex> knots)
where T : ISplineContainer
Parameters
Type | Name | Description |
---|---|---|
T | container | The target SplineContainer. |
IReadOnlyList<SplineKnotIndex> | knots | The knot to unlink. |
Type Parameters
Name | Description |
---|---|
T | A type implementing ISplineContainer. |