Merge branch 'master' of github.com:nmfisher/polyvox_filament

This commit is contained in:
Nick Fisher
2023-02-26 12:22:14 +08:00
12 changed files with 172 additions and 106 deletions

View File

@@ -8,9 +8,10 @@
typedef struct ResourceBuffer ResourceBuffer;
///
/// A wrapper for a single set of frame-data that may animate multiples bones/mesh nodes.
/// Frame data for animating multiples bones for multiple meshes.
/// [data]
///
struct BoneAnimation {
const char* const* const boneNames;
const char* const* const meshNames;
@@ -52,10 +53,11 @@ void grab_begin(void* viewer, float x, float y, bool pan);
void grab_update(void* viewer, float x, float y);
void grab_end(void* viewer);
void apply_weights(void* asset, float* const weights, int count);
void apply_weights(void* asset, const char* const entityName, float* const weights, int count);
void set_animation(
void* asset,
const char* const entityName,
const float* const morphData,
int numMorphWeights,
const BoneAnimation* const boneAnimations,

View File

@@ -63,10 +63,10 @@ namespace polyvox {
void setAnimationFrame(int animationIndex, int animationFrame);
///
/// Manually set the weights for all morph targets in the assets to the provided values.
/// Set the weights for all [count] morph targets in this asset's entity named [inst] to [weights].
/// See [setAnimation] if you want to do the same across a number of frames (and extended to bone transforms).
///
void setMorphTargetWeights(float* weights, int count);
void setMorphTargetWeights(const char* const entityName, float* weights, int count);
///
/// Animates the asset's morph targets/bone transforms according to the frame weights/transforms specified in [morphData]/[boneData].
@@ -76,6 +76,7 @@ namespace polyvox {
/// [morphData] and [boneData] will both be copied, so remember to free these after calling this function.
///
void setAnimation(
const char* entityName,
const float* const morphData,
int numMorphWeights,
const BoneAnimation* const targets,
@@ -84,7 +85,6 @@ namespace polyvox {
float frameLengthInMs
);
void fillEntitiesByName(const char** name, int count, vector<Entity>& out);
size_t getBoneIndex(const char* name);
Entity getNode(const char* name);

View File

@@ -2,10 +2,13 @@
#define SCENE_ASSET_ANIMATION_H_
#include "utils/Entity.h"
#include <filament/RenderableManager.h>
namespace polyvox {
using namespace std;
using Instance = utils::EntityInstance<filament::RenderableManager>;
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
@@ -73,21 +76,26 @@ namespace polyvox {
//
struct RuntimeAnimation {
Instance mInstance;
int frameNumber = -1;
int mNumFrames = -1;
float mFrameLengthInMs = 0;
time_point_t startTime;
float* mMorphFrameData = nullptr;
int mNumMorphWeights = 0;
unique_ptr<vector<BoneTransformTarget>> mTargets;
RuntimeAnimation(const float* const morphData,
RuntimeAnimation(Instance instance,
const float* const morphData,
int numMorphWeights,
unique_ptr<vector<BoneTransformTarget>>& targets,
int numFrames,
float frameLengthInMs) :
mInstance(instance),
mNumFrames(numFrames),
mFrameLengthInMs(frameLengthInMs),
mNumMorphWeights(numMorphWeights),

View File

@@ -141,12 +141,13 @@ extern "C" {
((FilamentViewer*)viewer)->grabEnd();
}
void apply_weights(void* asset, float* const weights, int count) {
((SceneAsset*)asset)->setMorphTargetWeights(weights, count);
void apply_weights(void* asset, const char* const entityName, float* const weights, int count) {
((SceneAsset*)asset)->setMorphTargetWeights(entityName, weights, count);
}
void set_animation(
void* asset,
const char* const entityName,
const float* const morphData,
int numMorphWeights,
const BoneAnimation* const boneAnimations,
@@ -154,6 +155,7 @@ extern "C" {
int numFrames,
float frameLengthInMs) {
((SceneAsset*)asset)->setAnimation(
entityName,
morphData,
numMorphWeights,
boneAnimations,

View File

@@ -4,6 +4,7 @@
#include <filament/Engine.h>
#include <filament/TransformManager.h>
#include <filament/Texture.h>
#include <filament/RenderableManager.h>
#include <gltfio/Animator.h>
#include <gltfio/AssetLoader.h>
@@ -48,15 +49,12 @@ SceneAsset::~SceneAsset() {
}
}
void SceneAsset::setMorphTargetWeights(float *weights, int count) {
RenderableManager &rm = _engine->getRenderableManager();
for (size_t i = 0, c = _asset->getEntityCount(); i != c; ++i) {
auto inst = rm.getInstance(_asset->getEntities()[i]);
rm.setMorphWeights(inst, weights, count);
}
void SceneAsset::setMorphTargetWeights(const char* const entityName, float *weights, int count) {
// TODO
}
void SceneAsset::setAnimation(
const char* entityName,
const float* const morphData,
int numMorphWeights,
const BoneAnimation* const boneAnimations,
@@ -107,13 +105,31 @@ void SceneAsset::setAnimation(
frameData
));
}
_runtimeAnimationBuffer = std::make_unique<RuntimeAnimation>(
morphData,
numMorphWeights,
transforms,
numFrames,
frameLengthInMs
);
RenderableManager &rm = _engine->getRenderableManager();
Instance inst;
for (size_t i = 0, c = _asset->getEntityCount(); i != c; ++i) {
auto entity = _asset->getEntities()[i];
auto name = _ncm->getName(_ncm->getInstance(entity));
if(strcmp(entityName,name)==0) {
inst = rm.getInstance(_asset->getEntities()[i]);
}
}
if(!inst) {
Log("Warning: failed to find Renderable instance for entity %s", entityName);
} else {
_runtimeAnimationBuffer = std::make_unique<RuntimeAnimation>(
inst,
morphData,
numMorphWeights,
transforms,
numFrames,
frameLengthInMs
);
}
}
void SceneAsset::updateAnimations() {
@@ -142,12 +158,15 @@ void SceneAsset::updateRuntimeAnimation() {
return;
}
RenderableManager &rm = _engine->getRenderableManager();
if (frameNumber > _runtimeAnimationBuffer->frameNumber) {
_runtimeAnimationBuffer->frameNumber = frameNumber;
if(_runtimeAnimationBuffer->mMorphFrameData) {
auto morphFramePtrOffset = frameNumber * _runtimeAnimationBuffer->mNumMorphWeights;
setMorphTargetWeights(_runtimeAnimationBuffer->mMorphFrameData + morphFramePtrOffset,
_runtimeAnimationBuffer->mNumMorphWeights);
rm.setMorphWeights(
_runtimeAnimationBuffer->mInstance,
_runtimeAnimationBuffer->mMorphFrameData + morphFramePtrOffset,
_runtimeAnimationBuffer->mNumMorphWeights);
}
if(_runtimeAnimationBuffer->mTargets->size() > 0) {

View File

@@ -97,9 +97,6 @@ SceneAsset *SceneAssetLoader::fromGlb(const char *uri) {
_scene->addEntities(asset->getEntities(), entityCount);
Log("Added %d entities to scene", entityCount);
size_t lightEntityCount = asset->getLightEntityCount();
Log("Found %d light entities in scene.", lightEntityCount );
_resourceLoader->loadResources(asset);
@@ -115,6 +112,10 @@ SceneAsset *SceneAssetLoader::fromGlb(const char *uri) {
rm.setCulling(entityInstance, true);
}
auto lights = asset->getLightEntities();
_scene->addEntities(lights, asset->getLightEntityCount());
Log("Added %d lights to scene from asset", asset->getLightEntityCount());
FilamentInstance* inst = asset->getInstance();
inst->getAnimator()->updateBoneMatrices();
@@ -131,8 +132,15 @@ SceneAsset *SceneAssetLoader::fromGlb(const char *uri) {
}
void SceneAssetLoader::remove(SceneAsset *asset) {
Log("Removing asset and all associated entities/lights.");
_scene->removeEntities(asset->_asset->getEntities(),
asset->_asset->getEntityCount());
_scene->removeEntities(asset->getLightEntities(),
asset->getLightEntityCount());
_resourceLoader->evictResourceData();
_assetLoader->destroyAsset(asset->_asset);
delete asset;