diff --git a/ios/include/AssetManager.hpp b/ios/include/AssetManager.hpp index 64b631d5..ce45bd6d 100644 --- a/ios/include/AssetManager.hpp +++ b/ios/include/AssetManager.hpp @@ -62,10 +62,10 @@ namespace polyvox /// @param entityId /// @param entityName /// @param skinIndex - /// @param boneIndex + /// @param boneName /// @param transform /// @return - bool setBoneTransform(EntityId entityId, const char *entityName, int skinIndex, int boneIndex, math::mat4f transform); + bool setBoneTransform(EntityId entityId, const char *entityName, int skinIndex, const char* boneName, math::mat4f transform); /// @brief Set frame data to animate the given bones/entities. /// @param entity diff --git a/ios/include/FlutterFilamentApi.h b/ios/include/FlutterFilamentApi.h index 0478e732..83ba85f8 100644 --- a/ios/include/FlutterFilamentApi.h +++ b/ios/include/FlutterFilamentApi.h @@ -127,7 +127,7 @@ extern "C" EntityId asset, const char *entityName, const float *const transform, - int boneIndex); + const char *boneName); FLUTTER_PLUGIN_EXPORT void play_animation(void *assetManager, EntityId asset, int index, bool loop, bool reverse, bool replaceActive, float crossfade); FLUTTER_PLUGIN_EXPORT void set_animation_frame(void *assetManager, EntityId asset, int animationIndex, int animationFrame); FLUTTER_PLUGIN_EXPORT void stop_animation(void *assetManager, EntityId asset, int index); diff --git a/ios/src/AssetManager.cpp b/ios/src/AssetManager.cpp index 06766ac7..14a3e99d 100644 --- a/ios/src/AssetManager.cpp +++ b/ios/src/AssetManager.cpp @@ -389,10 +389,10 @@ namespace polyvox } } - bool AssetManager::setBoneTransform(EntityId entityId, const char *entityName, int32_t skinIndex, int32_t boneIndex, math::mat4f localTransform) + bool AssetManager::setBoneTransform(EntityId entityId, const char *entityName, int32_t skinIndex, const char* boneName, math::mat4f localTransform) { - Log("Setting transform for bone %d/skin %d for mesh target %s", boneIndex, skinIndex, entityName); + Log("Setting transform for bone %s/skin %d for mesh target %s", boneName, skinIndex, entityName); const auto &pos = _entityIdLookup.find(entityId); if (pos == _entityIdLookup.end()) @@ -411,6 +411,25 @@ namespace polyvox TransformManager &transformManager = _engine->getTransformManager(); const auto &filamentInstance = sceneAsset.asset->getInstance(); + + int numJoints = filamentInstance->getJointCountAt(skinIndex); + auto joints = filamentInstance->getJointsAt(skinIndex); + int boneIndex = -1; + for (int i = 0; i < numJoints; i++) + { + const char *jointName = _ncm->getName(_ncm->getInstance(joints[i])); + if (strcmp(jointName, boneName) == 0) + { + boneIndex = i; + break; + } + } + if(boneIndex == -1) { + Log("Failed to find bone %s", boneName); + return false; + } + + utils::Entity joint = filamentInstance->getJointsAt(skinIndex)[boneIndex]; if (joint.isNull()) diff --git a/ios/src/FlutterFilamentApi.cpp b/ios/src/FlutterFilamentApi.cpp index 375c4693..76d4aaf5 100644 --- a/ios/src/FlutterFilamentApi.cpp +++ b/ios/src/FlutterFilamentApi.cpp @@ -344,7 +344,7 @@ extern "C" EntityId entityId, const char *entityName, const float *const transform, - int boneIndex) + const char* boneName) { auto matrix = math::mat4f( @@ -362,7 +362,7 @@ extern "C" transform[13], transform[14], transform[15]); - return ((AssetManager *)assetManager)->setBoneTransform(entityId, entityName, 0, boneIndex, matrix); + return ((AssetManager *)assetManager)->setBoneTransform(entityId, entityName, 0, boneName, matrix); } FLUTTER_PLUGIN_EXPORT void play_animation( diff --git a/lib/animations/animation_data.dart b/lib/animations/animation_data.dart index 879f04d1..04a860eb 100644 --- a/lib/animations/animation_data.dart +++ b/lib/animations/animation_data.dart @@ -27,6 +27,9 @@ class MorphAnimationData { Iterable getData(String morphName) sync* { int index = morphTargets.indexOf(morphName); + if (index == -1) { + throw Exception("No data for morph $morphName"); + } for (int i = 0; i < numFrames; i++) { yield data[(i * numMorphTargets) + index]; } diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index 08be8c63..2443c899 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -280,7 +280,7 @@ abstract class FilamentController { /// Sets the local joint transform for the bone at the given index in [entity] for the mesh under [meshName]. /// Future setBoneTransform( - FilamentEntity entity, String meshName, int boneIndex, Matrix4 data); + FilamentEntity entity, String meshName, String boneName, Matrix4 data); /// /// Removes/destroys the specified entity from the scene. diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index 6028f3b4..669c9a39 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -1161,16 +1161,20 @@ class FilamentControllerFFI extends FilamentController { } @override - Future setBoneTransform(FilamentEntity entity, String meshName, int boneIndex, - Matrix4 data) async { + Future setBoneTransform(FilamentEntity entity, String meshName, + String boneName, Matrix4 data) async { var ptr = calloc(16); for (int i = 0; i < 16; i++) { ptr.elementAt(i).value = data.storage[i]; } - set_bone_transform(_assetManager!, entity, - meshName.toNativeUtf8().cast(), ptr, boneIndex); + var meshNamePtr = meshName.toNativeUtf8(allocator: calloc).cast(); + var boneNamePtr = boneName.toNativeUtf8(allocator: calloc).cast(); + + set_bone_transform(_assetManager!, entity, meshNamePtr, ptr, boneNamePtr); calloc.free(ptr); + calloc.free(meshNamePtr); + calloc.free(boneNamePtr); } } diff --git a/lib/generated_bindings.dart b/lib/generated_bindings.dart index 01828110..292c028f 100644 --- a/lib/generated_bindings.dart +++ b/lib/generated_bindings.dart @@ -387,15 +387,19 @@ external void add_bone_animation( ); @ffi.Native< - ffi.Bool Function(ffi.Pointer, EntityId, - ffi.Pointer, ffi.Pointer, ffi.Int)>( + ffi.Bool Function( + ffi.Pointer, + EntityId, + ffi.Pointer, + ffi.Pointer, + ffi.Pointer)>( symbol: 'set_bone_transform', assetId: 'flutter_filament_plugin') external bool set_bone_transform( ffi.Pointer assetManager, int asset, ffi.Pointer entityName, ffi.Pointer transform, - int boneIndex, + ffi.Pointer boneName, ); @ffi.Native< @@ -1049,30 +1053,6 @@ external bool set_morph_animation_ffi( double frameLengthInMs, ); -@ffi.Native< - ffi.Void Function( - ffi.Pointer, - EntityId, - ffi.Pointer, - ffi.Int, - ffi.Int, - ffi.Pointer>, - ffi.Pointer>, - ffi.Int, - ffi.Float)>( - symbol: 'set_bone_animation_ffi', assetId: 'flutter_filament_plugin') -external void set_bone_animation_ffi( - ffi.Pointer assetManager, - int asset, - ffi.Pointer frameData, - int numFrames, - int numBones, - ffi.Pointer> boneNames, - ffi.Pointer> meshName, - int numMeshTargets, - double frameLengthInMs, -); - @ffi.Native< ffi.Void Function(ffi.Pointer, EntityId, ffi.Int, ffi.Bool, ffi.Bool, ffi.Bool, ffi.Float)>(