mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 18:41:10 +02:00
Merge pull request #4014 from ZappoMan/animationFrameIndex
Fix stuttering in animation caused when animation plays too long without lastFrame set
This commit is contained in:
commit
9a1c521df2
4 changed files with 35 additions and 8 deletions
|
@ -12,16 +12,18 @@
|
||||||
#include "AnimationCache.h"
|
#include "AnimationCache.h"
|
||||||
#include "AnimationLoop.h"
|
#include "AnimationLoop.h"
|
||||||
|
|
||||||
|
const float AnimationLoop::MAXIMUM_POSSIBLE_FRAME = 100000.0f;
|
||||||
|
|
||||||
AnimationLoop::AnimationLoop() :
|
AnimationLoop::AnimationLoop() :
|
||||||
_fps(30.0f),
|
_fps(30.0f),
|
||||||
_loop(false),
|
_loop(false),
|
||||||
_hold(false),
|
_hold(false),
|
||||||
_startAutomatically(false),
|
_startAutomatically(false),
|
||||||
_firstFrame(0.0f),
|
_firstFrame(0.0f),
|
||||||
_lastFrame(FLT_MAX),
|
_lastFrame(MAXIMUM_POSSIBLE_FRAME),
|
||||||
_running(false),
|
_running(false),
|
||||||
_frameIndex(0.0f),
|
_frameIndex(0.0f),
|
||||||
_maxFrameIndexHint(FLT_MAX)
|
_maxFrameIndexHint(MAXIMUM_POSSIBLE_FRAME)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,10 +55,9 @@ AnimationLoop::AnimationLoop(float fps, bool loop, bool hold, bool startAutomati
|
||||||
void AnimationLoop::simulate(float deltaTime) {
|
void AnimationLoop::simulate(float deltaTime) {
|
||||||
_frameIndex += deltaTime * _fps;
|
_frameIndex += deltaTime * _fps;
|
||||||
|
|
||||||
|
|
||||||
// If we knew the number of frames from the animation, we'd consider using it here
|
// If we knew the number of frames from the animation, we'd consider using it here
|
||||||
// animationGeometry.animationFrames.size()
|
// animationGeometry.animationFrames.size()
|
||||||
float maxFrame = _maxFrameIndexHint;
|
float maxFrame = _maxFrameIndexHint;
|
||||||
float endFrameIndex = qMin(_lastFrame, maxFrame - (_loop ? 0.0f : 1.0f));
|
float endFrameIndex = qMin(_lastFrame, maxFrame - (_loop ? 0.0f : 1.0f));
|
||||||
float startFrameIndex = qMin(_firstFrame, endFrameIndex);
|
float startFrameIndex = qMin(_firstFrame, endFrameIndex);
|
||||||
if ((!_loop && (_frameIndex < startFrameIndex || _frameIndex > endFrameIndex)) || startFrameIndex == endFrameIndex) {
|
if ((!_loop && (_frameIndex < startFrameIndex || _frameIndex > endFrameIndex)) || startFrameIndex == endFrameIndex) {
|
||||||
|
|
|
@ -16,6 +16,8 @@ class AnimationDetails;
|
||||||
|
|
||||||
class AnimationLoop {
|
class AnimationLoop {
|
||||||
public:
|
public:
|
||||||
|
static const float MAXIMUM_POSSIBLE_FRAME;
|
||||||
|
|
||||||
AnimationLoop();
|
AnimationLoop();
|
||||||
AnimationLoop(const AnimationDetails& animationDetails);
|
AnimationLoop(const AnimationDetails& animationDetails);
|
||||||
AnimationLoop(float fps, bool loop, bool hold, bool startAutomatically, float firstFrame,
|
AnimationLoop(float fps, bool loop, bool hold, bool startAutomatically, float firstFrame,
|
||||||
|
@ -33,10 +35,10 @@ public:
|
||||||
void setStartAutomatically(bool startAutomatically);
|
void setStartAutomatically(bool startAutomatically);
|
||||||
bool getStartAutomatically() const { return _startAutomatically; }
|
bool getStartAutomatically() const { return _startAutomatically; }
|
||||||
|
|
||||||
void setFirstFrame(float firstFrame) { _firstFrame = firstFrame; }
|
void setFirstFrame(float firstFrame) { _firstFrame = glm::clamp(firstFrame, 0.0f, MAXIMUM_POSSIBLE_FRAME); }
|
||||||
float getFirstFrame() const { return _firstFrame; }
|
float getFirstFrame() const { return _firstFrame; }
|
||||||
|
|
||||||
void setLastFrame(float lastFrame) { _lastFrame = lastFrame; }
|
void setLastFrame(float lastFrame) { _lastFrame = glm::clamp(lastFrame, 0.0f, MAXIMUM_POSSIBLE_FRAME); }
|
||||||
float getLastFrame() const { return _lastFrame; }
|
float getLastFrame() const { return _lastFrame; }
|
||||||
|
|
||||||
void setRunning(bool running);
|
void setRunning(bool running);
|
||||||
|
@ -45,7 +47,7 @@ public:
|
||||||
void setFrameIndex(float frameIndex) { _frameIndex = glm::clamp(frameIndex, _firstFrame, _lastFrame); }
|
void setFrameIndex(float frameIndex) { _frameIndex = glm::clamp(frameIndex, _firstFrame, _lastFrame); }
|
||||||
float getFrameIndex() const { return _frameIndex; }
|
float getFrameIndex() const { return _frameIndex; }
|
||||||
|
|
||||||
void setMaxFrameIndexHint(float value) { _maxFrameIndexHint = value; }
|
void setMaxFrameIndexHint(float value) { _maxFrameIndexHint = glm::clamp(value, 0.0f, MAXIMUM_POSSIBLE_FRAME); }
|
||||||
float getMaxFrameIndexHint() const { return _maxFrameIndexHint; }
|
float getMaxFrameIndexHint() const { return _maxFrameIndexHint; }
|
||||||
|
|
||||||
void start() { setRunning(true); }
|
void start() { setRunning(true); }
|
||||||
|
|
|
@ -401,6 +401,19 @@ void ModelEntityItem::setAnimationURL(const QString& url) {
|
||||||
_animationURL = url;
|
_animationURL = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ModelEntityItem::setAnimationFrameIndex(float value) {
|
||||||
|
#ifdef WANT_DEBUG
|
||||||
|
if (isAnimatingSomething()) {
|
||||||
|
qDebug() << "ModelEntityItem::setAnimationFrameIndex()";
|
||||||
|
qDebug() << " value:" << value;
|
||||||
|
qDebug() << " was:" << _animationLoop.getFrameIndex();
|
||||||
|
qDebug() << " model URL:" << getModelURL();
|
||||||
|
qDebug() << " animation URL:" << getAnimationURL();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
_animationLoop.setFrameIndex(value);
|
||||||
|
}
|
||||||
|
|
||||||
void ModelEntityItem::setAnimationSettings(const QString& value) {
|
void ModelEntityItem::setAnimationSettings(const QString& value) {
|
||||||
// the animations setting is a JSON string that may contain various animation settings.
|
// the animations setting is a JSON string that may contain various animation settings.
|
||||||
// if it includes fps, frameIndex, or running, those values will be parsed out and
|
// if it includes fps, frameIndex, or running, those values will be parsed out and
|
||||||
|
@ -416,6 +429,17 @@ void ModelEntityItem::setAnimationSettings(const QString& value) {
|
||||||
|
|
||||||
if (settingsMap.contains("frameIndex")) {
|
if (settingsMap.contains("frameIndex")) {
|
||||||
float frameIndex = settingsMap["frameIndex"].toFloat();
|
float frameIndex = settingsMap["frameIndex"].toFloat();
|
||||||
|
#ifdef WANT_DEBUG
|
||||||
|
if (isAnimatingSomething()) {
|
||||||
|
qDebug() << "ModelEntityItem::setAnimationSettings() calling setAnimationFrameIndex()...";
|
||||||
|
qDebug() << " model URL:" << getModelURL();
|
||||||
|
qDebug() << " animation URL:" << getAnimationURL();
|
||||||
|
qDebug() << " settings:" << value;
|
||||||
|
qDebug() << " settingsMap[frameIndex]:" << settingsMap["frameIndex"];
|
||||||
|
qDebug(" frameIndex: %20.5f", frameIndex);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
setAnimationFrameIndex(frameIndex);
|
setAnimationFrameIndex(frameIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ public:
|
||||||
void setModelURL(const QString& url) { _modelURL = url; }
|
void setModelURL(const QString& url) { _modelURL = url; }
|
||||||
void setAnimationURL(const QString& url);
|
void setAnimationURL(const QString& url);
|
||||||
static const float DEFAULT_ANIMATION_FRAME_INDEX;
|
static const float DEFAULT_ANIMATION_FRAME_INDEX;
|
||||||
void setAnimationFrameIndex(float value) { _animationLoop.setFrameIndex(value); }
|
void setAnimationFrameIndex(float value);
|
||||||
void setAnimationSettings(const QString& value);
|
void setAnimationSettings(const QString& value);
|
||||||
|
|
||||||
static const bool DEFAULT_ANIMATION_IS_PLAYING;
|
static const bool DEFAULT_ANIMATION_IS_PLAYING;
|
||||||
|
|
Loading…
Reference in a new issue