Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed interpolation for discrete types #53548

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions core/variant/variant_setget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,11 @@ Variant Variant::duplicate(bool deep) const {
}
}

template <class T>
static _ALWAYS_INLINE_ T ilerp(T a, T b, float c) {
return T(Math::round(a + (b - a) * c));
}

void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) {
if (a.type != b.type) {
if (a.is_num() && b.is_num()) {
Expand All @@ -1883,7 +1888,7 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst)
case INT: {
int64_t va = a._data._int;
int64_t vb = b._data._int;
r_dst = int(va + vb * c + 0.5);
r_dst = ilerp(va, vb, c);
}
return;
case FLOAT: {
Expand All @@ -1901,7 +1906,7 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst)
int32_t vbx = reinterpret_cast<const Vector2i *>(b._data._mem)->x;
int32_t vay = reinterpret_cast<const Vector2i *>(a._data._mem)->y;
int32_t vby = reinterpret_cast<const Vector2i *>(b._data._mem)->y;
r_dst = Vector2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5));
r_dst = Vector2i(ilerp(vax, vbx, c), ilerp(vay, vby, c));
}
return;
case RECT2: {
Expand All @@ -1916,14 +1921,15 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst)

int32_t vax = ra->position.x;
int32_t vay = ra->position.y;
int32_t vbx = ra->size.x;
int32_t vby = ra->size.y;
int32_t vcx = rb->position.x;
int32_t vcy = rb->position.y;
int32_t vdx = rb->size.x;
int32_t vdy = rb->size.y;
int32_t vaw = ra->size.x;
int32_t vah = ra->size.y;

r_dst = Rect2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vcx + vdx * c + 0.5), int32_t(vcy + vdy * c + 0.5));
int32_t vbx = rb->position.x;
int32_t vby = rb->position.y;
int32_t vbw = rb->size.x;
int32_t vbh = rb->size.y;

r_dst = Rect2i(ilerp(vax, vbx, c), ilerp(vay, vby, c), ilerp(vaw, vbw, c), ilerp(vah, vbh, c));
}
return;
case VECTOR3: {
Expand All @@ -1937,7 +1943,7 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst)
int32_t vby = reinterpret_cast<const Vector3i *>(b._data._mem)->y;
int32_t vaz = reinterpret_cast<const Vector3i *>(a._data._mem)->z;
int32_t vbz = reinterpret_cast<const Vector3i *>(b._data._mem)->z;
r_dst = Vector3i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vaz + vbz * c + 0.5));
r_dst = Vector3i(ilerp(vax, vbx, c), ilerp(vay, vby, c), ilerp(vaz, vbz, c));
}
return;
case AABB: {
Expand Down Expand Up @@ -2000,7 +2006,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
case INT: {
int64_t va = a._data._int;
int64_t vb = b._data._int;
r_dst = int(va + (vb - va) * c);
r_dst = ilerp(va, vb, c);
}
return;
case FLOAT: {
Expand Down Expand Up @@ -2058,7 +2064,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
int32_t vbx = reinterpret_cast<const Vector2i *>(b._data._mem)->x;
int32_t vay = reinterpret_cast<const Vector2i *>(a._data._mem)->y;
int32_t vby = reinterpret_cast<const Vector2i *>(b._data._mem)->y;
r_dst = Vector2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5));
r_dst = Vector2i(ilerp(vax, vbx, c), ilerp(vay, vby, c));
}
return;

Expand All @@ -2072,14 +2078,15 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &

int32_t vax = ra->position.x;
int32_t vay = ra->position.y;
int32_t vbx = ra->size.x;
int32_t vby = ra->size.y;
int32_t vcx = rb->position.x;
int32_t vcy = rb->position.y;
int32_t vdx = rb->size.x;
int32_t vdy = rb->size.y;
int32_t vaw = ra->size.x;
int32_t vah = ra->size.y;

int32_t vbx = rb->position.x;
int32_t vby = rb->position.y;
int32_t vbw = rb->size.x;
int32_t vbh = rb->size.y;

r_dst = Rect2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vcx + vdx * c + 0.5), int32_t(vcy + vdy * c + 0.5));
r_dst = Rect2i(ilerp(vax, vbx, c), ilerp(vay, vby, c), ilerp(vaw, vbw, c), ilerp(vah, vbh, c));
}
return;

Expand All @@ -2094,7 +2101,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
int32_t vby = reinterpret_cast<const Vector3i *>(b._data._mem)->y;
int32_t vaz = reinterpret_cast<const Vector3i *>(a._data._mem)->z;
int32_t vbz = reinterpret_cast<const Vector3i *>(b._data._mem)->z;
r_dst = Vector3i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vaz + vbz * c + 0.5));
r_dst = Vector3i(ilerp(vax, vbx, c), ilerp(vay, vby, c), ilerp(vaz, vbz, c));
}
return;

Expand Down