diff --git a/android/src/main/cpp/filament_api.cpp b/android/src/main/cpp/filament_api.cpp index 54a91b0f..89f1b322 100644 --- a/android/src/main/cpp/filament_api.cpp +++ b/android/src/main/cpp/filament_api.cpp @@ -49,8 +49,8 @@ extern "C" { ((FilamentViewer*)viewer)->loadGltf(assetPath, relativePath); } - void set_camera(void* viewer, const char* nodeName) { - ((FilamentViewer*)viewer)->setCamera(nodeName); + bool set_camera(void* viewer, const char* nodeName) { + return ((FilamentViewer*)viewer)->setCamera(nodeName); } void* filament_viewer_new( @@ -133,10 +133,12 @@ extern "C" { } } - void free_pointer(void** ptr, int size) { + void free_pointer(char*** ptr, int size) { + __android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Freeing %d char pointers", size); for(int i = 0; i < size; i++) { - free(ptr[i]); + free((*ptr)[i]); } + free(*ptr); } void release_source_assets(void* viewer) { diff --git a/example/assets/cube.bin b/example/assets/cube.bin index 830b692b..ff00a72c 100644 Binary files a/example/assets/cube.bin and b/example/assets/cube.bin differ diff --git a/example/assets/cube.gltf b/example/assets/cube.gltf index d0d00f82..6ba696b9 100644 --- a/example/assets/cube.gltf +++ b/example/assets/cube.gltf @@ -1,6 +1,6 @@ { "asset" : { - "generator" : "Khronos glTF Blender I/O v1.6.16", + "generator" : "Khronos glTF Blender I/O v1.4.40", "version" : "2.0" }, "scene" : 0, @@ -10,7 +10,6 @@ "nodes" : [ 0, 1, - 2, 3 ] } @@ -20,34 +19,6 @@ "mesh" : 0, "name" : "Cube" }, - { - "name" : "Light", - "rotation" : [ - 0.16907575726509094, - 0.7558803558349609, - -0.27217137813568115, - 0.570947527885437 - ], - "translation" : [ - 4.076245307922363, - 5.903861999511719, - -1.0054539442062378 - ] - }, - { - "name" : "Camera", - "rotation" : [ - 0.483536034822464, - 0.33687159419059753, - -0.20870360732078552, - 0.7804827094078064 - ], - "translation" : [ - 7.358891487121582, - 4.958309173583984, - 6.925790786743164 - ] - }, { "mesh" : 1, "name" : "Cone", @@ -56,12 +27,51 @@ 0, -4.3612141609191895 ] + }, + { + "camera" : 0, + "name" : "Camera_Orientation", + "rotation" : [ + -0.7071067690849304, + 0, + 0, + 0.7071067690849304 + ] + }, + { + "children" : [ + 2 + ], + "name" : "Camera", + "rotation" : [ + 0.5797627568244934, + 0.0033576595596969128, + -0.013425502926111221, + 0.8146679401397705 + ], + "translation" : [ + 0, + 6.8069868087768555, + 10.594138145446777 + ] + } + ], + "cameras" : [ + { + "name" : "Camera.001", + "perspective" : { + "aspectRatio" : 1.7777777777777777, + "yfov" : 0.39959652046304894, + "zfar" : 1000, + "znear" : 0.10000000149011612 + }, + "type" : "perspective" } ], "materials" : [ { "doubleSided" : true, - "name" : "Material", + "name" : "Material.001", "pbrMetallicRoughness" : { "baseColorFactor" : [ 0.800000011920929, @@ -88,7 +98,7 @@ "Key 8" ] }, - "name" : "Cube", + "name" : "Cube.001", "primitives" : [ { "attributes" : { @@ -207,7 +217,7 @@ { "bufferView" : 0, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 1, 1, @@ -223,13 +233,13 @@ { "bufferView" : 1, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 2, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC2" }, { @@ -241,7 +251,7 @@ { "bufferView" : 4, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 0.14205700159072876, 0.36317670345306396, @@ -257,13 +267,13 @@ { "bufferView" : 5, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 6, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 0.14205700159072876, 0.6298741102218628, @@ -279,13 +289,13 @@ { "bufferView" : 7, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 8, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 1.1911587715148926, 0.7055504322052002, @@ -301,13 +311,13 @@ { "bufferView" : 9, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 10, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 1.0930120944976807, 0.12705659866333008, @@ -323,13 +333,13 @@ { "bufferView" : 11, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 12, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 1.0931031703948975, 7.137829303741455, @@ -345,13 +355,13 @@ { "bufferView" : 13, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 14, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 0.7663934230804443, 0.12705659866333008, @@ -367,13 +377,13 @@ { "bufferView" : 15, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 16, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 1.0939359664916992, 0.12705659866333008, @@ -389,13 +399,13 @@ { "bufferView" : 17, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 18, "componentType" : 5126, - "count" : 264, + "count" : 276, "max" : [ 0.9689376950263977, 0.12705659866333008, @@ -411,13 +421,13 @@ { "bufferView" : 19, "componentType" : 5126, - "count" : 264, + "count" : 276, "type" : "VEC3" }, { "bufferView" : 20, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 1, 1, @@ -433,13 +443,13 @@ { "bufferView" : 21, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" }, { "bufferView" : 22, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC2" }, { @@ -451,7 +461,7 @@ { "bufferView" : 24, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 0, 0.5201449394226074, @@ -467,13 +477,13 @@ { "bufferView" : 25, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" }, { "bufferView" : 26, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 5.200448989868164, 0, @@ -489,13 +499,13 @@ { "bufferView" : 27, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" }, { "bufferView" : 28, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 0, 0, @@ -511,13 +521,13 @@ { "bufferView" : 29, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" }, { "bufferView" : 30, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 0, 0, @@ -533,13 +543,13 @@ { "bufferView" : 31, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" }, { "bufferView" : 32, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 0, 0, @@ -555,13 +565,13 @@ { "bufferView" : 33, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" }, { "bufferView" : 34, "componentType" : 5126, - "count" : 128, + "count" : 148, "max" : [ 0, 0, @@ -577,195 +587,195 @@ { "bufferView" : 35, "componentType" : 5126, - "count" : 128, + "count" : 148, "type" : "VEC3" } ], "bufferViews" : [ { "buffer" : 0, - "byteLength" : 3168, + "byteLength" : 3312, "byteOffset" : 0 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 3168 + "byteLength" : 3312, + "byteOffset" : 3312 }, { "buffer" : 0, - "byteLength" : 2112, - "byteOffset" : 6336 + "byteLength" : 2208, + "byteOffset" : 6624 }, { "buffer" : 0, "byteLength" : 552, - "byteOffset" : 8448 + "byteOffset" : 8832 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 9000 + "byteLength" : 3312, + "byteOffset" : 9384 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 12168 + "byteLength" : 3312, + "byteOffset" : 12696 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 15336 + "byteLength" : 3312, + "byteOffset" : 16008 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 18504 + "byteLength" : 3312, + "byteOffset" : 19320 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 21672 + "byteLength" : 3312, + "byteOffset" : 22632 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 24840 + "byteLength" : 3312, + "byteOffset" : 25944 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 28008 + "byteLength" : 3312, + "byteOffset" : 29256 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 31176 + "byteLength" : 3312, + "byteOffset" : 32568 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 34344 + "byteLength" : 3312, + "byteOffset" : 35880 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 37512 + "byteLength" : 3312, + "byteOffset" : 39192 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 40680 + "byteLength" : 3312, + "byteOffset" : 42504 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 43848 + "byteLength" : 3312, + "byteOffset" : 45816 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 47016 + "byteLength" : 3312, + "byteOffset" : 49128 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 50184 + "byteLength" : 3312, + "byteOffset" : 52440 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 53352 + "byteLength" : 3312, + "byteOffset" : 55752 }, { "buffer" : 0, - "byteLength" : 3168, - "byteOffset" : 56520 + "byteLength" : 3312, + "byteOffset" : 59064 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 59688 + "byteLength" : 1776, + "byteOffset" : 62376 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 61224 + "byteLength" : 1776, + "byteOffset" : 64152 }, { "buffer" : 0, - "byteLength" : 1024, - "byteOffset" : 62760 + "byteLength" : 1184, + "byteOffset" : 65928 }, { "buffer" : 0, "byteLength" : 372, - "byteOffset" : 63784 + "byteOffset" : 67112 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 64156 + "byteLength" : 1776, + "byteOffset" : 67484 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 65692 + "byteLength" : 1776, + "byteOffset" : 69260 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 67228 + "byteLength" : 1776, + "byteOffset" : 71036 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 68764 + "byteLength" : 1776, + "byteOffset" : 72812 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 70300 + "byteLength" : 1776, + "byteOffset" : 74588 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 71836 + "byteLength" : 1776, + "byteOffset" : 76364 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 73372 + "byteLength" : 1776, + "byteOffset" : 78140 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 74908 + "byteLength" : 1776, + "byteOffset" : 79916 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 76444 + "byteLength" : 1776, + "byteOffset" : 81692 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 77980 + "byteLength" : 1776, + "byteOffset" : 83468 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 79516 + "byteLength" : 1776, + "byteOffset" : 85244 }, { "buffer" : 0, - "byteLength" : 1536, - "byteOffset" : 81052 + "byteLength" : 1776, + "byteOffset" : 87020 } ], "buffers" : [ { - "byteLength" : 82588, + "byteLength" : 88796, "uri" : "cube.bin" } ] diff --git a/example/lib/main.dart b/example/lib/main.dart index 703ce136..df82cc4e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -83,15 +83,21 @@ class _MyAppState extends State { _filamentController.zoom(-1.0); }, child: const Text('zoom in')), + ElevatedButton( onPressed: () { _filamentController.zoom(1.0); }, child: const Text('zoom out')), + ElevatedButton( + onPressed: () { + _filamentController.setCamera("Camera.001"); + }, + child: const Text('Set Camera')), Builder(builder:(innerCtx) => ElevatedButton( onPressed: () async { final names = await _filamentController - .getTargetNames("Cube"); + .getTargetNames("Cube.001"); await showDialog( builder: (ctx) { diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index 7dae42ce..32f665dc 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -268,10 +268,10 @@ void FilamentViewer::loadGlb(const char* const uri) { Log("Loading GLB at URI %s", uri); if(_asset) { + _asset->releaseSourceData(); _resourceLoader->evictResourceData(); _scene->removeEntities(_asset->getEntities(), _asset->getEntityCount()); _assetLoader->destroyAsset(_asset); - _freeResource(_assetBuffer); } _asset = nullptr; _animator = nullptr; @@ -339,55 +339,83 @@ void FilamentViewer::loadGltf(const char* const uri, const char* const relativeR Log("Load complete for GLTF at URI %s", uri); - transformToUnitCube(); + // transformToUnitCube(); } -void FilamentViewer::setCamera(const char* cameraName) { +bool FilamentViewer::setCamera(const char* cameraName) { FFilamentAsset* asset = (FFilamentAsset*)_asset; gltfio::NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap : asset->mNodeMap; - + Log("Setting camera to %s", cameraName); for (auto pair : sourceNodes) { cgltf_node const *node = pair.first; - if(node->camera) { - Log("Got camera %s of type %s ", node->camera->name, node->camera->type); + if(!node->camera) { + if(node->name) { + Log("No camera found under node %s", node->name); + } else { + Log("No camera found under unnamed node."); + } + continue; + } - if(strcmp(cameraName, node->camera->name) == 0) { - filament::math::mat4 mat( - 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->parent->translation[0], - node->parent->translation[1], - node->parent->translation[2], - 1 - ); + Log("Found camera under node %s", node->name); - quatf rot1(node->parent->rotation[0],node->parent->rotation[1], node->parent->rotation[2], node->parent->rotation[3]); - quatf rot2(node->rotation[0],node->rotation[1], node->rotation[2], node->rotation[3]); - quatf rot3 = rot1 * rot2; - filament::math::mat4 rotm(rot3); - + 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[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->parent->translation[0], + node->parent->translation[1], + node->parent->translation[2], + 1 + ); + + quatf rot1(node->parent->rotation[0],node->parent->rotation[1], node->parent->rotation[2], node->parent->rotation[3]); + quatf rot2(node->rotation[0],node->rotation[1], node->rotation[2], node->rotation[3]); + quatf rot3 = rot1 * rot2; + filament::math::mat4 rotm(rot3); + filament::math::mat4 result = mat * rotm; + Entity cameraEntity = EntityManager::get().create(); + Camera* cam = _engine->createCamera(cameraEntity); + + const Viewport& vp = _view->getViewport(); + + const double aspect = (double)vp.width / vp.height; + + cam->setLensProjection(_cameraFocalLength, aspect, kNearPlane, kFarPlane); + + if(!cam) { + Log("Couldn't create camera"); + } else { _engine->getTransformManager().setTransform( - _engine->getTransformManager().getInstance(_mainCamera->getEntity()), result); - - } + _engine->getTransformManager().getInstance(cameraEntity), result); + + _view->setCamera(cam); + return true; + } } } + return false; } StringList FilamentViewer::getTargetNames(const char* meshName) { @@ -404,18 +432,12 @@ StringList FilamentViewer::getTargetNames(const char* meshName) { cgltf_node const *node = pair.first; cgltf_mesh const *mesh = node->mesh; - if(node->camera) { - Log("Got camera %s of type %s", node->camera->name, node->camera->type); - } - if (mesh) { Log("Mesh : %s ",mesh->name); if(strcmp(meshName, mesh->name) == 0) { return StringList((const char**)mesh->target_names, (int) mesh->target_names_count); } - } else { - Log("No mesh attached to node"); - } + } } return StringList(nullptr, 0); } @@ -429,7 +451,7 @@ void FilamentViewer::loadSkybox(const char* const skyboxPath, const char* const new image::KtxBundle(static_cast(skyboxBuffer.data), static_cast(skyboxBuffer.size)); _skyboxTexture = image::ktx::createTexture(_engine, skyboxBundle, false); _skybox = filament::Skybox::Builder().environment(_skyboxTexture).build(*_engine); - // _skybox = Skybox::Builder().color({0.1, 0.125, 0.25, 1.0}).build(*_engine); + _scene->setSkybox(_skybox); _freeResource(skyboxBuffer); diff --git a/ios/src/FilamentViewer.hpp b/ios/src/FilamentViewer.hpp index f25bb990..d4f00f82 100644 --- a/ios/src/FilamentViewer.hpp +++ b/ios/src/FilamentViewer.hpp @@ -90,7 +90,7 @@ namespace polyvox { // void animateWeights(float* data, int numWeights, int length, float frameRate); // void animateBones(); void playAnimation(int index); - void setCamera(const char* nodeName); + bool setCamera(const char* nodeName); void destroySwapChain(); void createSwapChain(void* surface); diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index f5da820e..27423482 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -17,6 +17,7 @@ abstract class FilamentController { Future> getTargetNames(String meshName); Future releaseSourceAssets(); Future playAnimation(int index); + Future setCamera(String name); /// /// Set the weights of all morph targets in the mesh to the specified weights at successive frames (where [framerate] is the number of times per second the weights should be updated). @@ -121,4 +122,8 @@ class PolyvoxFilamentController extends FilamentController { Future playAnimation(int index) async { await _channel.invokeMethod("playAnimation", index); } + + Future setCamera(String name) async { + await _channel.invokeMethod("setCamera", name); + } }