optimize ModelEntityItem::getAnimationFrame()

This commit is contained in:
ZappoMan 2015-08-07 13:43:57 -07:00
parent f4c3c30f6a
commit cec0f8ed1d
5 changed files with 30 additions and 14 deletions

View file

@ -91,6 +91,10 @@ QVector<FBXAnimationFrame> Animation::getFrames() const {
return _geometry.animationFrames; return _geometry.animationFrames;
} }
const QVector<FBXAnimationFrame>& Animation::getFramesReference() const {
return _geometry.animationFrames;
}
void Animation::setGeometry(const FBXGeometry& geometry) { void Animation::setGeometry(const FBXGeometry& geometry) {
_geometry = geometry; _geometry = geometry;
finishedLoading(true); finishedLoading(true);

View file

@ -57,6 +57,8 @@ public:
Q_INVOKABLE QStringList getJointNames() const; Q_INVOKABLE QStringList getJointNames() const;
Q_INVOKABLE QVector<FBXAnimationFrame> getFrames() const; Q_INVOKABLE QVector<FBXAnimationFrame> getFrames() const;
const QVector<FBXAnimationFrame>& getFramesReference() const;
protected: protected:

View file

@ -275,7 +275,7 @@ void RenderableModelEntityItem::render(RenderArgs* args) {
} }
if (jointsMapped()) { if (jointsMapped()) {
QVector<glm::quat> frameData = getAnimationFrame(); auto frameData = getAnimationFrame();
for (int i = 0; i < frameData.size(); i++) { for (int i = 0; i < frameData.size(); i++) {
_model->setJointState(i, true, frameData[i]); _model->setJointState(i, true, frameData[i]);
} }

View file

@ -39,6 +39,7 @@ ModelEntityItem::ModelEntityItem(const EntityItemID& entityItemID, const EntityI
setProperties(properties); setProperties(properties);
_lastAnimated = usecTimestampNow(); _lastAnimated = usecTimestampNow();
_jointMappingCompleted = false; _jointMappingCompleted = false;
_lastKnownFrameIndex = -1;
_color[0] = _color[1] = _color[2] = 0; _color[0] = _color[1] = _color[2] = 0;
} }
@ -217,15 +218,17 @@ void ModelEntityItem::mapJoints(const QStringList& modelJointNames) {
} }
} }
QVector<glm::quat> ModelEntityItem::getAnimationFrame() { const QVector<glm::quat>& ModelEntityItem::getAnimationFrame() {
QVector<glm::quat> frameData;
if (!hasAnimation() || !_jointMappingCompleted) { if (!hasAnimation() || !_jointMappingCompleted) {
return frameData; return _lastKnownFrameData;
} }
AnimationPointer myAnimation = getAnimation(_animationURL); AnimationPointer myAnimation = getAnimation(_animationURL); // FIXME: this could be optimized
if (myAnimation && myAnimation->isLoaded()) { if (myAnimation && myAnimation->isLoaded()) {
QVector<FBXAnimationFrame> frames = myAnimation->getFrames();
const QVector<FBXAnimationFrame>& frames = myAnimation->getFramesReference(); // NOTE: getFrames() is too heavy
int frameCount = frames.size(); int frameCount = frames.size();
if (frameCount > 0) { if (frameCount > 0) {
int animationFrameIndex = (int)(glm::floor(getAnimationFrameIndex())) % frameCount; int animationFrameIndex = (int)(glm::floor(getAnimationFrameIndex())) % frameCount;
@ -233,18 +236,22 @@ QVector<glm::quat> ModelEntityItem::getAnimationFrame() {
animationFrameIndex = 0; animationFrameIndex = 0;
} }
QVector<glm::quat> rotations = frames[animationFrameIndex].rotations; if (animationFrameIndex != _lastKnownFrameIndex) {
_lastKnownFrameIndex = animationFrameIndex;
const QVector<glm::quat>& rotations = frames[animationFrameIndex].rotations;
frameData.resize(_jointMapping.size()); _lastKnownFrameData.resize(_jointMapping.size());
for (int j = 0; j < _jointMapping.size(); j++) { for (int j = 0; j < _jointMapping.size(); j++) {
int rotationIndex = _jointMapping[j]; int rotationIndex = _jointMapping[j];
if (rotationIndex != -1 && rotationIndex < rotations.size()) { if (rotationIndex != -1 && rotationIndex < rotations.size()) {
frameData[j] = rotations[rotationIndex]; _lastKnownFrameData[j] = rotations[rotationIndex];
}
} }
} }
} }
} }
return frameData; return _lastKnownFrameData;
} }
bool ModelEntityItem::isAnimatingSomething() const { bool ModelEntityItem::isAnimatingSomething() const {

View file

@ -106,7 +106,7 @@ public:
float getAnimationLastFrame() const { return _animationLoop.getLastFrame(); } float getAnimationLastFrame() const { return _animationLoop.getLastFrame(); }
void mapJoints(const QStringList& modelJointNames); void mapJoints(const QStringList& modelJointNames);
QVector<glm::quat> getAnimationFrame(); const QVector<glm::quat>& getAnimationFrame();
bool jointsMapped() const { return _jointMappingCompleted; } bool jointsMapped() const { return _jointMappingCompleted; }
bool getAnimationIsPlaying() const { return _animationLoop.isRunning(); } bool getAnimationIsPlaying() const { return _animationLoop.isRunning(); }
@ -123,6 +123,9 @@ public:
static void cleanupLoadedAnimations(); static void cleanupLoadedAnimations();
protected: protected:
QVector<glm::quat> _lastKnownFrameData;
int _lastKnownFrameIndex;
bool isAnimatingSomething() const; bool isAnimatingSomething() const;