add moveCameraToAsset

This commit is contained in:
Nick Fisher
2023-09-25 22:30:07 +08:00
parent 64cfea2952
commit 833022e4e0
15 changed files with 79 additions and 13 deletions

View File

@@ -139,7 +139,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
}, '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<double>.generate(120, (i) => i / 120).expand((x) {
@@ -244,6 +247,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
_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,

View File

@@ -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);

View File

@@ -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);

View File

@@ -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"

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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) {

View File

@@ -104,8 +104,11 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
? 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<FilamentGestureDetector> {
? 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);

View File

@@ -143,7 +143,6 @@ class _FilamentWidgetState extends State<FilamentWidget> {
if (_textureId == null) {
return Container(color: Colors.transparent);
}
var texture = Texture(
key: ObjectKey("texture_$_textureId"),
textureId: _textureId!,

View File

@@ -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))

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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();

View File

@@ -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);
}