separate Gltf/Morph/BoneAnimationComponentManager definitions
move gltf animation instantiation to GltfAnimationComponentManager (this helps ensure we are creating the component on the correct entity)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include <chrono>
|
||||
#include <variant>
|
||||
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "components/GltfAnimationComponentManager.hpp"
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
@@ -15,9 +15,75 @@ namespace thermion
|
||||
}
|
||||
}
|
||||
|
||||
bool GltfAnimationComponentManager::addGltfAnimation(FilamentInstance *target, int index, bool loop, bool reverse, bool replaceActive, float crossfade, float startOffset) {
|
||||
|
||||
EntityInstanceBase::Type componentInstance = getInstance(target->getRoot());
|
||||
|
||||
auto &animationComponent = this->elementAt<0>(componentInstance);
|
||||
|
||||
animationComponent.target = target;
|
||||
|
||||
if (replaceActive)
|
||||
{
|
||||
if (animationComponent.animations.size() > 0)
|
||||
{
|
||||
auto &last = animationComponent.animations.back();
|
||||
animationComponent.fadeGltfAnimationIndex = last.index;
|
||||
animationComponent.fadeDuration = crossfade;
|
||||
auto now = high_resolution_clock::now();
|
||||
auto elapsedInSecs = float(std::chrono::duration_cast<std::chrono::milliseconds>(now - last.start).count()) / 1000.0f;
|
||||
animationComponent.fadeOutAnimationStart = elapsedInSecs;
|
||||
animationComponent.animations.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
animationComponent.fadeGltfAnimationIndex = -1;
|
||||
animationComponent.fadeDuration = 0.0f;
|
||||
}
|
||||
}
|
||||
else if (crossfade > 0)
|
||||
{
|
||||
Log("ERROR: crossfade only supported when replaceActive is true.");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
animationComponent.fadeGltfAnimationIndex = -1;
|
||||
animationComponent.fadeDuration = 0.0f;
|
||||
}
|
||||
|
||||
GltfAnimation animation;
|
||||
animation.startOffset = startOffset;
|
||||
animation.index = index;
|
||||
animation.start = std::chrono::high_resolution_clock::now();
|
||||
animation.loop = loop;
|
||||
animation.reverse = reverse;
|
||||
animation.durationInSecs = target->getAnimator()->getAnimationDuration(index);
|
||||
|
||||
bool found = false;
|
||||
|
||||
// don't play the animation if it's already running
|
||||
for (int i = 0; i < animationComponent.animations.size(); i++)
|
||||
{
|
||||
if (animationComponent.animations[i].index == index)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
animationComponent.animations.push_back(animation);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void GltfAnimationComponentManager::removeAnimationComponent(FilamentInstance *target) {
|
||||
if(hasComponent(target->getRoot())) {
|
||||
removeComponent(target->getRoot());
|
||||
TRACE("Found component, component removed");
|
||||
} else {
|
||||
TRACE("Component not found, skipping removal");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user