merge moveCameraToAsset changes

This commit is contained in:
Nick Fisher
2023-09-27 16:10:10 +10:00
15 changed files with 116 additions and 23 deletions

View File

@@ -55,6 +55,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
bool _initialized = false; bool _initialized = false;
bool _coneHidden = false; bool _coneHidden = false;
bool _frustumCulling = true;
@override @override
void initState() { void initState() {
@@ -144,7 +145,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
}, 'transform to unit cube'), }, 'transform to unit cube'),
_item(() async { _item(() async {
_filamentController.setPosition(_cube!, 1.0, 1.0, -1.0); _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 { _item(() async {
var frameData = Float32List.fromList( var frameData = Float32List.fromList(
List<double>.generate(120, (i) => i / 120).expand((x) { List<double>.generate(120, (i) => i / 120).expand((x) {
@@ -249,6 +253,18 @@ class _ExampleWidgetState extends State<ExampleWidget> {
_filamentController.setToneMapping(ToneMapper.LINEAR); _filamentController.setToneMapping(ToneMapper.LINEAR);
}, "Set tone mapping to linear")); }, "Set tone mapping to linear"));
children.add(_item(() {
_filamentController.moveCameraToAsset(_cube!);
}, "Move camera to asset"));
children.add(_item(() {
setState(() {
_frustumCulling = !_frustumCulling;
});
_filamentController.setViewFrustumCulling(_frustumCulling);
}, "${_frustumCulling ? "Disable" : "Enable"} frustum culling"));
return Stack(children: [ return Stack(children: [
Positioned( Positioned(
bottom: 100, bottom: 100,

View File

@@ -86,6 +86,8 @@ namespace polyvox {
void setBackgroundImage(const char* resourcePath, bool fillHeight); void setBackgroundImage(const char* resourcePath, bool fillHeight);
void clearBackgroundImage(); void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp); void setBackgroundImagePosition(float x, float y, bool clamp);
void moveCameraToAsset(EntityId entityId);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity); void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z); void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z); void setCameraRotation(float rads, float x, float y, float z);

View File

@@ -108,6 +108,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_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_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
void set_scale(void* assetManager, EntityId asset, float scale); 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_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_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); void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z);

View File

@@ -21,6 +21,7 @@
#include "Log.hpp" #include "Log.hpp"
#include "AssetManager.hpp" #include "AssetManager.hpp"
#include "material/UnlitMaterialProvider.hpp"
#include "material/FileMaterialProvider.hpp" #include "material/FileMaterialProvider.hpp"
// #include "gltfio/materials/uberarchive.h" // #include "gltfio/materials/uberarchive.h"

View File

@@ -908,9 +908,23 @@ void FilamentViewer::setCameraPosition(float x, float y, float z) {
cam.setModelMatrix(_cameraPosition * _cameraRotation); 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) { void FilamentViewer::setCameraRotation(float rads, float x, float y, float z) {
Camera& cam =_view->getCamera(); Camera& cam =_view->getCamera();
_cameraRotation = math::mat4f::rotation(rads, math::float3(x,y,z)); _cameraRotation = math::mat4f::rotation(rads, math::float3(x,y,z));
cam.setModelMatrix(_cameraPosition * _cameraRotation); cam.setModelMatrix(_cameraPosition * _cameraRotation);
} }

View File

@@ -96,6 +96,10 @@ extern "C" {
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName); return ((FilamentViewer*)viewer)->setCamera(asset, nodeName);
} }
void move_camera_to_asset(const void* const viewer, EntityId asset) {
((FilamentViewer*)viewer)->moveCameraToAsset(asset, asset);
}
void set_camera_focus_distance(const void* const viewer, float distance) { void set_camera_focus_distance(const void* const viewer, float distance) {
((FilamentViewer*)viewer)->setCameraFocusDistance(distance); ((FilamentViewer*)viewer)->setCameraFocusDistance(distance);
} }

View File

@@ -4,6 +4,7 @@ import 'dart:ui' as ui;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:polyvox_filament/animations/bone_animation_data.dart'; import 'package:polyvox_filament/animations/bone_animation_data.dart';
import 'package:polyvox_filament/animations/morph_animation_data.dart'; import 'package:polyvox_filament/animations/morph_animation_data.dart';
import 'package:polyvox_filament/generated_bindings_web.dart';
typedef AssetManager = int; typedef AssetManager = int;
typedef FilamentEntity = int; typedef FilamentEntity = int;
@@ -523,6 +524,20 @@ class FilamentController {
await _channel.invokeMethod("setCameraPosition", [x, y, z]); 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 setViewFrustumCulling(bool enabled) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setViewFrustumCulling", enabled);
}
Future setCameraExposure( Future setCameraExposure(
double aperture, double shutterSpeed, double sensitivity) async { double aperture, double shutterSpeed, double sensitivity) async {
if (_viewer == null || _resizing) { if (_viewer == null || _resizing) {

View File

@@ -13,13 +13,15 @@ class FilamentGestureDetector extends StatefulWidget {
final FilamentController controller; final FilamentController controller;
final bool showControlOverlay; final bool showControlOverlay;
final bool enableControls; final bool enableControls;
final double zoomDelta;
const FilamentGestureDetector( const FilamentGestureDetector(
{Key? key, {Key? key,
required this.controller, required this.controller,
this.child, this.child,
this.showControlOverlay = false, this.showControlOverlay = false,
this.enableControls = true}) this.enableControls = true,
this.zoomDelta = 1})
: super(key: key); : super(key: key);
@override @override
@@ -91,8 +93,9 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
if (pointerSignal is PointerScrollEvent) { if (pointerSignal is PointerScrollEvent) {
_scrollTimer?.cancel(); _scrollTimer?.cancel();
widget.controller.zoomBegin(); widget.controller.zoomBegin();
widget.controller widget.controller.zoomUpdate(pointerSignal.scrollDelta.dy > 0
.zoomUpdate(pointerSignal.scrollDelta.dy > 0 ? 1 : -1); ? widget.zoomDelta
: -widget.zoomDelta);
_scrollTimer = Timer(Duration(milliseconds: 100), () { _scrollTimer = Timer(Duration(milliseconds: 100), () {
widget.controller.zoomEnd(); widget.controller.zoomEnd();
_scrollTimer = null; _scrollTimer = null;
@@ -104,8 +107,11 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
? null ? null
: (d) async { : (d) async {
if (d.buttons == kTertiaryButton || _rotating) { if (d.buttons == kTertiaryButton || _rotating) {
widget.controller print("Starting at ${d.position}");
.rotateStart(d.localPosition.dx, d.localPosition.dy); 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 { } else {
widget.controller widget.controller
.panStart(d.localPosition.dx, d.localPosition.dy); .panStart(d.localPosition.dx, d.localPosition.dy);
@@ -116,7 +122,7 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
: (PointerMoveEvent d) async { : (PointerMoveEvent d) async {
if (d.buttons == kTertiaryButton || _rotating) { if (d.buttons == kTertiaryButton || _rotating) {
widget.controller widget.controller
.rotateUpdate(d.localPosition.dx, d.localPosition.dy); .rotateUpdate(d.position.dx * 2.0, d.position.dy * 2.0);
} else { } else {
widget.controller widget.controller
.panUpdate(d.localPosition.dx, d.localPosition.dy); .panUpdate(d.localPosition.dx, d.localPosition.dy);

View File

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

View File

@@ -35,6 +35,8 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
var path:String? = nil var path:String? = nil
print("Received request to load \(uriString)")
if(uriString.hasPrefix("file://")) { if(uriString.hasPrefix("file://")) {
path = String(uriString.dropFirst(7)) path = String(uriString.dropFirst(7))
} else { } else {
@@ -47,12 +49,13 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
if(path != nil) { if(path != nil) {
do { do {
print("Attempting to load file at path \(path!)") print("Loading file at path \(path!)")
let data = try Data(contentsOf: URL(fileURLWithPath:path!)) let data = try Data(contentsOf: URL(fileURLWithPath:path!))
let nsData = data as NSData let nsData = data as NSData
let resId = UInt32(instance.resources.count) let resId = UInt32(instance.resources.count)
instance.resources[resId] = nsData instance.resources[resId] = nsData
let length = nsData.length let length = nsData.length
print("Got file of length \(length)")
return ResourceBuffer(data:nsData.bytes, size:UInt32(nsData.count), id:UInt32(resId)) return ResourceBuffer(data:nsData.bytes, size:UInt32(nsData.count), id:UInt32(resId))
} catch { } catch {
print("ERROR LOADING RESOURCE") print("ERROR LOADING RESOURCE")
@@ -613,7 +616,12 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
} }
let success = set_camera(viewer, asset, nodeName) let success = set_camera(viewer, asset, nodeName)
result(success) result(success)
case "moveCameraToAsset":
move_camera_to_asset(viewer, call.arguments as! EntityId)
result(true)
case "setViewFrustumCulling":
set_view_frustum_culling(viewer, call.arguments as! Bool)
result(true)
case "setCameraPosition": case "setCameraPosition":
let args = call.arguments as! [Any] 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)) set_camera_position(viewer, Float(args[0] as! Double), Float(args[1] as! Double), Float(args[2] as! Double))

View File

@@ -81,6 +81,8 @@ namespace polyvox {
void setBackgroundImage(const char* resourcePath); void setBackgroundImage(const char* resourcePath);
void clearBackgroundImage(); void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp); void setBackgroundImagePosition(float x, float y, bool clamp);
void moveCameraToAsset(EntityId entityId);
void setViewFrustumCulling(bool enabled);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity); void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z); void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z); void setCameraRotation(float rads, float x, float y, float z);

View File

@@ -89,6 +89,8 @@ void transform_to_unit_cube(void* assetManager, EntityId asset);
void set_position(void* assetManager, EntityId asset, float x, float y, float z); 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_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
void set_scale(void* assetManager, EntityId asset, float scale); void set_scale(void* assetManager, EntityId asset, float scale);
void move_camera_to_asset(const void* const viewer, EntityId asset);
void set_view_frustum_culling(const void* const viewer, bool enabled);
void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity); 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_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); void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z);

View File

@@ -51,19 +51,16 @@ _scene(scene) {
_gltfResourceLoader = new ResourceLoader({.engine = _engine, _gltfResourceLoader = new ResourceLoader({.engine = _engine,
.normalizeSkinningWeights = true }); .normalizeSkinningWeights = true });
sdfsdfds
auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/default.uberz"); auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/default.uberz");
_ubershaderProvider = gltfio::createUbershaderProvider( _ubershaderProvider = gltfio::createUbershaderProvider(
_engine, uberdata.data, uberdata.size); _engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
// _ubershaderProvider = gltfio::createJitShaderProvider(_engine, true); // _ubershaderProvider = gltfio::createJitShaderProvider(_engine, true);
// _ubershaderProvider = new StandardMaterialProvider(_engine); // _ubershaderProvider = new StandardMaterialProvider(_engine);
EntityManager &em = EntityManager::get(); EntityManager &em = EntityManager::get();
//_unlitProvider = new UnlitMaterialProvider(_engine);
// auto rb = _resourceLoaderWrapper->load("file:///mnt/hdd_2tb/home/hydroxide/projects/polyvox/flutter/polyvox_filament/materials/toon.filamat");
// auto toonProvider = new FileMaterialProvider(_engine, rb.data, (size_t) rb.size);
_assetLoader = AssetLoader::create({_engine, _ubershaderProvider, _ncm, &em }); _assetLoader = AssetLoader::create({_engine, _ubershaderProvider, _ncm, &em });
_gltfResourceLoader->addTextureProvider("image/ktx2", _ktxDecoder); _gltfResourceLoader->addTextureProvider("image/ktx2", _ktxDecoder);
@@ -74,7 +71,6 @@ sdfsdfds
AssetManager::~AssetManager() { AssetManager::~AssetManager() {
_gltfResourceLoader->asyncCancelLoad(); _gltfResourceLoader->asyncCancelLoad();
_ubershaderProvider->destroyMaterials(); _ubershaderProvider->destroyMaterials();
//_unlitProvider->destroyMaterials();
destroyAll(); destroyAll();
AssetLoader::destroy(&_assetLoader); AssetLoader::destroy(&_assetLoader);

View File

@@ -861,6 +861,27 @@ void FilamentViewer::updateViewportAndCameraProjection(
contentScaleFactor); 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();
auto eye = corners.vertices[0] * 1.5;
auto lookAt = corners.vertices[7];
cam.lookAt(eye, lookAt);
Log("Moved camera to %f %f %f, lookAt %f %f %f, near %f far %f", eye[0], eye[1], eye[2], lookAt[0], lookAt[1], lookAt[2], cam.getNear(), cam.getCullingFar());
}
void FilamentViewer::setViewFrustumCulling(bool enabled) {
_view->setFrustumCullingEnabled(enabled);
}
void FilamentViewer::setCameraPosition(float x, float y, float z) { void FilamentViewer::setCameraPosition(float x, float y, float z) {
Camera& cam =_view->getCamera(); Camera& cam =_view->getCamera();
@@ -915,7 +936,7 @@ void FilamentViewer::grabUpdate(float x, float y) {
return; return;
} }
Camera& cam =_view->getCamera(); Camera& cam =_view->getCamera();
auto eye = cam.getPosition();// math::float3 {0.0f, 0.5f, 50.0f } ;// ; // auto eye = cam.getPosition();
auto target = eye + cam.getForwardVector(); auto target = eye + cam.getForwardVector();
auto upward = cam.getUpVector(); auto upward = cam.getUpVector();
Viewport const& vp = _view->getViewport(); Viewport const& vp = _view->getViewport();
@@ -924,9 +945,7 @@ void FilamentViewer::grabUpdate(float x, float y) {
cam.setModelMatrix(trans); cam.setModelMatrix(trans);
} else { } else {
auto trans = cam.getModelMatrix() * mat4::rotation( auto trans = cam.getModelMatrix() * mat4::rotation(
0.02,
0.01,
// math::float3 { 0.0f, 1.0f, 0.0f });
math::float3 { (y - _startY) / vp.height, (x - _startX) / vp.width, 0.0f }); math::float3 { (y - _startY) / vp.height, (x - _startX) / vp.width, 0.0f });
cam.setModelMatrix(trans); cam.setModelMatrix(trans);
} }

View File

@@ -97,6 +97,14 @@ extern "C" {
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName); 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_view_frustum_culling(const void* const viewer, bool enabled) {
((FilamentViewer*)viewer)->setViewFrustumCulling(enabled);
}
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) { FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) {
((FilamentViewer*)viewer)->setCameraFocusDistance(distance); ((FilamentViewer*)viewer)->setCameraFocusDistance(distance);
} }