replace calls to private Filament code with public API for morph target/animation names & cameras

This commit is contained in:
Nick Fisher
2022-04-16 23:04:33 +08:00
parent 53ab72bcff
commit 72fc692305
3 changed files with 79 additions and 107 deletions

View File

@@ -79,7 +79,7 @@ class _MyAppState extends State<MyApp> {
}),
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<MyApp> {
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<MyApp> {
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<MyApp> {
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<Widget>()
.toList() +
<Widget>[
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<MyApp> {
// .values
// .toList(),
// )
// ElevatedButton(
// child: const Text('init'),
// onPressed: () async {
// await _filamentController.initialize();
// }),
// ElevatedButton(
// child: const Text('init'),
// onPressed: () async {
// await _filamentController.initialize();
// }),

View File

@@ -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<vector<string>> 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<vector<string>> names = make_unique<vector<string>>();
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)

View File

@@ -42,12 +42,6 @@ namespace polyvox {
typedef std::chrono::time_point<std::chrono::high_resolution_clock> 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<vector<string>> getTargetNames(const char* meshName);
unique_ptr<vector<string>> getAnimationNames();
Manipulator<float>* manipulator;
void applyWeights(float* weights, int count);