AnimClip & accumulateTime smoother looping anims

Looping animations should have an extra frame of interpolation between the start and end frames.
This commit is contained in:
Anthony J. Thibault 2015-10-16 12:11:46 -07:00
parent 8e7e94c501
commit 073cec41c4
2 changed files with 11 additions and 7 deletions

View file

@ -47,16 +47,17 @@ const AnimPoseVec& AnimClip::evaluate(const AnimVariantMap& animVars, float dt,
}
if (_anim.size()) {
int frameCount = _anim.size();
int prevIndex = (int)glm::floor(_frame);
int nextIndex = (int)glm::ceil(_frame);
if (_loopFlag && nextIndex >= frameCount) {
nextIndex = 0;
int nextIndex;
if (_loopFlag && _frame >= _endFrame) {
nextIndex = (int)glm::ceil(_startFrame);
} else {
nextIndex = (int)glm::ceil(_frame);
}
// It can be quite possible for the user to set _startFrame and _endFrame to
// values before or past valid ranges. We clamp the frames here.
int frameCount = _anim.size();
prevIndex = std::min(std::max(0, prevIndex), frameCount - 1);
nextIndex = std::min(std::max(0, nextIndex), frameCount - 1);

View file

@ -29,8 +29,7 @@ float accumulateTime(float startFrame, float endFrame, float timeScale, float cu
float frame = currentFrame;
const float clampedStartFrame = std::min(startFrame, endFrame);
if (clampedStartFrame == endFrame) {
// when clampedStartFrame >= endFrame
if (fabsf(clampedStartFrame - endFrame) < 1.0f) {
frame = endFrame;
} else if (timeScale > 0.0f) {
// accumulate time, keeping track of loops and end of animation events.
@ -38,6 +37,10 @@ float accumulateTime(float startFrame, float endFrame, float timeScale, float cu
float framesRemaining = (dt * timeScale) * FRAMES_PER_SECOND;
while (framesRemaining > 0.0f) {
float framesTillEnd = endFrame - frame;
// when looping, add one frame between start and end.
if (loopFlag) {
framesTillEnd += 1.0f;
}
if (framesRemaining >= framesTillEnd) {
if (loopFlag) {
// anim loop