From 2cfb8ee78bd6f5c222ef114396c2146c5ac42e8f Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 1 Sep 2022 14:27:45 +1000 Subject: [PATCH] add android bindings for set_frame_interval and use frame time for render --- .../app/polyvox/filament/FilamentInterop.kt | 5 ++- .../polyvox/filament/PolyvoxFilamentPlugin.kt | 40 ++++++++++--------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt index 9dda5f84..457ac314 100644 --- a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt +++ b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt @@ -38,7 +38,9 @@ interface FilamentInterop : Library { fun set_camera(viewer:Pointer, asset:Pointer, nodeName:String) : Boolean; - fun render(viewer:Pointer); + fun render(viewer:Pointer, frameTimeInNanos:Long); + + fun set_frame_interval(viewer:Pointer, interval:Float); fun create_swap_chain(viewer:Pointer, surface:Surface, env:JNIEnv); @@ -59,6 +61,7 @@ interface FilamentInterop : Library { fun animate_weights(asset:Pointer, frames:FloatArray, numWeights:Int, numFrames:Int, frameRate:Float); 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_count(asset:Pointer) : Int; diff --git a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt index 903a9b2c..dc74737a 100644 --- a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt +++ b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt @@ -27,7 +27,7 @@ import android.content.pm.PackageManager import io.flutter.FlutterInjector -import android.os.CountDownTimer +import android.os.CountDownTimer import android.os.Handler import android.opengl.GLU @@ -92,12 +92,14 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { private val startTime = System.nanoTime() override fun doFrame(frameTimeNanos: Long) { choreographer.postFrameCallback(this) + executor.execute { - if(_viewer != null) { - if(!surface.isValid()) { - Log.v(TAG, "INVALID") - } - _lib.render(_viewer!!) + if(_viewer == null) { + + } else if(!surface.isValid()) { + Log.v(TAG, "INVALID") + } else { + _lib.render(_viewer!!, frameTimeNanos) } } } @@ -114,7 +116,7 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { /// when the Flutter Engine is detached from the Activity private lateinit var channel : MethodChannel - /// Keep a referene to the plugin binding so we can use the TextureRegistry when initialize is called from the platform channel. + /// Keep a reference to the plugin binding so we can use the TextureRegistry when initialize is called from the platform channel. private lateinit var flutterPluginBinding : FlutterPlugin.FlutterPluginBinding private var lifecycle: Lifecycle? = null @@ -147,10 +149,9 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { override fun onAttachedToActivity(binding: ActivityPluginBinding) { lifecycle = (binding.lifecycle as? HiddenLifecycleReference)?.lifecycle activity = binding.activity + activity.window.setFormat(PixelFormat.RGBA_8888) choreographer = Choreographer.getInstance() choreographer.postFrameCallback(frameCallback) - activity.window.setFormat(PixelFormat.RGBA_8888) - } fun getAssetPath(path:String) : String { @@ -170,13 +171,12 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { val entry = flutterPluginBinding.textureRegistry.createSurfaceTexture(); executor.execute { + if(_viewer != null) { - synchronized(lock) { - print("Deleting existing viewer") - _lib.filament_viewer_delete(_viewer!!); - print("Deleted viewer") - _viewer = null; - } + print("Deleting existing viewer") + _lib.filament_viewer_delete(_viewer!!); + print("Deleted viewer") + _viewer = null; } if(surfaceTexture != null) { print("Releasing existing texture") @@ -187,7 +187,6 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { val width = args[0] val height = args[1] - surfaceTexture = entry.surfaceTexture() surfaceTexture!!.setDefaultBufferSize(width, height) @@ -198,9 +197,6 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { surface as Object, JNIEnv.CURRENT, (activity as Context).assets) - - - _lib.update_viewport_and_camera_projection(_viewer!!, width, height, 1.0f); result.success(entry.id().toInt()) @@ -217,6 +213,12 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { _lib.update_viewport_and_camera_projection(_viewer!!, width, height, scale); result.success(null) } + } + "setFrameInterval" -> { + executor.execute { + _lib.set_frame_interval(_viewer!!, (call.arguments as Double).toFloat()); + result.success(null) + } } "setBackgroundImage" -> { executor.execute {