diff --git a/ios/Classes/SwiftPolyvoxFilamentPlugin.swift b/ios/Classes/SwiftPolyvoxFilamentPlugin.swift index d41794e7..f3bb7d3f 100644 --- a/ios/Classes/SwiftPolyvoxFilamentPlugin.swift +++ b/ios/Classes/SwiftPolyvoxFilamentPlugin.swift @@ -447,6 +447,18 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture play_animation(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, Int32(index), loop, reverse, crossfade) result(true) + case "getAnimationDuration": + guard let args = call.arguments as? [Any], args.count == 3, + let assetManager = args[0] as? Int64, + let asset = args[1] as? EntityId, + let animationIndex = args[2] as? Int else { + result(FlutterError(code: "INVALID_ARGUMENTS", message: "Expected correct arguments for getAnimationDuration", details: nil)) + return + } + + let dur = get_animation_duration(unsafeBitCast(assetManager, to:UnsafeMutableRawPointer.self), asset, Int32(animationIndex)) + result(dur) + case "setAnimationFrame": guard let args = call.arguments as? [Any], args.count == 4, let assetManager = args[0] as? Int64, diff --git a/ios/include/AssetManager.hpp b/ios/include/AssetManager.hpp index 26908c2d..d433b382 100644 --- a/ios/include/AssetManager.hpp +++ b/ios/include/AssetManager.hpp @@ -28,6 +28,7 @@ namespace polyvox { void remove(EntityId entity); void destroyAll(); unique_ptr> getAnimationNames(EntityId entity); + float getAnimationDuration(EntityId entity, int animationIndex); unique_ptr> getMorphTargetNames(EntityId entity, const char *meshName); void transformToUnitCube(EntityId e); inline void updateTransform(EntityId e); diff --git a/ios/include/PolyvoxFilamentApi.h b/ios/include/PolyvoxFilamentApi.h index 3fb9c013..d651524f 100644 --- a/ios/include/PolyvoxFilamentApi.h +++ b/ios/include/PolyvoxFilamentApi.h @@ -73,6 +73,7 @@ void set_animation_frame(void* assetManager, EntityId asset, int animationIndex, void stop_animation(void* assetManager, EntityId asset, int index); int get_animation_count(void* assetManager, EntityId asset); void get_animation_name(void* assetManager, EntityId asset, char *const outPtr, int index); +float get_animation_duration(void* assetManager, EntityId asset, int index); void get_morph_target_name(void* assetManager, EntityId asset, const char *meshName, char *const outPtr, int index); int get_morph_target_name_count(void* assetManager, EntityId asset, const char *meshName); void remove_asset(const void* const viewer, EntityId asset); diff --git a/ios/src/AssetManager.cpp b/ios/src/AssetManager.cpp index 8bd61e8c..ee87b11e 100644 --- a/ios/src/AssetManager.cpp +++ b/ios/src/AssetManager.cpp @@ -708,6 +708,20 @@ void AssetManager::setAnimationFrame(EntityId entity, int animationIndex, int an asset.mAnimator->updateBoneMatrices(); } +float AssetManager::getAnimationDuration(EntityId entity, int animationIndex) { + const auto& pos = _entityIdLookup.find(entity); + + unique_ptr> names = make_unique>(); + + if(pos == _entityIdLookup.end()) { + Log("ERROR: asset not found for entity id."); + return -1.0f; + } + + auto& asset = _assets[pos->second]; + return asset.mAnimator->getAnimationDuration(animationIndex); +} + unique_ptr> AssetManager::getAnimationNames(EntityId entity) { const auto& pos = _entityIdLookup.find(entity); diff --git a/ios/src/PolyvoxFilamentApi.cpp b/ios/src/PolyvoxFilamentApi.cpp index fb91dab6..a106a027 100644 --- a/ios/src/PolyvoxFilamentApi.cpp +++ b/ios/src/PolyvoxFilamentApi.cpp @@ -280,15 +280,16 @@ extern "C" { // ((AssetManager*)assetManager)->setAnimationFrame(asset, animationIndex, animationFrame); } + + FLUTTER_PLUGIN_EXPORT float get_animation_duration(void* assetManager, EntityId asset, int animationIndex) { + return ((AssetManager*)assetManager)->getAnimationDuration(asset, animationIndex); + } + FLUTTER_PLUGIN_EXPORT int get_animation_count( void* assetManager, EntityId asset) { - //std::packaged_task lambda([=]() mutable { auto names = ((AssetManager*)assetManager)->getAnimationNames(asset); return names->size(); - - - //return fut.get(); } FLUTTER_PLUGIN_EXPORT void get_animation_name( diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index 52892309..622798d5 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -246,6 +246,13 @@ class FilamentController { return names.cast(); } + Future getAnimationDuration( + FilamentEntity asset, int animationIndex) async { + var duration = await _channel.invokeMethod( + "getAnimationDuration", [_assetManager, asset, animationIndex]); + return duration as double; + } + /// /// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs]. /// [morphWeights] is a list of doubles in frame-major format.