add android integration & make iOS API consistent
add pubspec android plugin update android CMakeLists add filament android libs for linking
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package app.polyvox.filament
|
||||
|
||||
import com.sun.jna.Library
|
||||
import com.sun.jna.Native
|
||||
import com.sun.jna.Pointer
|
||||
import com.sun.jna.ptr.PointerByReference
|
||||
import com.sun.jna.ptr.IntByReference
|
||||
import com.sun.jna.Structure
|
||||
import com.sun.jna.NativeLibrary
|
||||
import com.sun.jna.StringArray
|
||||
import com.sun.jna.JNIEnv
|
||||
|
||||
import android.view.Surface
|
||||
|
||||
import android.content.res.AssetManager
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
interface FilamentInterop : Library {
|
||||
|
||||
fun filament_viewer_new(
|
||||
layer:Object,
|
||||
opaqueShaderPath:String,
|
||||
fadeShaderPath:String,
|
||||
env:JNIEnv,
|
||||
am:AssetManager
|
||||
) : Pointer;
|
||||
|
||||
fun load_skybox(viewer:Pointer, skyboxPath:String, iblPath:String) : Pointer;
|
||||
|
||||
fun load_glb(viewer:Pointer, uri:String) : Pointer;
|
||||
|
||||
fun load_gltf(viewer:Pointer, uri:String, relativeResourcePath:String) : Pointer;
|
||||
|
||||
fun set_camera(viewer:Pointer, nodeName:String) : Pointer;
|
||||
|
||||
fun render(viewer:Pointer);
|
||||
|
||||
fun create_swap_chain(viewer:Pointer, surface:Surface, env:JNIEnv);
|
||||
|
||||
fun destroy_swap_chain(viewer:Pointer);
|
||||
|
||||
fun update_viewport_and_camera_projection(viewer:Pointer, width:Int, height:Int, scaleFactor:Float);
|
||||
|
||||
fun scroll(viewer:Pointer, x:Float, y:Float, z:Float);
|
||||
|
||||
fun grab_begin(viewer:Pointer, x:Int, y:Int, pan:Boolean)
|
||||
|
||||
fun grab_update(viewer:Pointer, x:Int, y:Int)
|
||||
|
||||
fun grab_end(viewer:Pointer)
|
||||
|
||||
fun apply_weights(viewer:Pointer, weights:FloatArray, size:Int);
|
||||
|
||||
fun get_target_names(viewer:Pointer, meshName:String, outPtr:PointerByReference, outLen:IntByReference);
|
||||
|
||||
fun free_pointer(ptr:Pointer, size:Int)
|
||||
|
||||
fun release_source_assets(viewer:Pointer)
|
||||
|
||||
}
|
||||
298
android/src/main/kotlin/app/polyvox/filament/FilamentView.kt
Normal file
298
android/src/main/kotlin/app/polyvox/filament/FilamentView.kt
Normal file
@@ -0,0 +1,298 @@
|
||||
package app.polyvox.filament
|
||||
import android.content.res.AssetManager
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.graphics.SurfaceTexture
|
||||
import android.graphics.PixelFormat
|
||||
|
||||
import io.flutter.FlutterInjector
|
||||
|
||||
import android.opengl.GLU
|
||||
import javax.microedition.khronos.egl.EGLConfig
|
||||
import javax.microedition.khronos.opengles.GL10
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.nio.FloatBuffer
|
||||
import android.hardware.Camera
|
||||
import android.opengl.GLSurfaceView
|
||||
import android.view.SurfaceView
|
||||
import android.view.TextureView
|
||||
import android.view.View
|
||||
import android.view.Surface
|
||||
import android.widget.TextView
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import io.flutter.plugin.common.BinaryMessenger
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.platform.PlatformView
|
||||
import java.io.IOException
|
||||
|
||||
import android.util.Log
|
||||
|
||||
import com.sun.jna.Library
|
||||
import com.sun.jna.Native
|
||||
import com.sun.jna.Pointer
|
||||
import com.sun.jna.ptr.PointerByReference
|
||||
import com.sun.jna.ptr.IntByReference
|
||||
import com.sun.jna.Structure
|
||||
import com.sun.jna.NativeLibrary
|
||||
import com.sun.jna.StringArray
|
||||
import com.sun.jna.JNIEnv
|
||||
|
||||
import android.R.attr.path
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.google.android.filament.android.DisplayHelper
|
||||
|
||||
import android.hardware.display.DisplayManager
|
||||
|
||||
import com.google.android.filament.android.*
|
||||
import com.google.android.filament.*
|
||||
import com.google.android.filament.SwapChain
|
||||
import com.google.android.filament.utils.*
|
||||
|
||||
import android.view.Choreographer
|
||||
import android.view.SurfaceHolder
|
||||
|
||||
|
||||
class FilamentView(
|
||||
private val viewId: Int,
|
||||
private val context: Context,
|
||||
private val activity: Activity,
|
||||
private val binaryMessenger: BinaryMessenger,
|
||||
private val creationParams : Map<String?, Any?>?
|
||||
) : DefaultLifecycleObserver,
|
||||
MethodChannel.MethodCallHandler,
|
||||
PlatformView {
|
||||
|
||||
companion object {
|
||||
const val TAG = "FilamentView"
|
||||
}
|
||||
|
||||
private val _view = SurfaceView(context)
|
||||
|
||||
override fun getView(): View {
|
||||
return _view
|
||||
}
|
||||
|
||||
private val _methodChannel: MethodChannel
|
||||
|
||||
private lateinit var _lib : FilamentInterop
|
||||
|
||||
private var _viewer : Pointer? = null
|
||||
|
||||
private lateinit var choreographer: Choreographer
|
||||
private lateinit var displayHelper : DisplayHelper
|
||||
private val frameScheduler = FrameCallback()
|
||||
|
||||
private lateinit var uiHelper : UiHelper
|
||||
|
||||
private lateinit var assetManager : AssetManager
|
||||
|
||||
init {
|
||||
MethodChannel(binaryMessenger, PolyvoxFilamentPlugin.VIEW_TYPE + '_' + viewId).also {
|
||||
_methodChannel = it
|
||||
it.setMethodCallHandler(this)
|
||||
}
|
||||
_lib = Native.loadLibrary("filament_interop", FilamentInterop::class.java, Collections.singletonMap(Library.OPTION_ALLOW_OBJECTS, true))
|
||||
|
||||
|
||||
choreographer = Choreographer.getInstance()
|
||||
|
||||
_view.setZOrderOnTop(false)
|
||||
_view.holder.setFormat(PixelFormat.OPAQUE)
|
||||
|
||||
_view.holder.addCallback (object : SurfaceHolder.Callback {
|
||||
override fun surfaceChanged(holder:SurfaceHolder, format:Int, width:Int, height:Int) {
|
||||
_lib.update_viewport_and_camera_projection(_viewer!!, width, height, 1.0f);
|
||||
}
|
||||
|
||||
override fun surfaceCreated(holder:SurfaceHolder) {
|
||||
_lib.destroy_swap_chain(_viewer!!)
|
||||
_lib.create_swap_chain(_viewer!!, _view.holder.surface, JNIEnv.CURRENT)
|
||||
}
|
||||
override fun surfaceDestroyed(holder:SurfaceHolder) {
|
||||
_lib.destroy_swap_chain(_viewer!!)
|
||||
}
|
||||
})
|
||||
assetManager = context.assets
|
||||
_viewer = _lib.filament_viewer_new(
|
||||
_view.holder.surface as Object,
|
||||
"unused",
|
||||
"unused",
|
||||
JNIEnv.CURRENT,
|
||||
assetManager)
|
||||
|
||||
choreographer.postFrameCallback(frameScheduler)
|
||||
|
||||
val mDisplayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
|
||||
|
||||
activity.window.setFormat(PixelFormat.RGBA_8888)
|
||||
|
||||
uiHelper = UiHelper(UiHelper.ContextErrorPolicy.DONT_CHECK)
|
||||
uiHelper.renderCallback = SurfaceCallback()
|
||||
uiHelper.attachTo(_view)
|
||||
}
|
||||
|
||||
override fun onFlutterViewAttached(flutterView:View) {
|
||||
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
_methodChannel.setMethodCallHandler(null)
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||
when (call.method) {
|
||||
"loadSkybox" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
val loader = FlutterInjector.instance().flutterLoader()
|
||||
_lib.load_skybox(_viewer!!, loader.getLookupKeyForAsset(args[0] as String), loader.getLookupKeyForAsset(args[1] as String))
|
||||
result.success("OK");
|
||||
}
|
||||
"loadGlb" -> {
|
||||
if (_viewer == null)
|
||||
return;
|
||||
val loader = FlutterInjector.instance().flutterLoader()
|
||||
_lib.load_glb(
|
||||
_viewer!!,
|
||||
loader.getLookupKeyForAsset(call.arguments as String)
|
||||
)
|
||||
result.success("OK");
|
||||
}
|
||||
"loadGltf" -> {
|
||||
if (_viewer == null)
|
||||
return;
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
val loader = FlutterInjector.instance().flutterLoader()
|
||||
_lib.load_gltf(
|
||||
_viewer!!,
|
||||
loader.getLookupKeyForAsset(args[0] as String),
|
||||
loader.getLookupKeyForAsset(args[1] as String)
|
||||
)
|
||||
result.success("OK");
|
||||
}
|
||||
"setCamera" -> {
|
||||
if (_viewer == null)
|
||||
return;
|
||||
_lib.set_camera(
|
||||
_viewer!!,
|
||||
call.arguments as String
|
||||
)
|
||||
result.success("OK");
|
||||
}
|
||||
"zoom" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
_lib.scroll(_viewer!!, 0.0f, 0.0f, (call.arguments as Double).toFloat())
|
||||
result.success("OK");
|
||||
}
|
||||
"getTargetNames" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
val arrPtr = PointerByReference();
|
||||
val countPtr = IntByReference();
|
||||
_lib.get_target_names(_viewer!!, call.arguments as String, arrPtr, countPtr)
|
||||
|
||||
val names = arrPtr.value.getStringArray(0, countPtr.value);
|
||||
|
||||
_lib.free_pointer(arrPtr.value, countPtr.getValue())
|
||||
val namesAsList = names.toCollection(ArrayList())
|
||||
result.success(namesAsList)
|
||||
}
|
||||
"applyWeights" -> {
|
||||
if(_viewer == null)
|
||||
return;
|
||||
val weights = call.arguments as ArrayList<Float>;
|
||||
|
||||
_lib.apply_weights(_viewer!!, weights.toFloatArray(), weights.size)
|
||||
result.success("OK");
|
||||
}
|
||||
"panStart" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.grab_begin(_viewer!!, args[0] as Int, args[1] as Int, true)
|
||||
result.success("OK");
|
||||
}
|
||||
"panUpdate" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.grab_update(_viewer!!, args[0] as Int, args[1] as Int)
|
||||
result.success("OK");
|
||||
}
|
||||
"panEnd" -> {
|
||||
_lib.grab_end(_viewer!!)
|
||||
result.success("OK");
|
||||
}
|
||||
"rotateStart" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.grab_begin(_viewer!!, args[0] as Int, args[1] as Int, false)
|
||||
result.success("OK");
|
||||
}
|
||||
"rotateUpdate" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.grab_update(_viewer!!, args[0] as Int, args[1] as Int)
|
||||
result.success("OK");
|
||||
}
|
||||
"rotateEnd" -> {
|
||||
_lib.grab_end(_viewer!!)
|
||||
result.success("OK");
|
||||
}
|
||||
"grabStart" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.grab_begin(_viewer!!, args[0] as Int, args[1] as Int, true)
|
||||
result.success("OK");
|
||||
}
|
||||
"grabUpdate" -> {
|
||||
val args = call.arguments as ArrayList<Any?>
|
||||
_lib.grab_update(_viewer!!, args[0] as Int, args[1] as Int)
|
||||
result.success("OK");
|
||||
}
|
||||
"grabEnd" -> {
|
||||
_lib.grab_end(_viewer!!)
|
||||
result.success("OK");
|
||||
}
|
||||
"releaseSourceAssets" -> {
|
||||
_lib.release_source_assets(_viewer!!)
|
||||
result.success("OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class SurfaceCallback : UiHelper.RendererCallback {
|
||||
override fun onNativeWindowChanged(surface: Surface) {
|
||||
_lib.destroy_swap_chain(_viewer!!)
|
||||
_lib.create_swap_chain(_viewer!!, surface, JNIEnv.CURRENT)
|
||||
}
|
||||
|
||||
override fun onDetachedFromSurface() {
|
||||
_lib.destroy_swap_chain(_viewer!!)
|
||||
}
|
||||
|
||||
override fun onResized(width: Int, height: Int) {
|
||||
_lib.update_viewport_and_camera_projection(_viewer!!, width, height, 1.0f)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inner class FrameCallback : Choreographer.FrameCallback {
|
||||
private val startTime = System.nanoTime()
|
||||
override fun doFrame(frameTimeNanos: Long) {
|
||||
choreographer.postFrameCallback(this)
|
||||
|
||||
// modelViewer.animator?.apply {
|
||||
// if (animationCount > 0) {
|
||||
// val elapsedTimeSeconds = (frameTimeNanos - startTime).toDouble() / 1_000_000_000
|
||||
// applyAnimation(0, elapsedTimeSeconds.toFloat())
|
||||
// }
|
||||
// updateBoneMatrices()
|
||||
// }
|
||||
_lib.render(_viewer!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package app.polyvox.filament
|
||||
|
||||
import io.flutter.plugin.common.BinaryMessenger
|
||||
import android.app.Activity
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import io.flutter.plugin.common.StandardMessageCodec
|
||||
import io.flutter.plugin.platform.PlatformView
|
||||
import io.flutter.plugin.platform.PlatformViewFactory
|
||||
|
||||
class FilamentViewFactory(
|
||||
private val activity: Activity,
|
||||
private val binaryMessenger: BinaryMessenger
|
||||
) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
|
||||
override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
|
||||
val creationParams = args as Map<String?, Any?>?
|
||||
return FilamentView(viewId, context, activity, binaryMessenger, creationParams)
|
||||
}
|
||||
}
|
||||
@@ -2,26 +2,53 @@ package app.polyvox.filament
|
||||
|
||||
import androidx.annotation.NonNull
|
||||
|
||||
import androidx.lifecycle.Lifecycle
|
||||
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
|
||||
import io.flutter.embedding.engine.plugins.lifecycle.HiddenLifecycleReference
|
||||
|
||||
|
||||
/** PolyvoxFilamentPlugin */
|
||||
class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler {
|
||||
class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
|
||||
companion object {
|
||||
const val VIEW_TYPE = "app.polyvox.filament/filament_view"
|
||||
}
|
||||
|
||||
/// The MethodChannel that will the communication between Flutter and native Android
|
||||
///
|
||||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
|
||||
/// when the Flutter Engine is detached from the Activity
|
||||
private lateinit var channel : MethodChannel
|
||||
|
||||
/// Keep a referene to the plugin binding so we can defer construction of a FilamentViewFactory
|
||||
/// until Activity is attached.
|
||||
private lateinit var flutterPluginBinding : FlutterPlugin.FlutterPluginBinding
|
||||
|
||||
private var lifecycle: Lifecycle? = null
|
||||
|
||||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "polyvox_filament")
|
||||
this.flutterPluginBinding = flutterPluginBinding
|
||||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "app.polyvox.filament")
|
||||
channel.setMethodCallHandler(this)
|
||||
}
|
||||
|
||||
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
|
||||
lifecycle = (binding.lifecycle as? HiddenLifecycleReference)?.lifecycle
|
||||
flutterPluginBinding
|
||||
.platformViewRegistry
|
||||
.registerViewFactory(VIEW_TYPE, FilamentViewFactory(binding.activity, flutterPluginBinding.binaryMessenger))
|
||||
}
|
||||
|
||||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
|
||||
|
||||
if (call.method == "getPlatformVersion") {
|
||||
result.success("Android ${android.os.Build.VERSION.RELEASE}")
|
||||
} else {
|
||||
@@ -32,4 +59,17 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler {
|
||||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel.setMethodCallHandler(null)
|
||||
}
|
||||
|
||||
|
||||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
|
||||
onAttachedToActivity(binding)
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivityForConfigChanges() {
|
||||
onDetachedFromActivity()
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivity() {
|
||||
lifecycle = null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user