merge
This commit is contained in:
@@ -40,23 +40,25 @@ namespace polyvox {
|
||||
size_t getLightEntityCount(EntityId e) const noexcept;
|
||||
void updateAnimations();
|
||||
|
||||
bool setBoneAnimationBuffer(
|
||||
EntityId entity,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
|
||||
bool setMorphAnimationBuffer(
|
||||
EntityId entity,
|
||||
const char* entityName,
|
||||
const float* const morphData,
|
||||
EntityId entityId,
|
||||
const char* entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
void setMorphTargetWeights(EntityId entityId, const char* const entityName, const float* const weights, int count);
|
||||
|
||||
bool setBoneAnimationBuffer(
|
||||
EntityId entity,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
int numBones,
|
||||
const char** const boneNames,
|
||||
const char** const meshName,
|
||||
int numMeshTargets,
|
||||
float frameLengthInMs);
|
||||
void playAnimation(EntityId e, int index, bool loop, bool reverse);
|
||||
void stopAnimation(EntityId e, int index);
|
||||
void setMorphTargetWeights(const char* const entityName, float *weights, int count);
|
||||
@@ -76,18 +78,15 @@ namespace polyvox {
|
||||
vector<SceneAsset> _assets;
|
||||
tsl::robin_map<EntityId, int> _entityIdLookup;
|
||||
|
||||
void setBoneTransform(
|
||||
FilamentInstance* instance,
|
||||
vector<BoneAnimationData> animations,
|
||||
int frameNumber
|
||||
);
|
||||
|
||||
utils::Entity findEntityByName(
|
||||
SceneAsset asset,
|
||||
const char* entityName
|
||||
);
|
||||
|
||||
inline void updateTransform(SceneAsset asset);
|
||||
inline void updateTransform(SceneAsset& asset);
|
||||
|
||||
inline void setBoneTransform(SceneAsset& asset, int frameNumber);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
@@ -65,29 +65,17 @@ bool set_morph_animation(
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
|
||||
void set_bone_animation(
|
||||
void set_bone_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
int numBones,
|
||||
const char** const boneNames,
|
||||
const char** const meshName,
|
||||
int numMeshTargets,
|
||||
float frameLengthInMs);
|
||||
|
||||
// void set_bone_transform(
|
||||
// EntityId asset,
|
||||
// const char* boneName,
|
||||
// const char* entityName,
|
||||
// float transX,
|
||||
// float transY,
|
||||
// float transZ,
|
||||
// float quatX,
|
||||
// float quatY,
|
||||
// float quatZ,
|
||||
// float quatW
|
||||
// );
|
||||
|
||||
void play_animation(void* assetManager, EntityId asset, int index, bool loop, bool reverse);
|
||||
void set_animation_frame(void* assetManager, EntityId asset, int animationIndex, int animationFrame);
|
||||
void stop_animation(void* assetManager, EntityId asset, int index);
|
||||
|
||||
@@ -27,17 +27,11 @@ extern "C" {
|
||||
//
|
||||
struct ResourceBuffer {
|
||||
#if defined(__cplusplus)
|
||||
ResourceBuffer(const void* data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
|
||||
ResourceBuffer& operator=(ResourceBuffer other) {
|
||||
data = other.data;
|
||||
size = other.size;
|
||||
id = other.id;
|
||||
return *this;
|
||||
}
|
||||
ResourceBuffer(const void* const data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
|
||||
#endif
|
||||
const void* data;
|
||||
uint32_t size;
|
||||
uint32_t id;
|
||||
const void * const data;
|
||||
const uint32_t size;
|
||||
const uint32_t id;
|
||||
};
|
||||
|
||||
typedef struct ResourceBuffer ResourceBuffer;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <filament/Renderer.h>
|
||||
#include <filament/Scene.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TransformManager.h>
|
||||
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
@@ -21,7 +22,7 @@
|
||||
extern "C" {
|
||||
#include "PolyvoxFilamentApi.h"
|
||||
}
|
||||
|
||||
template class std::vector<float>;
|
||||
namespace polyvox {
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
@@ -36,35 +37,6 @@ namespace polyvox {
|
||||
bool mReverse = false;
|
||||
float mDuration = 0;
|
||||
bool mAnimating = false;
|
||||
|
||||
// AnimationStatus() {
|
||||
// Log("default constr");
|
||||
// }
|
||||
|
||||
// AnimationStatus(AnimationStatus& a) {
|
||||
// mStart = a.mStart;
|
||||
// mLoop = a.mLoop;
|
||||
// mReverse = a.mReverse;
|
||||
// mDuration = a.mDuration;
|
||||
// mFrameNumber = a.mFrameNumber;
|
||||
// }
|
||||
|
||||
// AnimationStatus& operator=(AnimationStatus a) {
|
||||
// mStart = a.mStart;
|
||||
// mLoop = a.mLoop;
|
||||
// mReverse = a.mReverse;
|
||||
// mDuration = a.mDuration;
|
||||
// mFrameNumber = a.mFrameNumber;
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
// AnimationStatus(AnimationStatus&& a) {
|
||||
// mStart = a.mStart;
|
||||
// mLoop = a.mLoop;
|
||||
// mReverse = a.mReverse;
|
||||
// mDuration = a.mDuration;
|
||||
// mFrameNumber = a.mFrameNumber;
|
||||
// }
|
||||
};
|
||||
|
||||
//
|
||||
@@ -78,24 +50,22 @@ namespace polyvox {
|
||||
int mNumMorphWeights = 0;
|
||||
};
|
||||
|
||||
///
|
||||
/// Frame data for the bones/meshes specified by [mBoneIndices] and [mMeshTargets].
|
||||
/// This is mainly used as a wrapper for animation data being transferred from the Dart to the native side.
|
||||
///
|
||||
struct BoneAnimationData {
|
||||
size_t skinIndex = 0;
|
||||
uint8_t mBoneIndex;
|
||||
utils::Entity mMeshTarget;
|
||||
vector<float> mFrameData;
|
||||
};
|
||||
|
||||
//
|
||||
// Use this to manually construct a buffer of frame data for bone animations.
|
||||
// Use this to construct a dynamic (i.e. non-glTF embedded) bone animation.
|
||||
// Only a single animation is supported at any time (i.e you can't blend animations).
|
||||
// Multiple bones are supported but these must be skinned to a single mesh target.
|
||||
//
|
||||
struct BoneAnimationBuffer {
|
||||
vector<utils::Entity> mMeshTargets;
|
||||
vector<uint8_t> mBones;
|
||||
vector<math::mat4f> mBaseTransforms;
|
||||
// vector<math::float3> mBaseTranslations; // these are the base transforms for the bones we will animate; the translations/rotations in mFrameData will be relative to this.
|
||||
// vector<math::quatf> mBaseRotations; // these are the base transforms for the bones we will animate; the translations/rotations in mFrameData will be relative to this.
|
||||
// vector<math::float3> mBaseScales; // these are the base transforms for the bones we will animate; the translations/rotations in mFrameData will be relative to this.
|
||||
size_t skinIndex = 0;
|
||||
int mNumFrames = -1;
|
||||
float mFrameLengthInMs = 0;
|
||||
vector<BoneAnimationData> mAnimations;
|
||||
vector<float> mFrameData;
|
||||
};
|
||||
|
||||
struct SceneAsset {
|
||||
@@ -129,12 +99,9 @@ namespace polyvox {
|
||||
|
||||
mAnimations.resize(2 + mAnimator->getAnimationCount());
|
||||
|
||||
for(int i=2; i < mAnimations.size(); i++) {
|
||||
mAnimations[i].mDuration = mAnimator->getAnimationDuration(i-2);
|
||||
for(int i=0; i < mAnimations.size() - 2; i++) {
|
||||
mAnimations[i].mDuration = mAnimator->getAnimationDuration(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ namespace polyvox {
|
||||
|
||||
|
||||
public:
|
||||
FileMaterialProvider(Engine* engine, void* const data, size_t size) {
|
||||
FileMaterialProvider(Engine* engine, const void* const data, const size_t size) {
|
||||
_m = Material::Builder()
|
||||
.package(data, size)
|
||||
.build(*engine);
|
||||
|
||||
@@ -24,7 +24,6 @@ A new flutter plugin project.
|
||||
'DEFINES_MODULE' => 'YES',
|
||||
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386',
|
||||
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
|
||||
'OTHER_CXXFLAGS' => '"--std=c++17" "-fmodules" "-fcxx-modules" "-stdlib=libc++" "-fvisibility=default" "$(inherited)"',
|
||||
'OTHER_CFLAGS' => '"-fvisibility=default" "$(inherited)"',
|
||||
'USER_HEADER_SEARCH_PATHS' => '"${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/include" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src/image" "${PODS_ROOT}/../.symlinks/plugins/polyvox_filament/ios/src/shaders" "$(inherited)"',
|
||||
'ALWAYS_SEARCH_USER_PATHS' => 'YES',
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#include "AssetManager.hpp"
|
||||
#include "Log.hpp"
|
||||
#include <thread>
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/TransformManager.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
#include <gltfio/TextureProvider.h>
|
||||
#include <gltfio/math.h>
|
||||
|
||||
#include <imageio/ImageDecoder.h>
|
||||
|
||||
@@ -52,8 +53,13 @@ AssetManager::AssetManager(ResourceLoaderWrapper* resourceLoaderWrapper,
|
||||
_ubershaderProvider = gltfio::createUbershaderProvider(
|
||||
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
|
||||
EntityManager &em = EntityManager::get();
|
||||
_assetLoader = AssetLoader::create({_engine, _ubershaderProvider, _ncm, &em });
|
||||
|
||||
_unlitProvider = new UnlitMaterialProvider(_engine);
|
||||
|
||||
// auto rb = _resourceLoaderWrapper->load("file:///mnt/hdd_2tb/home/hydroxide/projects/polyvox/flutter/polyvox_filament/materials/toon.filamat");
|
||||
// auto toonProvider = new FileMaterialProvider(_engine, rb.data, (size_t) rb.size);
|
||||
|
||||
_assetLoader = AssetLoader::create({_engine, _ubershaderProvider, _ncm, &em });
|
||||
|
||||
_gltfResourceLoader->addTextureProvider("image/png", _stbDecoder);
|
||||
_gltfResourceLoader->addTextureProvider("image/jpeg", _stbDecoder);
|
||||
@@ -213,13 +219,42 @@ void AssetManager::updateAnimations() {
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
|
||||
for (auto& asset : _assets) {
|
||||
|
||||
if(!asset.mAnimating) {
|
||||
continue;
|
||||
}
|
||||
asset.mAnimating = false;
|
||||
|
||||
// GLTF animations
|
||||
for(int j = 0; j < asset.mAnimations.size() - 2; j++) {
|
||||
AnimationStatus& anim = asset.mAnimations[j];
|
||||
|
||||
if(!anim.mAnimating) {
|
||||
// Log("Skipping anim at %d", j);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto elapsed = float(std::chrono::duration_cast<std::chrono::milliseconds>(now - anim.mStart).count()) / 1000.0f;
|
||||
|
||||
if(anim.mLoop || elapsed < anim.mDuration) {
|
||||
asset.mAnimator->applyAnimation(j, elapsed);
|
||||
asset.mAnimating = true;
|
||||
} else if(elapsed - anim.mDuration < 0.3) {
|
||||
// cross-fade
|
||||
// animator->applyCrossFade(j-2, anim.mDuration - 0.05, elapsed / 0.3);
|
||||
// asset.mAnimating = true;
|
||||
// anim.mStart = time_point_t::max();
|
||||
} else {
|
||||
// stop
|
||||
anim.mStart = time_point_t::max();
|
||||
anim.mAnimating = false;
|
||||
Log("Finished");
|
||||
}
|
||||
asset.mAnimator->updateBoneMatrices();
|
||||
}
|
||||
|
||||
// dynamically constructed morph animation
|
||||
AnimationStatus& morphAnimation = asset.mAnimations[0];
|
||||
AnimationStatus& morphAnimation = asset.mAnimations[asset.mAnimations.size() - 2];
|
||||
|
||||
if(morphAnimation.mAnimating) {
|
||||
|
||||
@@ -254,74 +289,79 @@ void AssetManager::updateAnimations() {
|
||||
}
|
||||
}
|
||||
|
||||
// // bone animation
|
||||
// AnimationStatus boneAnimation = asset.mAnimations[1];
|
||||
// elapsed = (now - boneAnimation.mStart).count();
|
||||
// bone animation
|
||||
AnimationStatus boneAnimation = asset.mAnimations[asset.mAnimations.size() - 1];
|
||||
if(boneAnimation.mAnimating) {
|
||||
auto elapsed = float(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now - boneAnimation.mStart
|
||||
).count()) / 1000.0f;
|
||||
int lengthInFrames = static_cast<int>(
|
||||
boneAnimation.mDuration * 1000.0f /
|
||||
asset.mBoneAnimationBuffer.mFrameLengthInMs
|
||||
);
|
||||
|
||||
// lengthInFrames = static_cast<int>(boneAnimation.mDuration / asset.mBoneAnimationBuffer.mFrameLengthInMs);
|
||||
// if more time has elapsed than the animation duration && not looping
|
||||
// mark the animation as complete
|
||||
if(elapsed >= boneAnimation.mDuration && !boneAnimation.mLoop) {
|
||||
boneAnimation.mStart = time_point_t::max();
|
||||
boneAnimation.mAnimating = false;
|
||||
} else {
|
||||
|
||||
// if(elapsed >= boneAnimation.mDuration) {
|
||||
// if(boneAnimation.mLoop) {
|
||||
// boneAnimation.mStart = now;
|
||||
// if(boneAnimation.mReverse) {
|
||||
// boneAnimation.mFrameNumber = lengthInFrames;
|
||||
// }
|
||||
// asset.mAnimating = true;
|
||||
// } else {
|
||||
// boneAnimation.mStart = time_point_t::max();
|
||||
// }
|
||||
// } else {
|
||||
// asset.mAnimating = true;
|
||||
// }
|
||||
asset.mAnimating = true;
|
||||
int frameNumber = static_cast<int>(elapsed * 1000.0f / asset.mBoneAnimationBuffer.mFrameLengthInMs) % lengthInFrames;
|
||||
|
||||
// frameNumber = static_cast<int>(elapsed / asset.mBoneAnimationBuffer.mFrameLengthInMs);
|
||||
// if(frameNumber < lengthInFrames) {
|
||||
// if(boneAnimation.mReverse) {
|
||||
// frameNumber = lengthInFrames - frameNumber;
|
||||
// }
|
||||
// boneAnimation.mFrameNumber = frameNumber;
|
||||
// setBoneTransform(
|
||||
// asset.mAsset->getInstance(),
|
||||
// asset.mBoneAnimationBuffer.mAnimations,
|
||||
// frameNumber
|
||||
// );
|
||||
// }
|
||||
// offset from the end if reverse
|
||||
if(boneAnimation.mReverse) {
|
||||
frameNumber = lengthInFrames - frameNumber;
|
||||
}
|
||||
|
||||
// GLTF animations
|
||||
|
||||
int j = -1;
|
||||
for(AnimationStatus& anim : asset.mAnimations) {
|
||||
j++;
|
||||
if(j < 2) {
|
||||
setBoneTransform(
|
||||
asset,
|
||||
frameNumber
|
||||
);
|
||||
}
|
||||
asset.mAnimator->updateBoneMatrices();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void AssetManager::setBoneTransform(SceneAsset& asset, int frameNumber) {
|
||||
|
||||
RenderableManager& rm = _engine->getRenderableManager();
|
||||
|
||||
const auto& filamentInstance = asset.mAsset->getInstance();
|
||||
|
||||
TransformManager &transformManager = _engine->getTransformManager();
|
||||
|
||||
int skinIndex = 0;
|
||||
|
||||
for(int i = 0; i < asset.mBoneAnimationBuffer.mBones.size(); i++) {
|
||||
auto mBoneIndex = asset.mBoneAnimationBuffer.mBones[i];
|
||||
auto frameDataOffset = (frameNumber * asset.mBoneAnimationBuffer.mBones.size() * 7) + (i * 7);
|
||||
|
||||
utils::Entity joint = filamentInstance->getJointsAt(skinIndex)[mBoneIndex];
|
||||
if(joint.isNull()) {
|
||||
Log("ERROR : joint not found");
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!anim.mAnimating) {
|
||||
continue;
|
||||
}
|
||||
vector<float>& fd = asset.mBoneAnimationBuffer.mFrameData;
|
||||
|
||||
auto elapsed = float(std::chrono::duration_cast<std::chrono::milliseconds>(now - anim.mStart).count()) / 1000.0f;
|
||||
math::mat4f localTransform(math::quatf {
|
||||
fd[frameDataOffset+3],
|
||||
fd[frameDataOffset+4],
|
||||
fd[frameDataOffset+5],
|
||||
fd[frameDataOffset+6],
|
||||
});
|
||||
|
||||
auto jointInstance = transformManager.getInstance(joint);
|
||||
|
||||
if(anim.mLoop || elapsed < anim.mDuration) {
|
||||
asset.mAnimator->applyAnimation(j-2, elapsed);
|
||||
asset.mAnimating = true;
|
||||
} else if(elapsed - anim.mDuration < 0.3) {
|
||||
// cross-fade
|
||||
// animator->applyCrossFade(j-2, anim.mDuration - 0.05, elapsed / 0.3);
|
||||
// asset.mAnimating = true;
|
||||
// anim.mStart = time_point_t::max();
|
||||
} else {
|
||||
// stop
|
||||
anim.mStart = time_point_t::max();
|
||||
}
|
||||
}
|
||||
if(asset.mAnimating) {
|
||||
if(!asset.mAnimator) {
|
||||
Log("WARNING");
|
||||
} else {
|
||||
asset.mAnimator->updateBoneMatrices();
|
||||
}
|
||||
}
|
||||
auto xform = asset.mBoneAnimationBuffer.mBaseTransforms[i];
|
||||
|
||||
transformManager.setTransform(jointInstance, xform * localTransform);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +459,7 @@ bool AssetManager::setMorphAnimationBuffer(
|
||||
asset.mMorphAnimationBuffer.mFrameLengthInMs = frameLengthInMs;
|
||||
asset.mMorphAnimationBuffer.mNumMorphWeights = numMorphWeights;
|
||||
|
||||
AnimationStatus& animation = asset.mAnimations[0];
|
||||
AnimationStatus& animation = asset.mAnimations[asset.mAnimations.size() - 2];
|
||||
animation.mDuration = (frameLengthInMs * numFrames) / 1000.0f;
|
||||
animation.mStart = high_resolution_clock::now();
|
||||
animation.mAnimating = true;
|
||||
@@ -432,21 +472,23 @@ bool AssetManager::setMorphAnimationBuffer(
|
||||
}
|
||||
|
||||
bool AssetManager::setBoneAnimationBuffer(
|
||||
EntityId entity,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
EntityId entityId,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
int numBones,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
int numMeshTargets,
|
||||
float frameLengthInMs) {
|
||||
|
||||
const auto& pos = _entityIdLookup.find(entity);
|
||||
|
||||
|
||||
const auto& pos = _entityIdLookup.find(entityId);
|
||||
if(pos == _entityIdLookup.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return false;
|
||||
}
|
||||
auto& asset = _assets[pos->second];
|
||||
|
||||
auto filamentInstance = asset.mAsset->getInstance();
|
||||
|
||||
size_t skinCount = filamentInstance->getSkinCount();
|
||||
@@ -455,95 +497,82 @@ bool AssetManager::setBoneAnimationBuffer(
|
||||
Log("WARNING - skin count > 1 not currently implemented. This will probably not work");
|
||||
}
|
||||
|
||||
TransformManager &transformManager = _engine->getTransformManager();
|
||||
|
||||
int skinIndex = 0;
|
||||
const utils::Entity* joints = filamentInstance->getJointsAt(skinIndex);
|
||||
size_t numJoints = filamentInstance->getJointCountAt(skinIndex);
|
||||
vector<int> boneIndices;
|
||||
for(int i = 0; i < length; i++) {
|
||||
|
||||
BoneAnimationBuffer& animationBuffer = asset.mBoneAnimationBuffer;
|
||||
|
||||
// if an animation has already been set, reset the transform for the respective bones
|
||||
for(int i = 0; i < animationBuffer.mBones.size(); i++) {
|
||||
auto boneIndex = animationBuffer.mBones[i];
|
||||
auto jointInstance = transformManager.getInstance(joints[boneIndex]);
|
||||
transformManager.setTransform(jointInstance, animationBuffer.mBaseTransforms[i]);
|
||||
}
|
||||
|
||||
asset.mAnimator->resetBoneMatrices();
|
||||
|
||||
animationBuffer.mBones.resize(numBones);
|
||||
animationBuffer.mBaseTransforms.resize(numBones);
|
||||
|
||||
for(int i = 0; i < numBones; i++) {
|
||||
Log("Bone %s", boneNames[i]);
|
||||
for(int j = 0; j < numJoints; j++) {
|
||||
const char* jointName = _ncm->getName(_ncm->getInstance(joints[j]));
|
||||
if(strcmp(jointName, boneNames[i]) == 0) {
|
||||
boneIndices.push_back(j);
|
||||
if(strcmp(jointName, boneNames[i]) == 0) {
|
||||
auto jointInstance = transformManager.getInstance(joints[j]);
|
||||
// auto currentXform = ;
|
||||
auto baseTransform = transformManager.getTransform(jointInstance); // inverse(filamentInstance->getInverseBindMatricesAt(skinIndex)[j]);
|
||||
animationBuffer.mBaseTransforms[i] = baseTransform;
|
||||
animationBuffer.mBones[i] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(boneIndices.size() != length) {
|
||||
if(animationBuffer.mBones.size() != numBones) {
|
||||
Log("Failed to find one or more bone indices");
|
||||
return false;
|
||||
}
|
||||
|
||||
asset.mBoneAnimationBuffer.mAnimations.clear();
|
||||
|
||||
for(int i = 0; i < length; i++) {
|
||||
BoneAnimationData boneAnimationData;
|
||||
boneAnimationData.mBoneIndex = boneIndices[i];
|
||||
animationBuffer.mFrameData.clear();
|
||||
// 7 == locX, locY, locZ, rotW, rotX, rotY, rotZ
|
||||
animationBuffer.mFrameData.resize(numFrames * numBones * 7);
|
||||
animationBuffer.mFrameData.insert(
|
||||
animationBuffer.mFrameData.begin(),
|
||||
frameData,
|
||||
frameData + numFrames * numBones * 7
|
||||
);
|
||||
|
||||
Log("%d frames for %d bones", numFrames, numBones);
|
||||
|
||||
animationBuffer.mFrameLengthInMs = frameLengthInMs;
|
||||
animationBuffer.mNumFrames = numFrames;
|
||||
|
||||
animationBuffer.mMeshTargets.clear();
|
||||
for(int i = 0; i < numMeshTargets; i++) {
|
||||
auto entity = findEntityByName(asset, meshNames[i]);
|
||||
|
||||
if(!entity) {
|
||||
Log("Mesh target %s for bone animation could not be found", meshNames[i]);
|
||||
return false;
|
||||
}
|
||||
|
||||
boneAnimationData.mMeshTarget = entity;
|
||||
|
||||
boneAnimationData.mFrameData.insert(
|
||||
boneAnimationData.mFrameData.begin(),
|
||||
frameData[i * numFrames * 7 * sizeof(float)], // 7 == x, y, z, w + three euler angles
|
||||
frameData[(i+1) * numFrames * 7 * sizeof(float)]
|
||||
);
|
||||
|
||||
asset.mBoneAnimationBuffer.mAnimations.push_back(boneAnimationData);
|
||||
Log("Added mesh target %s", meshNames[i]);
|
||||
animationBuffer.mMeshTargets.push_back(entity);
|
||||
}
|
||||
|
||||
auto& animation = asset.mAnimations[asset.mAnimations.size() - 1];
|
||||
animation.mStart = std::chrono::high_resolution_clock::now();
|
||||
animation.mAnimating = true;
|
||||
animation.mReverse = false;
|
||||
animation.mDuration = (frameLengthInMs * numFrames) / 1000.0f;
|
||||
asset.mAnimating = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void AssetManager::setBoneTransform(
|
||||
FilamentInstance* filamentInstance,
|
||||
vector<BoneAnimationData> animations,
|
||||
int frameNumber) {
|
||||
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
|
||||
TransformManager &transformManager = _engine->getTransformManager();
|
||||
|
||||
auto frameDataOffset = frameNumber * 7;
|
||||
|
||||
int skinIndex = 0;
|
||||
|
||||
for(auto& animation : animations) {
|
||||
|
||||
math::mat4f inverseGlobalTransform = inverse(
|
||||
transformManager.getWorldTransform(
|
||||
transformManager.getInstance(animation.mMeshTarget)
|
||||
)
|
||||
);
|
||||
|
||||
utils::Entity joint = filamentInstance->getJointsAt(skinIndex)[animation.mBoneIndex];
|
||||
|
||||
math::mat4f localTransform(math::quatf{
|
||||
animation.mFrameData[frameDataOffset+6],
|
||||
animation.mFrameData[frameDataOffset+3],
|
||||
animation.mFrameData[frameDataOffset+4],
|
||||
animation.mFrameData[frameDataOffset+5]
|
||||
});
|
||||
|
||||
const math::mat4f& inverseBindMatrix = filamentInstance->getInverseBindMatricesAt(animation.skinIndex)[animation.mBoneIndex];
|
||||
auto jointInstance = transformManager.getInstance(joint);
|
||||
math::mat4f globalJointTransform = transformManager.getWorldTransform(jointInstance);
|
||||
|
||||
math::mat4f boneTransform = inverseGlobalTransform * globalJointTransform * localTransform * inverseBindMatrix;
|
||||
auto renderable = rm.getInstance(animation.mMeshTarget);
|
||||
rm.setBones(
|
||||
renderable,
|
||||
&boneTransform,
|
||||
1,
|
||||
animation.mBoneIndex
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse) {
|
||||
const auto& pos = _entityIdLookup.find(e);
|
||||
@@ -552,11 +581,12 @@ void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse)
|
||||
return;
|
||||
}
|
||||
auto& asset = _assets[pos->second];
|
||||
|
||||
asset.mAnimations[index+2].mAnimating = true;
|
||||
asset.mAnimations[index+2].mStart = std::chrono::high_resolution_clock::now();
|
||||
asset.mAnimations[index+2].mLoop = loop;
|
||||
asset.mAnimations[index+2].mReverse = reverse;
|
||||
Log("Playing animation at %d", index);
|
||||
|
||||
asset.mAnimations[index].mStart = std::chrono::high_resolution_clock::now();
|
||||
asset.mAnimations[index].mLoop = loop;
|
||||
asset.mAnimations[index].mReverse = reverse;
|
||||
asset.mAnimations[index].mAnimating = true;
|
||||
// Log("new start %d, dur is %f", std::chrono::duration_cast<std::chrono::milliseconds>(asset.mAnimations[index+2].mStart.time_since_epoch()).count(), asset.mAnimations[index+2].mDuration);
|
||||
asset.mAnimating = true;
|
||||
}
|
||||
@@ -568,7 +598,7 @@ void AssetManager::stopAnimation(EntityId entityId, int index) {
|
||||
return;
|
||||
}
|
||||
auto& asset = _assets[pos->second];
|
||||
asset.mAnimations[index+2].mStart = time_point_t::max();
|
||||
asset.mAnimations[index].mStart = time_point_t::max();
|
||||
}
|
||||
|
||||
void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int renderableIndex) {
|
||||
@@ -727,7 +757,7 @@ void AssetManager::transformToUnitCube(EntityId entity) {
|
||||
tm.setTransform(tm.getInstance(inst->getRoot()), transform);
|
||||
}
|
||||
|
||||
void AssetManager::updateTransform(SceneAsset asset) {
|
||||
void AssetManager::updateTransform(SceneAsset& asset) {
|
||||
auto &tm = _engine->getTransformManager();
|
||||
auto transform =
|
||||
asset.mPosition * asset.mRotation * math::mat4f::scaling(asset.mScale);
|
||||
@@ -809,3 +839,40 @@ size_t AssetManager::getLightEntityCount(EntityId entity) const noexcept {
|
||||
|
||||
|
||||
} // namespace polyvox
|
||||
|
||||
|
||||
// auto& inverseBindMatrix = filamentInstance->getInverseBindMatricesAt(skinIndex)[mBoneIndex];
|
||||
|
||||
// auto globalJointTransform = transformManager.getWorldTransform(jointInstance);
|
||||
|
||||
// for(auto& target : asset.mBoneAnimationBuffer.mMeshTargets) {
|
||||
|
||||
// auto inverseGlobalTransform = inverse(
|
||||
// transformManager.getWorldTransform(
|
||||
// transformManager.getInstance(target)
|
||||
// )
|
||||
// );
|
||||
|
||||
// auto boneTransform = inverseGlobalTransform * globalJointTransform * localTransform * inverseBindMatrix;
|
||||
// auto renderable = rm.getInstance(target);
|
||||
// rm.setBones(
|
||||
// renderable,
|
||||
// &boneTransform,
|
||||
// 1,
|
||||
// mBoneIndex
|
||||
// );
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
// 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
// 0.0f, -1.0f, 0.0f, 0.0f,
|
||||
// 0.0f, 0.0f, 0.0f, 1.0f
|
||||
// };
|
||||
// Log("TRANSFORM");
|
||||
// Log("%f %f %f %f", localTransform[0][0], localTransform[1][0], localTransform[2][0], localTransform[3][0] ) ;
|
||||
// Log("%f %f %f %f", localTransform[0][1], localTransform[1][1], localTransform[2][1], localTransform[3][1] ) ;
|
||||
// Log("%f %f %f %f", localTransform[0][2], localTransform[1][2], localTransform[2][2], localTransform[3][2] ) ;
|
||||
// Log("%f %f %f %f", localTransform[0][3], localTransform[1][3], localTransform[2][3], localTransform[3][3] ) ;
|
||||
// transformManager.getTransform(jointInstance);
|
||||
@@ -788,7 +788,7 @@ void FilamentViewer::render(uint64_t frameTimeInNanos) {
|
||||
}
|
||||
|
||||
if(_frameCount == 60) {
|
||||
//Log("1 sec average for asset animation update %f", _elapsed);
|
||||
// Log("1 sec average for asset animation update %f", _elapsed / 60);
|
||||
_elapsed = 0;
|
||||
_frameCount = 0;
|
||||
}
|
||||
|
||||
@@ -378,20 +378,22 @@ extern "C" {
|
||||
FLUTTER_PLUGIN_EXPORT void set_bone_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
int numBones,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
int numMeshTargets,
|
||||
float frameLengthInMs) {
|
||||
//std::packaged_task<void()> lambda([=]() mutable {
|
||||
((AssetManager*)assetManager)->setBoneAnimationBuffer(
|
||||
asset,
|
||||
length,
|
||||
frameData,
|
||||
numFrames,
|
||||
numBones,
|
||||
boneNames,
|
||||
meshNames,
|
||||
frameData,
|
||||
numFrames,
|
||||
numMeshTargets,
|
||||
frameLengthInMs
|
||||
);
|
||||
//});
|
||||
@@ -437,7 +439,6 @@ extern "C" {
|
||||
bool reverse) {
|
||||
|
||||
//std::packaged_task<void()> lambda([=]() mutable {
|
||||
std::cout << "Playing animation" << std::endl;
|
||||
((AssetManager*)assetManager)->playAnimation(asset, index, loop, reverse);
|
||||
//});
|
||||
// auto fut = _tp->add_task(lambda);
|
||||
|
||||
Reference in New Issue
Block a user