Add Slerp methods on Vectors

#3682
This commit is contained in:
Phantom
2026-02-05 12:43:29 +01:00
committed by Wojtek Figat
parent 66894b71fa
commit f57df83d26
6 changed files with 135 additions and 0 deletions

View File

@@ -954,6 +954,33 @@ namespace FlaxEngine
return result;
}
/// <summary>
/// Performs a spherical linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end" />.</param>
/// <param name="result">>When the method completes, contains the linear interpolation of the two vectors.</param>
public static void Slerp(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
{
var dot = Mathr.Clamp(Dot(start, end), -1.0f, 1.0f);
var theta = Mathr.Acos(dot) * amount;
Vector2 relativeVector = (end - start * dot).Normalized;
result = ((start * Mathr.Cos(theta)) + (relativeVector * Mathr.Sin(theta)));
}
/// <summary>
/// Performs a spherical linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end" />.</param>
public static Vector2 Slerp(Vector2 start, Vector2 end, float amount)
{
Slerp(ref start, ref end, amount, out Vector2 result);
return result;
}
/// <summary>
/// Performs a gradual change of a vector towards a specified target over time
/// </summary>

View File

@@ -558,6 +558,24 @@ public:
return result;
}
// Performs a spherical linear interpolation between two vectors.
static void Slerp(const Vector2Base& start, const Vector2Base& end, T amount, Vector2Base& result)
{
T dot = Math::Clamp(Dot(start, end), -1.0f, 1.0f);
T theta = Math::Acos(dot) * amount;
Vector2Base relativeVector = end - start * dot;
relativeVector.Normalize();
result = ((start * Math::Cos(theta)) + (relativeVector * Math::Sin(theta)));
}
// Performs a spherical linear interpolation between two vectors.
static Vector2Base Slerp(const Vector2Base& start, const Vector2Base& end, T amount)
{
Vector2Base result;
Slerp(start, end, amount, result);
return result;
}
public:
/// <summary>
/// Calculates the area of the triangle.

View File

@@ -1043,6 +1043,33 @@ namespace FlaxEngine
return result;
}
/// <summary>
/// Performs a spherical linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end" />.</param>
/// <param name="result">When the method completes, contains the linear interpolation of the two vectors.</param>
public static void Slerp(ref Vector3 start, ref Vector3 end, float amount, out Vector3 result)
{
var dot = Mathr.Clamp(Dot(start, end), -1.0f, 1.0f);
var theta = Mathr.Acos(dot) * amount;
Vector3 relativeVector = (end - start * dot).Normalized;
result = ((start * Mathr.Cos(theta)) + (relativeVector * Mathr.Sin(theta)));
}
/// <summary>
/// Performs a spherical linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end" />.</param>
public static Vector3 Slerp(Vector3 start, Vector3 end, float amount)
{
Slerp(ref start, ref end, amount, out var result);
return result;
}
/// <summary>
/// Performs a gradual change of a vector towards a specified target over time
/// </summary>

View File

@@ -686,6 +686,24 @@ public:
return result;
}
// Performs a spherical linear interpolation between two vectors.
static void Slerp(const Vector3Base& start, const Vector3Base& end, T amount, Vector3Base& result)
{
T dot = Math::Clamp(Dot(start, end), -1.0f, 1.0f);
T theta = Math::Acos(dot) * amount;
Vector3Base relativeVector = end - start * dot;
relativeVector.Normalize();
result = ((start * Math::Cos(theta)) + (relativeVector * Math::Sin(theta)));
}
// Performs a spherical linear interpolation between two vectors.
static Vector3Base Slerp(const Vector3Base& start, const Vector3Base& end, T amount)
{
Vector3Base result;
Slerp(start, end, amount, result);
return result;
}
// Performs a cubic interpolation between two vectors.
static void SmoothStep(const Vector3Base& start, const Vector3Base& end, T amount, Vector3Base& result)
{

View File

@@ -891,6 +891,33 @@ namespace FlaxEngine
return result;
}
/// <summary>
/// Performs a spherical linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end" />.</param>
/// <param name="result">When the method completes, contains the linear interpolation of the two vectors.</param>
public static void Slerp(ref Vector4 start, ref Vector4 end, Real amount, out Vector4 result)
{
var dot = Mathr.Clamp(Dot(start, end), -1.0f, 1.0f);
var theta = Mathr.Acos(dot) * amount;
Vector4 relativeVector = (end - start * dot).Normalized;
result = ((start * Mathr.Cos(theta)) + (relativeVector * Mathr.Sin(theta)));
}
/// <summary>
/// Performs a spherical linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end" />.</param>
public static Vector4 Slerp(Vector4 start, Vector4 end, Real amount)
{
Slerp(ref start, ref end, amount, out var result);
return result;
}
/// <summary>
/// Performs a cubic interpolation between two vectors.
/// </summary>

View File

@@ -566,6 +566,24 @@ public:
return result;
}
// Performs a spherical linear interpolation between two vectors.
static void Slerp(const Vector4Base& start, const Vector4Base& end, T amount, Vector4Base& result)
{
T dot = Math::Clamp(Dot(start, end), -1.0f, 1.0f);
T theta = Math::Acos(dot) * amount;
Vector4Base relativeVector = end - start * dot;
relativeVector.Normalize();
result = ((start * Math::Cos(theta)) + (relativeVector * Math::Sin(theta)));
}
// Performs a spherical linear interpolation between two vectors.
static Vector4Base Slerp(const Vector4Base& start, const Vector4Base& end, T amount)
{
Vector4Base result;
Slerp(start, end, amount, result);
return result;
}
FLAXENGINE_API static Vector4Base Transform(const Vector4Base& v, const Matrix& m);
};