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

Fix blend animation to solve TRS track bug & blend order inconsistency #57675

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions core/math/quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ Quaternion Quaternion::inverse() const {
return Quaternion(-x, -y, -z, w);
}

Quaternion Quaternion::log() const {
Quaternion src = *this;
Vector3 src_v = src.get_axis() * src.get_angle();
return Quaternion(src_v.x, src_v.y, src_v.z, 0);
}

Quaternion Quaternion::exp() const {
Quaternion src = *this;
Vector3 src_v = Vector3(src.x, src.y, src.z);
float theta = src_v.length();
if (theta < CMP_EPSILON) {
return Quaternion(0, 0, 0, 1);
}
return Quaternion(src_v.normalized(), theta);
}

Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const {
#ifdef MATH_CHECKS
ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized.");
Expand Down Expand Up @@ -190,6 +206,9 @@ Quaternion::operator String() const {
}

Vector3 Quaternion::get_axis() const {
if (Math::abs(w) > 1 - CMP_EPSILON) {
return Vector3(x, y, z);
}
real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
return Vector3(x * r, y * r, z * r);
}
Expand Down
2 changes: 2 additions & 0 deletions core/math/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ struct _NO_DISCARD_ Quaternion {
Quaternion normalized() const;
bool is_normalized() const;
Quaternion inverse() const;
Quaternion log() const;
Quaternion exp() const;
_FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
real_t angle_to(const Quaternion &p_to) const;

Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,8 @@ static void _register_variant_builtin_methods() {
bind_method(Quaternion, is_normalized, sarray(), varray());
bind_method(Quaternion, is_equal_approx, sarray("to"), varray());
bind_method(Quaternion, inverse, sarray(), varray());
bind_method(Quaternion, log, sarray(), varray());
bind_method(Quaternion, exp, sarray(), varray());
bind_method(Quaternion, angle_to, sarray("to"), varray());
bind_method(Quaternion, dot, sarray("with"), varray());
bind_method(Quaternion, slerp, sarray("to", "weight"), varray());
Expand Down
10 changes: 10 additions & 0 deletions doc/classes/Quaternion.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
Returns the dot product of two quaternions.
</description>
</method>
<method name="exp" qualifiers="const">
<return type="Quaternion" />
<description>
</description>
</method>
<method name="get_angle" qualifiers="const">
<return type="float" />
<description>
Expand Down Expand Up @@ -138,6 +143,11 @@
Returns the length of the quaternion, squared.
</description>
</method>
<method name="log" qualifiers="const">
<return type="Quaternion" />
<description>
</description>
</method>
<method name="normalized" qualifiers="const">
<return type="Quaternion" />
<description>
Expand Down
96 changes: 46 additions & 50 deletions scene/animation/animation_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Stri
}
}

if (!p_seek && p_optimize && !any_valid) { //pointless to go on, all are zero
return 0;
}

String new_path;
AnimationNode *new_parent;

Expand All @@ -289,6 +285,10 @@ double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Stri
new_parent = parent;
new_path = String(parent->base_path) + String(p_subpath) + "/";
}

if (!p_seek && p_optimize && !any_valid) {
return p_node->_pre_process(new_path, new_parent, state, 0, p_seek, p_connections);
}
return p_node->_pre_process(new_path, new_parent, state, p_time, p_seek, p_connections);
}

Expand Down Expand Up @@ -618,6 +618,11 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) {
int bone_idx = sk->find_bone(path.get_subname(0));
if (bone_idx != -1) {
track_xform->bone_idx = bone_idx;
Transform3D rest = sk->get_bone_rest(bone_idx);
track_xform->init_loc = rest.origin;
track_xform->ref_rot = rest.basis.get_rotation_quaternion();
track_xform->init_rot = track_xform->ref_rot.log();
track_xform->init_scale = rest.basis.get_scale();
}
}

