add clearAssets method to viewer

This commit is contained in:
Nick Fisher
2022-08-14 21:30:58 +10:00
parent 7d24621925
commit 6a7499099d
8 changed files with 59 additions and 3 deletions

View File

@@ -194,5 +194,9 @@ extern "C" {
void remove_asset(void* viewer, void* asset) { void remove_asset(void* viewer, void* asset) {
((FilamentViewer*)viewer)->removeAsset((SceneAsset*)asset); ((FilamentViewer*)viewer)->removeAsset((SceneAsset*)asset);
} }
void clear_assets(void* viewer) {
((FilamentViewer*)viewer)->clearAssets();
}
} }

View File

@@ -64,6 +64,8 @@ interface FilamentInterop : Library {
fun remove_asset(viewer:Pointer, asset:Pointer); fun remove_asset(viewer:Pointer, asset:Pointer);
fun clear_assets(viewer:Pointer);
fun remove_skybox(viewer:Pointer); fun remove_skybox(viewer:Pointer);
fun remove_ibl(viewer:Pointer); fun remove_ibl(viewer:Pointer);

View File

@@ -331,6 +331,10 @@ PlatformView {
_lib.remove_asset(_viewer!!, Pointer(call.arguments as Long)) _lib.remove_asset(_viewer!!, Pointer(call.arguments as Long))
result.success("OK"); result.success("OK");
} }
"clearAssets" -> {
_lib.clear_assets(_viewer!!)
result.success("OK");
}
"playAnimation" -> { "playAnimation" -> {
val args = call.arguments as ArrayList<Any?> val args = call.arguments as ArrayList<Any?>
_lib.play_animation(Pointer(args[0] as Long), args[1] as Int, args[2] as Boolean) _lib.play_animation(Pointer(args[0] as Long), args[1] as Int, args[2] as Boolean)

View File

@@ -359,6 +359,7 @@ SceneAsset *FilamentViewer::loadGlb(const char *const uri) {
Log("Unknown error loading asset."); Log("Unknown error loading asset.");
} else { } else {
_assets.push_back(asset); _assets.push_back(asset);
Log("GLB loaded, asset at index %d", _assets.size() - 1);
} }
return asset; return asset;
} }
@@ -376,10 +377,25 @@ SceneAsset *FilamentViewer::loadGltf(const char *const uri,
return asset; 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) { void FilamentViewer::removeAsset(SceneAsset *asset) {
mtx.lock(); mtx.lock();
_sceneAssetLoader->remove(asset); _sceneAssetLoader->remove(asset);
_view->setCamera(_mainCamera); // todo - what if we are using a camera from this asset?
bool erased = false; bool erased = false;
for (auto it = _assets.begin(); it != _assets.end();) { for (auto it = _assets.begin(); it != _assets.end();) {
if (*it == asset) { if (*it == asset) {

View File

@@ -57,6 +57,8 @@ namespace polyvox {
SceneAsset* loadGlb(const char* const uri); SceneAsset* loadGlb(const char* const uri);
SceneAsset* loadGltf(const char* const uri, const char* relativeResourcePath); SceneAsset* loadGltf(const char* const uri, const char* relativeResourcePath);
void removeAsset(SceneAsset* asset); void removeAsset(SceneAsset* asset);
// removes all add assets from the current scene
void clearAssets();
void updateViewportAndCameraProjection(int height, int width, float scaleFactor); void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
void render(); void render();

View File

@@ -31,7 +31,9 @@ SceneAsset::SceneAsset(FilamentAsset *asset, Engine *engine,
Log("Created animation buffers for %d", _embeddedAnimationStatus.size()); 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) { void SceneAsset::applyWeights(float *weights, int count) {
RenderableManager &rm = _engine->getRenderableManager(); RenderableManager &rm = _engine->getRenderableManager();
@@ -110,6 +112,19 @@ void SceneAsset::stopAnimation(int index) {
_embeddedAnimationStatus[index].started = false; _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() { void SceneAsset::updateEmbeddedAnimations() {
auto now = high_resolution_clock::now(); auto now = high_resolution_clock::now();
for (auto &status : _embeddedAnimationStatus) { for (auto &status : _embeddedAnimationStatus) {

View File

@@ -115,9 +115,10 @@ SceneAsset *SceneAssetLoader::fromGlb(const char *uri) {
} }
void SceneAssetLoader::remove(SceneAsset *asset) { void SceneAssetLoader::remove(SceneAsset *asset) {
_resourceLoader->evictResourceData();
_scene->removeEntities(asset->_asset->getEntities(), _scene->removeEntities(asset->_asset->getEntities(),
asset->_asset->getEntityCount()); asset->_asset->getEntityCount());
_resourceLoader->evictResourceData();
_assetLoader->destroyAsset(asset->_asset); _assetLoader->destroyAsset(asset->_asset);
delete asset;
} }
} // namespace polyvox } // namespace polyvox

View File

@@ -23,7 +23,9 @@ abstract class FilamentController {
Future<List<String>> getTargetNames(FilamentAsset asset, String meshName); Future<List<String>> getTargetNames(FilamentAsset asset, String meshName);
Future<List<String>> getAnimationNames(FilamentAsset asset); Future<List<String>> getAnimationNames(FilamentAsset asset);
Future removeAsset(FilamentAsset asset); Future removeAsset(FilamentAsset asset);
Future clearAssets();
Future playAnimation(FilamentAsset asset, int index, {bool loop = false}); Future playAnimation(FilamentAsset asset, int index, {bool loop = false});
Future playAnimations(FilamentAsset asset, List<int> indices, {bool loop = false});
Future stopAnimation(FilamentAsset asset); Future stopAnimation(FilamentAsset asset);
Future setCamera(FilamentAsset asset, String name); Future setCamera(FilamentAsset asset, String name);
@@ -150,6 +152,10 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("removeAsset", asset); await _channel.invokeMethod("removeAsset", asset);
} }
Future clearAssets() async {
await _channel.invokeMethod("clearAssets");
}
Future zoom(double z) async { Future zoom(double z) async {
await _channel.invokeMethod("zoom", z); await _channel.invokeMethod("zoom", z);
} }
@@ -158,6 +164,12 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("playAnimation", [asset, index, loop]); await _channel.invokeMethod("playAnimation", [asset, index, loop]);
} }
Future playAnimations(FilamentAsset asset, List<int> indices, {bool loop = false}) async {
return Future.wait(indices.map((index) {
return _channel.invokeMethod("playAnimation", [asset, index, loop]);
}));
}
Future stopAnimation(FilamentAsset asset) async { Future stopAnimation(FilamentAsset asset) async {
await _channel.invokeMethod("stopAnimation"); await _channel.invokeMethod("stopAnimation");
} }