add getters from animation names and play animation method

This commit is contained in:
Nick Fisher
2022-03-22 22:33:50 +08:00
parent 8e3b530b46
commit 1410fb9ea7
6 changed files with 525 additions and 381 deletions

View File

@@ -6,10 +6,11 @@
#include <android/native_activity.h> #include <android/native_activity.h>
using namespace polyvox; using namespace polyvox;
using namespace std;
static AAssetManager* am; static AAssetManager* am;
std::vector<AAsset*> _assets; vector<AAsset*> _assets;
uint64_t id = -1; uint64_t id = -1;
static polyvox::ResourceBuffer loadResource(const char* name) { static polyvox::ResourceBuffer loadResource(const char* name) {
@@ -122,30 +123,44 @@ extern "C" {
((FilamentViewer*)viewer)->animateWeights((float*)data, numWeights, numFrames, frameRate); ((FilamentViewer*)viewer)->animateWeights((float*)data, numWeights, numFrames, frameRate);
} }
void get_target_names(void* viewer, char* meshName, char*** outPtr, int* countPtr ) { void play_animation(void* viewer, int index) {
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Playing embedded animation %d", index);
((FilamentViewer*)viewer)->playAnimation(index);
}
char** get_animation_names(void* viewer, int* countPtr) {
auto names = ((FilamentViewer*)viewer)->getAnimationNames();
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Got %d animation names", names->size());
char** names_c;
names_c = new char*[names->size()];
for(int i = 0; i < names->size(); i++) {
names_c[i] = (char*) names->at(i).c_str();
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Alloced animation name %s ", (char*) names->at(i).c_str());
}
(*countPtr) = names->size();
return names_c;
}
char** get_target_names(void* viewer, char* meshName, int* countPtr ) {
StringList names = ((FilamentViewer*)viewer)->getTargetNames(meshName); StringList names = ((FilamentViewer*)viewer)->getTargetNames(meshName);
*countPtr = names.count;
*outPtr = (char**)malloc(sizeof(char*) * names.count);
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Got %d names", names.count); __android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Got %d names", names.count);
*countPtr = names.count;
char** retval;
retval = new char*[names.count];
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Allocated char* array of size %d", names.count);
for(int i =0; i < names.count; i++) { for(int i =0; i < names.count; i++) {
std::string as_str(names.strings[i]); retval[i] = (char*)names.strings[i];
(*outPtr)[i] = (char*)malloc(sizeof(char) * as_str.length());
strcpy((*outPtr)[i], as_str.c_str());
} }
return retval;
} }
void free_pointer(char*** ptr, int size) { void free_pointer(char** ptr, int num) {
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Freeing %d char pointers", size); free(ptr);
for(int i = 0; i < size; i++) {
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "%d", i);
// free((*ptr)[i]);
}
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Free complete");
// free(*ptr);
} }
void release_source_assets(void* viewer) { void release_source_assets(void* viewer) {

View File

@@ -54,10 +54,14 @@ interface FilamentInterop : Library {
fun animate_weights(viewer:Pointer, frames:FloatArray, numWeights:Int, numFrames:Int, frameRate:Float); fun animate_weights(viewer:Pointer, frames:FloatArray, numWeights:Int, numFrames:Int, frameRate:Float);
fun get_target_names(viewer:Pointer, meshName:String, outPtr:PointerByReference, outLen:IntByReference); fun get_target_names(viewer:Pointer, meshName:String, outLen:IntByReference) : Pointer;
fun free_pointer(ptr:PointerByReference, size:Int) fun get_animation_names(viewer:Pointer, outLen:IntByReference) : Pointer;
fun release_source_assets(viewer:Pointer) fun play_animation(viewer:Pointer, index:Int);
fun free_pointer(ptr:Pointer, size:Int);
fun release_source_assets(viewer:Pointer);
} }

View File

@@ -201,19 +201,38 @@ PlatformView {
"getTargetNames" -> { "getTargetNames" -> {
if(_viewer == null) if(_viewer == null)
return; return;
val arrPtr = PointerByReference();
val countPtr = IntByReference(); val countPtr = IntByReference();
_lib.get_target_names(_viewer!!, call.arguments as String, arrPtr, countPtr) val arrPtr = _lib.get_target_names(_viewer!!, call.arguments as String, countPtr)
val names = arrPtr.value.getStringArray(0, countPtr.value); val names = arrPtr.getStringArray(0, countPtr.value);
Log.v(TAG, "Got target names $names") for(i in 0..countPtr.value-1) {
Log.v(TAG, "Got target names ${names[i]} ${names[i].length}")
}
val namesAsList = names.toCollection(ArrayList()) val namesAsList = names.toCollection(ArrayList())
_lib.free_pointer(arrPtr, countPtr.getValue()) _lib.free_pointer(arrPtr, countPtr.getValue())
Log.v(TAG, "Free complete") result.success(namesAsList)
}
"getAnimationNames" -> {
if(_viewer == null)
return;
val countPtr = IntByReference();
val arrPtr = _lib.get_animation_names(_viewer!!, countPtr)
val names = arrPtr.getStringArray(0, countPtr.value);
for(i in 0..countPtr.value-1) {
Log.v(TAG, "Got animation names ${names[i]} ${names[i].length}")
}
val namesAsList = names.toCollection(ArrayList())
_lib.free_pointer(arrPtr, 1)
result.success(namesAsList) result.success(namesAsList)
} }
@@ -282,6 +301,13 @@ PlatformView {
_lib.release_source_assets(_viewer!!) _lib.release_source_assets(_viewer!!)
result.success("OK"); result.success("OK");
} }
"playAnimation" -> {
_lib.play_animation(_viewer!!, call.arguments as Int)
result.success("OK")
}
else -> {
result.notImplemented()
}
} }
} }

