make Android use new API, add delete_viewer method
This commit is contained in:
@@ -14,8 +14,6 @@ static AAssetManager* am;
|
||||
static vector<AAsset*> _assets;
|
||||
uint64_t id = -1;
|
||||
|
||||
static FilamentViewer* _viewer;
|
||||
|
||||
static ResourceBuffer loadResource(const char* name) {
|
||||
|
||||
id++;
|
||||
@@ -51,169 +49,17 @@ static void freeResource(uint32_t id) {
|
||||
|
||||
extern "C" {
|
||||
|
||||
void set_background_image(void* viewer, const char* path) {
|
||||
((FilamentViewer*)viewer)->setBackgroundImage(path);
|
||||
}
|
||||
|
||||
void load_skybox(void* viewer, const char* skyboxPath) {
|
||||
((FilamentViewer*)viewer)->loadSkybox(skyboxPath);
|
||||
}
|
||||
|
||||
void load_ibl(void* viewer, const char* iblPath) {
|
||||
((FilamentViewer*)viewer)->loadIbl(iblPath);
|
||||
}
|
||||
|
||||
void remove_skybox(void* viewer) {
|
||||
((FilamentViewer*)viewer)->removeSkybox();
|
||||
}
|
||||
|
||||
|
||||
void remove_ibl(void* viewer) {
|
||||
((FilamentViewer*)viewer)->removeIbl();
|
||||
}
|
||||
|
||||
void* load_glb(void* viewer, const char* assetPath) {
|
||||
return ((FilamentViewer*)viewer)->loadGlb(assetPath);
|
||||
}
|
||||
|
||||
void* load_gltf(void* viewer, const char* assetPath, const char* relativePath) {
|
||||
return ((FilamentViewer*)viewer)->loadGltf(assetPath, relativePath);
|
||||
}
|
||||
|
||||
bool set_camera(void* viewer, void* asset, const char* nodeName) {
|
||||
return ((FilamentViewer*)viewer)->setCamera((SceneAsset*)asset, nodeName);
|
||||
}
|
||||
|
||||
void* filament_viewer_new(
|
||||
void* filament_viewer_new_android(
|
||||
jobject surface,
|
||||
JNIEnv* env,
|
||||
jobject assetManager
|
||||
) {
|
||||
if(_viewer) {
|
||||
return _viewer;
|
||||
}
|
||||
ANativeWindow* layer = ANativeWindow_fromSurface(env, surface);
|
||||
am = AAssetManager_fromJava(env, assetManager);
|
||||
_viewer = new FilamentViewer((void*)layer, loadResource, freeResource);
|
||||
return _viewer;
|
||||
return new FilamentViewer((void*)layer, loadResource, freeResource);
|
||||
}
|
||||
|
||||
void render(
|
||||
void* viewer
|
||||
) {
|
||||
((FilamentViewer*)viewer)->render();
|
||||
}
|
||||
|
||||
void destroy_swap_chain(void* viewer) {
|
||||
((FilamentViewer*)viewer)->destroySwapChain();
|
||||
}
|
||||
|
||||
void create_swap_chain(void* viewer, jobject surface, JNIEnv* env) {
|
||||
ANativeWindow* layer = ANativeWindow_fromSurface(env, surface);
|
||||
if(!layer) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Couldn't get native window from surface");
|
||||
return;
|
||||
}
|
||||
((FilamentViewer*)viewer)->createSwapChain(layer);
|
||||
}
|
||||
|
||||
void* get_renderer(void* viewer) {
|
||||
return ((FilamentViewer*)viewer)->getRenderer();
|
||||
}
|
||||
|
||||
void update_viewport_and_camera_projection(void* viewer, int width, int height, float scaleFactor) {
|
||||
return ((FilamentViewer*)viewer)->updateViewportAndCameraProjection(width, height, scaleFactor);
|
||||
|
||||
}
|
||||
|
||||
void scroll(void* viewer, float x, float y , float z) {
|
||||
return ((FilamentViewer*)viewer)->manipulator->scroll(x, y, z);
|
||||
}
|
||||
|
||||
void grab_begin(void* viewer, int x, int y, bool pan) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Grab begin at %d %d %d", x, y, pan);
|
||||
((FilamentViewer*)viewer)->manipulator->grabBegin(x, y, pan);
|
||||
|
||||
}
|
||||
|
||||
void grab_update(void* viewer, int x, int y) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Grab update at %d %d", x, y);
|
||||
((FilamentViewer*)viewer)->manipulator->grabUpdate(x, y);
|
||||
}
|
||||
|
||||
void grab_end(void* viewer) {
|
||||
((FilamentViewer*)viewer)->manipulator->grabEnd();
|
||||
}
|
||||
|
||||
void apply_weights(void* asset, float* weights, int count) {
|
||||
((SceneAsset*)asset)->applyWeights(weights, count);
|
||||
}
|
||||
|
||||
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);
|
||||
((SceneAsset*)asset)->animateWeights((float*)data, numWeights, numFrames, frameRate);
|
||||
}
|
||||
|
||||
void play_animation(void* asset, int index, bool loop) {
|
||||
__android_log_print(ANDROID_LOG_VERBOSE, "filament_api", "Playing embedded animation %d", index);
|
||||
((SceneAsset*)asset)->playAnimation(index, loop);
|
||||
}
|
||||
|
||||
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()];
|
||||
for(int i = 0; i < names->size(); i++) {
|
||||
names_c[i] = (char*) malloc(names->at(i).size() +1);
|
||||
strcpy(names_c[i], names->at(i).c_str());
|
||||
}
|
||||
(*countPtr) = names->size();
|
||||
return names_c;
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
*countPtr = names->size();
|
||||
|
||||
char** retval;
|
||||
retval = new char*[names->size()];
|
||||
|
||||
for(int i =0; i < names->size(); i++) {
|
||||
retval[i] = (char*)(names->at(i).c_str());
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void free_pointer(char** ptr, int num) {
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void remove_asset(void* viewer, void* asset) {
|
||||
((FilamentViewer*)viewer)->removeAsset((SceneAsset*)asset);
|
||||
}
|
||||
|
||||
void clear_assets(void* viewer) {
|
||||
((FilamentViewer*)viewer)->clearAssets();
|
||||
}
|
||||
|
||||
void set_texture(void* asset, const char* assetPath, int renderableIndex) {
|
||||
((SceneAsset*)asset)->setTexture(assetPath, renderableIndex);
|
||||
}
|
||||
|
||||
void transform_to_unit_cube(void* asset) {
|
||||
((SceneAsset*)asset)->transformToUnitCube();
|
||||
}
|
||||
|
||||
void set_position(void* asset, float x, float y, float z) {
|
||||
((SceneAsset*)asset)->setPosition(x, y, z);
|
||||
}
|
||||
|
||||
void set_rotation(void* asset, float rads, float x, float y, float z) {
|
||||
((SceneAsset*)asset)->setRotation(rads, x, y, z);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,12 +18,16 @@ import java.nio.ByteBuffer
|
||||
|
||||
interface FilamentInterop : Library {
|
||||
|
||||
fun filament_viewer_new(
|
||||
fun filament_viewer_new_android(
|
||||
layer:Object,
|
||||
env:JNIEnv,
|
||||
am:AssetManager
|
||||
) : Pointer;
|
||||
|
||||
fun filament_viewer_delete(
|
||||
viewer:Pointer,
|
||||
) : Pointer;
|
||||
|
||||
fun load_skybox(viewer:Pointer, skyboxPath:String) : Pointer;
|
||||
|
||||
fun load_ibl(viewer:Pointer, skyboxPath:String) : Pointer;
|
||||
@@ -54,9 +58,11 @@ interface FilamentInterop : Library {
|
||||
|
||||
fun animate_weights(asset:Pointer, frames:FloatArray, numWeights:Int, numFrames:Int, frameRate:Float);
|
||||
|
||||
fun get_target_names(asset:Pointer, meshName:String, outLen:IntByReference) : Pointer;
|
||||
fun get_target_name_count(asset:Pointer, meshName:String) : Int;
|
||||
fun get_target_name(asset:Pointer, meshName:String, outPtr:Pointer, index:Int);
|
||||
|
||||
fun get_animation_names(asset:Pointer, outLen:IntByReference) : Pointer;
|
||||
fun get_animation_count(asset:Pointer) : Int;
|
||||
fun get_animation_name(asset:Pointer, outPtr:Pointer, index:Int);
|
||||
|
||||
fun play_animation(asset:Pointer, index:Int, loop:Boolean);
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ import android.util.Log
|
||||
import com.sun.jna.Library
|
||||
import com.sun.jna.Native
|
||||
import com.sun.jna.Pointer
|
||||
import com.sun.jna.Memory
|
||||
import com.sun.jna.ptr.PointerByReference
|
||||
import com.sun.jna.ptr.IntByReference
|
||||
import com.sun.jna.Structure
|
||||
@@ -77,14 +78,20 @@ import android.view.SurfaceHolder
|
||||
/** PolyvoxFilamentPlugin */
|
||||
class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
|
||||
private val lock = Object()
|
||||
|
||||
inner class FrameCallback : Choreographer.FrameCallback {
|
||||
private val startTime = System.nanoTime()
|
||||
override fun doFrame(frameTimeNanos: Long) {
|
||||
choreographer.postFrameCallback(this)
|
||||
if(!surface.isValid()) {
|
||||
Log.v(TAG, "INVALID")
|
||||
choreographer.postFrameCallback(this)
|
||||
synchronized(lock) {
|
||||
if(_viewer != null) {
|
||||
if(!surface.isValid()) {
|
||||
Log.v(TAG, "INVALID")
|
||||
}
|
||||
_lib.render(_viewer!!)
|
||||
}
|
||||
}
|
||||
_lib.render(_viewer!!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +122,7 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
private lateinit var assetManager : AssetManager
|
||||
|
||||
private lateinit var surface: Surface
|
||||
private lateinit var surfaceTexture: SurfaceTexture
|
||||
private var surfaceTexture: SurfaceTexture? = null
|
||||
|
||||
private lateinit var activity:Activity
|
||||
|
||||
@@ -135,6 +142,18 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||
when (call.method) {
|
||||
"initialize" -> {
|
||||
if(_viewer != null) {
|
||||
synchronized(lock) {
|
||||
print("Deleting existing viewer")
|
||||
_lib.filament_viewer_delete(_viewer!!);
|
||||
_viewer = null;
|
||||
}
|
||||
}
|
||||
if(surfaceTexture != null) {
|
||||
print("Releasing existing texture")
|
||||
surfaceTexture!!.release()
|
||||
surfaceTexture = null;
|
||||
}
|
||||
val args = call.arguments as ArrayList<Int>
|
||||
val width = args[0]
|
||||
val height = args[1]
|
||||
@@ -145,11 +164,11 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
|
||||
surfaceTexture = entry.surfaceTexture()
|
||||
|
||||
surfaceTexture.setDefaultBufferSize(width, height)
|
||||
surfaceTexture!!.setDefaultBufferSize(width, height)
|
||||
|
||||
surface = Surface(surfaceTexture)
|
||||
surface = Surface(surfaceTexture!!)
|
||||
|
||||
_viewer = _lib.filament_viewer_new(
|
||||
_viewer = _lib.filament_viewer_new_android(
|
||||
surface as Object,
|
||||
JNIEnv.CURRENT,
|
||||
(activity as Context).assets)
|
||||
@@ -167,7 +186,7 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
val width = args[0]
|
||||
val height = args[1]
|
||||
|
||||
surfaceTexture.setDefaultBufferSize(width, height)
|
||||
surfaceTexture!!.setDefaultBufferSize(width, height)
|
||||
_lib.update_viewport_and_camera_projection(_viewer!!, width, height, 1.0f);
|
||||
result.success(null)
|
||||
}
|
||||
@@ -275,40 +294,28 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
result.success("OK");
|
||||
}
|
||||
"getTargetNames" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
|
||||
val countPtr = IntByReference();
|
||||
val args = call.arguments as ArrayList<*>
|
||||
val namesPtr = _lib.get_target_names(Pointer(args[0] as Long), args[1] as String, countPtr)
|
||||
|
||||
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}")
|
||||
val assetPtr = Pointer(args[0] as Long)
|
||||
val meshName = args[1] as String
|
||||
val names = mutableListOf<String>()
|
||||
val outPtr = Memory(256)
|
||||
for(i in 0.._lib.get_target_name_count(assetPtr, meshName) - 1) {
|
||||
_lib.get_target_name(assetPtr, meshName, outPtr, i)
|
||||
val name = outPtr.getString(0)
|
||||
names.add(name)
|
||||
}
|
||||
|
||||
val namesAsList = names.toCollection(ArrayList())
|
||||
|
||||
_lib.free_pointer(namesPtr, countPtr.getValue())
|
||||
|
||||
result.success(namesAsList)
|
||||
result.success(names)
|
||||
}
|
||||
"getAnimationNames" -> {
|
||||
val assetPtr = Pointer(call.arguments as Long)
|
||||
val countPtr = IntByReference();
|
||||
val arrPtr = _lib.get_animation_names(assetPtr, countPtr)
|
||||
|
||||
val names = arrPtr.getStringArray(0, countPtr.value);
|
||||
|
||||
for(i in 0..countPtr.value-1) {
|
||||
val name = names[i];
|
||||
Log.v(TAG, "Got animation names ${name} ${name.length}")
|
||||
val names = mutableListOf<String>()
|
||||
val outPtr = Memory(256)
|
||||
for(i in 0.._lib.get_animation_count(assetPtr) - 1) {
|
||||
_lib.get_animation_name(assetPtr, outPtr, i)
|
||||
val name = outPtr.getString(0)
|
||||
names.add(name)
|
||||
}
|
||||
|
||||
_lib.free_pointer(arrPtr, 1)
|
||||
|
||||
result.success(names.toCollection(ArrayList()))
|
||||
result.success(names)
|
||||
}
|
||||
"applyWeights" -> {
|
||||
val args = call.arguments as ArrayList<*>
|
||||
|
||||
Reference in New Issue
Block a user