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

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