mirror of
https://github.com/godotengine/godot.git
synced 2024-11-25 13:43:15 +00:00
Refactor interpolating functions in some classes to use Math class
This commit is contained in:
parent
f16c5b564b
commit
b217c41d36
@ -105,12 +105,10 @@ struct _NO_DISCARD_ Color {
|
||||
|
||||
_FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
|
||||
Color res = *this;
|
||||
|
||||
res.r += (p_weight * (p_to.r - r));
|
||||
res.g += (p_weight * (p_to.g - g));
|
||||
res.b += (p_weight * (p_to.b - b));
|
||||
res.a += (p_weight * (p_to.a - a));
|
||||
|
||||
res.r = Math::lerp(res.r, p_to.r, p_weight);
|
||||
res.g = Math::lerp(res.g, p_to.g, p_weight);
|
||||
res.b = Math::lerp(res.b, p_to.b, p_weight);
|
||||
res.a = Math::lerp(res.a, p_to.a, p_weight);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -243,10 +243,8 @@ _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
|
||||
|
||||
Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const {
|
||||
Vector2 res = *this;
|
||||
|
||||
res.x += (p_weight * (p_to.x - x));
|
||||
res.y += (p_weight * (p_to.y - y));
|
||||
|
||||
res.x = Math::lerp(res.x, p_to.x, p_weight);
|
||||
res.y = Math::lerp(res.y, p_to.y, p_weight);
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -279,27 +277,16 @@ Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_
|
||||
|
||||
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
|
||||
Vector2 res = *this;
|
||||
|
||||
/* Formula from Wikipedia article on Bezier curves. */
|
||||
real_t omt = (1.0 - p_t);
|
||||
real_t omt2 = omt * omt;
|
||||
real_t omt3 = omt2 * omt;
|
||||
real_t t2 = p_t * p_t;
|
||||
real_t t3 = t2 * p_t;
|
||||
|
||||
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
|
||||
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
||||
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
|
||||
Vector2 res = *this;
|
||||
|
||||
/* Formula from Wikipedia article on Bezier curves. */
|
||||
real_t omt = (1.0 - p_t);
|
||||
real_t omt2 = omt * omt;
|
||||
real_t t2 = p_t * p_t;
|
||||
|
||||
Vector2 d = (p_control_1 - res) * 3.0 * omt2 + (p_control_2 - p_control_1) * 6.0 * omt * p_t + (p_end - p_control_2) * 3.0 * t2;
|
||||
return d;
|
||||
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
||||
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
|
||||
|
@ -209,10 +209,11 @@ Vector3 Vector3::round() const {
|
||||
}
|
||||
|
||||
Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
|
||||
return Vector3(
|
||||
x + (p_weight * (p_to.x - x)),
|
||||
y + (p_weight * (p_to.y - y)),
|
||||
z + (p_weight * (p_to.z - z)));
|
||||
Vector3 res = *this;
|
||||
res.x = Math::lerp(res.x, p_to.x, p_weight);
|
||||
res.y = Math::lerp(res.y, p_to.y, p_weight);
|
||||
res.z = Math::lerp(res.z, p_to.z, p_weight);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
|
||||
@ -255,27 +256,18 @@ Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_
|
||||
|
||||
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
|
||||
Vector3 res = *this;
|
||||
|
||||
/* Formula from Wikipedia article on Bezier curves. */
|
||||
real_t omt = (1.0 - p_t);
|
||||
real_t omt2 = omt * omt;
|
||||
real_t omt3 = omt2 * omt;
|
||||
real_t t2 = p_t * p_t;
|
||||
real_t t3 = t2 * p_t;
|
||||
|
||||
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
|
||||
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
||||
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
||||
res.z = Math::bezier_interpolate(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector3 Vector3::bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
|
||||
Vector3 res = *this;
|
||||
|
||||
/* Formula from Wikipedia article on Bezier curves. */
|
||||
real_t omt = (1.0 - p_t);
|
||||
real_t omt2 = omt * omt;
|
||||
real_t t2 = p_t * p_t;
|
||||
|
||||
Vector3 d = (p_control_1 - res) * 3.0 * omt2 + (p_control_2 - p_control_1) * 6.0 * omt * p_t + (p_end - p_control_2) * 3.0 * t2;
|
||||
return d;
|
||||
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
||||
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
||||
res.z = Math::bezier_derivative(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
|
||||
return res;
|
||||
}
|
||||
|
||||
real_t Vector3::distance_to(const Vector3 &p_to) const {
|
||||
|
@ -130,11 +130,12 @@ Vector4 Vector4::round() const {
|
||||
}
|
||||
|
||||
Vector4 Vector4::lerp(const Vector4 &p_to, const real_t p_weight) const {
|
||||
return Vector4(
|
||||
x + (p_weight * (p_to.x - x)),
|
||||
y + (p_weight * (p_to.y - y)),
|
||||
z + (p_weight * (p_to.z - z)),
|
||||
w + (p_weight * (p_to.w - w)));
|
||||
Vector4 res = *this;
|
||||
res.x = Math::lerp(res.x, p_to.x, p_weight);
|
||||
res.y = Math::lerp(res.y, p_to.y, p_weight);
|
||||
res.z = Math::lerp(res.z, p_to.z, p_weight);
|
||||
res.w = Math::lerp(res.w, p_to.w, p_weight);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const {
|
||||
|
@ -249,14 +249,11 @@ namespace Godot
|
||||
/// <returns>The interpolated vector.</returns>
|
||||
public readonly Vector2 BezierInterpolate(Vector2 control1, Vector2 control2, Vector2 end, real_t t)
|
||||
{
|
||||
// Formula from Wikipedia article on Bezier curves
|
||||
real_t omt = 1 - t;
|
||||
real_t omt2 = omt * omt;
|
||||
real_t omt3 = omt2 * omt;
|
||||
real_t t2 = t * t;
|
||||
real_t t3 = t2 * t;
|
||||
|
||||
return this * omt3 + control1 * omt2 * t * 3 + control2 * omt * t2 * 3 + end * t3;
|
||||
return new Vector2
|
||||
(
|
||||
Mathf.BezierInterpolate(x, control1.x, control2.x, end.x, t),
|
||||
Mathf.BezierInterpolate(y, control1.y, control2.y, end.y, t)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -243,14 +243,12 @@ namespace Godot
|
||||
/// <returns>The interpolated vector.</returns>
|
||||
public readonly Vector3 BezierInterpolate(Vector3 control1, Vector3 control2, Vector3 end, real_t t)
|
||||
{
|
||||
// Formula from Wikipedia article on Bezier curves
|
||||
real_t omt = 1 - t;
|
||||
real_t omt2 = omt * omt;
|
||||
real_t omt3 = omt2 * omt;
|
||||
real_t t2 = t * t;
|
||||
real_t t3 = t2 * t;
|
||||
|
||||
return this * omt3 + control1 * omt2 * t * 3 + control2 * omt * t2 * 3 + end * t3;
|
||||
return new Vector3
|
||||
(
|
||||
Mathf.BezierInterpolate(x, control1.x, control2.x, end.x, t),
|
||||
Mathf.BezierInterpolate(y, control1.y, control2.y, end.y, t),
|
||||
Mathf.BezierInterpolate(z, control1.z, control2.z, end.z, t)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -2779,13 +2779,7 @@ void TextMesh::_generate_glyph_mesh_data(const GlyphMeshKey &p_key, const Glyph
|
||||
real_t step = CLAMP(curve_step / (p0 - p3).length(), 0.01, 0.5);
|
||||
real_t t = step;
|
||||
while (t < 1.0) {
|
||||
real_t omt = (1.0 - t);
|
||||
real_t omt2 = omt * omt;
|
||||
real_t omt3 = omt2 * omt;
|
||||
real_t t2 = t * t;
|
||||
real_t t3 = t2 * t;
|
||||
|
||||
Vector2 point = p0 * omt3 + p1 * omt2 * t * 3.0 + p2 * omt * t2 * 3.0 + p3 * t3;
|
||||
Vector2 point = p0.bezier_interpolate(p1, p2, p3, t);
|
||||
Vector2 p = point * pixel_size + origin;
|
||||
polygon.push_back(ContourPoint(p, false));
|
||||
t += step;
|
||||
|
Loading…
Reference in New Issue
Block a user