Fix pingpong with loop wrap is not working

This commit is contained in:
Silc Renew 2022-12-25 12:50:35 +09:00
parent f382a2b59b
commit 8745c206c4
3 changed files with 117 additions and 159 deletions

View File

@ -220,7 +220,7 @@ Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const
ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight); ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
Quaternion q2 = to_q * ln.exp(); Quaternion q2 = to_q * ln.exp();
// To cancel error made by Expmap ambiguity, do blends. // To cancel error made by Expmap ambiguity, do blending.
return q1.slerp(q2, p_weight); return q1.slerp(q2, p_weight);
} }
@ -271,7 +271,7 @@ Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b
ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
Quaternion q2 = to_q * ln.exp(); Quaternion q2 = to_q * ln.exp();
// To cancel error made by Expmap ambiguity, do blends. // To cancel error made by Expmap ambiguity, do blending.
return q1.slerp(q2, p_weight); return q1.slerp(q2, p_weight);
} }

View File

@ -194,7 +194,7 @@ namespace Godot
0); 0);
Quaternion q2 = toQ * ln.Exp(); Quaternion q2 = toQ * ln.Exp();
// To cancel error made by Expmap ambiguity, do blends. // To cancel error made by Expmap ambiguity, do blending.
return q1.Slerp(q2, weight); return q1.Slerp(q2, weight);
} }
@ -263,7 +263,7 @@ namespace Godot
0); 0);
Quaternion q2 = toQ * ln.Exp(); Quaternion q2 = toQ * ln.Exp();
// To cancel error made by Expmap ambiguity, do blends. // To cancel error made by Expmap ambiguity, do blending.
return q1.Slerp(q2, weight); return q1.Slerp(q2, weight);
} }

View File

