From 2b1339b5601ff60965469a98c7079433123e99d3 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Wed, 25 Sep 2024 23:09:27 +0800 Subject: [PATCH] feat: expose more camera methods --- thermion_dart/ffigen/native.yaml | 2 +- .../viewer/ffi/src/camera_ffi.dart | 13 ++++---- .../viewer/ffi/src/thermion_dart.g.dart | 18 ++--------- .../viewer/shared_types/camera.dart | 6 ++-- .../native/include/ThermionDartApi.h | 6 ++-- thermion_dart/native/src/ThermionDartApi.cpp | 5 ++++ thermion_dart/test/integration_test.dart | 30 ++++++++++++++----- 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/thermion_dart/ffigen/native.yaml b/thermion_dart/ffigen/native.yaml index 67ed918f..f95f8bd6 100644 --- a/thermion_dart/ffigen/native.yaml +++ b/thermion_dart/ffigen/native.yaml @@ -1,4 +1,4 @@ -output: '../lib/thermion_dart/viewer/ffi/thermion_dart.g.dart' +output: '../lib/thermion_dart/viewer/ffi/src/thermion_dart.g.dart' headers: entry-points: - '../native/include/ThermionDartRenderThreadApi.h' diff --git a/thermion_dart/lib/thermion_dart/viewer/ffi/src/camera_ffi.dart b/thermion_dart/lib/thermion_dart/viewer/ffi/src/camera_ffi.dart index dd7623fc..1f9f1397 100644 --- a/thermion_dart/lib/thermion_dart/viewer/ffi/src/camera_ffi.dart +++ b/thermion_dart/lib/thermion_dart/viewer/ffi/src/camera_ffi.dart @@ -11,12 +11,13 @@ class ThermionFFICamera extends Camera { ThermionFFICamera(this.pointer); @override - Future setProjectionMatrixWithCulling(Matrix4 projectionMatrix, - double near, double far) async { + Future setProjectionMatrixWithCulling( + Matrix4 projectionMatrix, double near, double far) async { Camera_setCustomProjectionWithCulling( - pointer, - matrix4ToDouble4x4(projectionMatrix), - near, - far); + pointer, matrix4ToDouble4x4(projectionMatrix), near, far); + } + + Future getModelMatrix() async { + return double4x4ToMatrix4(Camera_getModelMatrix(pointer)); } } diff --git a/thermion_dart/lib/thermion_dart/viewer/ffi/src/thermion_dart.g.dart b/thermion_dart/lib/thermion_dart/viewer/ffi/src/thermion_dart.g.dart index e63e4e2b..43cd6ea3 100644 --- a/thermion_dart/lib/thermion_dart/viewer/ffi/src/thermion_dart.g.dart +++ b/thermion_dart/lib/thermion_dart/viewer/ffi/src/thermion_dart.g.dart @@ -16,16 +16,6 @@ external ffi.Pointer make_resource_loader( ffi.Pointer owner, ); -@ffi.Native< - ffi.Pointer Function(ffi.Pointer, ffi.Pointer, - ffi.Pointer, ffi.Pointer)>(isLeaf: true) -external ffi.Pointer create_filament_viewer( - ffi.Pointer context, - ffi.Pointer loader, - ffi.Pointer platform, - ffi.Pointer uberArchivePath, -); - @ffi.Native)>(isLeaf: true) external void destroy_filament_viewer( ffi.Pointer viewer, @@ -906,11 +896,9 @@ external void Camera_setCustomProjectionWithCulling( double far, ); -@ffi.Native Function(ffi.Pointer, EntityId)>( - isLeaf: true) -external ffi.Pointer get_camera_component( - ffi.Pointer engine, - int entity, +@ffi.Native)>(isLeaf: true) +external double4x4 Camera_getModelMatrix( + ffi.Pointer camera, ); @ffi.Native< diff --git a/thermion_dart/lib/thermion_dart/viewer/shared_types/camera.dart b/thermion_dart/lib/thermion_dart/viewer/shared_types/camera.dart index d08c4eff..79e7fd20 100644 --- a/thermion_dart/lib/thermion_dart/viewer/shared_types/camera.dart +++ b/thermion_dart/lib/thermion_dart/viewer/shared_types/camera.dart @@ -1,8 +1,8 @@ import 'package:vector_math/vector_math_64.dart'; abstract class Camera { + Future setProjectionMatrixWithCulling( + Matrix4 projectionMatrix, double near, double far); - Future setProjectionMatrixWithCulling(Matrix4 projectionMatrix, double near, double far); - + Future getModelMatrix(); } - diff --git a/thermion_dart/native/include/ThermionDartApi.h b/thermion_dart/native/include/ThermionDartApi.h index aa619773..92824cae 100644 --- a/thermion_dart/native/include/ThermionDartApi.h +++ b/thermion_dart/native/include/ThermionDartApi.h @@ -230,10 +230,10 @@ extern "C" EMSCRIPTEN_KEEPALIVE void set_camera_lens_projection(TCamera *camera, double near, double far, double aspect, double focalLength); EMSCRIPTEN_KEEPALIVE void set_camera_focus_distance(TCamera *camera, float focusDistance); EMSCRIPTEN_KEEPALIVE void set_camera_manipulator_options(TViewer *viewer, _ManipulatorMode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed); - EMSCRIPTEN_KEEPALIVE void Camera_setCustomProjectionWithCulling(TCamera* camera, double4x4 projectionMatrix, double near, double far); - EMSCRIPTEN_KEEPALIVE TCamera *get_camera_component(TEngine *engine, EntityId entity); - + EMSCRIPTEN_KEEPALIVE void Camera_setCustomProjectionWithCulling(TCamera* camera, double4x4 projectionMatrix, double near, double far); + EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera* camera); + EMSCRIPTEN_KEEPALIVE TCamera *Engine_getCameraComponent(TEngine *engine, EntityId entity); EMSCRIPTEN_KEEPALIVE int hide_mesh(void *sceneManager, EntityId entity, const char *meshName); EMSCRIPTEN_KEEPALIVE int reveal_mesh(void *sceneManager, EntityId entity, const char *meshName); diff --git a/thermion_dart/native/src/ThermionDartApi.cpp b/thermion_dart/native/src/ThermionDartApi.cpp index 234230b0..28d7d607 100644 --- a/thermion_dart/native/src/ThermionDartApi.cpp +++ b/thermion_dart/native/src/ThermionDartApi.cpp @@ -1068,6 +1068,11 @@ EMSCRIPTEN_KEEPALIVE void Camera_setCustomProjectionWithCulling(TCamera* tCamera camera->setCustomProjection(convert_double4x4_to_mat4(projectionMatrix), near, far); } +EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera* tCamera) { + auto * camera = reinterpret_cast(tCamera); + return convert_mat4_to_double4x4(camera->getModelMatrix()); +} + EMSCRIPTEN_KEEPALIVE TCamera *Engine_getCameraComponent(TEngine* tEngine, EntityId entityId) { auto * engine = reinterpret_cast(tEngine); auto * camera = engine->getCameraComponent(utils::Entity::import(entityId)); diff --git a/thermion_dart/test/integration_test.dart b/thermion_dart/test/integration_test.dart index e23af73e..0c597993 100644 --- a/thermion_dart/test/integration_test.dart +++ b/thermion_dart/test/integration_test.dart @@ -104,24 +104,40 @@ void main() async { }); test('set custom projection/culling matrix', () async { - var viewer = await createViewer(bg:kRed, cameraPosition:Vector3(0,0,4)); + var viewer = + await createViewer(bg: kRed, cameraPosition: Vector3(0, 0, 4)); var camera = await viewer.getMainCamera(); final cube = await viewer.createGeometry(GeometryHelper.cube()); - // cube is visible when inside the frustum, cube is visible var projectionMatrix = makeOrthographicMatrix(-10.0, 10.0, -10.0, 10.0, 0.05, 10000); await camera.setProjectionMatrixWithCulling( projectionMatrix, 0.05, 10000); - await _capture(viewer, "camera_projection_culling_matrix_object_in_frustum"); - + await _capture( + viewer, "camera_projection_culling_matrix_object_in_frustum"); + // cube no longer visible when the far plane is moved closer to camera so cube is outside projectionMatrix = makeOrthographicMatrix(-10.0, 10.0, -10.0, 10.0, 0.05, 1); - await camera.setProjectionMatrixWithCulling( - projectionMatrix, 0.05, 1); - await _capture(viewer, "camera_projection_culling_matrix_object_outside_frustum"); + await camera.setProjectionMatrixWithCulling(projectionMatrix, 0.05, 1); + await _capture( + viewer, "camera_projection_culling_matrix_object_outside_frustum"); + }); + + test('setting camera transform updates model matrix', () async { + var viewer = await createViewer(); + + var cameraEntity = await viewer.getMainCameraEntity(); + var camera = await viewer.getMainCamera(); + + await viewer.setPosition(cameraEntity, 1, 0, 0); + + var modelMatrix = await viewer.getCameraModelMatrix(); + expect(modelMatrix.getColumn(3).x, 1.0); + expect(modelMatrix.getColumn(3).y, 0.0); + expect(modelMatrix.getColumn(3).z, 0.0); + expect(modelMatrix.getColumn(3).w, 1.0); }); });