add removeSkybox

This commit is contained in:
Nick Fisher
2022-07-16 17:34:56 +10:00
parent e3ab9ced81
commit 09fe0343de
8 changed files with 74 additions and 50 deletions

View File

@@ -9,10 +9,11 @@ using namespace polyvox;
using namespace std; using namespace std;
static AAssetManager* am; static AAssetManager* am;
static vector<AAsset*> _assets; static vector<AAsset*> _assets;
uint64_t id = -1; uint64_t id = -1;
static FilamentViewer* _viewer;
static polyvox::ResourceBuffer loadResource(const char* name) { static polyvox::ResourceBuffer loadResource(const char* name) {
id++; id++;
@@ -50,6 +51,10 @@ extern "C" {
((FilamentViewer*)viewer)->loadSkybox(skyboxPath, iblPath); ((FilamentViewer*)viewer)->loadSkybox(skyboxPath, iblPath);
} }
void remove_skybox(void* viewer) {
((FilamentViewer*)viewer)->removeSkybox();
}
void load_glb(void* viewer, const char* assetPath) { void load_glb(void* viewer, const char* assetPath) {
((FilamentViewer*)viewer)->loadGlb(assetPath); ((FilamentViewer*)viewer)->loadGlb(assetPath);
} }
@@ -67,9 +72,13 @@ extern "C" {
JNIEnv* env, JNIEnv* env,
jobject assetManager jobject assetManager
) { ) {
if(_viewer) {
return _viewer;
}
ANativeWindow* layer = ANativeWindow_fromSurface(env, surface); ANativeWindow* layer = ANativeWindow_fromSurface(env, surface);
am = AAssetManager_fromJava(env, assetManager); am = AAssetManager_fromJava(env, assetManager);
return new FilamentViewer((void*)layer, loadResource, freeResource); _viewer = new FilamentViewer((void*)layer, loadResource, freeResource);
return _viewer;
} }
void render( void render(
@@ -166,10 +175,6 @@ extern "C" {
free(ptr); free(ptr);
} }
void release_source_assets(void* viewer) {
((FilamentViewer*)viewer)->releaseSourceAssets();
}
void remove_asset(void* viewer) { void remove_asset(void* viewer) {
((FilamentViewer*)viewer)->removeAsset(); ((FilamentViewer*)viewer)->removeAsset();
} }

View File

@@ -64,5 +64,7 @@ interface FilamentInterop : Library {
fun remove_asset(viewer:Pointer); fun remove_asset(viewer:Pointer);
fun remove_skybox(viewer:Pointer);
} }

View File