Expand All @@ -644,7 +649,6 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) {
} break;
case Animation::TYPE_BLEND_SHAPE: {
#ifndef _3D_DISABLED

if (path.get_subname_count() != 1) {
ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not contain a blend shape subname: '" + String(path) + "'");
continue;
Expand Down Expand Up @@ -942,23 +946,16 @@ void AnimationTree::_process_graph(double p_delta) {

real_t blend = (*as.track_blends)[blend_idx] * weight;

if (blend < CMP_EPSILON) {
continue; //nothing to blend
}

switch (ttype) {
case Animation::TYPE_POSITION_3D: {
#ifndef _3D_DISABLED
TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);

if (t->process_pass != process_pass) {
t->process_pass = process_pass;
t->loc = Vector3();
t->rot = Quaternion();
t->rot_blend_accum = 0;
t->scale = Vector3(1, 1, 1);
t->loc = t->init_loc;
t->rot = t->init_rot;
t->scale = t->init_scale;
}

if (track->root_motion) {
double prev_time = time - delta;
if (!backward) {
Expand Down Expand Up @@ -1036,22 +1033,19 @@ void AnimationTree::_process_graph(double p_delta) {
continue;
}

t->loc = t->loc.lerp(loc, blend);
t->loc += (loc - t->init_loc) * blend;
}
#endif // _3D_DISABLED
} break;
case Animation::TYPE_ROTATION_3D: {
#ifndef _3D_DISABLED
TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);

if (t->process_pass != process_pass) {
t->process_pass = process_pass;
t->loc = Vector3();
t->rot = Quaternion();
t->rot_blend_accum = 0;
t->scale = Vector3(1, 1, 1);
t->loc = t->init_loc;
t->rot = t->init_rot;
t->scale = t->init_scale;
}

if (track->root_motion) {
double prev_time = time - delta;
if (!backward) {
Expand Down Expand Up @@ -1097,8 +1091,7 @@ void AnimationTree::_process_graph(double p_delta) {
continue;
}
a->rotation_track_interpolate(i, (double)a->get_length(), &rot[1]);
Quaternion q = Quaternion().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
t->rot = (t->rot * q).normalized();
t->rot += (rot[1].log() - rot[0].log()) * blend;
prev_time = 0;
}
} else {
Expand All @@ -1108,8 +1101,7 @@ void AnimationTree::_process_graph(double p_delta) {
continue;
}
a->rotation_track_interpolate(i, 0, &rot[1]);
Quaternion q = Quaternion().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
t->rot = (t->rot * q).normalized();
t->rot += (rot[1].log() - rot[0].log()) * blend;
prev_time = 0;
}
}
Expand All @@ -1120,8 +1112,7 @@ void AnimationTree::_process_graph(double p_delta) {
}

a->rotation_track_interpolate(i, time, &rot[1]);
Quaternion q = Quaternion().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
t->rot = (t->rot * q).normalized();
t->rot += (rot[1].log() - rot[0].log()) * blend;
prev_time = !backward ? 0 : (double)a->get_length();

} else {
Expand All @@ -1132,29 +1123,22 @@ void AnimationTree::_process_graph(double p_delta) {
continue;
}

if (t->rot_blend_accum == 0) {
t->rot = rot;
t->rot_blend_accum = blend;
} else {
real_t rot_total = t->rot_blend_accum + blend;
t->rot = rot.slerp(t->rot, t->rot_blend_accum / rot_total).normalized();
t->rot_blend_accum = rot_total;
if (signbit(rot.dot(t->ref_rot))) {
rot = -rot;
}
t->rot += (rot.log() - t->init_rot) * blend;
}
#endif // _3D_DISABLED
} break;
case Animation::TYPE_SCALE_3D: {
#ifndef _3D_DISABLED
TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);

if (t->process_pass != process_pass) {
t->process_pass = process_pass;
t->loc = Vector3();
t->rot = Quaternion();
t->rot_blend_accum = 0;
t->scale = Vector3(1, 1, 1);
t->loc = t->init_loc;
t->rot = t->init_rot;
t->scale = t->init_scale;
}

if (track->root_motion) {
double prev_time = time - delta;
if (!backward) {
Expand Down Expand Up @@ -1232,7 +1216,7 @@ void AnimationTree::_process_graph(double p_delta) {
continue;
}

t->scale = t->scale.lerp(scale, blend);
t->scale += (scale - t->init_scale) * blend;
}
#endif // _3D_DISABLED
} break;
Expand All @@ -1254,7 +1238,7 @@ void AnimationTree::_process_graph(double p_delta) {
continue;
}

t->value = Math::lerp(t->value, value, (float)blend);
t->value += value * blend;
#endif // _3D_DISABLED
} break;
case Animation::TYPE_VALUE: {
Expand All @@ -1271,13 +1255,16 @@ void AnimationTree::_process_graph(double p_delta) {
}

if (t->process_pass != process_pass) {
t->value = value;
t->process_pass = process_pass;
t->value = value;
t->value.zero();
}

Variant::interpolate(t->value, value, blend, t->value);

Variant::blend(t->value, value, blend, t->value);
} else {
if (blend < CMP_EPSILON) {
continue; //nothing to blend
}
List<int> indices;
a->value_track_get_key_indices(i, time, delta, &indices, pingponged);

Expand All @@ -1289,7 +1276,10 @@ void AnimationTree::_process_graph(double p_delta) {

} break;
case Animation::TYPE_METHOD: {
if (delta == 0) {
if (blend < CMP_EPSILON) {
continue; //nothing to blend
}
if (!seeked && Math::is_zero_approx(delta)) {
continue;
}
TrackCacheMethod *t = static_cast<TrackCacheMethod *>(track);
Expand All @@ -1312,14 +1302,16 @@ void AnimationTree::_process_graph(double p_delta) {
real_t bezier = a->bezier_track_interpolate(i, time);

if (t->process_pass != process_pass) {
t->value = bezier;
t->process_pass = process_pass;
t->value = 0;
}

t->value = Math::lerp(t->value, bezier, blend);

t->value += bezier * blend;
} break;
case Animation::TYPE_AUDIO: {
if (blend < CMP_EPSILON) {
continue; //nothing to blend
}
TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);

if (seeked) {
Expand Down Expand Up @@ -1431,6 +1423,9 @@ void AnimationTree::_process_graph(double p_delta) {
}
} break;
case Animation::TYPE_ANIMATION: {
if (blend < CMP_EPSILON) {
continue; //nothing to blend
}
TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track);

AnimationPlayer *player2 = Object::cast_to<AnimationPlayer>(t->object);
Expand Down Expand Up @@ -1521,6 +1516,7 @@ void AnimationTree::_process_graph(double p_delta) {
case Animation::TYPE_POSITION_3D: {
#ifndef _3D_DISABLED
TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
t->rot = t->rot.exp();

if (t->root_motion) {
Transform3D xform;
Expand Down
5 changes: 4 additions & 1 deletion scene/animation/animation_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ class AnimationTree : public Node {
bool loc_used = false;
bool rot_used = false;
bool scale_used = false;
Vector3 init_loc = Vector3(0, 0, 0);
Quaternion ref_rot = Quaternion(0, 0, 0, 1);
Quaternion init_rot = Quaternion(0, 0, 0, 0);
Vector3 init_scale = Vector3(1, 1, 1);
Vector3 loc;
Quaternion rot;
real_t rot_blend_accum = 0.0;
Vector3 scale;

TrackCacheTransform() {
Expand Down