From 7a153396318d226e293c70d3c26465c5a66b3baf Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Wed, 13 Apr 2016 15:38:03 -0700 Subject: [PATCH] Use AnimationCache for models --- interface/src/Application.cpp | 2 -- .../src/RenderableModelEntityItem.cpp | 8 ++--- .../src/RenderableModelEntityItem.h | 2 +- libraries/entities/src/ModelEntityItem.cpp | 31 ++++--------------- libraries/entities/src/ModelEntityItem.h | 9 ++---- 5 files changed, 13 insertions(+), 39 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ebfdaff0e3..118f5425bd 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1238,8 +1238,6 @@ Application::~Application() { _physicsEngine->setCharacterController(nullptr); - ModelEntityItem::cleanupLoadedAnimations(); - // remove avatars from physics engine DependencyManager::get()->clearOtherAvatars(); VectorOfMotionStates motionStates; diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 879ff01056..bbd1266513 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -274,11 +274,11 @@ bool RenderableModelEntityItem::getAnimationFrame() { if (!hasRenderAnimation() || !_jointMappingCompleted) { return false; } - AnimationPointer myAnimation = getAnimation(getRenderAnimationURL()); // FIXME: this could be optimized - if (myAnimation && myAnimation->isLoaded()) { - const QVector& frames = myAnimation->getFramesReference(); // NOTE: getFrames() is too heavy - auto& fbxJoints = myAnimation->getGeometry().joints; + if (_animation && _animation->isLoaded()) { + + const QVector& frames = _animation->getFramesReference(); // NOTE: getFrames() is too heavy + auto& fbxJoints = _animation->getGeometry().joints; int frameCount = frames.size(); if (frameCount > 0) { diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.h b/libraries/entities-renderer/src/RenderableModelEntityItem.h index bf55c829e9..59208d209d 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.h +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.h @@ -82,7 +82,7 @@ public: virtual int getJointIndex(const QString& name) const override; virtual QStringList getJointNames() const override; - // These operate on a copy of the renderAnimationProperties, so they can be accessed + // These operate on a copy of the animationProperties, so they can be accessed // without having the entityTree lock. bool hasRenderAnimation() const { return !_renderAnimationProperties.getURL().isEmpty(); } const QString& getRenderAnimationURL() const { return _renderAnimationProperties.getURL(); } diff --git a/libraries/entities/src/ModelEntityItem.cpp b/libraries/entities/src/ModelEntityItem.cpp index e5511c0b25..8e45f4dd5e 100644 --- a/libraries/entities/src/ModelEntityItem.cpp +++ b/libraries/entities/src/ModelEntityItem.cpp @@ -205,37 +205,18 @@ void ModelEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBit } -QMap ModelEntityItem::_loadedAnimations; // TODO: improve cleanup by leveraging the AnimationPointer(s) - -void ModelEntityItem::cleanupLoadedAnimations() { - foreach(AnimationPointer animation, _loadedAnimations) { - animation.clear(); - } - _loadedAnimations.clear(); -} - -AnimationPointer ModelEntityItem::getAnimation(const QString& url) { - AnimationPointer animation; - - // if we don't already have this model then create it and initialize it - if (_loadedAnimations.find(url) == _loadedAnimations.end()) { - animation = DependencyManager::get()->getAnimation(url); - _loadedAnimations[url] = animation; - } else { - animation = _loadedAnimations[url]; - } - return animation; -} - void ModelEntityItem::mapJoints(const QStringList& modelJointNames) { // if we don't have animation, or we're already joint mapped then bail early if (!hasAnimation() || jointsMapped()) { return; } - AnimationPointer myAnimation = getAnimation(_animationProperties.getURL()); - if (myAnimation && myAnimation->isLoaded()) { - QStringList animationJointNames = myAnimation->getJointNames(); + if (!_animation || _animation->getURL().toString() != getAnimationURL()) { + _animation = DependencyManager::get()->getAnimation(getAnimationURL()); + } + + if (_animation && _animation->isLoaded()) { + QStringList animationJointNames = _animation->getJointNames(); if (modelJointNames.size() > 0 && animationJointNames.size() > 0) { _jointMapping.resize(modelJointNames.size()); diff --git a/libraries/entities/src/ModelEntityItem.h b/libraries/entities/src/ModelEntityItem.h index d0e0909b27..82bb6ca47c 100644 --- a/libraries/entities/src/ModelEntityItem.h +++ b/libraries/entities/src/ModelEntityItem.h @@ -105,6 +105,7 @@ public: void mapJoints(const QStringList& modelJointNames); bool jointsMapped() const { return _jointMappingURL == getAnimationURL() && _jointMappingCompleted; } + AnimationPointer getAnimation() const { return _animation; } bool getAnimationIsPlaying() const { return _animationLoop.getRunning(); } float getAnimationCurrentFrame() const { return _animationLoop.getCurrentFrame(); } float getAnimationFPS() const { return _animationLoop.getFPS(); } @@ -115,8 +116,6 @@ public: virtual bool shouldBePhysical() const; - static void cleanupLoadedAnimations(); - virtual glm::vec3 getJointPosition(int jointIndex) const { return glm::vec3(); } virtual glm::quat getJointRotation(int jointIndex) const { return glm::quat(); } @@ -156,6 +155,7 @@ protected: QUrl _parsedModelURL; QString _compoundShapeURL; + AnimationPointer _animation; AnimationPropertyGroup _animationProperties; AnimationLoop _animationLoop; @@ -168,11 +168,6 @@ protected: bool _jointMappingCompleted; QVector _jointMapping; // domain is index into model-joints, range is index into animation-joints QString _jointMappingURL; - - static AnimationPointer getAnimation(const QString& url); - static QMap _loadedAnimations; - static AnimationCache _animationCache; - }; #endif // hifi_ModelEntityItem_h