@ -2463,145 +2463,127 @@ T Animation::_interpolate(const Vector<TKey<T>> &p_keys, double p_time, Interpol
int idx = _find(p_keys, p_time, p_backward); int idx = _find(p_keys, p_time, p_backward);
ERR_FAIL_COND_V(idx == -2, T()); ERR_FAIL_COND_V(idx == -2, T());
int maxi = len - 1;
bool is_start_edge = idx == -1;
bool is_end_edge = p_backward ? idx == 0 : idx >= maxi;
int next = 0;
real_t c = 0.0; real_t c = 0.0;
// prepare for all cases of interpolation // Prepare for all cases of interpolation.
real_t delta = 0.0;
real_t from = 0.0;
if (loop_mode == LOOP_LINEAR && p_loop_wrap) { int pre = -1;
// loop int next = -1;
if (!p_backward) { int post = -1;
// no backward real_t pre_t = 0.0;
if (idx >= 0) { real_t to_t = 0.0;
if (idx < len - 1) { real_t post_t = 0.0;
next = idx + 1;
real_t delta = p_keys[next].time - p_keys[idx].time;
real_t from = p_time - p_keys[idx].time;
if (Math::is_zero_approx(delta)) { bool use_cubic = p_interp == INTERPOLATION_CUBIC || p_interp == INTERPOLATION_CUBIC_ANGLE;
c = 0;
} else {
c = from / delta;
}
} else {
next = 0;
real_t delta = (length - p_keys[idx].time) + p_keys[next].time;
real_t from = p_time - p_keys[idx].time;
if (Math::is_zero_approx(delta)) { if (!p_loop_wrap || loop_mode == LOOP_NONE) {
c = 0; if (is_start_edge) {
} else { idx = p_backward ? maxi : 0;
c = from / delta; }
} next = CLAMP(idx + (p_backward ? -1 : 1), 0, maxi);
} if (use_cubic) {
} else { pre = CLAMP(idx + (p_backward ? 1 : -1), 0, maxi);
// on loop, behind first key post = CLAMP(idx + (p_backward ? -2 : 2), 0, maxi);
idx = len - 1; }
next = 0; } else if (loop_mode == LOOP_LINEAR) {
if (is_start_edge) {
idx = p_backward ? 0 : maxi;
}
next = Math::posmod(idx + (p_backward ? -1 : 1), len);
if (use_cubic) {
pre = Math::posmod(idx + (p_backward ? 1 : -1), len);
post = Math::posmod(idx + (p_backward ? -2 : 2), len);
}
if (is_start_edge) {
if (!p_backward) {
real_t endtime = (length - p_keys[idx].time); real_t endtime = (length - p_keys[idx].time);
if (endtime < 0) { // may be keys past the end if (endtime < 0) { // may be keys past the end
endtime = 0; endtime = 0;
} }
real_t delta = endtime + p_keys[next].time; delta = endtime + p_keys[next].time;
real_t from = endtime + p_time; from = endtime + p_time;
if (Math::is_zero_approx(delta)) {
c = 0;
} else {
c = from / delta;
}
}
} else {
// backward
if (idx <= len - 1) {
if (idx > 0) {
next = idx - 1;
real_t delta = (length - p_keys[next].time) - (length - p_keys[idx].time);
real_t from = (length - p_time) - (length - p_keys[idx].time);
if (Math::is_zero_approx(delta)) {
c = 0;
} else {
c = from / delta;
}
} else {
next = len - 1;
real_t delta = p_keys[idx].time + (length - p_keys[next].time);
real_t from = (length - p_time) - (length - p_keys[idx].time);
if (Math::is_zero_approx(delta)) {
c = 0;
} else {
c = from / delta;
}
}
} else { } else {
// on loop, in front of last key
idx = 0;
next = len - 1;
real_t endtime = p_keys[idx].time; real_t endtime = p_keys[idx].time;
if (endtime > length) { // may be keys past the end if (endtime > length) { // may be keys past the end
endtime = length; endtime = length;
} }
real_t delta = p_keys[next].time - endtime; delta = endtime + length - p_keys[next].time;
real_t from = p_time - endtime; from = endtime + length - p_time;
}
if (Math::is_zero_approx(delta)) { } else if (is_end_edge) {
c = 0; if (!p_backward) {
} else { delta = (length - p_keys[idx].time) + p_keys[next].time;
c = from / delta; from = p_time - p_keys[idx].time;
} } else {
delta = p_keys[idx].time + (length - p_keys[next].time);
from = (length - p_time) - (length - p_keys[idx].time);
} }
} }
} else { // no loop } else {
if (is_start_edge) {
idx = p_backward ? len : -1;
}
next = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? -1 : 1)) + 0.5f, (float)len) - 0.5f);
if (use_cubic) {
pre = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? 1 : -1)) + 0.5f, (float)len) - 0.5f);
post = (int)Math::round(Math::pingpong((float)(idx + (p_backward ? -2 : 2)) + 0.5f, (float)len) - 0.5f);
}
idx = (int)Math::round(Math::pingpong((float)idx + 0.5f, (float)len) - 0.5f);
if (is_start_edge) {
if (!p_backward) {
real_t endtime = p_keys[idx].time;
if (endtime < 0) { // may be keys past the end
endtime = 0;
}
delta = endtime + p_keys[next].time;
from = endtime + p_time;
} else {
real_t endtime = length - p_keys[idx].time;
if (endtime > length) { // may be keys past the end
endtime = length;
}
delta = endtime + length - p_keys[next].time;
from = endtime + length - p_time;
}
} else if (is_end_edge) {
if (!p_backward) {
delta = length * 2.0 - p_keys[idx].time - p_keys[next].time;
from = p_time - p_keys[idx].time;
} else {
delta = p_keys[idx].time + p_keys[next].time;
from = (length - p_time) - (length - p_keys[idx].time);
}
}
}
if (!is_start_edge && !is_end_edge) {
if (!p_backward) { if (!p_backward) {
if (idx >= 0) { delta = p_keys[next].time - p_keys[idx].time;
if (idx < len - 1) { from = p_time - p_keys[idx].time;
next = idx + 1;
real_t delta = p_keys[next].time - p_keys[idx].time;
real_t from = p_time - p_keys[idx].time;
if (Math::is_zero_approx(delta)) {
c = 0;
} else {
c = from / delta;
}
} else {
next = idx;
}
} else {
idx = next = 0;
}
} else { } else {
if (idx <= len - 1) { delta = (length - p_keys[next].time) - (length - p_keys[idx].time);
if (idx > 0) { from = (length - p_time) - (length - p_keys[idx].time);
next = idx - 1;
real_t delta = (length - p_keys[next].time) - (length - p_keys[idx].time);
real_t from = (length - p_time) - (length - p_keys[idx].time);
if (Math::is_zero_approx(delta)) {
c = 0;
} else {
c = from / delta;
}
} else {
next = idx;
}
} else {
idx = next = len - 1;
}
} }
} }
if (Math::is_zero_approx(delta)) {
c = 0;
} else {
c = from / delta;
}
if (p_ok) { if (p_ok) {
*p_ok = true; *p_ok = true;
} }
real_t tr = p_keys[idx].transition; real_t tr = p_keys[idx].transition;
if (tr == 0) {
if (tr == 0 || idx == next) { // Don't interpolate if not needed.
// don't interpolate if not needed
return p_keys[idx].value; return p_keys[idx].value;
} }
@ -2621,48 +2603,11 @@ T Animation::_interpolate(const Vector<TKey<T>> &p_keys, double p_time, Interpol
} break; } break;
case INTERPOLATION_CUBIC: case INTERPOLATION_CUBIC:
case INTERPOLATION_CUBIC_ANGLE: { case INTERPOLATION_CUBIC_ANGLE: {
int pre = 0; if (!p_loop_wrap || loop_mode == LOOP_NONE) {
int post = 0; pre_t = p_keys[pre].time - p_keys[idx].time;
if (!p_backward) { to_t = p_keys[next].time - p_keys[idx].time;
pre = idx - 1; post_t = p_keys[post].time - p_keys[idx].time;
if (pre < 0) { } else if (loop_mode == LOOP_LINEAR) {
if (loop_mode == LOOP_LINEAR && p_loop_wrap) {
pre = len - 1;
} else {
pre = 0;
}
}
post = next + 1;
if (post >= len) {
if (loop_mode == LOOP_LINEAR && p_loop_wrap) {
post = 0;
} else {
post = next;
}
}
} else {
pre = idx + 1;
if (pre >= len) {
if (loop_mode == LOOP_LINEAR && p_loop_wrap) {
pre = 0;
} else {
pre = idx;
}
}
post = next - 1;
if (post < 0) {
if (loop_mode == LOOP_LINEAR && p_loop_wrap) {
post = len - 1;
} else {
post = 0;
}
}
}
real_t pre_t = 0.0;
real_t to_t = 0.0;
real_t post_t = 0.0;
if (loop_mode == LOOP_LINEAR && p_loop_wrap) {
pre_t = pre > idx ? -length + p_keys[pre].time - p_keys[idx].time : p_keys[pre].time - p_keys[idx].time; pre_t = pre > idx ? -length + p_keys[pre].time - p_keys[idx].time : p_keys[pre].time - p_keys[idx].time;
to_t = next < idx ? length + p_keys[next].time - p_keys[idx].time : p_keys[next].time - p_keys[idx].time; to_t = next < idx ? length + p_keys[next].time - p_keys[idx].time : p_keys[next].time - p_keys[idx].time;
post_t = next < idx || post <= idx ? length + p_keys[post].time - p_keys[idx].time : p_keys[post].time - p_keys[idx].time; post_t = next < idx || post <= idx ? length + p_keys[post].time - p_keys[idx].time : p_keys[post].time - p_keys[idx].time;
@ -2670,6 +2615,19 @@ T Animation::_interpolate(const Vector<TKey<T>> &p_keys, double p_time, Interpol
pre_t = p_keys[pre].time - p_keys[idx].time; pre_t = p_keys[pre].time - p_keys[idx].time;
to_t = p_keys[next].time - p_keys[idx].time; to_t = p_keys[next].time - p_keys[idx].time;
post_t = p_keys[post].time - p_keys[idx].time; post_t = p_keys[post].time - p_keys[idx].time;
if ((pre > idx && idx == next && post < next) || (pre < idx && idx == next && post > next)) {
pre_t = p_keys[idx].time - p_keys[pre].time;
} else if (pre == idx) {
pre_t = idx < next ? -p_keys[idx].time * 2.0 : (length - p_keys[idx].time) * 2.0;
}
if (idx == next) {
to_t = pre < idx ? (length - p_keys[idx].time) * 2.0 : -p_keys[idx].time * 2.0;
post_t = p_keys[next].time - p_keys[post].time + to_t;
} else if (next == post) {
post_t = idx < next ? (length - p_keys[next].time) * 2.0 + to_t : -p_keys[next].time * 2.0 + to_t;
}
} }
if (p_interp == INTERPOLATION_CUBIC_ANGLE) { if (p_interp == INTERPOLATION_CUBIC_ANGLE) {