View File

@@ -73,16 +73,19 @@ using namespace gltfio;
using namespace utils; using namespace utils;
using namespace std::chrono; using namespace std::chrono;
namespace gltfio { namespace gltfio
{
MaterialProvider *createUbershaderLoader(filament::Engine *engine); MaterialProvider *createUbershaderLoader(filament::Engine *engine);
} }
namespace filament { namespace filament
{
class IndirectLight; class IndirectLight;
class LightManager; class LightManager;
} }
namespace gltfio { namespace gltfio
{
MaterialProvider *createGPUMorphShaderLoader( MaterialProvider *createGPUMorphShaderLoader(
const void *opaqueData, const void *opaqueData,
uint64_t opaqueDataSize, uint64_t opaqueDataSize,
@@ -93,7 +96,8 @@ namespace gltfio {
filament::math::quatf *rotation, filament::math::float3 *scale); filament::math::quatf *rotation, filament::math::float3 *scale);
} }
namespace polyvox { namespace polyvox
{
const double kNearPlane = 0.05; // 5 cm const double kNearPlane = 0.05; // 5 cm
const double kFarPlane = 1000.0; // 1 km const double kFarPlane = 1000.0; // 1 km
@@ -103,7 +107,8 @@ const float kShutterSpeed = 1.0f / 125.0f;
const float kSensitivity = 100.0f; const float kSensitivity = 100.0f;
filament::math::mat4f composeMatrix(const filament::math::float3 &translation, filament::math::mat4f composeMatrix(const filament::math::float3 &translation,
const filament::math::quatf& rotation, const filament::math::float3& scale) { const filament::math::quatf &rotation, const filament::math::float3 &scale)
{
float tx = translation[0]; float tx = translation[0];
float ty = translation[1]; float ty = translation[1];
float tz = translation[2]; float tz = translation[2];
@@ -139,7 +144,8 @@ FilamentViewer::FilamentViewer(
_freeResource(freeResource), _freeResource(freeResource),
opaqueShaderResources(nullptr, 0, 0), opaqueShaderResources(nullptr, 0, 0),
fadeShaderResources(nullptr, 0, 0), fadeShaderResources(nullptr, 0, 0),
_assetBuffer(nullptr, 0, 0) { _assetBuffer(nullptr, 0, 0)
{
_engine = Engine::create(Engine::Backend::OPENGL); _engine = Engine::create(Engine::Backend::OPENGL);
_renderer = _engine->createRenderer(); _renderer = _engine->createRenderer();
@@ -165,7 +171,8 @@ FilamentViewer::FilamentViewer(
// options.minScale = filament::math::float2{ minScale }; // options.minScale = filament::math::float2{ minScale };
// options.maxScale = filament::math::float2{ maxScale }; // options.maxScale = filament::math::float2{ maxScale };
// options.sharpness = sharpness; // options.sharpness = sharpness;
options.quality = View::QualityLevel::MEDIUM;; options.quality = View::QualityLevel::MEDIUM;
;
_view->setDynamicResolutionOptions(options); _view->setDynamicResolutionOptions(options);
View::MultiSampleAntiAliasingOptions multiSampleAntiAliasingOptions; View::MultiSampleAntiAliasingOptions multiSampleAntiAliasingOptions;
@@ -179,53 +186,59 @@ FilamentViewer::FilamentViewer(
_ncm = new NameComponentManager(em); _ncm = new NameComponentManager(em);
_assetLoader = AssetLoader::create({_engine, _materialProvider, _ncm, &em}); _assetLoader = AssetLoader::create({_engine, _materialProvider, _ncm, &em});
_resourceLoader = new ResourceLoader( _resourceLoader = new ResourceLoader(
{.engine = _engine, .normalizeSkinningWeights = true, .recomputeBoundingBoxes = false}); {.engine = _engine, .normalizeSkinningWeights = true, .recomputeBoundingBoxes = true});
manipulator = manipulator =
Manipulator<float>::Builder().orbitHomePosition(0.0f, 0.0f, 0.0f).targetPosition(0.0f, 0.0f, -4.0f).build(Mode::ORBIT); Manipulator<float>::Builder().orbitHomePosition(0.0f, 0.0f, 0.05f).targetPosition(0.0f, 0.0f, 0.0f).build(Mode::ORBIT);
_asset = nullptr; _asset = nullptr;
} }
FilamentViewer::~FilamentViewer() { FilamentViewer::~FilamentViewer()
{
} }
Renderer* FilamentViewer::getRenderer() { Renderer *FilamentViewer::getRenderer()
{
return _renderer; return _renderer;
} }
void FilamentViewer::createSwapChain(void* surface) { void FilamentViewer::createSwapChain(void *surface)
{
_swapChain = _engine->createSwapChain(surface); _swapChain = _engine->createSwapChain(surface);
// Log("swapchain created."); // Log("swapchain created.");
} }
void FilamentViewer::destroySwapChain() { void FilamentViewer::destroySwapChain()
if(_swapChain) { {
if (_swapChain)
{
_engine->destroy(_swapChain); _engine->destroy(_swapChain);
_swapChain = nullptr; _swapChain = nullptr;
} }
// Log("swapchain destroyed."); // Log("swapchain destroyed.");
} }
void FilamentViewer::applyWeights(float* weights, int count) { void FilamentViewer::applyWeights(float *weights, int count)
{
for (size_t i = 0, c = _asset->getEntityCount(); i != c; ++i) { for (size_t i = 0, c = _asset->getEntityCount(); i != c; ++i)
{
_asset->setMorphWeights( _asset->setMorphWeights(
_asset->getEntities()[i], _asset->getEntities()[i],
weights, weights,
count count);
);
} }
} }
void FilamentViewer::loadResources(string relativeResourcePath) { void FilamentViewer::loadResources(string relativeResourcePath)
{
const char *const *const resourceUris = _asset->getResourceUris(); const char *const *const resourceUris = _asset->getResourceUris();
const size_t resourceUriCount = _asset->getResourceUriCount(); const size_t resourceUriCount = _asset->getResourceUriCount();
Log("Loading %d resources for asset", resourceUriCount); Log("Loading %d resources for asset", resourceUriCount);
for (size_t i = 0; i < resourceUriCount; i++) { for (size_t i = 0; i < resourceUriCount; i++)
{
string uri = relativeResourcePath + string(resourceUris[i]); string uri = relativeResourcePath + string(resourceUris[i]);
ResourceBuffer buf = _loadResource(uri.c_str()); ResourceBuffer buf = _loadResource(uri.c_str());
@@ -244,7 +257,8 @@ void FilamentViewer::loadResources(string relativeResourcePath) {
_resourceLoader->loadResources(_asset); _resourceLoader->loadResources(_asset);
const Entity *entities = _asset->getEntities(); const Entity *entities = _asset->getEntities();
RenderableManager &rm = _engine->getRenderableManager(); RenderableManager &rm = _engine->getRenderableManager();
for(int i =0; i< _asset->getEntityCount(); i++) { for (int i = 0; i < _asset->getEntityCount(); i++)
{
Entity e = entities[i]; Entity e = entities[i];
auto inst = rm.getInstance(e); auto inst = rm.getInstance(e);
rm.setCulling(inst, false); rm.setCulling(inst, false);
@@ -255,19 +269,21 @@ void FilamentViewer::loadResources(string relativeResourcePath) {
_scene->addEntities(_asset->getEntities(), _asset->getEntityCount()); _scene->addEntities(_asset->getEntities(), _asset->getEntityCount());
}; };
void FilamentViewer::releaseSourceAssets() { void FilamentViewer::releaseSourceAssets()
{
Log("Releasing source data"); Log("Releasing source data");
_asset->releaseSourceData(); _asset->releaseSourceData();
// _freeResource(opaqueShaderResources); // _freeResource(opaqueShaderResources);
// _freeResource(fadeShaderResources); // _freeResource(fadeShaderResources);
} }
void FilamentViewer::loadGlb(const char *const uri)
void FilamentViewer::loadGlb(const char* const uri) { {
Log("Loading GLB at URI %s", uri); Log("Loading GLB at URI %s", uri);
if(_asset) { if (_asset)
{
_asset->releaseSourceData(); _asset->releaseSourceData();
_resourceLoader->evictResourceData(); _resourceLoader->evictResourceData();
_scene->removeEntities(_asset->getEntities(), _asset->getEntityCount()); _scene->removeEntities(_asset->getEntities(), _asset->getEntityCount());
@@ -281,7 +297,8 @@ void FilamentViewer::loadGlb(const char* const uri) {
_asset = _assetLoader->createAssetFromBinary( _asset = _assetLoader->createAssetFromBinary(
(const uint8_t *)rbuf.data, rbuf.size); (const uint8_t *)rbuf.data, rbuf.size);
if (!_asset) { if (!_asset)
{
Log("Unknown error loading GLB asset."); Log("Unknown error loading GLB asset.");
exit(1); exit(1);
} }
@@ -296,7 +313,8 @@ void FilamentViewer::loadGlb(const char* const uri) {
const Entity *entities = _asset->getEntities(); const Entity *entities = _asset->getEntities();
RenderableManager &rm = _engine->getRenderableManager(); RenderableManager &rm = _engine->getRenderableManager();
for(int i =0; i< _asset->getEntityCount(); i++) { for (int i = 0; i < _asset->getEntityCount(); i++)
{
Entity e = entities[i]; Entity e = entities[i];
auto inst = rm.getInstance(e); auto inst = rm.getInstance(e);
rm.setCulling(inst, false); rm.setCulling(inst, false);
@@ -304,14 +322,18 @@ void FilamentViewer::loadGlb(const char* const uri) {
_freeResource(rbuf); _freeResource(rbuf);
_animator->updateBoneMatrices();
Log("Successfully loaded GLB."); Log("Successfully loaded GLB.");
} }
void FilamentViewer::loadGltf(const char* const uri, const char* const relativeResourcePath) { void FilamentViewer::loadGltf(const char *const uri, const char *const relativeResourcePath)
{
Log("Loading GLTF at URI %s", uri); Log("Loading GLTF at URI %s", uri);
if(_asset) { if (_asset)
{
Log("Asset already exists"); Log("Asset already exists");
_resourceLoader->evictResourceData(); _resourceLoader->evictResourceData();
_scene->removeEntities(_asset->getEntities(), _asset->getEntityCount()); _scene->removeEntities(_asset->getEntities(), _asset->getEntityCount());
@@ -328,7 +350,8 @@ void FilamentViewer::loadGltf(const char* const uri, const char* const relativeR
_asset = _assetLoader->createAssetFromJson((uint8_t *)_assetBuffer.data, _assetBuffer.size); _asset = _assetLoader->createAssetFromJson((uint8_t *)_assetBuffer.data, _assetBuffer.size);
Log("Created asset from JSON"); Log("Created asset from JSON");
if (!_asset) { if (!_asset)
{
Log("Unable to parse asset"); Log("Unable to parse asset");
exit(1); exit(1);
} }
@@ -340,36 +363,35 @@ void FilamentViewer::loadGltf(const char* const uri, const char* const relativeR
Log("Load complete for GLTF at URI %s", uri); Log("Load complete for GLTF at URI %s", uri);
// transformToUnitCube(); // transformToUnitCube();
} }
bool FilamentViewer::setCamera(const char* cameraName) { ///
/// Sets the active camera to the GLTF camera specified by [name].
/// Blender export arranges cameras as follows
/// - parent node with global (?) matrix
/// --- child node with "camera" property set to camera node name
/// - camera node
/// We therefore find the first node where the "camera" property is equal to the requested name,
/// then use the parent transform matrix.
///
bool FilamentViewer::setCamera(const char *cameraName)
{
FFilamentAsset *asset = (FFilamentAsset *)_asset; FFilamentAsset *asset = (FFilamentAsset *)_asset;
gltfio::NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap gltfio::NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap
: asset->mNodeMap; : asset->mNodeMap;
Log("Setting camera to %s", cameraName); Log("Setting camera to node %s", cameraName);
for (auto pair : sourceNodes) { for (auto pair : sourceNodes)
{
cgltf_node const *node = pair.first; cgltf_node const *node = pair.first;
if(!node->camera) { if (strcmp(cameraName, node->name) != 0)
if(node->name) { {
Log("No camera found under node %s", node->name);
} else {
Log("No camera found under unnamed node.");
}
continue; continue;
} }
Log("Found camera under node %s", node->name); Log("Node %s : Matrix : %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f %03f Translation : %03f %03f %03f Rotation %03f %03f %03f %03f Scale %03f %03f %03f",
node->name,
if(node->camera->name) {
Log("Checking camera : %s", node->camera->name);
}
if(strcmp(cameraName, node->camera->name) == 0) {
Log("Found camera.");
filament::math::mat4 mat(
node->matrix[0], node->matrix[0],
node->matrix[1], node->matrix[1],
node->matrix[2], node->matrix[2],
@@ -382,18 +404,46 @@ bool FilamentViewer::setCamera(const char* cameraName) {
node->matrix[9], node->matrix[9],
node->matrix[10], node->matrix[10],
node->matrix[11], node->matrix[11],
node->parent->translation[0], node->matrix[12],
node->parent->translation[1], node->matrix[13],
node->parent->translation[2], node->matrix[14],
1 node->matrix[15],
node->translation[0],
node->translation[1],
node->translation[2],
node->rotation[0],
node->rotation[1],
node->rotation[2],
node->rotation[3],
node->scale[0],
node->scale[1],
node->scale[2]
); );
mat4f t = mat4f::translation(float3 { node->translation[0],node->translation[1],node->translation[2] });
mat4f r { quatf { node->rotation[3], node->rotation[0], node->rotation[1], node->rotation[2] } };
mat4f transform = t * r;
quatf rot1(node->parent->rotation[0],node->parent->rotation[1], node->parent->rotation[2], node->parent->rotation[3]); if (!node->camera)
quatf rot2(node->rotation[0],node->rotation[1], node->rotation[2], node->rotation[3]); {
quatf rot3 = rot1 * rot2; cgltf_node* leaf = node->children[0];
filament::math::mat4 rotm(rot3);
filament::math::mat4 result = mat * rotm; Log("Child 1 trans : %03f %03f %03f rot : %03f %03f %03f %03f ", leaf->translation[0], leaf->translation[1],leaf->translation[2], leaf->rotation[0],leaf->rotation[1],leaf->rotation[2],leaf->rotation[3]);
if (!leaf->camera) {
leaf = leaf->children[0];
Log("Child 2 %03f %03f %03f %03f %03f %03f %03f ", leaf->translation[0], leaf->translation[1],leaf->translation[2], leaf->rotation[0],leaf->rotation[1],leaf->rotation[2],leaf->rotation[3]);
if (!leaf->camera) {
Log("Could not find GLTF camera under node or its ssecond or third child nodes.");
exit(-1);
}
}
Log("Using rotation from leaf node.");
mat4f child_rot { quatf { leaf->rotation[3], leaf->rotation[0], leaf->rotation[1], leaf->rotation[2] } };
transform *= child_rot;
}
Entity cameraEntity = EntityManager::get().create(); Entity cameraEntity = EntityManager::get().create();
Camera *cam = _engine->createCamera(cameraEntity); Camera *cam = _engine->createCamera(cameraEntity);
@@ -402,39 +452,66 @@ bool FilamentViewer::setCamera(const char* cameraName) {
const double aspect = (double)vp.width / vp.height; const double aspect = (double)vp.width / vp.height;
// todo - pull focal length from gltf node
cam->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane); cam->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane);
if(!cam) { if (!cam)
{
Log("Couldn't create camera"); Log("Couldn't create camera");
} else { }
else
{
_engine->getTransformManager().setTransform( _engine->getTransformManager().setTransform(
_engine->getTransformManager().getInstance(cameraEntity), result); _engine->getTransformManager().getInstance(cameraEntity), transform
);
_view->setCamera(cam); _view->setCamera(cam);
return true; return true;
} }
} }
}
return false; return false;
} }
StringList FilamentViewer::getTargetNames(const char* meshName) { unique_ptr<vector<string>> FilamentViewer::getAnimationNames()
{
size_t count = _animator->getAnimationCount();
Log("Found %d animations in asset.", count);
unique_ptr<vector<string>> names = make_unique<vector<string>>();
for (size_t i = 0; i < count; i++)
{
names->push_back(_animator->getAnimationName(i));
}
return names;
}
StringList FilamentViewer::getTargetNames(const char *meshName)
{
FFilamentAsset *asset = (FFilamentAsset *)_asset; FFilamentAsset *asset = (FFilamentAsset *)_asset;
NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap : asset->mNodeMap; NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap : asset->mNodeMap;
if(sourceNodes.empty()) { if (sourceNodes.empty())
{
Log("Asset source nodes empty?"); Log("Asset source nodes empty?");
return StringList(nullptr, 0); return StringList(nullptr, 0);
} }
Log("Fetching morph target names for mesh %s", meshName); Log("Fetching morph target names for mesh %s", meshName);
for (auto pair : sourceNodes) { for (auto pair : sourceNodes)
{
cgltf_node const *node = pair.first; cgltf_node const *node = pair.first;
cgltf_mesh const *mesh = node->mesh; cgltf_mesh const *mesh = node->mesh;
if (mesh) { if (mesh)
{
Log("Mesh : %s ", mesh->name); Log("Mesh : %s ", mesh->name);
if(strcmp(meshName, mesh->name) == 0) { if (strcmp(meshName, mesh->name) == 0)
{
return StringList((const char **)mesh->target_names, (int)mesh->target_names_count); return StringList((const char **)mesh->target_names, (int)mesh->target_names_count);
} }
} }
@@ -442,8 +519,8 @@ StringList FilamentViewer::getTargetNames(const char* meshName) {
return StringList(nullptr, 0); return StringList(nullptr, 0);
} }
void FilamentViewer::loadSkybox(const char *const skyboxPath, const char *const iblPath, AAssetManager *am)
void FilamentViewer::loadSkybox(const char* const skyboxPath, const char* const iblPath, AAssetManager* am) { {
ResourceBuffer skyboxBuffer = _loadResource(skyboxPath); ResourceBuffer skyboxBuffer = _loadResource(skyboxPath);
@@ -485,11 +562,12 @@ void FilamentViewer::loadSkybox(const char* const skyboxPath, const char* const
_scene->addEntity(_sun); _scene->addEntity(_sun);
Log("Skybox/IBL load complete."); Log("Skybox/IBL load complete.");
} }
void FilamentViewer::transformToUnitCube() { void FilamentViewer::transformToUnitCube()
if (!_asset) { {
if (!_asset)
{
Log("No asset, cannot transform."); Log("No asset, cannot transform.");
return; return;
} }
@@ -503,7 +581,8 @@ void FilamentViewer::transformToUnitCube() {
tm.setTransform(tm.getInstance(_asset->getRoot()), transform); tm.setTransform(tm.getInstance(_asset->getRoot()), transform);
} }
void FilamentViewer::cleanup() { void FilamentViewer::cleanup()
{
_resourceLoader->asyncCancelLoad(); _resourceLoader->asyncCancelLoad();
_assetLoader->destroyAsset(_asset); _assetLoader->destroyAsset(_asset);
_materialProvider->destroyMaterials(); _materialProvider->destroyMaterials();
@@ -511,30 +590,39 @@ void FilamentViewer::cleanup() {
_freeResource(_assetBuffer); _freeResource(_assetBuffer);
}; };
void FilamentViewer::render() { void FilamentViewer::render()
if (!_view || !_mainCamera || !_swapChain) { {
if (!_view || !_mainCamera || !_swapChain)
{
Log("Not ready for rendering"); Log("Not ready for rendering");
return; return;
} }
if(morphAnimationBuffer) { if (morphAnimationBuffer)
{
updateMorphAnimation(); updateMorphAnimation();
} }
if(embeddedAnimationBuffer) {
updateEmbeddedAnimation();
}
math::float3 eye, target, upward; math::float3 eye, target, upward;
manipulator->getLookAt(&eye, &target, &upward); manipulator->getLookAt(&eye, &target, &upward);
_mainCamera->lookAt(eye, target, upward); _mainCamera->lookAt(eye, target, upward);
// Render the scene, unless the renderer wants to skip the frame. // Render the scene, unless the renderer wants to skip the frame.
if (_renderer->beginFrame(_swapChain)) { if (_renderer->beginFrame(_swapChain))
{
_renderer->render(_view); _renderer->render(_view);
_renderer->endFrame(); _renderer->endFrame();
} }
} }
void FilamentViewer::updateViewportAndCameraProjection(int width, int height, float contentScaleFactor)
void FilamentViewer::updateViewportAndCameraProjection(int width, int height, float contentScaleFactor) { {
if (!_view || !_mainCamera) { if (!_view || !_mainCamera)
{
Log("Skipping camera update, no view or camrea"); Log("Skipping camera update, no view or camrea");
return; return;
} }
@@ -548,56 +636,63 @@ void FilamentViewer::updateViewportAndCameraProjection(int width, int height, fl
Log("Set viewport to %d %d", _width, _height); Log("Set viewport to %d %d", _width, _height);
} }
void FilamentViewer::animateWeights(float* data, int numWeights, int numFrames, float frameRate) { void FilamentViewer::animateWeights(float *data, int numWeights, int numFrames, float frameRate)
{
morphAnimationBuffer = std::make_unique<MorphAnimationBuffer>(data, numWeights, numFrames, 1000 / frameRate); morphAnimationBuffer = std::make_unique<MorphAnimationBuffer>(data, numWeights, numFrames, 1000 / frameRate);
} }
void FilamentViewer::updateMorphAnimation() { void FilamentViewer::updateMorphAnimation()
{
if(morphAnimationBuffer->frameIndex >= morphAnimationBuffer->numFrames) { if (morphAnimationBuffer->frameIndex >= morphAnimationBuffer->numFrames)
{
morphAnimationBuffer = nullptr; morphAnimationBuffer = nullptr;
return; return;
} }
if(morphAnimationBuffer->frameIndex == -1) { if (morphAnimationBuffer->frameIndex == -1)
{
morphAnimationBuffer->frameIndex++; morphAnimationBuffer->frameIndex++;
morphAnimationBuffer->startTime = std::chrono::high_resolution_clock::now(); morphAnimationBuffer->startTime = std::chrono::high_resolution_clock::now();
applyWeights(morphAnimationBuffer->frameData, morphAnimationBuffer->numWeights); applyWeights(morphAnimationBuffer->frameData, morphAnimationBuffer->numWeights);
} else { }
else
{
std::chrono::duration<double, std::milli> dur = std::chrono::high_resolution_clock::now() - morphAnimationBuffer->startTime; std::chrono::duration<double, std::milli> dur = std::chrono::high_resolution_clock::now() - morphAnimationBuffer->startTime;
int frameIndex = dur.count() / morphAnimationBuffer->frameLength; int frameIndex = dur.count() / morphAnimationBuffer->frameLength;
if(frameIndex != morphAnimationBuffer->frameIndex) { if (frameIndex != morphAnimationBuffer->frameIndex)
{
morphAnimationBuffer->frameIndex = frameIndex; morphAnimationBuffer->frameIndex = frameIndex;
applyWeights(morphAnimationBuffer->frameData + (morphAnimationBuffer->frameIndex * morphAnimationBuffer->numWeights), morphAnimationBuffer->numWeights); applyWeights(morphAnimationBuffer->frameData + (morphAnimationBuffer->frameIndex * morphAnimationBuffer->numWeights), morphAnimationBuffer->numWeights);
} }
} }
}
void FilamentViewer::playAnimation(int index) {
embeddedAnimationBuffer = make_unique<EmbeddedAnimationBuffer>(index, _animator->getAnimationDuration(index));
}
void FilamentViewer::updateEmbeddedAnimation() {
duration<double> dur = duration_cast<duration<double>>(std::chrono::high_resolution_clock::now() - embeddedAnimationBuffer->lastTime);
float startTime = 0;
if(!embeddedAnimationBuffer->hasStarted) {
embeddedAnimationBuffer->hasStarted = true;
embeddedAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
} else if(dur.count() >= embeddedAnimationBuffer->duration) {
embeddedAnimationBuffer = nullptr;
return;
} else {
startTime = dur.count();
}
_animator->applyAnimation(embeddedAnimationBuffer->animationIndex, startTime);
_animator->updateBoneMatrices();
}
} }
}
// void FilamentViewer::updateEmbeddedAnimation() {
// duration<double> dur = duration_cast<duration<double>>(std::chrono::high_resolution_clock::now() - embeddedAnimationBuffer->lastTime);
// float startTime = 0;
// if(!embeddedAnimationBuffer->hasStarted) {
// embeddedAnimationBuffer->hasStarted = true;
// embeddedAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
// } else if(dur.count() >= embeddedAnimationBuffer->duration) {
// embeddedAnimationBuffer = nullptr;
// return;
// } else {
// startTime = dur.count();
// }
// _animator->applyAnimation(embeddedAnimationBuffer->animationIndex, startTime);
// _animator->updateBoneMatrices();
// }
// // // //
// //if(morphAnimationBuffer.frameIndex >= morphAnimationBuffer.numFrames) { // //if(morphAnimationBuffer.frameIndex >= morphAnimationBuffer.numFrames) {
// // this.morphAnimationBuffer = null; // // this.morphAnimationBuffer = null;
@@ -617,9 +712,6 @@ void FilamentViewer::updateMorphAnimation() {
// // morphAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now(); // // morphAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
// // } // // }
// //} // //}
// void FilamentViewer::playAnimation(int index) {
// embeddedAnimationBuffer = make_unique<EmbeddedAnimationBuffer>(index, _animator->getAnimationDuration(index));
// }
// void FilamentViewer::createMorpher(const char* meshName, int* primitives, int numPrimitives) { // void FilamentViewer::createMorpher(const char* meshName, int* primitives, int numPrimitives) {

View File

@@ -54,6 +54,16 @@ namespace polyvox {
const int count; const int count;
}; };
struct EmbeddedAnimationBuffer {
EmbeddedAnimationBuffer(int animationIndex, float duration) : animationIndex(animationIndex), duration(duration) {}
bool hasStarted = false;
int animationIndex;
float duration = 0;
time_point_t lastTime;
};
struct ResourceBuffer { struct ResourceBuffer {
ResourceBuffer(const void* data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {}; ResourceBuffer(const void* data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
@@ -102,6 +112,7 @@ namespace polyvox {
// void createMorpher(const char* meshName, int* primitives, int numPrimitives); // void createMorpher(const char* meshName, int* primitives, int numPrimitives);
void releaseSourceAssets(); void releaseSourceAssets();
StringList getTargetNames(const char* meshName); StringList getTargetNames(const char* meshName);
unique_ptr<vector<string>> getAnimationNames();
Manipulator<float>* manipulator; Manipulator<float>* manipulator;
void applyWeights(float* weights, int count); void applyWeights(float* weights, int count);
void animateWeights(float* data, int numWeights, int length, float frameRate); void animateWeights(float* data, int numWeights, int length, float frameRate);
@@ -158,27 +169,16 @@ namespace polyvox {
float _cameraFocalLength = 0.0f; float _cameraFocalLength = 0.0f;
void updateMorphAnimation(); void updateMorphAnimation();
// void updateEmbeddedAnimation(); void updateEmbeddedAnimation();
// animation flags; // animation flags;
bool isAnimating; bool isAnimating;
unique_ptr<MorphAnimationBuffer> morphAnimationBuffer; unique_ptr<MorphAnimationBuffer> morphAnimationBuffer;
// unique_ptr<EmbeddedAnimationBuffer> embeddedAnimationBuffer; unique_ptr<EmbeddedAnimationBuffer> embeddedAnimationBuffer;
}; };
} }
// struct EmbeddedAnimationBuffer {
// EmbeddedAnimationBuffer(int animationIndex, float duration) : animationIndex(animationIndex), duration(duration) {}
// bool hasStarted = false;
// int animationIndex;
// float duration = 0;
// time_point_t lastTime;
// };

View File

@@ -15,6 +15,7 @@ abstract class FilamentController {
Future rotateEnd(); Future rotateEnd();
Future applyWeights(List<double> weights); Future applyWeights(List<double> weights);
Future<List<String>> getTargetNames(String meshName); Future<List<String>> getTargetNames(String meshName);
Future<List<String>> getAnimationNames();
Future releaseSourceAssets(); Future releaseSourceAssets();
Future playAnimation(int index); Future playAnimation(int index);
Future setCamera(String name); Future setCamera(String name);
@@ -102,6 +103,12 @@ class PolyvoxFilamentController extends FilamentController {
return result; return result;
} }
Future<List<String>> getAnimationNames() async {
var result = (await _channel.invokeMethod("getAnimationNames"))
.cast<String>();
return result;
}
Future animate(List<double> weights, int numWeights, double frameRate) async { Future animate(List<double> weights, int numWeights, double frameRate) async {
await _channel await _channel
.invokeMethod("animateWeights", [weights, numWeights, frameRate]); .invokeMethod("animateWeights", [weights, numWeights, frameRate]);