diff --git a/example/lib/main.dart b/example/lib/main.dart index 48e4b745..d813f8b2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -139,7 +139,10 @@ class _ExampleWidgetState extends State { }, 'transform to unit cube'), _item(() async { _filamentController.setPosition(_cube!, 1.0, 1.0, -1.0); - }, 'set position to 1, 1, -1'), + }, 'set cube position to 1, 1, -1'), + _item(() async { + _filamentController.setPosition(_cube!, 1.0, 1.0, -1.0); + }, 'move camera to cube position'), _item(() async { var frameData = Float32List.fromList( List.generate(120, (i) => i / 120).expand((x) { @@ -244,6 +247,10 @@ class _ExampleWidgetState extends State { _filamentController.setToneMapping(ToneMapper.LINEAR); }, "Set tone mapping to linear")); + children.add(_item(() { + _filamentController.moveCameraToAsset(_cube!); + }, "Move camera to asset")); + return Stack(children: [ Positioned.fill( bottom: 100, diff --git a/ios/include/FilamentViewer.hpp b/ios/include/FilamentViewer.hpp index 52c224dc..e3a420de 100644 --- a/ios/include/FilamentViewer.hpp +++ b/ios/include/FilamentViewer.hpp @@ -81,6 +81,8 @@ namespace polyvox { void setBackgroundImage(const char* resourcePath, bool fillHeight); void clearBackgroundImage(); void setBackgroundImagePosition(float x, float y, bool clamp); + void moveCameraToAsset(EntityId entityId); + void setCameraExposure(float aperture, float shutterSpeed, float sensitivity); void setCameraPosition(float x, float y, float z); void setCameraRotation(float rads, float x, float y, float z); diff --git a/ios/include/PolyvoxFilamentApi.h b/ios/include/PolyvoxFilamentApi.h index b227c6d8..4fcd3437 100644 --- a/ios/include/PolyvoxFilamentApi.h +++ b/ios/include/PolyvoxFilamentApi.h @@ -89,6 +89,7 @@ void transform_to_unit_cube(void* assetManager, EntityId asset); void set_position(void* assetManager, EntityId asset, float x, float y, float z); void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z); void set_scale(void* assetManager, EntityId asset, float scale); +void move_camera_to_asset(const void* const viewer, EntityId asset); void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity); void set_camera_position(const void* const viewer, float x, float y, float z); void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z); diff --git a/ios/src/AssetManager.cpp b/ios/src/AssetManager.cpp index 8002a912..845086df 100644 --- a/ios/src/AssetManager.cpp +++ b/ios/src/AssetManager.cpp @@ -21,7 +21,6 @@ #include "Log.hpp" #include "AssetManager.hpp" -#include "material/StandardMaterialProvider.hpp" #include "material/UnlitMaterialProvider.hpp" #include "material/FileMaterialProvider.hpp" #include "gltfio/materials/uberarchive.h" diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index 75149547..9e759d60 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -113,7 +113,7 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper* #if TARGET_OS_IPHONE _engine = Engine::create(Engine::Backend::METAL); #else - _engine = Engine::create(Engine::Backend::OPENGL, nullptr, (void*)context, nullptr); + _engine = Engine::create(Engine::Backend::OPENGL); //L, nullptr, (void*)context, nullptr); #endif _renderer = _engine->createRenderer(); @@ -870,9 +870,23 @@ void FilamentViewer::setCameraPosition(float x, float y, float z) { cam.setModelMatrix(_cameraPosition * _cameraRotation); } +void FilamentViewer::moveCameraToAsset(EntityId entityId) { + + auto asset = _assetManager->getAssetByEntityId(entityId); + if(!asset) { + Log("Failed to find asset attached to specified entity id."); + return; + } + + const filament::Aabb bb = asset->mAsset->getBoundingBox(); + Camera& cam =_view->getCamera(); + _cameraPosition = math::mat4f::translation(bb.getCorners()); + _cameraRotation = math::mat4f(); + cam.setModelMatrix(_cameraPosition * _cameraRotation); +} + void FilamentViewer::setCameraRotation(float rads, float x, float y, float z) { Camera& cam =_view->getCamera(); - _cameraRotation = math::mat4f::rotation(rads, math::float3(x,y,z)); cam.setModelMatrix(_cameraPosition * _cameraRotation); } diff --git a/ios/src/PolyvoxFilamentApi.cpp b/ios/src/PolyvoxFilamentApi.cpp index 19419b49..d90c9395 100644 --- a/ios/src/PolyvoxFilamentApi.cpp +++ b/ios/src/PolyvoxFilamentApi.cpp @@ -97,6 +97,10 @@ extern "C" { return ((FilamentViewer*)viewer)->setCamera(asset, nodeName); } + FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) { + ((FilamentViewer*)viewer)->moveCameraToAsset(asset, asset); + } + FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) { ((FilamentViewer*)viewer)->setCameraFocusDistance(distance); } diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index fb611e7b..2539264f 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -3,6 +3,7 @@ import 'dart:ui' as ui; import 'package:flutter/services.dart'; import 'package:polyvox_filament/animations/bone_animation_data.dart'; import 'package:polyvox_filament/animations/morph_animation_data.dart'; +import 'package:polyvox_filament/generated_bindings_web.dart'; typedef AssetManager = int; typedef FilamentEntity = int; @@ -519,6 +520,13 @@ class FilamentController { await _channel.invokeMethod("setCameraPosition", [x, y, z]); } + Future moveCameraToAsset(FilamentEntity asset) async { + if (_viewer == null || _resizing) { + throw Exception("No viewer available, ignoring"); + } + await _channel.invokeMethod("moveCameraToAsset", asset); + } + Future setCameraExposure( double aperture, double shutterSpeed, double sensitivity) async { if (_viewer == null || _resizing) { diff --git a/lib/filament_gesture_detector.dart b/lib/filament_gesture_detector.dart index 6a8e644b..b398dd36 100644 --- a/lib/filament_gesture_detector.dart +++ b/lib/filament_gesture_detector.dart @@ -104,8 +104,11 @@ class _FilamentGestureDetectorState extends State { ? null : (d) async { if (d.buttons == kTertiaryButton || _rotating) { - widget.controller - .rotateStart(d.localPosition.dx, d.localPosition.dy); + print("Starting at ${d.position}"); + widget.controller.rotateStart( + d.position.dx * 2.0, + d.position.dy * + 2.0); // multiply by 2.0 to account for pixel density, TODO don't hardcode } else { widget.controller .panStart(d.localPosition.dx, d.localPosition.dy); @@ -115,8 +118,9 @@ class _FilamentGestureDetectorState extends State { ? null : (PointerMoveEvent d) async { if (d.buttons == kTertiaryButton || _rotating) { + print("Updating at ${d.position}"); widget.controller - .rotateUpdate(d.localPosition.dx, d.localPosition.dy); + .rotateUpdate(d.position.dx * 2.0, d.position.dy * 2.0); } else { widget.controller .panUpdate(d.localPosition.dx, d.localPosition.dy); diff --git a/lib/filament_widget.dart b/lib/filament_widget.dart index 20977965..ce5dc9bb 100644 --- a/lib/filament_widget.dart +++ b/lib/filament_widget.dart @@ -143,7 +143,6 @@ class _FilamentWidgetState extends State { if (_textureId == null) { return Container(color: Colors.transparent); } - var texture = Texture( key: ObjectKey("texture_$_textureId"), textureId: _textureId!, diff --git a/macos/Classes/SwiftPolyvoxFilamentPlugin.swift b/macos/Classes/SwiftPolyvoxFilamentPlugin.swift index e81166c1..1c6dfe55 100644 --- a/macos/Classes/SwiftPolyvoxFilamentPlugin.swift +++ b/macos/Classes/SwiftPolyvoxFilamentPlugin.swift @@ -34,6 +34,8 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture var uriString = String(cString:uri!) var path:String? = nil + + print("Received request to load \(uriString)") if(uriString.hasPrefix("file://")) { path = String(uriString.dropFirst(7)) @@ -47,12 +49,13 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture if(path != nil) { do { - print("Attempting to load file at path \(path!)") + print("Loading file at path \(path!)") let data = try Data(contentsOf: URL(fileURLWithPath:path!)) let nsData = data as NSData let resId = UInt32(instance.resources.count) instance.resources[resId] = nsData let length = nsData.length + print("Got file of length \(length)") return ResourceBuffer(data:nsData.bytes, size:UInt32(nsData.count), id:UInt32(resId)) } catch { print("ERROR LOADING RESOURCE") @@ -613,7 +616,9 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture } let success = set_camera(viewer, asset, nodeName) result(success) - + case "moveCameraToAsset": + move_camera_to_asset(viewer, call.arguments as! EntityId) + result(true) case "setCameraPosition": let args = call.arguments as! [Any] set_camera_position(viewer, Float(args[0] as! Double), Float(args[1] as! Double), Float(args[2] as! Double)) diff --git a/macos/include/FilamentViewer.hpp b/macos/include/FilamentViewer.hpp index 10dd8633..3d6a8f55 100644 --- a/macos/include/FilamentViewer.hpp +++ b/macos/include/FilamentViewer.hpp @@ -81,6 +81,7 @@ namespace polyvox { void setBackgroundImage(const char* resourcePath); void clearBackgroundImage(); void setBackgroundImagePosition(float x, float y, bool clamp); + void moveCameraToAsset(EntityId entityId); void setCameraExposure(float aperture, float shutterSpeed, float sensitivity); void setCameraPosition(float x, float y, float z); void setCameraRotation(float rads, float x, float y, float z); diff --git a/macos/include/PolyvoxFilamentApi.h b/macos/include/PolyvoxFilamentApi.h index 5fe2eca0..ac4ec8e5 100644 --- a/macos/include/PolyvoxFilamentApi.h +++ b/macos/include/PolyvoxFilamentApi.h @@ -89,6 +89,7 @@ void transform_to_unit_cube(void* assetManager, EntityId asset); void set_position(void* assetManager, EntityId asset, float x, float y, float z); void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z); void set_scale(void* assetManager, EntityId asset, float scale); +void move_camera_to_asset(const void* const viewer, EntityId asset); void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity); void set_camera_position(const void* const viewer, float x, float y, float z); void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z); diff --git a/macos/src/AssetManager.cpp b/macos/src/AssetManager.cpp index e923aa73..ea5067e0 100644 --- a/macos/src/AssetManager.cpp +++ b/macos/src/AssetManager.cpp @@ -52,9 +52,13 @@ _scene(scene) { _gltfResourceLoader = new ResourceLoader({.engine = _engine, .normalizeSkinningWeights = true }); - auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/materials.uberz"); + // auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/materials.uberz"); + // _ubershaderProvider = gltfio::createUbershaderProvider( + // _engine, uberdata.data, uberdata.size); _ubershaderProvider = gltfio::createUbershaderProvider( - _engine, uberdata.data, uberdata.size); + _engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE); + + // _ubershaderProvider = gltfio::createJitShaderProvider(_engine, true); // _ubershaderProvider = new StandardMaterialProvider(_engine); @@ -74,7 +78,6 @@ _scene(scene) { AssetManager::~AssetManager() { _gltfResourceLoader->asyncCancelLoad(); _ubershaderProvider->destroyMaterials(); - //_unlitProvider->destroyMaterials(); destroyAll(); AssetLoader::destroy(&_assetLoader); diff --git a/macos/src/FilamentViewer.cpp b/macos/src/FilamentViewer.cpp index f4c03d7e..778e75f7 100644 --- a/macos/src/FilamentViewer.cpp +++ b/macos/src/FilamentViewer.cpp @@ -861,6 +861,20 @@ void FilamentViewer::updateViewportAndCameraProjection( contentScaleFactor); } +void FilamentViewer::moveCameraToAsset(EntityId entityId) { + + auto asset = _assetManager->getAssetByEntityId(entityId); + if(!asset) { + Log("Failed to find asset attached to specified entity id."); + return; + } + + const filament::Aabb bb = asset->getBoundingBox(); + auto corners = bb.getCorners(); + Camera& cam =_view->getCamera(); + cam.lookAt(corners.vertices[0], corners.vertices[7]); +} + void FilamentViewer::setCameraPosition(float x, float y, float z) { Camera& cam =_view->getCamera(); diff --git a/macos/src/PolyvoxFilamentApi.cpp b/macos/src/PolyvoxFilamentApi.cpp index f7a8d7d2..bca5349f 100644 --- a/macos/src/PolyvoxFilamentApi.cpp +++ b/macos/src/PolyvoxFilamentApi.cpp @@ -97,6 +97,10 @@ extern "C" { return ((FilamentViewer*)viewer)->setCamera(asset, nodeName); } + FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) { + ((FilamentViewer*)viewer)->moveCameraToAsset(asset); + } + FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) { ((FilamentViewer*)viewer)->setCameraFocusDistance(distance); }