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:
@@ -129,7 +129,7 @@ extern "C"
|
||||
const float *const morphData,
|
||||
int numWeights);
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void AnimationManager_setGltfAnimationFrame(
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_setGltfAnimationFrame(
|
||||
TAnimationManager *tAnimationManager,
|
||||
TSceneAsset *tSceneAsset,
|
||||
int animationIndex,
|
||||
|
||||
@@ -301,7 +301,7 @@ namespace thermion
|
||||
void AnimationManager_resetToRestPoseRenderThread(TAnimationManager *tAnimationManager, EntityId entityId, uint32_t requestId, VoidCallback onComplete);
|
||||
|
||||
void GltfAssetLoader_createRenderThread(TEngine *tEngine, TMaterialProvider *tMaterialProvider, void (*callback)(TGltfAssetLoader *));
|
||||
void GltfResourceLoader_createRenderThread(TEngine *tEngine, const char* relativeResourcePath, void (*callback)(TGltfResourceLoader *));
|
||||
void GltfResourceLoader_createRenderThread(TEngine *tEngine, void (*callback)(TGltfResourceLoader *));
|
||||
void GltfResourceLoader_destroyRenderThread(TEngine *tEngine, TGltfResourceLoader *tResourceLoader, uint32_t requestId, VoidCallback onComplete);
|
||||
void GltfResourceLoader_loadResourcesRenderThread(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset, void (*callback)(bool));
|
||||
void GltfResourceLoader_addResourceDataRenderThread(TGltfResourceLoader *tGltfResourceLoader, const char *uri, uint8_t *data, size_t length, uint32_t requestId, VoidCallback onComplete);
|
||||
|
||||
19
thermion_dart/native/include/components/Animation.hpp
Normal file
19
thermion_dart/native/include/components/Animation.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace thermion
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
|
||||
|
||||
struct Animation
|
||||
{
|
||||
time_point_t start = time_point_t::max();
|
||||
float startOffset;
|
||||
bool loop = false;
|
||||
bool reverse = false;
|
||||
float durationInSecs = 0;
|
||||
};
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <variant>
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/Renderer.h>
|
||||
#include <filament/Scene.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TransformManager.h>
|
||||
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/math.h>
|
||||
|
||||
#include <utils/SingleInstanceComponentManager.h>
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
template class std::vector<float>;
|
||||
namespace thermion
|
||||
{
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
using namespace utils;
|
||||
using namespace std::chrono;
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
|
||||
|
||||
struct Animation
|
||||
{
|
||||
time_point_t start = time_point_t::max();
|
||||
float startOffset;
|
||||
bool loop = false;
|
||||
bool reverse = false;
|
||||
float durationInSecs = 0;
|
||||
};
|
||||
|
||||
/// @brief
|
||||
/// The status of an animation embedded in a glTF object.
|
||||
/// @param index refers to the index of the animation in the animations property of the underlying object.
|
||||
///
|
||||
struct GltfAnimation : Animation
|
||||
{
|
||||
int index = -1;
|
||||
};
|
||||
|
||||
//
|
||||
// The status of a morph target animation created dynamically at runtime (not glTF embedded).
|
||||
//
|
||||
struct MorphAnimation : Animation
|
||||
{
|
||||
int lengthInFrames;
|
||||
float frameLengthInMs = 0;
|
||||
std::vector<float> frameData;
|
||||
std::vector<int> morphIndices;
|
||||
};
|
||||
|
||||
struct BoneAnimation : Animation {
|
||||
int lengthInFrames;
|
||||
size_t boneIndex;
|
||||
size_t skinIndex = 0;
|
||||
float frameLengthInMs = 0;
|
||||
std::vector<math::mat4f> frameData;
|
||||
float fadeOutInSecs = 0;
|
||||
float fadeInInSecs = 0;
|
||||
float maxDelta = 1.0f;
|
||||
};
|
||||
|
||||
/// @brief
|
||||
///
|
||||
///
|
||||
struct BoneAnimationComponent
|
||||
{
|
||||
FilamentInstance * target;
|
||||
std::vector<BoneAnimation> animations;
|
||||
};
|
||||
|
||||
/// @brief
|
||||
///
|
||||
///
|
||||
struct MorphAnimationComponent
|
||||
{
|
||||
std::vector<MorphAnimation> animations;
|
||||
};
|
||||
|
||||
/// @brief
|
||||
///
|
||||
///
|
||||
struct GltfAnimationComponent
|
||||
{
|
||||
FilamentInstance * target;
|
||||
// the index of the last active glTF animation,
|
||||
// used to cross-fade
|
||||
int fadeGltfAnimationIndex = -1;
|
||||
float fadeDuration = 0.0f;
|
||||
float fadeOutAnimationStart = 0.0f;
|
||||
std::vector<GltfAnimation> animations;
|
||||
};
|
||||
|
||||
|
||||
class GltfAnimationComponentManager : public utils::SingleInstanceComponentManager<GltfAnimationComponent> {
|
||||
public:
|
||||
GltfAnimationComponentManager(
|
||||
filament::TransformManager &transformManager,
|
||||
filament::RenderableManager &renderableManager) :
|
||||
mTransformManager(transformManager), mRenderableManager(renderableManager) {};
|
||||
~GltfAnimationComponentManager() = default;
|
||||
void addAnimationComponent(FilamentInstance *target);
|
||||
void removeAnimationComponent(FilamentInstance *target);
|
||||
void update();
|
||||
|
||||
private:
|
||||
filament::TransformManager &mTransformManager;
|
||||
filament::RenderableManager &mRenderableManager;
|
||||
};
|
||||
|
||||
class BoneAnimationComponentManager : public utils::SingleInstanceComponentManager<BoneAnimationComponent> {
|
||||
public:
|
||||
BoneAnimationComponentManager(
|
||||
filament::TransformManager &transformManager,
|
||||
filament::RenderableManager &renderableManager) :
|
||||
mTransformManager(transformManager), mRenderableManager(renderableManager) {};
|
||||
~BoneAnimationComponentManager() {};
|
||||
|
||||
void addAnimationComponent(FilamentInstance *target);
|
||||
void removeAnimationComponent(FilamentInstance *target);
|
||||
void update();
|
||||
|
||||
private:
|
||||
filament::TransformManager &mTransformManager;
|
||||
filament::RenderableManager &mRenderableManager;
|
||||
};
|
||||
|
||||
class MorphAnimationComponentManager : public utils::SingleInstanceComponentManager<MorphAnimationComponent> {
|
||||
public:
|
||||
MorphAnimationComponentManager(
|
||||
filament::TransformManager &transformManager,
|
||||
filament::RenderableManager &renderableManager) :
|
||||
mTransformManager(transformManager), mRenderableManager(renderableManager) {};
|
||||
~MorphAnimationComponentManager() {};
|
||||
|
||||
void addAnimationComponent(Entity entity);
|
||||
void removeAnimationComponent(Entity entity);
|
||||
void update();
|
||||
|
||||
private:
|
||||
filament::TransformManager &mTransformManager;
|
||||
filament::RenderableManager &mRenderableManager;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
#include <gltfio/FilamentInstance.h>
|
||||
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
namespace thermion
|
||||
{
|
||||
using namespace filament;
|
||||
using namespace std::chrono;
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
|
||||
|
||||
struct Animation
|
||||
{
|
||||
time_point_t start = time_point_t::max();
|
||||
float startOffset;
|
||||
bool loop = false;
|
||||
bool reverse = false;
|
||||
float durationInSecs = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/Renderer.h>
|
||||
#include <filament/Scene.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TransformManager.h>
|
||||
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/math.h>
|
||||
|
||||
#include <utils/SingleInstanceComponentManager.h>
|
||||
|
||||
#include "Log.hpp"
|
||||
#include "components/Animation.hpp"
|
||||
|
||||
namespace thermion
|
||||
{
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
using namespace utils;
|
||||
|
||||
struct BoneAnimation : Animation {
|
||||
int lengthInFrames;
|
||||
size_t boneIndex;
|
||||
size_t skinIndex = 0;
|
||||
float frameLengthInMs = 0;
|
||||
std::vector<math::mat4f> frameData;
|
||||
float fadeOutInSecs = 0;
|
||||
float fadeInInSecs = 0;
|
||||
float maxDelta = 1.0f;
|
||||
};
|
||||
|
||||
/// @brief
|
||||
///
|
||||
///
|
||||
struct BoneAnimationComponent
|
||||
{
|
||||
filament::gltfio::FilamentInstance * target;
|
||||
std::vector<BoneAnimation> animations;
|
||||
};
|
||||
|
||||
class BoneAnimationComponentManager : public utils::SingleInstanceComponentManager<BoneAnimationComponent> {
|
||||
public:
|
||||
BoneAnimationComponentManager(
|
||||
filament::TransformManager &transformManager,
|
||||
filament::RenderableManager &renderableManager) :
|
||||
mTransformManager(transformManager), mRenderableManager(renderableManager) {};
|
||||
~BoneAnimationComponentManager() {};
|
||||
|
||||
void addAnimationComponent(FilamentInstance *target);
|
||||
void removeAnimationComponent(FilamentInstance *target);
|
||||
void update();
|
||||
|
||||
private:
|
||||
filament::TransformManager &mTransformManager;
|
||||
filament::RenderableManager &mRenderableManager;
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/Renderer.h>
|
||||
#include <filament/Scene.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TransformManager.h>
|
||||
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/math.h>
|
||||
|
||||
#include <utils/SingleInstanceComponentManager.h>
|
||||
|
||||
#include "Log.hpp"
|
||||
#include "scene/GltfSceneAssetInstance.hpp"
|
||||
#include "components/Animation.hpp"
|
||||
|
||||
template class std::vector<float>;
|
||||
namespace thermion
|
||||
{
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
using namespace utils;
|
||||
using namespace std::chrono;
|
||||
|
||||
/// @brief
|
||||
/// The status of an animation embedded in a glTF object.
|
||||
/// @param index refers to the index of the animation in the animations property of the underlying object.
|
||||
///
|
||||
struct GltfAnimation : Animation
|
||||
{
|
||||
int index = -1;
|
||||
};
|
||||
|
||||
|
||||
/// @brief
|
||||
///
|
||||
///
|
||||
struct GltfAnimationComponent
|
||||
{
|
||||
filament::gltfio::FilamentInstance * target;
|
||||
// the index of the last active glTF animation,
|
||||
// used to cross-fade
|
||||
int fadeGltfAnimationIndex = -1;
|
||||
float fadeDuration = 0.0f;
|
||||
float fadeOutAnimationStart = 0.0f;
|
||||
std::vector<GltfAnimation> animations;
|
||||
};
|
||||
|
||||
class GltfAnimationComponentManager : public utils::SingleInstanceComponentManager<GltfAnimationComponent> {
|
||||
public:
|
||||
GltfAnimationComponentManager(
|
||||
filament::TransformManager &transformManager,
|
||||
filament::RenderableManager &renderableManager) :
|
||||
mTransformManager(transformManager), mRenderableManager(renderableManager) {};
|
||||
~GltfAnimationComponentManager() = default;
|
||||
void addAnimationComponent(FilamentInstance *target);
|
||||
void removeAnimationComponent(FilamentInstance *target);
|
||||
|
||||
bool addGltfAnimation(FilamentInstance *target, int index, bool loop, bool reverse, bool replaceActive, float crossfade, float startOffset);
|
||||
// GltfAnimationComponent getAnimationComponentInstance(FilamentInstance *target);
|
||||
void update();
|
||||
|
||||
private:
|
||||
filament::TransformManager &mTransformManager;
|
||||
filament::RenderableManager &mRenderableManager;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/Renderer.h>
|
||||
#include <filament/Scene.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TransformManager.h>
|
||||
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/math.h>
|
||||
|
||||
#include <utils/SingleInstanceComponentManager.h>
|
||||
|
||||
#include "Log.hpp"
|
||||
#include "components/Animation.hpp"
|
||||
|
||||
namespace thermion
|
||||
{
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
using namespace utils;
|
||||
using namespace std::chrono;
|
||||
|
||||
//
|
||||
// The status of a morph target animation created dynamically at runtime (not glTF embedded).
|
||||
//
|
||||
struct MorphAnimation : Animation
|
||||
{
|
||||
int lengthInFrames;
|
||||
float frameLengthInMs = 0;
|
||||
std::vector<float> frameData;
|
||||
std::vector<int> morphIndices;
|
||||
};
|
||||
|
||||
|
||||
/// @brief
|
||||
///
|
||||
///
|
||||
struct MorphAnimationComponent
|
||||
{
|
||||
std::vector<MorphAnimation> animations;
|
||||
};
|
||||
|
||||
class MorphAnimationComponentManager : public utils::SingleInstanceComponentManager<MorphAnimationComponent> {
|
||||
public:
|
||||
MorphAnimationComponentManager(
|
||||
filament::TransformManager &transformManager,
|
||||
filament::RenderableManager &renderableManager) :
|
||||
mTransformManager(transformManager), mRenderableManager(renderableManager) {};
|
||||
~MorphAnimationComponentManager() {};
|
||||
|
||||
void addAnimationComponent(Entity entity);
|
||||
void removeAnimationComponent(Entity entity);
|
||||
void update();
|
||||
|
||||
private:
|
||||
filament::TransformManager &mTransformManager;
|
||||
filament::RenderableManager &mRenderableManager;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -9,10 +9,12 @@
|
||||
#include "c_api/APIBoundaryTypes.h"
|
||||
|
||||
#include "components/CollisionComponentManager.hpp"
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "GltfSceneAssetInstance.hpp"
|
||||
#include "GltfSceneAsset.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include "components/GltfAnimationComponentManager.hpp"
|
||||
#include "components/MorphAnimationComponentManager.hpp"
|
||||
#include "components/BoneAnimationComponentManager.hpp"
|
||||
#include "scene/GltfSceneAssetInstance.hpp"
|
||||
#include "scene/GltfSceneAsset.hpp"
|
||||
#include "scene/SceneAsset.hpp"
|
||||
|
||||
namespace thermion
|
||||
{
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <utils/NameComponentManager.h>
|
||||
|
||||
#include "scene/GltfSceneAssetInstance.hpp"
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "components/CollisionComponentManager.hpp"
|
||||
|
||||
#include "scene/SceneAsset.hpp"
|
||||
|
||||
@@ -31,49 +31,93 @@ extern "C"
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_addGltfAnimationComponent(TAnimationManager *tAnimationManager, TSceneAsset *tSceneAsset)
|
||||
{
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf || !sceneAsset->isInstance()) {
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GltfSceneAssetInstance *instance;
|
||||
|
||||
if (sceneAsset->isInstance())
|
||||
{
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset);
|
||||
} else {
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset->getInstanceAt(0));
|
||||
}
|
||||
|
||||
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
|
||||
animationManager->addGltfAnimationComponent(reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset));
|
||||
animationManager->addGltfAnimationComponent(instance);
|
||||
return true;
|
||||
}
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_removeGltfAnimationComponent(TAnimationManager *tAnimationManager, TSceneAsset *tSceneAsset)
|
||||
{
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf || !sceneAsset->isInstance()) {
|
||||
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GltfSceneAssetInstance *instance;
|
||||
|
||||
if (sceneAsset->isInstance())
|
||||
{
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset);
|
||||
} else {
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset->getInstanceAt(0));
|
||||
}
|
||||
|
||||
|
||||
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
animationManager->removeGltfAnimationComponent(reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset));
|
||||
animationManager->removeGltfAnimationComponent(instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_addBoneAnimationComponent(TAnimationManager *tAnimationManager, TSceneAsset *tSceneAsset)
|
||||
{
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf || !sceneAsset->isInstance()) {
|
||||
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GltfSceneAssetInstance *instance;
|
||||
|
||||
if (sceneAsset->isInstance())
|
||||
{
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset);
|
||||
} else {
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset->getInstanceAt(0));
|
||||
}
|
||||
|
||||
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
|
||||
animationManager->addBoneAnimationComponent(reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset));
|
||||
animationManager->addBoneAnimationComponent(instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_removeBoneAnimationComponent(TAnimationManager *tAnimationManager, TSceneAsset *tSceneAsset)
|
||||
{
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf || !sceneAsset->isInstance()) {
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GltfSceneAssetInstance *instance;
|
||||
|
||||
if (sceneAsset->isInstance())
|
||||
{
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset);
|
||||
} else {
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset->getInstanceAt(0));
|
||||
}
|
||||
|
||||
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
animationManager->removeBoneAnimationComponent(reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset));
|
||||
animationManager->removeBoneAnimationComponent(instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -124,15 +168,27 @@ extern "C"
|
||||
return true;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void AnimationManager_resetToRestPose(TAnimationManager *tAnimationManager, TSceneAsset *sceneAsset)
|
||||
EMSCRIPTEN_KEEPALIVE void AnimationManager_resetToRestPose(TAnimationManager *tAnimationManager, TSceneAsset *tSceneAsset)
|
||||
{
|
||||
auto *animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
|
||||
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
|
||||
{
|
||||
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
|
||||
animationManager->resetToRestPose(instance);
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if (sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
Log("Error - incorrect asset type, cannot reset to reset pose");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
GltfSceneAssetInstance *instance;
|
||||
|
||||
if (sceneAsset->isInstance())
|
||||
{
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset);
|
||||
} else {
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset->getInstanceAt(0));
|
||||
}
|
||||
|
||||
animationManager->resetToRestPose(instance);
|
||||
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_addBoneAnimation(
|
||||
@@ -297,19 +353,31 @@ extern "C"
|
||||
return true;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void AnimationManager_setGltfAnimationFrame(
|
||||
EMSCRIPTEN_KEEPALIVE bool AnimationManager_setGltfAnimationFrame(
|
||||
TAnimationManager *tAnimationManager,
|
||||
TSceneAsset *tSceneAsset,
|
||||
int animationIndex,
|
||||
int frame)
|
||||
{
|
||||
auto *animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
auto asset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
|
||||
{
|
||||
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
|
||||
animationManager->setGltfAnimationFrame(instance, animationIndex, frame);
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
if (sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GltfSceneAssetInstance *instance;
|
||||
|
||||
if (sceneAsset->isInstance())
|
||||
{
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset);
|
||||
} else {
|
||||
instance = reinterpret_cast<GltfSceneAssetInstance *>(sceneAsset->getInstanceAt(0));
|
||||
}
|
||||
|
||||
animationManager->setGltfAnimationFrame(instance, animationIndex, frame);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE float AnimationManager_getGltfAnimationDuration(
|
||||
@@ -320,7 +388,7 @@ extern "C"
|
||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||
|
||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
||||
return false;
|
||||
return -1.0;
|
||||
}
|
||||
|
||||
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <chrono>
|
||||
#include <variant>
|
||||
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "components/BoneAnimationComponentManager.hpp"
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <chrono>
|
||||
#include <variant>
|
||||
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "components/MorphAnimationComponentManager.hpp"
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "scene/AnimationManager.hpp"
|
||||
#include "scene/SceneAsset.hpp"
|
||||
#include "scene/GltfSceneAssetInstance.hpp"
|
||||
@@ -334,71 +332,7 @@ namespace thermion
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_gltfAnimationComponentManager->hasComponent(instance->getEntity()))
|
||||
{
|
||||
_gltfAnimationComponentManager->addComponent(instance->getEntity());
|
||||
Log("ERROR: specified entity is not animatable (has no animation component attached).");
|
||||
return;
|
||||
}
|
||||
|
||||
auto animationComponentInstance = _gltfAnimationComponentManager->getInstance(instance->getEntity());
|
||||
|
||||
auto &animationComponent = _gltfAnimationComponentManager->elementAt<0>(animationComponentInstance);
|
||||
|
||||
animationComponent.target = instance->getInstance();
|
||||
|
||||
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;
|
||||
}
|
||||
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 = instance->getInstance()->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);
|
||||
}
|
||||
_gltfAnimationComponentManager->addGltfAnimation(instance->getInstance(), index, loop, reverse, replaceActive, crossfade, startOffset);
|
||||
}
|
||||
|
||||
void AnimationManager::stopGltfAnimation(GltfSceneAssetInstance *instance, int index)
|
||||
@@ -515,35 +449,47 @@ namespace thermion
|
||||
|
||||
bool AnimationManager::addGltfAnimationComponent(GltfSceneAssetInstance *instance)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_gltfAnimationComponentManager->addAnimationComponent(instance->getInstance());
|
||||
TRACE("Added glTF animation component");
|
||||
return true;
|
||||
}
|
||||
|
||||
void AnimationManager::removeGltfAnimationComponent(GltfSceneAssetInstance *instance)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_gltfAnimationComponentManager->removeAnimationComponent(instance->getInstance());
|
||||
TRACE("Removed glTF animation component");
|
||||
}
|
||||
|
||||
bool AnimationManager::addBoneAnimationComponent(GltfSceneAssetInstance *instance)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_boneAnimationComponentManager->addAnimationComponent(instance->getInstance());
|
||||
TRACE("Added bone animation component");
|
||||
return true;
|
||||
}
|
||||
|
||||
void AnimationManager::removeBoneAnimationComponent(GltfSceneAssetInstance *instance)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_boneAnimationComponentManager->removeAnimationComponent(instance->getInstance());
|
||||
TRACE("Removed bone animation component");
|
||||
}
|
||||
|
||||
bool AnimationManager::addMorphAnimationComponent(utils::Entity entity)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_morphAnimationComponentManager->addAnimationComponent(entity);
|
||||
TRACE("Added morph animation component");
|
||||
return true;
|
||||
}
|
||||
|
||||
void AnimationManager::removeMorphAnimationComponent(utils::Entity entity)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_morphAnimationComponentManager->removeAnimationComponent(entity);
|
||||
TRACE("Removed morph animation component");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,19 +12,18 @@
|
||||
#include <filament/VertexBuffer.h>
|
||||
#include <filament/IndexBuffer.h>
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/FilamentInstance.h>
|
||||
#include <gltfio/MaterialProvider.h>
|
||||
|
||||
#include <utils/NameComponentManager.h>
|
||||
|
||||
#include "scene/GltfSceneAssetInstance.hpp"
|
||||
#include "components/AnimationComponentManager.hpp"
|
||||
#include "components/CollisionComponentManager.hpp"
|
||||
|
||||
#include "scene/SceneAsset.hpp"
|
||||
|
||||
|
||||
|
||||
namespace thermion
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user