fix morph animations
This commit is contained in:
@@ -75,7 +75,8 @@ namespace polyvox {
|
|||||||
MaterialProvider* _ubershaderProvider = nullptr;
|
MaterialProvider* _ubershaderProvider = nullptr;
|
||||||
gltfio::ResourceLoader* _gltfResourceLoader = nullptr;
|
gltfio::ResourceLoader* _gltfResourceLoader = nullptr;
|
||||||
gltfio::TextureProvider* _stbDecoder = nullptr;
|
gltfio::TextureProvider* _stbDecoder = nullptr;
|
||||||
tsl::robin_map<EntityId, SceneAsset> _assets;
|
vector<SceneAsset> _assets;
|
||||||
|
tsl::robin_map<EntityId, int> _entityIdLookup;
|
||||||
|
|
||||||
void setBoneTransform(
|
void setBoneTransform(
|
||||||
FilamentInstance* instance,
|
FilamentInstance* instance,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Log.hpp"
|
||||||
|
|
||||||
#include <filament/Engine.h>
|
#include <filament/Engine.h>
|
||||||
#include <filament/RenderableManager.h>
|
#include <filament/RenderableManager.h>
|
||||||
#include <filament/Renderer.h>
|
#include <filament/Renderer.h>
|
||||||
@@ -36,9 +38,38 @@ namespace polyvox {
|
|||||||
bool mReverse = false;
|
bool mReverse = false;
|
||||||
float mDuration = 0;
|
float mDuration = 0;
|
||||||
int mFrameNumber = -1;
|
int mFrameNumber = -1;
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// Use this to manually construct a buffer of frame data for morph animations.
|
// Use this to manually construct a buffer of frame data for morph animations.
|
||||||
//
|
//
|
||||||
struct MorphAnimationBuffer {
|
struct MorphAnimationBuffer {
|
||||||
@@ -70,13 +101,10 @@ namespace polyvox {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SceneAsset {
|
struct SceneAsset {
|
||||||
|
bool mAnimating = false;
|
||||||
FilamentAsset* mAsset = nullptr;
|
FilamentAsset* mAsset = nullptr;
|
||||||
Animator* mAnimator = nullptr;
|
Animator* mAnimator = nullptr;
|
||||||
|
|
||||||
// animation flags;
|
|
||||||
bool mAnimating = false;
|
|
||||||
|
|
||||||
// fixed-sized vector containing the status of the morph, bone and GLTF animations.
|
// fixed-sized vector containing the status of the morph, bone and GLTF animations.
|
||||||
// entries 0 and 1 are the morph/bone animations.
|
// entries 0 and 1 are the morph/bone animations.
|
||||||
// subsequent entries are the GLTF animations.
|
// subsequent entries are the GLTF animations.
|
||||||
@@ -96,10 +124,40 @@ namespace polyvox {
|
|||||||
|
|
||||||
float mScale = 1;
|
float mScale = 1;
|
||||||
|
|
||||||
|
// SceneAsset(const SceneAsset&& a) {
|
||||||
|
// Log("MOVE");
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// SceneAsset(const SceneAsset& a) {
|
||||||
|
// mAsset = a.mAsset;
|
||||||
|
// mAnimator = a.mAnimator;
|
||||||
|
// mAnimations = a.mAnimations;
|
||||||
|
// mMorphAnimationBuffer = a.mMorphAnimationBuffer;
|
||||||
|
// mBoneAnimationBuffer = a.mBoneAnimationBuffer;
|
||||||
|
// mTexture = a.mTexture;
|
||||||
|
// mPosition = a.mPosition;
|
||||||
|
// mRotation = a.mRotation;
|
||||||
|
// mScale = a.mScale;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// SceneAsset& operator=(SceneAsset a) {
|
||||||
|
// mAsset = a.mAsset;
|
||||||
|
// mAnimator = a.mAnimator;
|
||||||
|
// mAnimations = a.mAnimations;
|
||||||
|
// mMorphAnimationBuffer = a.mMorphAnimationBuffer;
|
||||||
|
// mBoneAnimationBuffer = a.mBoneAnimationBuffer;
|
||||||
|
// mTexture = a.mTexture;
|
||||||
|
// mPosition = a.mPosition;
|
||||||
|
// mRotation = a.mRotation;
|
||||||
|
// mScale = a.mScale;
|
||||||
|
// return *this;
|
||||||
|
// }
|
||||||
|
|
||||||
SceneAsset(
|
SceneAsset(
|
||||||
FilamentAsset* asset
|
FilamentAsset* asset
|
||||||
) : mAsset(asset) {
|
) : mAsset(asset) {
|
||||||
|
|
||||||
mAnimator = mAsset->getInstance()->getAnimator();
|
mAnimator = mAsset->getInstance()->getAnimator();
|
||||||
|
|
||||||
mAnimations.resize(2 + mAnimator->getAnimationCount());
|
mAnimations.resize(2 + mAnimator->getAnimationCount());
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "AssetManager.hpp"
|
#include "AssetManager.hpp"
|
||||||
#include "Log.hpp"
|
#include "Log.hpp"
|
||||||
|
#include <thread>
|
||||||
#include <filament/Engine.h>
|
#include <filament/Engine.h>
|
||||||
#include <filament/TransformManager.h>
|
#include <filament/TransformManager.h>
|
||||||
#include <filament/Texture.h>
|
#include <filament/Texture.h>
|
||||||
@@ -119,11 +119,14 @@ EntityId AssetManager::loadGltf(const char *uri,
|
|||||||
|
|
||||||
Log("Load complete for GLTF at URI %s", uri);
|
Log("Load complete for GLTF at URI %s", uri);
|
||||||
SceneAsset sceneAsset(asset);
|
SceneAsset sceneAsset(asset);
|
||||||
|
|
||||||
|
|
||||||
utils::Entity e = EntityManager::get().create();
|
utils::Entity e = EntityManager::get().create();
|
||||||
|
|
||||||
EntityId eid = Entity::smuggle(e);
|
EntityId eid = Entity::smuggle(e);
|
||||||
|
|
||||||
_assets.emplace(eid, sceneAsset);
|
_entityIdLookup.emplace(eid, _assets.size());
|
||||||
|
_assets.push_back(sceneAsset);
|
||||||
|
|
||||||
return eid;
|
return eid;
|
||||||
}
|
}
|
||||||
@@ -131,8 +134,6 @@ EntityId AssetManager::loadGltf(const char *uri,
|
|||||||
EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
|
EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
|
||||||
|
|
||||||
Log("Loading GLB at URI %s", uri);
|
Log("Loading GLB at URI %s", uri);
|
||||||
_loadResource("BLORTY");
|
|
||||||
Log("blorty");
|
|
||||||
|
|
||||||
ResourceBuffer rbuf = _loadResource(uri);
|
ResourceBuffer rbuf = _loadResource(uri);
|
||||||
|
|
||||||
@@ -183,14 +184,14 @@ EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
|
|||||||
utils::Entity e = EntityManager::get().create();
|
utils::Entity e = EntityManager::get().create();
|
||||||
EntityId eid = Entity::smuggle(e);
|
EntityId eid = Entity::smuggle(e);
|
||||||
|
|
||||||
_assets.emplace(eid, sceneAsset);
|
_entityIdLookup.emplace(eid, _assets.size());
|
||||||
|
_assets.push_back(sceneAsset);
|
||||||
|
|
||||||
return eid;
|
return eid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::destroyAll() {
|
void AssetManager::destroyAll() {
|
||||||
for (auto kp : _assets) {
|
for (auto& asset : _assets) {
|
||||||
auto asset = kp.second;
|
|
||||||
_scene->removeEntities(asset.mAsset->getEntities(),
|
_scene->removeEntities(asset.mAsset->getEntities(),
|
||||||
asset.mAsset->getEntityCount());
|
asset.mAsset->getEntityCount());
|
||||||
|
|
||||||
@@ -204,11 +205,11 @@ void AssetManager::destroyAll() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FilamentAsset* AssetManager::getAssetByEntityId(EntityId entityId) {
|
FilamentAsset* AssetManager::getAssetByEntityId(EntityId entityId) {
|
||||||
const auto& pos = _assets.find(entityId);
|
const auto& pos = _entityIdLookup.find(entityId);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return pos->second.mAsset;
|
return _assets[pos->second].mAsset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -217,117 +218,118 @@ void AssetManager::updateAnimations() {
|
|||||||
|
|
||||||
RenderableManager &rm = _engine->getRenderableManager();
|
RenderableManager &rm = _engine->getRenderableManager();
|
||||||
|
|
||||||
for (auto kp : _assets) {
|
for (auto& asset : _assets) {
|
||||||
auto asset = kp.second;
|
if(!asset.mAnimating) {
|
||||||
if(asset.mAnimating) {
|
continue;
|
||||||
|
|
||||||
asset.mAnimating = false;
|
|
||||||
|
|
||||||
// morph animation
|
|
||||||
AnimationStatus morphAnimation = asset.mAnimations[0];
|
|
||||||
auto elapsed = (now - morphAnimation.mStart).count();
|
|
||||||
|
|
||||||
int lengthInFrames = static_cast<int>(morphAnimation.mDuration / asset.mMorphAnimationBuffer.mFrameLengthInMs);
|
|
||||||
|
|
||||||
if(elapsed >= morphAnimation.mDuration) {
|
|
||||||
if(morphAnimation.mLoop) {
|
|
||||||
morphAnimation.mStart = now;
|
|
||||||
if(morphAnimation.mReverse) {
|
|
||||||
morphAnimation.mFrameNumber = lengthInFrames;
|
|
||||||
}
|
|
||||||
asset.mAnimating = true;
|
|
||||||
} else {
|
|
||||||
morphAnimation.mStart = time_point_t::max();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
asset.mAnimating = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int frameNumber = static_cast<int>(elapsed / asset.mMorphAnimationBuffer.mFrameLengthInMs);
|
|
||||||
if(frameNumber < lengthInFrames) {
|
|
||||||
if(morphAnimation.mReverse) {
|
|
||||||
frameNumber = lengthInFrames - frameNumber;
|
|
||||||
}
|
|
||||||
rm.setMorphWeights(
|
|
||||||
*(asset.mMorphAnimationBuffer.mInstance),
|
|
||||||
asset.mMorphAnimationBuffer.mFrameData.data() + (morphAnimation.mFrameNumber * asset.mMorphAnimationBuffer.mNumMorphWeights),
|
|
||||||
asset.mMorphAnimationBuffer.mNumMorphWeights);
|
|
||||||
}
|
|
||||||
|
|
||||||
// bone animation
|
|
||||||
AnimationStatus boneAnimation = asset.mAnimations[1];
|
|
||||||
elapsed = (now - boneAnimation.mStart).count();
|
|
||||||
|
|
||||||
lengthInFrames = static_cast<int>(boneAnimation.mDuration / asset.mBoneAnimationBuffer.mFrameLengthInMs);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GLTF animations
|
|
||||||
|
|
||||||
Animator* animator = asset.mAnimator;
|
|
||||||
|
|
||||||
for(int j = 2; j < asset.mAnimations.size(); j++) {
|
|
||||||
|
|
||||||
AnimationStatus anim = asset.mAnimations[j];
|
|
||||||
|
|
||||||
elapsed = (now - anim.mStart).count();
|
|
||||||
|
|
||||||
if(elapsed < anim.mDuration) {
|
|
||||||
if(anim.mLoop) {
|
|
||||||
animator->applyAnimation(j-2, anim.mDuration - elapsed);
|
|
||||||
} else {
|
|
||||||
animator->applyAnimation(j-2, elapsed);
|
|
||||||
}
|
|
||||||
asset.mAnimating = true;
|
|
||||||
} else if(anim.mLoop) {
|
|
||||||
animator->applyAnimation(j-2, float(elapsed) ); //% anim.mDuration
|
|
||||||
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;
|
|
||||||
} else {
|
|
||||||
// stop
|
|
||||||
anim.mStart = time_point_t::max();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
asset.mAnimator->updateBoneMatrices();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
asset.mAnimating = false;
|
||||||
|
|
||||||
|
// // morph animation
|
||||||
|
// AnimationStatus morphAnimation = asset.mAnimations[0];
|
||||||
|
// auto elapsed = (now - morphAnimation.mStart).count();
|
||||||
|
|
||||||
|
// int lengthInFrames = static_cast<int>(morphAnimation.mDuration / asset.mMorphAnimationBuffer.mFrameLengthInMs);
|
||||||
|
|
||||||
|
// if(elapsed >= morphAnimation.mDuration) {
|
||||||
|
// if(morphAnimation.mLoop) {
|
||||||
|
// morphAnimation.mStart = now;
|
||||||
|
// if(morphAnimation.mReverse) {
|
||||||
|
// morphAnimation.mFrameNumber = lengthInFrames;
|
||||||
|
// }
|
||||||
|
// asset.mAnimating = true;
|
||||||
|
// } else {
|
||||||
|
// morphAnimation.mStart = time_point_t::max();
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// asset.mAnimating = true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// int frameNumber = static_cast<int>(elapsed / asset.mMorphAnimationBuffer.mFrameLengthInMs);
|
||||||
|
// if(frameNumber < lengthInFrames) {
|
||||||
|
// if(morphAnimation.mReverse) {
|
||||||
|
// frameNumber = lengthInFrames - frameNumber;
|
||||||
|
// }
|
||||||
|
// rm.setMorphWeights(
|
||||||
|
// *(asset.mMorphAnimationBuffer.mInstance),
|
||||||
|
// asset.mMorphAnimationBuffer.mFrameData.data() + (morphAnimation.mFrameNumber * asset.mMorphAnimationBuffer.mNumMorphWeights),
|
||||||
|
// asset.mMorphAnimationBuffer.mNumMorphWeights);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // bone animation
|
||||||
|
// AnimationStatus boneAnimation = asset.mAnimations[1];
|
||||||
|
// elapsed = (now - boneAnimation.mStart).count();
|
||||||
|
|
||||||
|
// lengthInFrames = static_cast<int>(boneAnimation.mDuration / asset.mBoneAnimationBuffer.mFrameLengthInMs);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// GLTF animations
|
||||||
|
|
||||||
|
Animator* animator = asset.mAnimator;
|
||||||
|
|
||||||
|
for(int j = 2; j < asset.mAnimations.size(); j++) {
|
||||||
|
|
||||||
|
AnimationStatus& anim = asset.mAnimations[j];
|
||||||
|
|
||||||
|
auto elapsed = float(std::chrono::duration_cast<std::chrono::milliseconds>(now - anim.mStart).count()) / 1000.0f;
|
||||||
|
|
||||||
|
if(elapsed < anim.mDuration) {
|
||||||
|
if(anim.mLoop) {
|
||||||
|
animator->applyAnimation(j-2, anim.mDuration - elapsed);
|
||||||
|
} else {
|
||||||
|
animator->applyAnimation(j-2, elapsed);
|
||||||
|
}
|
||||||
|
asset.mAnimating = true;
|
||||||
|
} else if(anim.mLoop) {
|
||||||
|
animator->applyAnimation(j-2, float(elapsed) ); //% anim.mDuration
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asset.mAnimator->updateBoneMatrices();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::remove(EntityId entityId) {
|
void AssetManager::remove(EntityId entityId) {
|
||||||
const auto& pos = _assets.find(entityId);
|
const auto& pos = _entityIdLookup.find(entityId);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("Couldn't find asset under specified entity id.");
|
Log("Couldn't find asset under specified entity id.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto sceneAsset = pos->second;
|
SceneAsset& sceneAsset = _assets[pos->second];
|
||||||
|
|
||||||
_scene->removeEntities(sceneAsset.mAsset->getEntities(),
|
_scene->removeEntities(sceneAsset.mAsset->getEntities(),
|
||||||
sceneAsset.mAsset->getEntityCount());
|
sceneAsset.mAsset->getEntityCount());
|
||||||
@@ -342,7 +344,7 @@ void AssetManager::remove(EntityId entityId) {
|
|||||||
}
|
}
|
||||||
EntityManager& em = EntityManager::get();
|
EntityManager& em = EntityManager::get();
|
||||||
em.destroy(Entity::import(entityId));
|
em.destroy(Entity::import(entityId));
|
||||||
_assets.erase(entityId);
|
sceneAsset.mAsset = nullptr; // still need to remove this somewhere...
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::setMorphTargetWeights(const char* const entityName, float *weights, int count) {
|
void AssetManager::setMorphTargetWeights(const char* const entityName, float *weights, int count) {
|
||||||
@@ -371,12 +373,12 @@ bool AssetManager::setMorphAnimationBuffer(
|
|||||||
int numFrames,
|
int numFrames,
|
||||||
float frameLengthInMs) {
|
float frameLengthInMs) {
|
||||||
|
|
||||||
const auto& pos = _assets.find(entityId);
|
const auto& pos = _entityIdLookup.find(entityId);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto asset = _assets[pos->second];
|
||||||
|
|
||||||
auto entity = findEntityByName(asset, entityName);
|
auto entity = findEntityByName(asset, entityName);
|
||||||
if(!entity) {
|
if(!entity) {
|
||||||
@@ -409,12 +411,12 @@ bool AssetManager::setBoneAnimationBuffer(
|
|||||||
int numFrames,
|
int numFrames,
|
||||||
float frameLengthInMs) {
|
float frameLengthInMs) {
|
||||||
|
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
|
|
||||||
auto filamentInstance = asset.mAsset->getInstance();
|
auto filamentInstance = asset.mAsset->getInstance();
|
||||||
|
|
||||||
@@ -515,36 +517,39 @@ void AssetManager::setBoneTransform(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse) {
|
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse) {
|
||||||
const auto& pos = _assets.find(e);
|
const auto& pos = _entityIdLookup.find(e);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
|
|
||||||
asset.mAnimations[index+2].mStart = high_resolution_clock::now();
|
Log("prev start %d", std::chrono::duration_cast<std::chrono::milliseconds>(asset.mAnimations[index+2].mStart.time_since_epoch()).count());
|
||||||
|
|
||||||
|
asset.mAnimations[index+2].mStart = std::chrono::high_resolution_clock::now();
|
||||||
asset.mAnimations[index+2].mLoop = loop;
|
asset.mAnimations[index+2].mLoop = loop;
|
||||||
asset.mAnimations[index+2].mReverse = reverse;
|
asset.mAnimations[index+2].mReverse = reverse;
|
||||||
|
asset.mAnimating = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::stopAnimation(EntityId entityId, int index) {
|
void AssetManager::stopAnimation(EntityId entityId, int index) {
|
||||||
const auto& pos = _assets.find(entityId);
|
const auto& pos = _entityIdLookup.find(entityId);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
asset.mAnimations[index+2].mStart = time_point_t::max();
|
asset.mAnimations[index+2].mStart = time_point_t::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int renderableIndex) {
|
void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int renderableIndex) {
|
||||||
|
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
|
|
||||||
Log("Loading texture at %s for renderableIndex %d", resourcePath, renderableIndex);
|
Log("Loading texture at %s for renderableIndex %d", resourcePath, renderableIndex);
|
||||||
|
|
||||||
@@ -609,12 +614,12 @@ void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int re
|
|||||||
|
|
||||||
|
|
||||||
void AssetManager::setAnimationFrame(EntityId entity, int animationIndex, int animationFrame) {
|
void AssetManager::setAnimationFrame(EntityId entity, int animationIndex, int animationFrame) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
auto offset = 60 * animationFrame * 1000; // TODO - don't hardcore 60fps framerate
|
auto offset = 60 * animationFrame * 1000; // TODO - don't hardcore 60fps framerate
|
||||||
asset.mAnimator->applyAnimation(animationIndex, offset);
|
asset.mAnimator->applyAnimation(animationIndex, offset);
|
||||||
asset.mAnimator->updateBoneMatrices();
|
asset.mAnimator->updateBoneMatrices();
|
||||||
@@ -622,15 +627,15 @@ void AssetManager::setAnimationFrame(EntityId entity, int animationIndex, int an
|
|||||||
|
|
||||||
unique_ptr<vector<string>> AssetManager::getAnimationNames(EntityId entity) {
|
unique_ptr<vector<string>> AssetManager::getAnimationNames(EntityId entity) {
|
||||||
|
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
|
|
||||||
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
||||||
|
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity id.");
|
Log("ERROR: asset not found for entity id.");
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
|
|
||||||
size_t count = asset.mAnimator->getAnimationCount();
|
size_t count = asset.mAnimator->getAnimationCount();
|
||||||
|
|
||||||
@@ -646,12 +651,12 @@ unique_ptr<vector<string>> AssetManager::getMorphTargetNames(EntityId entity, co
|
|||||||
|
|
||||||
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
||||||
|
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
|
|
||||||
const utils::Entity *entities = asset.mAsset->getEntities();
|
const utils::Entity *entities = asset.mAsset->getEntities();
|
||||||
|
|
||||||
@@ -673,12 +678,12 @@ unique_ptr<vector<string>> AssetManager::getMorphTargetNames(EntityId entity, co
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::transformToUnitCube(EntityId entity) {
|
void AssetManager::transformToUnitCube(EntityId entity) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
|
|
||||||
Log("Transforming asset to unit cube.");
|
Log("Transforming asset to unit cube.");
|
||||||
auto &tm = _engine->getTransformManager();
|
auto &tm = _engine->getTransformManager();
|
||||||
@@ -701,75 +706,75 @@ void AssetManager::updateTransform(SceneAsset asset) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::setScale(EntityId entity, float scale) {
|
void AssetManager::setScale(EntityId entity, float scale) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
asset.mScale = scale;
|
asset.mScale = scale;
|
||||||
updateTransform(asset);
|
updateTransform(asset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::setPosition(EntityId entity, float x, float y, float z) {
|
void AssetManager::setPosition(EntityId entity, float x, float y, float z) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
asset.mPosition = math::mat4f::translation(math::float3(x,y,z));
|
asset.mPosition = math::mat4f::translation(math::float3(x,y,z));
|
||||||
updateTransform(asset);
|
updateTransform(asset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::setRotation(EntityId entity, float rads, float x, float y, float z) {
|
void AssetManager::setRotation(EntityId entity, float rads, float x, float y, float z) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
asset.mRotation = math::mat4f::rotation(rads, math::float3(x,y,z));
|
asset.mRotation = math::mat4f::rotation(rads, math::float3(x,y,z));
|
||||||
updateTransform(asset);
|
updateTransform(asset);
|
||||||
}
|
}
|
||||||
|
|
||||||
const utils::Entity *AssetManager::getCameraEntities(EntityId entity) {
|
const utils::Entity *AssetManager::getCameraEntities(EntityId entity) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
return asset.mAsset->getCameraEntities();
|
return asset.mAsset->getCameraEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t AssetManager::getCameraEntityCount(EntityId entity) {
|
size_t AssetManager::getCameraEntityCount(EntityId entity) {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
return asset.mAsset->getCameraEntityCount();
|
return asset.mAsset->getCameraEntityCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
const utils::Entity* AssetManager::getLightEntities(EntityId entity) const noexcept {
|
const utils::Entity* AssetManager::getLightEntities(EntityId entity) const noexcept {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
return asset.mAsset->getLightEntities();
|
return asset.mAsset->getLightEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t AssetManager::getLightEntityCount(EntityId entity) const noexcept {
|
size_t AssetManager::getLightEntityCount(EntityId entity) const noexcept {
|
||||||
const auto& pos = _assets.find(entity);
|
const auto& pos = _entityIdLookup.find(entity);
|
||||||
if(pos == _assets.end()) {
|
if(pos == _entityIdLookup.end()) {
|
||||||
Log("ERROR: asset not found for entity.");
|
Log("ERROR: asset not found for entity.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
auto asset = pos->second;
|
auto& asset = _assets[pos->second];
|
||||||
return asset.mAsset->getLightEntityCount();
|
return asset.mAsset->getLightEntityCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -790,7 +790,7 @@ void FilamentViewer::render(uint64_t frameTimeInNanos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(_frameCount == 60) {
|
if(_frameCount == 60) {
|
||||||
Log("1 sec average for asset animation update %f", _elapsed);
|
// Log("1 sec average for asset animation update %f", _elapsed);
|
||||||
_elapsed = 0;
|
_elapsed = 0;
|
||||||
_frameCount = 0;
|
_frameCount = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,6 +358,7 @@ class FilamentController {
|
|||||||
|
|
||||||
void playAnimation(FilamentEntity asset, int index,
|
void playAnimation(FilamentEntity asset, int index,
|
||||||
{bool loop = false, bool reverse = false}) async {
|
{bool loop = false, bool reverse = false}) async {
|
||||||
|
print("Playing animation @ $index");
|
||||||
_nativeLibrary.play_animation(
|
_nativeLibrary.play_animation(
|
||||||
_assetManager, asset, index, loop ? 1 : 0, reverse ? 1 : 0);
|
_assetManager, asset, index, loop ? 1 : 0, reverse ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,8 +133,6 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
|||||||
onPointerSignal: !widget.enableControls
|
onPointerSignal: !widget.enableControls
|
||||||
? null
|
? null
|
||||||
: (pointerSignal) async {
|
: (pointerSignal) async {
|
||||||
print("ponter signal");
|
|
||||||
|
|
||||||
// scroll-wheel zoom on desktop
|
// scroll-wheel zoom on desktop
|
||||||
if (pointerSignal is PointerScrollEvent) {
|
if (pointerSignal is PointerScrollEvent) {
|
||||||
_scrollTimer?.cancel();
|
_scrollTimer?.cancel();
|
||||||
|
|||||||
Reference in New Issue
Block a user