From e778c097db4c34effb3097376f5f985377e99ab8 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sun, 14 Aug 2022 21:30:58 +1000 Subject: [PATCH] add clearAssets method to viewer --- android/src/main/cpp/filament_android.cpp | 4 ++++ .../app/polyvox/filament/FilamentInterop.kt | 2 ++ .../app/polyvox/filament/FilamentView.kt | 4 ++++ ios/src/FilamentViewer.cpp | 18 +++++++++++++++++- ios/src/FilamentViewer.hpp | 2 ++ ios/src/SceneAsset.cpp | 17 ++++++++++++++++- ios/src/SceneAssetLoader.cpp | 3 ++- lib/filament_controller.dart | 12 ++++++++++++ 8 files changed, 59 insertions(+), 3 deletions(-) diff --git a/android/src/main/cpp/filament_android.cpp b/android/src/main/cpp/filament_android.cpp index 5e718545..d43b4d5d 100644 --- a/android/src/main/cpp/filament_android.cpp +++ b/android/src/main/cpp/filament_android.cpp @@ -194,5 +194,9 @@ extern "C" { void remove_asset(void* viewer, void* asset) { ((FilamentViewer*)viewer)->removeAsset((SceneAsset*)asset); } + + void clear_assets(void* viewer) { + ((FilamentViewer*)viewer)->clearAssets(); + } } diff --git a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt index 5001417e..cab012f7 100644 --- a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt +++ b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt @@ -64,6 +64,8 @@ interface FilamentInterop : Library { fun remove_asset(viewer:Pointer, asset:Pointer); + fun clear_assets(viewer:Pointer); + fun remove_skybox(viewer:Pointer); fun remove_ibl(viewer:Pointer); diff --git a/android/src/main/kotlin/app/polyvox/filament/FilamentView.kt b/android/src/main/kotlin/app/polyvox/filament/FilamentView.kt index 3e1f9c3b..aa9bcb17 100644 --- a/android/src/main/kotlin/app/polyvox/filament/FilamentView.kt +++ b/android/src/main/kotlin/app/polyvox/filament/FilamentView.kt @@ -331,6 +331,10 @@ PlatformView { _lib.remove_asset(_viewer!!, Pointer(call.arguments as Long)) result.success("OK"); } + "clearAssets" -> { + _lib.clear_assets(_viewer!!) + result.success("OK"); + } "playAnimation" -> { val args = call.arguments as ArrayList _lib.play_animation(Pointer(args[0] as Long), args[1] as Int, args[2] as Boolean) diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index 11e4377c..81a01472 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -359,6 +359,7 @@ SceneAsset *FilamentViewer::loadGlb(const char *const uri) { Log("Unknown error loading asset."); } else { _assets.push_back(asset); + Log("GLB loaded, asset at index %d", _assets.size() - 1); } return asset; } @@ -376,10 +377,25 @@ SceneAsset *FilamentViewer::loadGltf(const char *const uri, return asset; } + +void FilamentViewer::clearAssets() { + Log("Clearing all assets"); + mtx.lock(); + int i = 0; + for (auto asset : _assets) { + _sceneAssetLoader->remove(asset); + Log("Cleared asset %d", i); + i++; + } + _assets.clear(); + mtx.unlock(); +} + void FilamentViewer::removeAsset(SceneAsset *asset) { mtx.lock(); _sceneAssetLoader->remove(asset); - _view->setCamera(_mainCamera); + // todo - what if we are using a camera from this asset? + bool erased = false; for (auto it = _assets.begin(); it != _assets.end();) { if (*it == asset) { diff --git a/ios/src/FilamentViewer.hpp b/ios/src/FilamentViewer.hpp index b739a180..4ec61913 100644 --- a/ios/src/FilamentViewer.hpp +++ b/ios/src/FilamentViewer.hpp @@ -57,6 +57,8 @@ namespace polyvox { SceneAsset* loadGlb(const char* const uri); SceneAsset* loadGltf(const char* const uri, const char* relativeResourcePath); void removeAsset(SceneAsset* asset); + // removes all add assets from the current scene + void clearAssets(); void updateViewportAndCameraProjection(int height, int width, float scaleFactor); void render(); diff --git a/ios/src/SceneAsset.cpp b/ios/src/SceneAsset.cpp index 853d915e..41413fca 100644 --- a/ios/src/SceneAsset.cpp +++ b/ios/src/SceneAsset.cpp @@ -31,7 +31,9 @@ SceneAsset::SceneAsset(FilamentAsset *asset, Engine *engine, Log("Created animation buffers for %d", _embeddedAnimationStatus.size()); } -SceneAsset::~SceneAsset() { _asset = nullptr; } +SceneAsset::~SceneAsset() { + // we defer all destructor work to SceneAssetLoader so we don't need to do anything here +} void SceneAsset::applyWeights(float *weights, int count) { RenderableManager &rm = _engine->getRenderableManager(); @@ -110,6 +112,19 @@ void SceneAsset::stopAnimation(int index) { _embeddedAnimationStatus[index].started = false; } +// void SceneAsset:swapTexture() { +// materialInstance = material2.createInstance() +// baseColor = loadTexture(filament.engine, context.resources, R.drawable.wall_tex_at, TextureType.COLOR) +// normal = loadTexture(filament.engine, context.resources,R.drawable.wall_tex_n,TextureType.NORMAL) +// ao = loadTexture(filament.engine, context.resources,R.drawable.wall_tex_ao,TextureType.DATA) +// roughnessMetallic = loadTexture(filament.engine, context.resources,R.drawable.wall_tex_ms,TextureType.DATA) +// val rm = filament.engine.renderableManager +// val sampler = TextureSampler() +// sampler.anisotropy = 8.0f +// material.setParameter("baseColorIndex",0) +// material.setParameter("baseColorMap",baseColor,sampler) +// } + void SceneAsset::updateEmbeddedAnimations() { auto now = high_resolution_clock::now(); for (auto &status : _embeddedAnimationStatus) { diff --git a/ios/src/SceneAssetLoader.cpp b/ios/src/SceneAssetLoader.cpp index 6f1deacd..783a54f9 100644 --- a/ios/src/SceneAssetLoader.cpp +++ b/ios/src/SceneAssetLoader.cpp @@ -115,9 +115,10 @@ SceneAsset *SceneAssetLoader::fromGlb(const char *uri) { } void SceneAssetLoader::remove(SceneAsset *asset) { - _resourceLoader->evictResourceData(); _scene->removeEntities(asset->_asset->getEntities(), asset->_asset->getEntityCount()); + _resourceLoader->evictResourceData(); _assetLoader->destroyAsset(asset->_asset); + delete asset; } } // namespace polyvox \ No newline at end of file diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index c395f585..e9a4cd06 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -23,7 +23,9 @@ abstract class FilamentController { Future> getTargetNames(FilamentAsset asset, String meshName); Future> getAnimationNames(FilamentAsset asset); Future removeAsset(FilamentAsset asset); + Future clearAssets(); Future playAnimation(FilamentAsset asset, int index, {bool loop = false}); + Future playAnimations(FilamentAsset asset, List indices, {bool loop = false}); Future stopAnimation(FilamentAsset asset); Future setCamera(FilamentAsset asset, String name); @@ -150,6 +152,10 @@ class PolyvoxFilamentController extends FilamentController { await _channel.invokeMethod("removeAsset", asset); } + Future clearAssets() async { + await _channel.invokeMethod("clearAssets"); + } + Future zoom(double z) async { await _channel.invokeMethod("zoom", z); } @@ -158,6 +164,12 @@ class PolyvoxFilamentController extends FilamentController { await _channel.invokeMethod("playAnimation", [asset, index, loop]); } + Future playAnimations(FilamentAsset asset, List indices, {bool loop = false}) async { + return Future.wait(indices.map((index) { + return _channel.invokeMethod("playAnimation", [asset, index, loop]); + })); + } + Future stopAnimation(FilamentAsset asset) async { await _channel.invokeMethod("stopAnimation"); }