From 72fc692305a4355b4e0c675da4803822b7a44e4d Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sat, 16 Apr 2022 23:04:33 +0800 Subject: [PATCH] replace calls to private Filament code with public API for morph target/animation names & cameras --- example/lib/main.dart | 44 +++++++++--- ios/src/FilamentViewer.cpp | 134 ++++++++++++------------------------- ios/src/FilamentViewer.hpp | 8 +-- 3 files changed, 79 insertions(+), 107 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 56d1ba7b..ccb12e57 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -79,7 +79,7 @@ class _MyAppState extends State { }), ElevatedButton( onPressed: () => - _filamentController.playAnimation(0, loop:_loop), + _filamentController.playAnimation(0, loop: _loop), child: const Text('play animation')), Checkbox( onChanged: (_) => setState(() { @@ -98,7 +98,7 @@ class _MyAppState extends State { child: const Text('zoom out')), ElevatedButton( onPressed: () { - _filamentController.setCamera("Camera"); + _filamentController.setCamera("Camera_Orientation"); }, child: const Text('set camera')), ElevatedButton( @@ -121,7 +121,7 @@ class _MyAppState extends State { builder: (innerCtx) => ElevatedButton( onPressed: () async { final names = await _filamentController - .getTargetNames("Cube.001"); + .getTargetNames("Cube"); await showDialog( builder: (ctx) { @@ -145,6 +145,34 @@ class _MyAppState extends State { context: innerCtx); }, child: const Text('get target names'))), + Builder( + builder: (innerCtx) => ElevatedButton( + onPressed: () async { + final names = + await _filamentController.getAnimationNames(); + + await showDialog( + builder: (ctx) { + return Container( + color: Colors.white, + height: 200, + width: 200, + child: Column( + mainAxisSize: MainAxisSize.min, + children: names + .map((name) => Text(name)) + .cast() + .toList() + + [ + ElevatedButton( + onPressed: () => + Navigator.of(ctx).pop(), + child: Text("Close")) + ])); + }, + context: innerCtx); + }, + child: const Text('get animation names'))), ElevatedButton( onPressed: () async { await _filamentController.panStart(1, 1); @@ -211,8 +239,8 @@ class _MyAppState extends State { // .values // .toList(), // ) - // ElevatedButton( - // child: const Text('init'), - // onPressed: () async { - // await _filamentController.initialize(); - // }), \ No newline at end of file +// ElevatedButton( +// child: const Text('init'), +// onPressed: () async { +// await _filamentController.initialize(); +// }), diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index f3636a34..3963b40f 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -351,99 +351,32 @@ namespace polyvox /// bool FilamentViewer::setCamera(const char *cameraName) { + size_t count = _asset->getCameraEntityCount(); + if(count == 0) + return false; - // gltfio::NodeMap &sourceNodes = _asset->isInstanced() ? asset->mInstances[0]->nodeMap - // : asset->mNodeMap; - // Log("Setting camera to node %s", cameraName); - // for (auto pair : sourceNodes) - // { - // cgltf_node const *node = pair.first; + const utils::Entity* cameras = _asset->getCameraEntities(); + Log("Found %d cameras in asset", count); + for(int i=0; i < count; i++) { + + auto inst = _ncm->getInstance(cameras[i]); + const char* name = _ncm->getName(inst); + Log("Camera %d : %s", i, name); + if (strcmp(name, cameraName) == 0) { - // if (strcmp(cameraName, node->name) != 0) - // { - // continue; - // } + Camera* camera = _engine->createCamera(cameras[i]); + const Viewport &vp = _view->getViewport(); - // 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, - // node->matrix[0], - // node->matrix[1], - // node->matrix[2], - // node->matrix[3], - // node->matrix[4], - // node->matrix[5], - // node->matrix[6], - // node->matrix[7], - // node->matrix[8], - // node->matrix[9], - // node->matrix[10], - // node->matrix[11], - // node->matrix[12], - // node->matrix[13], - // node->matrix[14], - // 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; + const double aspect = (double)vp.width / vp.height; - // if (!node->camera) - // { - // cgltf_node* leaf = node->children[0]; + // todo - pull focal length from gltf node - // 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(); - // Camera *cam = _engine->createCamera(cameraEntity); - - // const Viewport &vp = _view->getViewport(); - - // const double aspect = (double)vp.width / vp.height; - - // // todo - pull focal length from gltf node - - // cam->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane); - - // if (!cam) - // { - // Log("Couldn't create camera"); - // } - // else - // { - // _engine->getTransformManager().setTransform( - // _engine->getTransformManager().getInstance(cameraEntity), transform - // ); - - // _view->setCamera(cam); - // return true; - // } - // } + camera->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane); + _view->setCamera(camera); + } + return true; + } + Log("Unable to locate camera under name %s ", cameraName); return false; } @@ -464,11 +397,28 @@ namespace polyvox return names; } - StringList FilamentViewer::getTargetNames(const char *meshName) + unique_ptr> FilamentViewer::getTargetNames(const char *meshName) { - // int count = asset->getMorphTargetCountAt() - // asset->getMorphTargetNameAt() - return StringList(nullptr, 0); + Log("Retrieving morph target names for mesh %s", meshName); + unique_ptr> names = make_unique>(); + const Entity *entities = _asset->getEntities(); + RenderableManager &rm = _engine->getRenderableManager(); + for (int i = 0; i < _asset->getEntityCount(); i++) + { + Entity e = entities[i]; + auto inst = _ncm->getInstance(e); + const char* name = _ncm->getName(inst); + Log("Got entity instance name %s", name); + if(strcmp(name, meshName) == 0) { + size_t count = _asset->getMorphTargetCountAt(e); + for(int j=0; j< count; j++) { + const char* morphName = _asset->getMorphTargetNameAt(e, j); + names->push_back(morphName); + } + break; + } + } + return names; } void FilamentViewer::loadSkybox(const char *const skyboxPath, const char *const iblPath) diff --git a/ios/src/FilamentViewer.hpp b/ios/src/FilamentViewer.hpp index 34b3b7dc..5d48a1f6 100644 --- a/ios/src/FilamentViewer.hpp +++ b/ios/src/FilamentViewer.hpp @@ -42,12 +42,6 @@ namespace polyvox { typedef std::chrono::time_point time_point_t; - struct StringList { - StringList(const char** strings, const int count) : strings(strings), count(count) {}; - const char** strings; - const int count; - }; - struct EmbeddedAnimationBuffer { EmbeddedAnimationBuffer(int animationIndex, float duration, bool loop) : animationIndex(animationIndex), duration(duration), loop(loop) {} bool hasStarted = false; @@ -105,7 +99,7 @@ namespace polyvox { void updateViewportAndCameraProjection(int height, int width, float scaleFactor); void render(); void releaseSourceAssets(); - StringList getTargetNames(const char* meshName); + unique_ptr> getTargetNames(const char* meshName); unique_ptr> getAnimationNames(); Manipulator* manipulator; void applyWeights(float* weights, int count);