From d3faaac6c0145d634c64f72c70f253cae98d2f61 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Fri, 26 Aug 2022 00:25:45 +1000 Subject: [PATCH] add camera utils to API --- .../app/polyvox/filament/FilamentInterop.kt | 5 +++ .../polyvox/filament/PolyvoxFilamentPlugin.kt | 35 ++++++++++++++++--- ios/src/FilamentViewer.cpp | 22 ++++++++++++ ios/src/FilamentViewer.hpp | 5 ++- ios/src/PolyvoxFilamentApi.cpp | 8 +++-- ios/src/PolyvoxFilamentApi.hpp | 2 ++ lib/filament_controller.dart | 18 ++++++++-- 7 files changed, 84 insertions(+), 11 deletions(-) diff --git a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt index 3bd72b54..468a5ad6 100644 --- a/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt +++ b/android/src/main/kotlin/app/polyvox/filament/FilamentInterop.kt @@ -85,5 +85,10 @@ interface FilamentInterop : Library { fun set_position(asset:Pointer, x:Float, y:Float, z:Float); fun set_rotation(asset:Pointer, rads:Float, x:Float, y:Float, z:Float); + + fun set_camera_position(asset:Pointer, x:Float, y:Float, z:Float); + fun set_camera_rotation(asset:Pointer, rads:Float, x:Float, y:Float, z:Float); + fun set_camera_focal_length(asset:Pointer, focalLength:Float); + fun set_camera_focus_distance(asset:Pointer, focusDistance:Float); } diff --git a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt index 1dc483c1..3b8033a8 100644 --- a/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt +++ b/android/src/main/kotlin/app/polyvox/filament/PolyvoxFilamentPlugin.kt @@ -212,9 +212,9 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { val args = call.arguments as ArrayList val width = args[0] val height = args[1] - + val scale = if(args.size > 2) (args[2] as Double).toFloat() else 1.0f surfaceTexture!!.setDefaultBufferSize(width, height) - _lib.update_viewport_and_camera_projection(_viewer!!, width, height, 1.0f); + _lib.update_viewport_and_camera_projection(_viewer!!, width, height, scale); result.success(null) } } @@ -291,6 +291,32 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { result.success("OK"); } } + "setCameraPosition" -> { + executor.execute { + val args = call.arguments as ArrayList<*> + _lib.set_camera_position(_viewer!!, (args[0] as Double).toFloat(), (args[1] as Double).toFloat(), (args[2] as Double).toFloat()) + result.success("OK"); + } + } + "setCameraRotation" -> { + executor.execute { + val args = call.arguments as ArrayList<*> + _lib.set_camera_rotation(_viewer!!, (args[0] as Double).toFloat(), (args[1] as Double).toFloat(), (args[2] as Double).toFloat(), (args[3] as Double).toFloat()) + result.success("OK"); + } + } + "setCameraFocalLength" -> { + executor.execute { + _lib.set_camera_focal_length(_viewer!!, (call.arguments as Double).toFloat()) + result.success("OK"); + } + } + "setCameraFocusDistance" -> { + executor.execute { + _lib.set_camera_focus_distance(_viewer!!, (call.arguments as Double).toFloat()) + result.success("OK"); + } + } "setTexture" -> { executor.execute { val args = call.arguments as ArrayList<*> @@ -460,14 +486,13 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { channel.setMethodCallHandler(null) - //_lib.destroy_swap_chain(_viewer!!) + _lib.destroy_swap_chain(_viewer!!) } override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { onAttachedToActivity(binding) - //_lib.create_swap_chain(_viewer!!, surface, JNIEnv.CURRENT) - + _lib.create_swap_chain(_viewer!!, surface, JNIEnv.CURRENT) } override fun onDetachedFromActivityForConfigChanges() { diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index 862c2b0b..d61cd45c 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -121,6 +121,7 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource, Entity camera = EntityManager::get().create(); _mainCamera = _engine->createCamera(camera); + Log("Main camera created"); _view = _engine->createView(); _view->setScene(_scene); @@ -133,6 +134,8 @@ FilamentViewer::FilamentViewer(void *layer, LoadResource loadResource, _view->setColorGrading(colorGrading); _cameraFocalLength = 28.0f; + _mainCamera->setLensProjection(_cameraFocalLength, 1.0f, kNearPlane, + kFarPlane); _mainCamera->setExposure(kAperture, kShutterSpeed, kSensitivity); #if TARGET_OS_IPHONE _swapChain = _engine->createSwapChain(layer, filament::backend::SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER); @@ -435,6 +438,25 @@ void FilamentViewer::removeAsset(SceneAsset *asset) { mtx.unlock(); } +/// +/// Set the focal length of the active camera. +/// +void FilamentViewer::setCameraFocalLength(float focalLength) { + Camera& cam =_view->getCamera(); + _cameraFocalLength = focalLength; + cam.setLensProjection(_cameraFocalLength, 1.0f, kNearPlane, + kFarPlane); +} + +/// +/// Set the focus distance of the active camera. +/// +void FilamentViewer::setCameraFocusDistance(float focusDistance) { + Camera& cam =_view->getCamera(); + _cameraFocusDistance = focusDistance; + cam.setFocusDistance(_cameraFocusDistance); +} + /// /// Sets the active camera to the first GLTF camera node found in the hierarchy. /// Useful when your asset only has one camera. diff --git a/ios/src/FilamentViewer.hpp b/ios/src/FilamentViewer.hpp index 1fb2c3bf..fb9068c6 100644 --- a/ios/src/FilamentViewer.hpp +++ b/ios/src/FilamentViewer.hpp @@ -76,6 +76,8 @@ namespace polyvox { void setCameraPosition(float x, float y, float z); void setCameraRotation(float rads, float x, float y, float z); + void setCameraFocalLength(float fl); + void setCameraFocusDistance(float focusDistance); private: void createImageRenderable(); @@ -120,7 +122,8 @@ namespace polyvox { bool _actualSize = false; - float _cameraFocalLength = 0.0f; + float _cameraFocalLength = 28.0f; + float _cameraFocusDistance = 0.0f; // these flags relate to the textured quad we use for rendering unlit background images Texture* _imageTexture = nullptr; diff --git a/ios/src/PolyvoxFilamentApi.cpp b/ios/src/PolyvoxFilamentApi.cpp index 12aa21a9..650b999b 100644 --- a/ios/src/PolyvoxFilamentApi.cpp +++ b/ios/src/PolyvoxFilamentApi.cpp @@ -57,11 +57,15 @@ extern "C" { } void set_camera_position(void* viewer, float x, float y, float z) { - return ((FilamentViewer*)viewer)->setCameraPosition(x, y, z); + ((FilamentViewer*)viewer)->setCameraPosition(x, y, z); } void set_camera_rotation(void* viewer, float rads, float x, float y, float z) { - return ((FilamentViewer*)viewer)->setCameraRotation(rads, x, y, z); + ((FilamentViewer*)viewer)->setCameraRotation(rads, x, y, z); + } + + void set_camera_focal_length(void* viewer, float focalLength) { + ((FilamentViewer*)viewer)->setCameraFocalLength(focalLength); } void render( diff --git a/ios/src/PolyvoxFilamentApi.hpp b/ios/src/PolyvoxFilamentApi.hpp index 37db0cde..46b8a08f 100644 --- a/ios/src/PolyvoxFilamentApi.hpp +++ b/ios/src/PolyvoxFilamentApi.hpp @@ -60,5 +60,7 @@ void stop_animation(void* asset, int index); void set_camera_position(void* viewer, float x, float y, float z); void set_camera_rotation(void* viewer, float rads, float x, float y, float z); +void set_camera_focal_length(void* viewer, float focalLength); +void set_camera_focus_distance(void* viewer, float focusDistance); #endif diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index 1a07a9ff..29d290aa 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -8,7 +8,7 @@ abstract class FilamentController { late int textureId; Future get initialized; Future initialize(int width, int height); - Future resize(int width, int height); + Future resize(int width, int height, {double contentScaleFactor=1}); Future setBackgroundImage(String path); Future loadSkybox(String skyboxPath); Future removeSkybox(); @@ -38,6 +38,10 @@ abstract class FilamentController { Future setPosition(FilamentAsset asset, double x, double y, double z); Future setRotation( FilamentAsset asset, double rads, double x, double y, double z); + Future setCameraFocalLength( + double focalLength); + Future setCameraFocusDistance( + double focusDistance); /// /// Set the weights of all morph targets in the mesh to the specified weights at successive frames (where each frame requires a duration of [frameLengthInMs]. @@ -68,8 +72,8 @@ class PolyvoxFilamentController extends FilamentController { _initialized.complete(true); } - Future resize(int width, int height) async { - await _channel.invokeMethod("resize", [width, height]); + Future resize(int width, int height, { double contentScaleFactor=1.0}) async { + await _channel.invokeMethod("resize", [width, height, contentScaleFactor]); } @override @@ -193,6 +197,14 @@ class PolyvoxFilamentController extends FilamentController { await _channel.invokeMethod("setCamera", [asset, name]); } + Future setCameraFocalLength(double focalLength) async { + await _channel.invokeMethod("setCameraFocalLength", focalLength); + } + + Future setCameraFocusDistance(double focusDistance) async { + await _channel.invokeMethod("setCameraFocusDistance", focusDistance); + } + Future setTexture(FilamentAsset asset, String assetPath, {int renderableIndex = 0}) async { await _channel