decouple assets from viewer to allow independent addition/removal/animation/etc
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "FilamentViewer.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <android/native_window_jni.h>
|
||||
@@ -41,6 +42,8 @@ static void freeResource(ResourceBuffer rb) {
|
||||
AAsset* asset = _assets[rb.id];
|
||||
if(asset) {
|
||||
AAsset_close(asset);
|
||||
} else {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Attempting to free resource at index [ %d ] that has already been released.", rb.id);
|
||||
}
|
||||
_assets[rb.id] = nullptr;
|
||||
}
|
||||
@@ -68,16 +71,16 @@ extern "C" {
|
||||
((FilamentViewer*)viewer)->removeIbl();
|
||||
}
|
||||
|
||||
void load_glb(void* viewer, const char* assetPath) {
|
||||
((FilamentViewer*)viewer)->loadGlb(assetPath);
|
||||
void* load_glb(void* viewer, const char* assetPath) {
|
||||
return ((FilamentViewer*)viewer)->loadGlb(assetPath);
|
||||
}
|
||||
|
||||
void load_gltf(void* viewer, const char* assetPath, const char* relativePath) {
|
||||
((FilamentViewer*)viewer)->loadGltf(assetPath, relativePath);
|
||||
void* load_gltf(void* viewer, const char* assetPath, const char* relativePath) {
|
||||
return ((FilamentViewer*)viewer)->loadGltf(assetPath, relativePath);
|
||||
}
|
||||
|
||||
bool set_camera(void* viewer, const char* nodeName) {
|
||||
return ((FilamentViewer*)viewer)->setCamera(nodeName);
|
||||
bool set_camera(void* viewer, void* asset, const char* nodeName) {
|
||||
return ((FilamentViewer*)viewer)->setCamera((SceneAsset*)asset, nodeName);
|
||||
}
|
||||
|
||||
void* filament_viewer_new(
|
||||
@@ -141,22 +144,22 @@ extern "C" {
|
||||
((FilamentViewer*)viewer)->manipulator->grabEnd();
|
||||
}
|
||||
|
||||
void apply_weights(void* viewer, float* weights, int count) {
|
||||
((FilamentViewer*)viewer)->applyWeights(weights, count);
|
||||
void apply_weights(void* asset, float* weights, int count) {
|
||||
((SceneAsset*)asset)->applyWeights(weights, count);
|
||||
}
|
||||
|
||||
void animate_weights(void* viewer, float* data, int numWeights, int numFrames, float frameRate) {
|
||||
void animate_weights(void* asset, float* data, int numWeights, int numFrames, float frameRate) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Animating %d frames, each with %d weights", numFrames, numWeights);
|
||||
((FilamentViewer*)viewer)->animateWeights((float*)data, numWeights, numFrames, frameRate);
|
||||
((SceneAsset*)asset)->animateWeights((float*)data, numWeights, numFrames, frameRate);
|
||||
}
|
||||
|
||||
void play_animation(void* viewer, int index, bool loop) {
|
||||
void play_animation(void* asset, int index, bool loop) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Playing embedded animation %d", index);
|
||||
((FilamentViewer*)viewer)->playAnimation(index, loop);
|
||||
((SceneAsset*)asset)->playAnimation(index, loop);
|
||||
}
|
||||
|
||||
char** get_animation_names(void* viewer, int* countPtr) {
|
||||
auto names = ((FilamentViewer*)viewer)->getAnimationNames();
|
||||
char** get_animation_names(void* asset, int* countPtr) {
|
||||
auto names = ((SceneAsset*)asset)->getAnimationNames();
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Got %d animation names", names->size());
|
||||
char** names_c;
|
||||
names_c = new char*[names->size()];
|
||||
@@ -168,8 +171,8 @@ extern "C" {
|
||||
return names_c;
|
||||
}
|
||||
|
||||
char** get_target_names(void* viewer, char* meshName, int* countPtr ) {
|
||||
unique_ptr<vector<string>> names = ((FilamentViewer*)viewer)->getTargetNames(meshName);
|
||||
char** get_target_names(void* asset, char* meshName, int* countPtr ) {
|
||||
unique_ptr<vector<string>> names = ((SceneAsset*)asset)->getTargetNames(meshName);
|
||||
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Got %d names", names->size());
|
||||
|
||||
@@ -188,8 +191,8 @@ extern "C" {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void remove_asset(void* viewer) {
|
||||
((FilamentViewer*)viewer)->removeAsset();
|
||||
void remove_asset(void* viewer, void* asset) {
|
||||
((FilamentViewer*)viewer)->removeAsset((SceneAsset*)asset);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ interface FilamentInterop : Library {
|
||||
|
||||
fun load_gltf(viewer:Pointer, uri:String, relativeResourcePath:String) : Pointer;
|
||||
|
||||
fun set_camera(viewer:Pointer, nodeName:String) : Boolean;
|
||||
fun set_camera(viewer:Pointer, asset:Pointer, nodeName:String) : Boolean;
|
||||
|
||||
fun render(viewer:Pointer);
|
||||
|
||||
@@ -50,21 +50,19 @@ interface FilamentInterop : Library {
|
||||
|
||||
fun grab_end(viewer:Pointer)
|
||||
|
||||
fun apply_weights(viewer:Pointer, weights:FloatArray, size:Int);
|
||||
fun apply_weights(asset:Pointer, weights:FloatArray, size:Int);
|
||||
|
||||
fun animate_weights(viewer:Pointer, frames:FloatArray, numWeights:Int, numFrames:Int, frameRate:Float);
|
||||
fun animate_weights(asset:Pointer, frames:FloatArray, numWeights:Int, numFrames:Int, frameRate:Float);
|
||||
|
||||
fun get_target_names(viewer:Pointer, meshName:String, outLen:IntByReference) : Pointer;
|
||||
fun get_target_names(asset:Pointer, meshName:String, outLen:IntByReference) : Pointer;
|
||||
|
||||
fun get_animation_names(viewer:Pointer, outLen:IntByReference) : Pointer;
|
||||
fun get_animation_names(asset:Pointer, outLen:IntByReference) : Pointer;
|
||||
|
||||
fun play_animation(viewer:Pointer, index:Int, loop:Boolean);
|
||||
fun play_animation(asset:Pointer, index:Int, loop:Boolean);
|
||||
|
||||
fun free_pointer(ptr:Pointer, size:Int);
|
||||
|
||||
fun release_source_assets(viewer:Pointer);
|
||||
|
||||
fun remove_asset(viewer:Pointer);
|
||||
fun remove_asset(viewer:Pointer, asset:Pointer);
|
||||
|
||||
fun remove_skybox(viewer:Pointer);
|
||||
|
||||
|
||||
@@ -192,32 +192,31 @@ PlatformView {
|
||||
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(
|
||||
val assetPtr = _lib.load_glb(
|
||||
_viewer!!,
|
||||
key
|
||||
)
|
||||
result.success("OK");
|
||||
result.success(Pointer.nativeValue(assetPtr));
|
||||
}
|
||||
"loadGltf" -> {
|
||||
if (_viewer == null)
|
||||
return;
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
val loader = FlutterInjector.instance().flutterLoader()
|
||||
_lib.load_gltf(
|
||||
val assetPtr = _lib.load_gltf(
|
||||
_viewer!!,
|
||||
loader.getLookupKeyForAsset(args[0] as String),
|
||||
loader.getLookupKeyForAsset(args[1] as String)
|
||||
)
|
||||
result.success("OK");
|
||||
result.success(Pointer.nativeValue(assetPtr));
|
||||
}
|
||||
"setCamera" -> {
|
||||
if (_viewer == null)
|
||||
return;
|
||||
val args = call.arguments as ArrayList<*>
|
||||
val success = _lib.set_camera(
|
||||
_viewer!!,
|
||||
call.arguments as String
|
||||
Pointer(args[0] as Long),
|
||||
args[1] as String,
|
||||
)
|
||||
if(success) {
|
||||
result.success("OK");
|
||||
@@ -236,9 +235,10 @@ PlatformView {
|
||||
return;
|
||||
|
||||
val countPtr = IntByReference();
|
||||
val arrPtr = _lib.get_target_names(_viewer!!, call.arguments as String, countPtr)
|
||||
val args = call.arguments as ArrayList<*>
|
||||
val namesPtr = _lib.get_target_names(Pointer(args[0] as Long), args[1] as String, countPtr)
|
||||
|
||||
val names = arrPtr.getStringArray(0, countPtr.value);
|
||||
val names = namesPtr.getStringArray(0, countPtr.value);
|
||||
|
||||
for(i in 0..countPtr.value-1) {
|
||||
Log.v(TAG, "Got target names ${names[i]} ${names[i].length}")
|
||||
@@ -246,16 +246,14 @@ PlatformView {
|
||||
|
||||
val namesAsList = names.toCollection(ArrayList())
|
||||
|
||||
_lib.free_pointer(arrPtr, countPtr.getValue())
|
||||
_lib.free_pointer(namesPtr, countPtr.getValue())
|
||||
|
||||
result.success(namesAsList)
|
||||
}
|
||||
"getAnimationNames" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
|
||||
val assetPtr = Pointer(call.arguments as Long)
|
||||
val countPtr = IntByReference();
|
||||
val arrPtr = _lib.get_animation_names(_viewer!!, countPtr)
|
||||
val arrPtr = _lib.get_animation_names(assetPtr, countPtr)
|
||||
|
||||
val names = arrPtr.getStringArray(0, countPtr.value);
|
||||
|
||||
@@ -269,23 +267,22 @@ PlatformView {
|
||||
result.success(names.toCollection(ArrayList()))
|
||||
}
|
||||
"applyWeights" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
val weights = call.arguments as ArrayList<Float>;
|
||||
val args = call.arguments as ArrayList<*>
|
||||
val assetPtr = Pointer(args[0] as Long)
|
||||
val weights = args[1] as ArrayList<Float>;
|
||||
|
||||
_lib.apply_weights(_viewer!!, weights.toFloatArray(), weights.size)
|
||||
_lib.apply_weights(assetPtr, weights.toFloatArray(), weights.size)
|
||||
result.success("OK");
|
||||
}
|
||||
"animateWeights" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
val frames = args[0] as ArrayList<Float>;
|
||||
val numWeights = args[1] as Int
|
||||
val numFrames = args[2] as Int
|
||||
val frameLenInMs = args[3] as Double
|
||||
val assetPtr = Pointer(args[0] as Long)
|
||||
val frames = args[1] as ArrayList<Float>;
|
||||
val numWeights = args[2] as Int
|
||||
val numFrames = args[3] as Int
|
||||
val frameLenInMs = args[4] as Double
|
||||
|
||||
_lib.animate_weights(_viewer!!, frames.toFloatArray(), numWeights, numFrames, frameLenInMs.toFloat())
|
||||
_lib.animate_weights(assetPtr, frames.toFloatArray(), numWeights, numFrames, frameLenInMs.toFloat())
|
||||
result.success("OK");
|
||||
}
|
||||
"panStart" -> {
|
||||
@@ -331,12 +328,12 @@ PlatformView {
|
||||
result.success("OK");
|
||||
}
|
||||
"removeAsset" -> {
|
||||
_lib.remove_asset(_viewer!!)
|
||||
_lib.remove_asset(_viewer!!, Pointer(call.arguments as Long))
|
||||
result.success("OK");
|
||||
}
|
||||
"playAnimation" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.play_animation(_viewer!!, args[0] as Int, args[1] as Boolean)
|
||||
_lib.play_animation(Pointer(args[0] as Long), args[1] as Int, args[2] as Boolean)
|
||||
result.success("OK")
|
||||
}
|
||||
else -> {
|
||||
|
||||
Reference in New Issue
Block a user