diff --git a/ios/include/SceneManager.hpp b/ios/include/SceneManager.hpp index b89e23b2..8281f861 100644 --- a/ios/include/SceneManager.hpp +++ b/ios/include/SceneManager.hpp @@ -130,7 +130,7 @@ namespace flutter_filament void addCollisionComponent(EntityId entity, void (*onCollisionCallback)(const EntityId entityId1, const EntityId entityId2), bool affectsCollidingTransform); void removeCollisionComponent(EntityId entityId); void setParent(EntityId child, EntityId parent); - void addAnimatableComponent(EntityId entity); + void addAnimationComponent(EntityId entity); /// @brief returns the number of instances of the FilamentAsset represented by the given entity. /// @param entityId diff --git a/ios/src/FlutterFilamentApi.cpp b/ios/src/FlutterFilamentApi.cpp index 175f7448..5adc4249 100644 --- a/ios/src/FlutterFilamentApi.cpp +++ b/ios/src/FlutterFilamentApi.cpp @@ -589,6 +589,10 @@ extern "C" ((SceneManager*)sceneManager)->addCollisionComponent(entityId, onCollisionCallback, affectsCollidingTransform); } + FLUTTER_PLUGIN_EXPORT void add_animation_component(void *const sceneManager, EntityId entityId) { + ((SceneManager*)sceneManager)->addAnimationComponent(entityId); + } + FLUTTER_PLUGIN_EXPORT EntityId create_geometry(void *const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, const char* materialPath) { return ((FilamentViewer*)viewer)->createGeometry(vertices, (size_t)numVertices, indices, numIndices, materialPath); } diff --git a/ios/src/SceneManager.cpp b/ios/src/SceneManager.cpp index bb358ca9..e96f2085 100644 --- a/ios/src/SceneManager.cpp +++ b/ios/src/SceneManager.cpp @@ -250,7 +250,6 @@ namespace flutter_filament auto instanceEntity = inst->getRoot(); auto instanceEntityId = Entity::smuggle(instanceEntity); _instances.emplace(instanceEntityId, inst); - addAnimatableComponent(instanceEntityId); } asset->releaseSourceData(); @@ -260,7 +259,7 @@ namespace flutter_filament return eid; } - void SceneManager::addAnimatableComponent(EntityId entityId) { + void SceneManager::addAnimationComponent(EntityId entityId) { auto* instance = getInstanceByEntityId(entityId); if (!instance) @@ -387,7 +386,7 @@ namespace flutter_filament const auto &pos = _instances.find(entityId); if (pos == _instances.end()) { - Log("Failed to find FilamentInstance for entity %d", entityId); + // Log("Failed to find FilamentInstance for entity %d", entityId); return nullptr; } return pos->second; @@ -398,7 +397,7 @@ namespace flutter_filament const auto &pos = _assets.find(entityId); if (pos == _assets.end()) { - Log("Failed to find FilamentAsset for entity %d", entityId); + // Log("Failed to find FilamentAsset for entity %d", entityId); return nullptr; } return pos->second; @@ -561,9 +560,9 @@ namespace flutter_filament // { // _engine->destroy(sceneAsset.texture); // } - - utils::EntityManager &em = utils::EntityManager::get(); - em.destroy(entity); +// +// utils::EntityManager &em = utils::EntityManager::get(); +// em.destroy(entity); } void SceneManager::setMorphTargetWeights(EntityId entityId, const char *const entityName, const float *const weights, const int count) @@ -604,13 +603,16 @@ namespace flutter_filament utils::Entity SceneManager::findChildEntityByName(EntityId entityId, const char *entityName) { std::lock_guard lock(_mutex); - const auto &pos = _instances.find(entityId); - if (pos == _instances.end()) + auto* instance = getInstanceByEntityId(entityId); + if (!instance) { - Log("Couldn't find asset under specified entity id."); - return utils::Entity(); + auto* asset = getAssetByEntityId(entityId); + if(!asset) { + return utils::Entity(); + } + instance = asset->getInstance(); } - const auto* instance = pos->second; + const auto entity = findEntityByName(instance, entityName); @@ -1199,7 +1201,6 @@ namespace flutter_filament return; } else { instance = asset->getInstance(); - } } auto collisionInstance = _collisionComponentManager->addComponent(instance->getRoot()); @@ -1212,20 +1213,20 @@ namespace flutter_filament const auto *instance = getInstanceByEntityId(entityId); if(!instance) { auto asset = getAssetByEntityId(entityId); - if(!asset) { + if(asset) { instance = asset->getInstance(); - } + } else { + return; + } } - const auto entity = Entity::import(entityId); const auto& tm = _engine->getTransformManager(); auto transformInstance = tm.getInstance(instance->getRoot()); auto worldTransform = tm.getWorldTransform(transformInstance); - // Log("World transform for %d is %f %f %f", entityId, worldTransform[3][0], worldTransform[3][1], worldTransform[3][2]); auto aabb = instance->getBoundingBox(); aabb = aabb.transform(worldTransform); - _collisionComponentManager->collides(entity, aabb); + _collisionComponentManager->collides(instance->getRoot(), aabb); } void SceneManager::updateAnimations() { @@ -1248,7 +1249,6 @@ namespace flutter_filament Aabb boundingBox; if (pos == _instances.end()) { - Log("WARNING: SceneAsset not found for entity."); isCollidable = false; entity = Entity::import(entityId); } else { diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index 445cf0c1..954a7603 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -610,12 +610,18 @@ abstract class FilamentController { /// Future setRecordingOutputDirectory(String outputDirectory); - // Stream get keyboardFocusRequested; - // void requestKeyboardFocus(); - + /// + /// Attach the keyboard/mouse to [entity]. + /// Future control(FilamentEntity entity, {double? translationSpeed, String? forwardAnimation}); + /// + /// An [entity] will only be animatable after an animation component is attached. + /// Any calls to [playAnimation]/[setBoneAnimation]/[setMorphAnimation] will have no visual effect until [addAnimationComponent] has been called on the instance. + /// + Future addAnimationComponent(FilamentEntity entity); + /// /// Makes [collidableEntity] collidable with /// (a) any entity whose transform is being controlled (via [control]) or diff --git a/lib/filament_controller_ffi.dart b/lib/filament_controller_ffi.dart index 44e8799a..5a69a3c8 100644 --- a/lib/filament_controller_ffi.dart +++ b/lib/filament_controller_ffi.dart @@ -1269,7 +1269,8 @@ class FilamentControllerFFI extends FilamentController { final outPtr = allocator(1); outPtr.value = 0; - pick_ffi(_viewer!, x, textureDetails.value!.height - y, outPtr); + pick_ffi(_viewer!, (x * _pixelRatio).toInt(), + textureDetails.value!.height - (y * _pixelRatio).toInt(), outPtr); int wait = 0; while (outPtr.value == 0) { await Future.delayed(const Duration(milliseconds: 32)); @@ -1497,6 +1498,11 @@ class FilamentControllerFFI extends FilamentController { } } + @override + Future addAnimationComponent(FilamentEntity entity) async { + add_animation_component(_sceneManager!, entity); + } + @override Future createGeometry( List vertices, List indices, String? materialPath) async { diff --git a/lib/generated_bindings.dart b/lib/generated_bindings.dart index d1c86dda..617a9286 100644 --- a/lib/generated_bindings.dart +++ b/lib/generated_bindings.dart @@ -225,10 +225,10 @@ external int get_instance_count( ); @ffi.Native< - ffi.Int Function( + ffi.Void Function( ffi.Pointer, EntityId, ffi.Pointer)>( symbol: 'get_instances', assetId: 'flutter_filament_plugin') -external int get_instances( +external void get_instances( ffi.Pointer sceneManager, int entityId, ffi.Pointer out, @@ -931,6 +931,13 @@ external void add_collision_component( bool affectsCollidingTransform, ); +@ffi.Native, EntityId)>( + symbol: 'add_animation_component', assetId: 'flutter_filament_plugin') +external void add_animation_component( + ffi.Pointer sceneManager, + int entityId, +); + @ffi.Native< EntityId Function(ffi.Pointer, ffi.Pointer, ffi.Int, ffi.Pointer, ffi.Int, ffi.Pointer)>(