From 9deafc7371111a65527e23c63eb85d860ab69402 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 10 Oct 2024 08:42:16 +0800 Subject: [PATCH] return View when picking, use uint32_t for morphIndices --- .../viewer/src/ffi/src/thermion_dart.g.dart | 93 +++++++++---------- .../src/ffi/src/thermion_viewer_ffi.dart | 66 +++++-------- .../native/include/FilamentViewer.hpp | 2 +- thermion_dart/native/include/SceneManager.hpp | 2 +- .../native/include/ThermionDartApi.h | 19 ++-- thermion_dart/native/src/SceneManager.cpp | 2 +- thermion_dart/native/src/ThermionDartApi.cpp | 18 ++-- 7 files changed, 88 insertions(+), 114 deletions(-) diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart index de0c1cda..7f0ae74c 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart +++ b/thermion_dart/lib/src/viewer/src/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 Viewer_create( - ffi.Pointer context, - ffi.Pointer loader, - ffi.Pointer platform, - ffi.Pointer uberArchivePath, -); - @ffi.Native)>(isLeaf: true) external void destroy_filament_viewer( ffi.Pointer viewer, @@ -152,6 +142,28 @@ external void Viewer_setViewRenderable( bool renderable, ); +@ffi.Native< + ffi.Void Function( + ffi.Pointer, + ffi.Pointer, + ffi.Int, + ffi.Int, + ffi.Pointer< + ffi.NativeFunction< + ffi.Void Function(EntityId entityId, ffi.Int x, ffi.Int y, + ffi.Pointer tView)>>)>(isLeaf: true) +external void Viewer_pick( + ffi.Pointer viewer, + ffi.Pointer tView, + int x, + int y, + ffi.Pointer< + ffi.NativeFunction< + ffi.Void Function(EntityId entityId, ffi.Int x, ffi.Int y, + ffi.Pointer tView)>> + callback, +); + @ffi.Native Function(ffi.Pointer)>(isLeaf: true) external ffi.Pointer Viewer_getEngine( ffi.Pointer viewer, @@ -398,25 +410,6 @@ external bool set_morph_target_weights( int numWeights, ); -@ffi.Native< - ffi.Bool Function( - ffi.Pointer, - EntityId, - ffi.Pointer, - ffi.Pointer, - ffi.Int, - ffi.Int, - ffi.Float)>(isLeaf: true) -external bool set_morph_animation( - ffi.Pointer sceneManager, - int entity, - ffi.Pointer morphData, - ffi.Pointer morphIndices, - int numMorphTargets, - int numFrames, - double frameLengthInMs, -); - @ffi.Native< ffi.Pointer Function( ffi.Pointer, TMaterialKey)>(isLeaf: true) @@ -669,6 +662,25 @@ external int SceneManager_loadGlbFromBuffer( int layer, ); +@ffi.Native< + ffi.Bool Function( + ffi.Pointer, + EntityId, + ffi.Pointer, + ffi.Pointer, + ffi.Int, + ffi.Int, + ffi.Float)>(isLeaf: true) +external bool SceneManager_setMorphAnimation( + ffi.Pointer sceneManager, + int entity, + ffi.Pointer morphData, + ffi.Pointer morphIndices, + int numMorphTargets, + int numFrames, + double frameLengthInMs, +); + @ffi.Native, EntityId)>( isLeaf: true) external bool update_bone_matrices( @@ -841,27 +853,6 @@ external int reveal_mesh( ffi.Pointer meshName, ); -@ffi.Native< - ffi.Void Function( - ffi.Pointer, - ffi.Pointer, - ffi.Int, - ffi.Int, - ffi.Pointer< - ffi.NativeFunction< - ffi.Void Function( - EntityId entityId, ffi.Int x, ffi.Int y)>>)>(isLeaf: true) -external void filament_pick( - ffi.Pointer viewer, - ffi.Pointer tView, - int x, - int y, - ffi.Pointer< - ffi.NativeFunction< - ffi.Void Function(EntityId entityId, ffi.Int x, ffi.Int y)>> - callback, -); - @ffi.Native< ffi.Pointer Function( ffi.Pointer, EntityId)>(isLeaf: true) @@ -1430,7 +1421,7 @@ external void Viewer_requestFrameRenderThread( external void View_setToneMappingRenderThread( ffi.Pointer tView, ffi.Pointer tEngine, - int arg2, + int thermion, ); @ffi.Native, ffi.Double)>(isLeaf: true) diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart index 9e51ba3a..409e1f0e 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart @@ -83,9 +83,9 @@ class ThermionViewerFFI extends ThermionViewer { this._driver = driver ?? nullptr; this._sharedContext = sharedContext ?? nullptr; - // _onPickResultCallable = - // NativeCallable.listener( - // _onPickResult); + _onPickResultCallable = + NativeCallable view)>.listener( + _onPickResult); _initialize(); } @@ -523,8 +523,8 @@ class ThermionViewerFFI extends ThermionViewer { throw Exception("Not yet implemented"); } final pathPtr = path.toNativeUtf8(allocator: allocator).cast(); - var entity = await withIntCallback((callback)=> load_glb_render_thread( - _sceneManager!, pathPtr, numInstances, keepData, callback)); + var entity = await withIntCallback((callback) => load_glb_render_thread( + _sceneManager!, pathPtr, numInstances, keepData, callback)); allocator.free(pathPtr); if (entity == _FILAMENT_ASSET_ERROR) { @@ -729,7 +729,7 @@ class ThermionViewerFFI extends ThermionViewer { var meshEntity = meshEntities[i]; if (targetMeshNames?.contains(meshName) == false) { - _logger.info("Skipping $meshName, not contained in target"); + // _logger.info("Skipping $meshName, not contained in target"); continue; } @@ -749,39 +749,24 @@ class ThermionViewerFFI extends ThermionViewer { Child meshes: ${meshNames}"""); } - var indices = - intersection.map((m) => meshMorphTargets.indexOf(m)).toList(); + var indices = Uint32List.fromList( + intersection.map((m) => meshMorphTargets.indexOf(m)).toList()); - var frameData = animation.extract(morphTargets: intersection); + // var frameData = animation.data; + var frameData = animation.subset(intersection); - assert(frameData.length == animation.numFrames * intersection.length); + assert( + frameData.data.length == animation.numFrames * intersection.length); - var dataPtr = allocator(frameData.length); - - // not currently working on WASM :( wasted a lot of time figuring that out as no error is thrown - // dataPtr - // .asTypedList(frameData.length) - // .setRange(0, frameData.length, frameData); - for (int i = 0; i < frameData.length; i++) { - dataPtr[i] = frameData[i]; - } - - final idxPtr = allocator(indices.length); - - for (int i = 0; i < indices.length; i++) { - idxPtr[i] = indices[i]; - } - - var result = set_morph_animation( + var result = SceneManager_setMorphAnimation( _sceneManager!, meshEntity, - dataPtr, - idxPtr, + frameData.data.address, + indices.address, indices.length, animation.numFrames, animation.frameLengthInMs); - allocator.free(dataPtr); - allocator.free(idxPtr); + if (!result) { throw Exception("Failed to set morph animation data for ${meshName}"); } @@ -1484,14 +1469,11 @@ class ThermionViewerFFI extends ThermionViewer { final view = FFIView(viewPtr, _viewer!); final viewport = await view.getViewport(); - // _pickResultController.add(( - // entity: entityId, - // x: (x / pixelRatio).toDouble(), - // y: (viewport.height - y) / pixelRatio - // )); + _pickResultController + .add((entity: entityId, x: x.ceil(), y: (viewport.height - y).ceil())); } - late NativeCallable + late NativeCallable view)> _onPickResultCallable; /// @@ -1499,11 +1481,11 @@ class ThermionViewerFFI extends ThermionViewer { /// @override void pick(int x, int y) async { - // x = (x * pixelRatio).ceil(); - // y = (viewportDimensions.$2 - (y * pixelRatio)).ceil(); - // final view = (await getViewAt(0)) as FFIView; - // filament_pick( - // _viewer!, view.view, x, y, _onPickResultCallable.nativeFunction); + final view = (await getViewAt(0)) as FFIView; + var viewport = await view.getViewport(); + y = (viewport.height - y).ceil(); + Viewer_pick( + _viewer!, view.view, x, y, _onPickResultCallable.nativeFunction); } /// diff --git a/thermion_dart/native/include/FilamentViewer.hpp b/thermion_dart/native/include/FilamentViewer.hpp index 5bc0deea..3fb4f33f 100644 --- a/thermion_dart/native/include/FilamentViewer.hpp +++ b/thermion_dart/native/include/FilamentViewer.hpp @@ -98,7 +98,7 @@ namespace thermion void clearBackgroundImage(); void setBackgroundImagePosition(float x, float y, bool clamp, uint32_t width, uint32_t height); - void pick(View *view, uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y)); + void pick(View *view, uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y, View *view)); Engine* getEngine() { return _engine; } diff --git a/thermion_dart/native/include/SceneManager.hpp b/thermion_dart/native/include/SceneManager.hpp index 08ff82ad..e486e4de 100644 --- a/thermion_dart/native/include/SceneManager.hpp +++ b/thermion_dart/native/include/SceneManager.hpp @@ -125,7 +125,7 @@ namespace thermion bool setMorphAnimationBuffer( EntityId entityId, const float *const morphData, - const int *const morphIndices, + const uint32_t *const morphIndices, int numMorphTargets, int numFrames, float frameLengthInMs); diff --git a/thermion_dart/native/include/ThermionDartApi.h b/thermion_dart/native/include/ThermionDartApi.h index 1af84dc5..20c37ac2 100644 --- a/thermion_dart/native/include/ThermionDartApi.h +++ b/thermion_dart/native/include/ThermionDartApi.h @@ -85,6 +85,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void Viewer_setMainCamera(TViewer *tViewer, TView *tView); EMSCRIPTEN_KEEPALIVE TSwapChain* Viewer_getSwapChainAt(TViewer *tViewer, int index); EMSCRIPTEN_KEEPALIVE void Viewer_setViewRenderable(TViewer *viewer, TSwapChain *swapChain, TView* view, bool renderable); + EMSCRIPTEN_KEEPALIVE void Viewer_pick(TViewer *viewer, TView* tView, int x, int y, void (*callback)(EntityId entityId, int x, int y, TView *tView)); // Engine EMSCRIPTEN_KEEPALIVE TEngine *Viewer_getEngine(TViewer* viewer); @@ -146,14 +147,6 @@ extern "C" EntityId entity, const float *const morphData, int numWeights); - EMSCRIPTEN_KEEPALIVE bool set_morph_animation( - TSceneManager *sceneManager, - EntityId entity, - const float *const morphData, - const int *const morphIndices, - int numMorphTargets, - int numFrames, - float frameLengthInMs); EMSCRIPTEN_KEEPALIVE TMaterialInstance *create_material_instance(TSceneManager *sceneManager, TMaterialKey materialConfig); EMSCRIPTEN_KEEPALIVE TMaterialInstance *create_unlit_material_instance(TSceneManager *sceneManager); @@ -210,6 +203,14 @@ extern "C" EMSCRIPTEN_KEEPALIVE void SceneManager_setVisibilityLayer(TSceneManager *tSceneManager, EntityId entity, int layer); EMSCRIPTEN_KEEPALIVE TScene* SceneManager_getScene(TSceneManager *tSceneManager); EMSCRIPTEN_KEEPALIVE EntityId SceneManager_loadGlbFromBuffer(TSceneManager *sceneManager, const uint8_t *const, size_t length, bool keepData, int priority, int layer); + EMSCRIPTEN_KEEPALIVE bool SceneManager_setMorphAnimation( + TSceneManager *sceneManager, + EntityId entity, + const float *const morphData, + const uint32_t *const morphIndices, + int numMorphTargets, + int numFrames, + float frameLengthInMs); EMSCRIPTEN_KEEPALIVE bool update_bone_matrices(TSceneManager *sceneManager, EntityId entityId); @@ -239,7 +240,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE int hide_mesh(TSceneManager *sceneManager, EntityId entity, const char *meshName); EMSCRIPTEN_KEEPALIVE int reveal_mesh(TSceneManager *sceneManager, EntityId entity, const char *meshName); - EMSCRIPTEN_KEEPALIVE void filament_pick(TViewer *viewer, TView* tView, int x, int y, void (*callback)(EntityId entityId, int x, int y)); + EMSCRIPTEN_KEEPALIVE const char *get_name_for_entity(TSceneManager *sceneManager, const EntityId entityId); EMSCRIPTEN_KEEPALIVE EntityId find_child_entity_by_name(TSceneManager *sceneManager, const EntityId parent, const char *name); EMSCRIPTEN_KEEPALIVE int get_entity_count(TSceneManager *sceneManager, const EntityId target, bool renderableOnly); diff --git a/thermion_dart/native/src/SceneManager.cpp b/thermion_dart/native/src/SceneManager.cpp index 9ee00394..8e4782c6 100644 --- a/thermion_dart/native/src/SceneManager.cpp +++ b/thermion_dart/native/src/SceneManager.cpp @@ -806,7 +806,7 @@ namespace thermion bool SceneManager::setMorphAnimationBuffer( EntityId entityId, const float *const morphData, - const int *const morphIndices, + const uint32_t *const morphIndices, int numMorphTargets, int numFrames, float frameLengthInMs) diff --git a/thermion_dart/native/src/ThermionDartApi.cpp b/thermion_dart/native/src/ThermionDartApi.cpp index e69ccfee..6be98916 100644 --- a/thermion_dart/native/src/ThermionDartApi.cpp +++ b/thermion_dart/native/src/ThermionDartApi.cpp @@ -50,6 +50,13 @@ extern "C" viewer->destroyRenderTarget(renderTarget); } + EMSCRIPTEN_KEEPALIVE void Viewer_pick(TViewer *tViewer, TView* tView, int x, int y, void (*callback)(EntityId entityId, int x, int y, TView *tView)) + { + auto *viewer = reinterpret_cast(tViewer); + auto *view = reinterpret_cast(tView); + ((FilamentViewer *)viewer)->pick(view, static_cast(x), static_cast(y), reinterpret_cast(callback)); + } + EMSCRIPTEN_KEEPALIVE void destroy_filament_viewer(TViewer *viewer) { delete ((FilamentViewer *)viewer); @@ -446,11 +453,11 @@ extern "C" return ((SceneManager *)sceneManager)->setMorphTargetWeights(asset, weights, numWeights); } - EMSCRIPTEN_KEEPALIVE bool set_morph_animation( + EMSCRIPTEN_KEEPALIVE bool SceneManager_setMorphAnimation( TSceneManager *sceneManager, EntityId asset, const float *const morphData, - const int *const morphIndices, + const uint32_t *const morphIndices, int numMorphTargets, int numFrames, float frameLengthInMs) @@ -793,13 +800,6 @@ extern "C" return ((SceneManager *)sceneManager)->reveal(asset, meshName); } - EMSCRIPTEN_KEEPALIVE void filament_pick(TViewer *tViewer, TView* tView, int x, int y, void (*callback)(EntityId entityId, int x, int y)) - { - auto *viewer = reinterpret_cast(tViewer); - auto *view = reinterpret_cast(tView); - ((FilamentViewer *)viewer)->pick(view, static_cast(x), static_cast(y), callback); - } - EMSCRIPTEN_KEEPALIVE const char *get_name_for_entity(TSceneManager *sceneManager, const EntityId entityId) { return ((SceneManager *)sceneManager)->getNameForEntity(entityId);