@@ -104,9 +104,9 @@ PlatformView {
_methodChannel.invokeMethod("ready", null) _methodChannel.invokeMethod("ready", null)
choreographer = Choreographer.getInstance() choreographer = Choreographer.getInstance()
// _view.setAlpha(0)
_view.setZOrderOnTop(false) _view.setZOrderOnTop(false)
_view.holder.setFormat(PixelFormat.OPAQUE) _view.holder.setFormat(PixelFormat.TRANSPARENT)
_view.holder.addCallback (object : SurfaceHolder.Callback { _view.holder.addCallback (object : SurfaceHolder.Callback {
override fun surfaceChanged(holder:SurfaceHolder, format:Int, width:Int, height:Int) { override fun surfaceChanged(holder:SurfaceHolder, format:Int, width:Int, height:Int) {
@@ -151,19 +151,34 @@ PlatformView {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) { when (call.method) {
"reloadAssets" -> {
// context = context.createPackageContext(context.getPackageName(), 0)
// val assetManager = context.getAssets()
// val flutterJNI = FlutterJNI.Factory.provideFlutterJNI()
// flutterJNI.updateJavaAssetManager(assetManager, flutterApplicationInfo.flutterAssetsDir)
}
"loadSkybox" -> { "loadSkybox" -> {
val args = call.arguments as ArrayList<Any?> val args = call.arguments as ArrayList<Any?>
val loader = FlutterInjector.instance().flutterLoader() val loader = FlutterInjector.instance().flutterLoader()
_lib.load_skybox(_viewer!!, loader.getLookupKeyForAsset(args[0] as String), loader.getLookupKeyForAsset(args[1] as String)) _lib.load_skybox(_viewer!!, loader.getLookupKeyForAsset(args[0] as String), loader.getLookupKeyForAsset(args[1] as String))
result.success("OK"); result.success("OK");
} }
"removeSkybox" -> {
_lib.remove_skybox(_viewer!!)
result.success(true);
}
"loadGlb" -> { "loadGlb" -> {
if (_viewer == null) if (_viewer == null)
return; return;
val loader = FlutterInjector.instance().flutterLoader() val loader = FlutterInjector.instance().flutterLoader()
val key = loader.getLookupKeyForAsset(call.arguments as String)
val key2 = loader.getLookupKeyForAsset(call.arguments as String, context.packageName)
val path = loader.findAppBundlePath()
Log.v(TAG, "key ${key} key2 ${key2} path : ${path}")
_lib.load_glb( _lib.load_glb(
_viewer!!, _viewer!!,
loader.getLookupKeyForAsset(call.arguments as String) key
) )
result.success("OK"); result.success("OK");
} }
@@ -297,10 +312,6 @@ PlatformView {
_lib.grab_end(_viewer!!) _lib.grab_end(_viewer!!)
result.success("OK"); result.success("OK");
} }
"releaseSourceAssets" -> {
_lib.release_source_assets(_viewer!!)
result.success("OK");
}
"removeAsset" -> { "removeAsset" -> {
_lib.remove_asset(_viewer!!) _lib.remove_asset(_viewer!!)
result.success("OK"); result.success("OK");

View File

@@ -1,7 +0,0 @@
package app.polyvox.filament_example
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}

View File

@@ -41,6 +41,9 @@ using namespace std;
if([@"loadSkybox" isEqualToString:call.method]) { if([@"loadSkybox" isEqualToString:call.method]) {
_viewer->loadSkybox([call.arguments[0] UTF8String], [call.arguments[1] UTF8String]); _viewer->loadSkybox([call.arguments[0] UTF8String], [call.arguments[1] UTF8String]);
result(@"OK"); result(@"OK");
} else if([@"loadSkybox" isEqualToString:call.method]) {
_viewer->removeSkybox();
result(@"OK");
} else if([@"loadGlb" isEqualToString:call.method]) { } else if([@"loadGlb" isEqualToString:call.method]) {
_viewer->loadGlb([call.arguments UTF8String]); _viewer->loadGlb([call.arguments UTF8String]);
result(@"OK"); result(@"OK");
@@ -68,9 +71,6 @@ using namespace std;
} else if([@"rotateEnd" isEqualToString:call.method]) { } else if([@"rotateEnd" isEqualToString:call.method]) {
_viewer->manipulator->grabEnd(); _viewer->manipulator->grabEnd();
result(@"OK"); result(@"OK");
} else if([@"releaseSourceAssets" isEqualToString:call.method]) {
_viewer->releaseSourceAssets();
result(@"OK");
} else if([@"animateWeights" isEqualToString:call.method]) { } else if([@"animateWeights" isEqualToString:call.method]) {
NSArray* frameData = call.arguments[0]; NSArray* frameData = call.arguments[0];
NSNumber* numWeights = call.arguments[1]; NSNumber* numWeights = call.arguments[1];

View File

@@ -59,6 +59,8 @@
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <mutex>
#include "Log.h" #include "Log.h"
using namespace filament; using namespace filament;
@@ -259,27 +261,11 @@ namespace polyvox
_scene->addEntities(_asset->getEntities(), _asset->getEntityCount()); _scene->addEntities(_asset->getEntities(), _asset->getEntityCount());
}; };
void FilamentViewer::releaseSourceAssets()
{
Log("Releasing source data");
_asset->releaseSourceData();
}
void FilamentViewer::loadGlb(const char *const uri) void FilamentViewer::loadGlb(const char *const uri)
{ {
Log("Loading GLB at URI %s", uri); Log("Loading GLB at URI %s", uri);
// if (_asset)
// {
// _asset->releaseSourceData();
// _resourceLoader->evictResourceData();
// _scene->removeEntities(_asset->getEntities(), _asset->getEntityCount());
// _assetLoader->destroyAsset(_asset);
// }
// _asset = nullptr;
// _animator = nullptr;
ResourceBuffer rbuf = _loadResource(uri); ResourceBuffer rbuf = _loadResource(uri);
_asset = _assetLoader->createAssetFromBinary( _asset = _assetLoader->createAssetFromBinary(
@@ -354,6 +340,8 @@ namespace polyvox
Log("No asset loaded, ignoring call."); Log("No asset loaded, ignoring call.");
return; return;
} }
mtx.lock();
_resourceLoader->evictResourceData(); _resourceLoader->evictResourceData();
_scene->removeEntities(_asset->getEntities(), _asset->getEntityCount()); _scene->removeEntities(_asset->getEntities(), _asset->getEntityCount());
@@ -362,6 +350,12 @@ namespace polyvox
_animator = nullptr; _animator = nullptr;
_morphAnimationBuffer = nullptr; _morphAnimationBuffer = nullptr;
_embeddedAnimationBuffer = nullptr; _embeddedAnimationBuffer = nullptr;
_view->setCamera(_mainCamera);
mtx.unlock();
}
void FilamentViewer::removeSkybox() {
_scene->setSkybox(nullptr);
} }
@@ -384,7 +378,7 @@ namespace polyvox
} }
const utils::Entity* cameras = _asset->getCameraEntities(); const utils::Entity* cameras = _asset->getCameraEntities();
Log("%d cameras found in current asset", cameraName, count); Log("%zu cameras found in current asset", cameraName, count);
for(int i=0; i < count; i++) { for(int i=0; i < count; i++) {
auto inst = _ncm->getInstance(cameras[i]); auto inst = _ncm->getInstance(cameras[i]);
@@ -411,7 +405,10 @@ namespace polyvox
unique_ptr<vector<string>> FilamentViewer::getAnimationNames() unique_ptr<vector<string>> FilamentViewer::getAnimationNames()
{ {
if(!_asset) {
Log("No asset, ignoring call.");
return nullptr;
}
size_t count = _animator->getAnimationCount(); size_t count = _animator->getAnimationCount();
Log("Found %d animations in asset.", count); Log("Found %d animations in asset.", count);
@@ -428,6 +425,10 @@ namespace polyvox
unique_ptr<vector<string>> FilamentViewer::getTargetNames(const char *meshName) unique_ptr<vector<string>> FilamentViewer::getTargetNames(const char *meshName)
{ {
if(!_asset) {
Log("No asset, ignoring call.");
return nullptr;
}
Log("Retrieving morph target names for mesh %s", meshName); Log("Retrieving morph target names for mesh %s", meshName);
unique_ptr<vector<string>> names = make_unique<vector<string>>(); unique_ptr<vector<string>> names = make_unique<vector<string>>();
const Entity *entities = _asset->getEntities(); const Entity *entities = _asset->getEntities();
@@ -512,20 +513,19 @@ namespace polyvox
void FilamentViewer::render() void FilamentViewer::render()
{ {
if (!_view || !_mainCamera || !_swapChain) if (!_view || !_mainCamera || !_swapChain)
{ {
Log("Not ready for rendering"); Log("Not ready for rendering");
return; return;
} }
if (_morphAnimationBuffer) mtx.lock();
{ if(_asset) {
updateMorphAnimation(); updateMorphAnimation();
}
if(_embeddedAnimationBuffer) {
updateEmbeddedAnimation(); updateEmbeddedAnimation();
} }
math::float3 eye, target, upward; math::float3 eye, target, upward;
manipulator->getLookAt(&eye, &target, &upward); manipulator->getLookAt(&eye, &target, &upward);
@@ -537,6 +537,7 @@ namespace polyvox
_renderer->render(_view); _renderer->render(_view);
_renderer->endFrame(); _renderer->endFrame();
} }
mtx.unlock();
} }
void FilamentViewer::updateViewportAndCameraProjection(int width, int height, float contentScaleFactor) void FilamentViewer::updateViewportAndCameraProjection(int width, int height, float contentScaleFactor)
@@ -607,6 +608,9 @@ namespace polyvox
} }
void FilamentViewer::updateEmbeddedAnimation() { void FilamentViewer::updateEmbeddedAnimation() {
if(!_embeddedAnimationBuffer) {
return;
}
duration<double> dur = duration_cast<duration<double>>(high_resolution_clock::now() - _embeddedAnimationBuffer->lastTime); duration<double> dur = duration_cast<duration<double>>(high_resolution_clock::now() - _embeddedAnimationBuffer->lastTime);
float startTime = 0; float startTime = 0;
if(!_embeddedAnimationBuffer->hasStarted) { if(!_embeddedAnimationBuffer->hasStarted) {

View File

@@ -92,14 +92,16 @@ namespace polyvox {
public: public:
FilamentViewer(void* layer, LoadResource loadResource, FreeResource freeResource); FilamentViewer(void* layer, LoadResource loadResource, FreeResource freeResource);
~FilamentViewer(); ~FilamentViewer();
void loadSkybox(const char* const skyboxUri, const char* const iblUri);
void removeSkybox();
void loadGlb(const char* const uri); void loadGlb(const char* const uri);
void loadGltf(const char* const uri, const char* relativeResourcePath); void loadGltf(const char* const uri, const char* relativeResourcePath);
void loadSkybox(const char* const skyboxUri, const char* const iblUri);
void removeAsset(); void removeAsset();
void updateViewportAndCameraProjection(int height, int width, float scaleFactor); void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
void render(); void render();
void releaseSourceAssets();
unique_ptr<vector<string>> getTargetNames(const char* meshName); unique_ptr<vector<string>> getTargetNames(const char* meshName);
unique_ptr<vector<string>> getAnimationNames(); unique_ptr<vector<string>> getAnimationNames();
Manipulator<float>* manipulator; Manipulator<float>* manipulator;
@@ -158,8 +160,8 @@ namespace polyvox {
AssetLoader* _assetLoader; AssetLoader* _assetLoader;
FilamentAsset* _asset = nullptr; FilamentAsset* _asset = nullptr;
// ResourceBuffer _assetBuffer;
NameComponentManager* _ncm; NameComponentManager* _ncm;
std::mutex mtx; // mutex to ensure thread safety when removing assets
Entity _sun; Entity _sun;
Texture* _skyboxTexture; Texture* _skyboxTexture;
@@ -184,6 +186,7 @@ namespace polyvox {
bool isAnimating; bool isAnimating;
unique_ptr<MorphAnimationBuffer> _morphAnimationBuffer; unique_ptr<MorphAnimationBuffer> _morphAnimationBuffer;
unique_ptr<EmbeddedAnimationBuffer> _embeddedAnimationBuffer; unique_ptr<EmbeddedAnimationBuffer> _embeddedAnimationBuffer;
}; };

View File

@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
abstract class FilamentController { abstract class FilamentController {
void onFilamentViewCreated(int id); void onFilamentViewCreated(int id);
Future loadSkybox(String skyboxPath, String lightingPath); Future loadSkybox(String skyboxPath, String lightingPath);
Future removeSkybox();
Future loadGlb(String path); Future loadGlb(String path);
Future loadGltf(String path, String relativeResourcePath); Future loadGltf(String path, String relativeResourcePath);
Future panStart(double x, double y); Future panStart(double x, double y);
@@ -62,6 +63,11 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("loadSkybox", [skyboxPath, lightingPath]); await _channel.invokeMethod("loadSkybox", [skyboxPath, lightingPath]);
} }
@override
Future removeSkybox() async {
await _channel.invokeMethod("removeSkybox");
}
Future loadGlb(String path) async { Future loadGlb(String path) async {
print("Loading GLB at $path "); print("Loading GLB at $path ");
await _channel.invokeMethod("loadGlb", path); await _channel.invokeMethod("loadGlb", path);