Skip to content

Commit 426487f

Browse files
committed
added lerp & slerp to cubic beziers
1 parent 898b64d commit 426487f

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Curves/BezierCubic2D.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,40 @@ public float GetPointComponent( int component, float t ) {
250250

251251
// Whole-curve properties & functions
252252

253+
#region Interpolation
254+
255+
/// <summary>Returns linear blend between two bézier curves</summary>
256+
/// <param name="a">The first curve</param>
257+
/// <param name="b">The second curve</param>
258+
/// <param name="t">A value from 0 to 1 to blend between <c>a</c> and <c>b</c></param>
259+
public static BezierCubic2D Lerp( BezierCubic2D a, BezierCubic2D b, float t ) {
260+
return new BezierCubic2D(
261+
Vector2.LerpUnclamped( a.p0, b.p0, t ),
262+
Vector2.LerpUnclamped( a.p1, b.p1, t ),
263+
Vector2.LerpUnclamped( a.p2, b.p2, t ),
264+
Vector2.LerpUnclamped( a.p3, b.p3, t )
265+
);
266+
}
267+
268+
/// <summary>Returns blend between two bézier curves,
269+
/// where the endpoints are linearly interpolated,
270+
/// while the tangents are spherically interpolated relative to their corresponding endpoint</summary>
271+
/// <param name="a">The first curve</param>
272+
/// <param name="b">The second curve</param>
273+
/// <param name="t">A value from 0 to 1 to blend between <c>a</c> and <c>b</c></param>
274+
public static BezierCubic2D Slerp( BezierCubic2D a, BezierCubic2D b, float t ) {
275+
Vector2 p0 = Vector2.LerpUnclamped( a.p0, b.p0, t );
276+
Vector2 p3 = Vector2.LerpUnclamped( a.p3, b.p3, t );
277+
return new BezierCubic2D(
278+
p0,
279+
p0 + (Vector2)Vector3.SlerpUnclamped( a.p1 - a.p0, b.p1 - b.p0, t ),
280+
p3 + (Vector2)Vector3.SlerpUnclamped( a.p2 - a.p3, b.p2 - b.p3, t ),
281+
p3
282+
);
283+
}
284+
285+
#endregion
286+
253287
#region Splitting
254288

255289
/// <summary>Splits this curve at the given t-value, into two curves of the exact same shape</summary>

Curves/BezierCubic3D.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,32 @@ public float GetPointComponent( int component, float t ) {
257257

258258
// Whole-curve properties & functions
259259

260+
#region Interpolation
261+
262+
/// <inheritdoc cref="BezierCubic2D.Lerp(BezierCubic2D,BezierCubic2D,float)"/>
263+
public static BezierCubic3D Lerp( BezierCubic3D a, BezierCubic3D b, float t ) {
264+
return new BezierCubic3D(
265+
Vector3.LerpUnclamped( a.p0, b.p0, t ),
266+
Vector3.LerpUnclamped( a.p1, b.p1, t ),
267+
Vector3.LerpUnclamped( a.p2, b.p2, t ),
268+
Vector3.LerpUnclamped( a.p3, b.p3, t )
269+
);
270+
}
271+
272+
/// <inheritdoc cref="BezierCubic2D.Slerp(BezierCubic2D,BezierCubic2D,float)"/>
273+
public static BezierCubic3D Slerp( BezierCubic3D a, BezierCubic3D b, float t ) {
274+
Vector3 p0 = Vector3.LerpUnclamped( a.p0, b.p0, t );
275+
Vector3 p3 = Vector3.LerpUnclamped( a.p3, b.p3, t );
276+
return new BezierCubic3D(
277+
p0,
278+
p0 + Vector3.SlerpUnclamped( a.p1 - a.p0, b.p1 - b.p0, t ),
279+
p3 + Vector3.SlerpUnclamped( a.p2 - a.p3, b.p2 - b.p3, t ),
280+
p3
281+
);
282+
}
283+
284+
#endregion
285+
260286
#region Splitting
261287

262288
/// <inheritdoc cref="BezierCubic2D.Split(float)"/>

0 commit comments

Comments
 (0)