From cec0f8ed1da5e2884da5494510b03a7d02b2d41a Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 7 Aug 2015 13:43:57 -0700 Subject: [PATCH] optimize ModelEntityItem::getAnimationFrame() --- libraries/animation/src/AnimationCache.cpp | 4 +++ libraries/animation/src/AnimationCache.h | 2 ++ .../src/RenderableModelEntityItem.cpp | 2 +- libraries/entities/src/ModelEntityItem.cpp | 31 ++++++++++++------- libraries/entities/src/ModelEntityItem.h | 5 ++- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/libraries/animation/src/AnimationCache.cpp b/libraries/animation/src/AnimationCache.cpp index d1c4408178..634e0589b7 100644 --- a/libraries/animation/src/AnimationCache.cpp +++ b/libraries/animation/src/AnimationCache.cpp @@ -91,6 +91,10 @@ QVector Animation::getFrames() const { return _geometry.animationFrames; } +const QVector& Animation::getFramesReference() const { + return _geometry.animationFrames; +} + void Animation::setGeometry(const FBXGeometry& geometry) { _geometry = geometry; finishedLoading(true); diff --git a/libraries/animation/src/AnimationCache.h b/libraries/animation/src/AnimationCache.h index 840d7a0355..6a0a77f659 100644 --- a/libraries/animation/src/AnimationCache.h +++ b/libraries/animation/src/AnimationCache.h @@ -57,6 +57,8 @@ public: Q_INVOKABLE QStringList getJointNames() const; Q_INVOKABLE QVector getFrames() const; + + const QVector& getFramesReference() const; protected: diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index e6441a5386..6d2ff30d4b 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -275,7 +275,7 @@ void RenderableModelEntityItem::render(RenderArgs* args) { } if (jointsMapped()) { - QVector frameData = getAnimationFrame(); + auto frameData = getAnimationFrame(); for (int i = 0; i < frameData.size(); i++) { _model->setJointState(i, true, frameData[i]); } diff --git a/libraries/entities/src/ModelEntityItem.cpp b/libraries/entities/src/ModelEntityItem.cpp index b0e7dcd022..362f5dc72a 100644 --- a/libraries/entities/src/ModelEntityItem.cpp +++ b/libraries/entities/src/ModelEntityItem.cpp @@ -39,6 +39,7 @@ ModelEntityItem::ModelEntityItem(const EntityItemID& entityItemID, const EntityI setProperties(properties); _lastAnimated = usecTimestampNow(); _jointMappingCompleted = false; + _lastKnownFrameIndex = -1; _color[0] = _color[1] = _color[2] = 0; } @@ -217,15 +218,17 @@ void ModelEntityItem::mapJoints(const QStringList& modelJointNames) { } } -QVector ModelEntityItem::getAnimationFrame() { - QVector frameData; +const QVector& ModelEntityItem::getAnimationFrame() { + if (!hasAnimation() || !_jointMappingCompleted) { - return frameData; + return _lastKnownFrameData; } - AnimationPointer myAnimation = getAnimation(_animationURL); + AnimationPointer myAnimation = getAnimation(_animationURL); // FIXME: this could be optimized if (myAnimation && myAnimation->isLoaded()) { - QVector frames = myAnimation->getFrames(); + + const QVector& frames = myAnimation->getFramesReference(); // NOTE: getFrames() is too heavy + int frameCount = frames.size(); if (frameCount > 0) { int animationFrameIndex = (int)(glm::floor(getAnimationFrameIndex())) % frameCount; @@ -233,18 +236,22 @@ QVector ModelEntityItem::getAnimationFrame() { animationFrameIndex = 0; } - QVector rotations = frames[animationFrameIndex].rotations; + if (animationFrameIndex != _lastKnownFrameIndex) { + _lastKnownFrameIndex = animationFrameIndex; + + const QVector& rotations = frames[animationFrameIndex].rotations; - frameData.resize(_jointMapping.size()); - for (int j = 0; j < _jointMapping.size(); j++) { - int rotationIndex = _jointMapping[j]; - if (rotationIndex != -1 && rotationIndex < rotations.size()) { - frameData[j] = rotations[rotationIndex]; + _lastKnownFrameData.resize(_jointMapping.size()); + for (int j = 0; j < _jointMapping.size(); j++) { + int rotationIndex = _jointMapping[j]; + if (rotationIndex != -1 && rotationIndex < rotations.size()) { + _lastKnownFrameData[j] = rotations[rotationIndex]; + } } } } } - return frameData; + return _lastKnownFrameData; } bool ModelEntityItem::isAnimatingSomething() const { diff --git a/libraries/entities/src/ModelEntityItem.h b/libraries/entities/src/ModelEntityItem.h index ae6a86cde1..e3d42e6b2c 100644 --- a/libraries/entities/src/ModelEntityItem.h +++ b/libraries/entities/src/ModelEntityItem.h @@ -106,7 +106,7 @@ public: float getAnimationLastFrame() const { return _animationLoop.getLastFrame(); } void mapJoints(const QStringList& modelJointNames); - QVector getAnimationFrame(); + const QVector& getAnimationFrame(); bool jointsMapped() const { return _jointMappingCompleted; } bool getAnimationIsPlaying() const { return _animationLoop.isRunning(); } @@ -123,6 +123,9 @@ public: static void cleanupLoadedAnimations(); protected: + QVector _lastKnownFrameData; + int _lastKnownFrameIndex; + bool isAnimatingSomething() const;