From 0ad73d06e0c82cfb10847d7615079f550c464a92 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 12 Dec 2024 14:20:12 +0800 Subject: [PATCH] internal: pass NameComponentManager to GltfSceneAsset, used for finding child entities by name --- .../native/include/scene/GltfSceneAsset.hpp | 7 ++++++ .../include/scene/GltfSceneAssetInstance.hpp | 24 +++++++++++++++++-- .../native/src/scene/GltfSceneAsset.cpp | 3 ++- .../native/src/scene/SceneManager.cpp | 8 +++++-- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/thermion_dart/native/include/scene/GltfSceneAsset.hpp b/thermion_dart/native/include/scene/GltfSceneAsset.hpp index 6db31079..6da6f7af 100644 --- a/thermion_dart/native/include/scene/GltfSceneAsset.hpp +++ b/thermion_dart/native/include/scene/GltfSceneAsset.hpp @@ -11,6 +11,9 @@ #include #include +#include + + #include "scene/GltfSceneAssetInstance.hpp" #include "components/AnimationComponentManager.hpp" #include "components/CollisionComponentManager.hpp" @@ -29,11 +32,13 @@ namespace thermion gltfio::FilamentAsset *asset, gltfio::AssetLoader *assetLoader, Engine *engine, + utils::NameComponentManager* ncm, MaterialInstance **materialInstances = nullptr, size_t materialInstanceCount = 0, int instanceIndex = -1) : _asset(asset), _assetLoader(assetLoader), _engine(engine), + _ncm(ncm), _materialInstances(materialInstances), _materialInstanceCount(materialInstanceCount) { @@ -150,6 +155,7 @@ namespace thermion } Entity findEntityByName(const char* name) override { + TRACE("Searching for entity with name %s", name); Entity entities[1]; auto found = _asset->getEntitiesByName(name, entities, 1); return entities[0]; @@ -159,6 +165,7 @@ namespace thermion gltfio::FilamentAsset *_asset; gltfio::AssetLoader *_assetLoader; Engine *_engine; + utils::NameComponentManager *_ncm; MaterialInstance **_materialInstances = nullptr; size_t _materialInstanceCount = 0; std::vector> _instances; diff --git a/thermion_dart/native/include/scene/GltfSceneAssetInstance.hpp b/thermion_dart/native/include/scene/GltfSceneAssetInstance.hpp index 4eddb29b..4a2041a2 100644 --- a/thermion_dart/native/include/scene/GltfSceneAssetInstance.hpp +++ b/thermion_dart/native/include/scene/GltfSceneAssetInstance.hpp @@ -11,6 +11,8 @@ #include #include +#include + #include "scene/SceneAsset.hpp" namespace thermion @@ -24,9 +26,11 @@ namespace thermion GltfSceneAssetInstance( gltfio::FilamentInstance *instance, Engine *engine, + utils::NameComponentManager* ncm, MaterialInstance **materialInstances = nullptr, size_t materialInstanceCount = 0, - int instanceIndex = -1) : _instance(instance), + int instanceIndex = -1) : _ncm(ncm), + _instance(instance), _materialInstances(materialInstances), _materialInstanceCount(materialInstanceCount) { @@ -102,7 +106,22 @@ namespace thermion } Entity findEntityByName(const char* name) override { - return Entity(); // not currently implemented + + TRACE("Searching for entity with name %s", name); + + for(int i = 0; i < getChildEntityCount(); i++) { + auto entity = getChildEntities()[i]; + auto nameInstance = _ncm->getInstance(entity); + auto entityName = _ncm->getName(nameInstance); + + if (strcmp(entityName, name) == 0) { + TRACE("Found entity name : %s", entityName); + return entity; + } + TRACE("Skipping entity : %s", entityName); + + } + return Entity(); } SceneAsset *getInstanceByEntity(utils::Entity entity) override { @@ -139,6 +158,7 @@ namespace thermion private: filament::Engine *_engine; + utils::NameComponentManager *_ncm; gltfio::FilamentInstance *_instance; MaterialInstance **_materialInstances = nullptr; size_t _materialInstanceCount = 0; diff --git a/thermion_dart/native/src/scene/GltfSceneAsset.cpp b/thermion_dart/native/src/scene/GltfSceneAsset.cpp index d7adf0ba..5deb6713 100644 --- a/thermion_dart/native/src/scene/GltfSceneAsset.cpp +++ b/thermion_dart/native/src/scene/GltfSceneAsset.cpp @@ -22,7 +22,7 @@ namespace thermion Log("No instances available for reuse. When loading the asset, you must pre-allocate the number of instances you wish to make available for use. Try increasing this number."); return std::nullptr_t(); } - Log("Creating instance %d", instanceNumber); + TRACE("Creating instance %d", instanceNumber); auto instance = _asset->getAssetInstances()[instanceNumber]; instance->recomputeBoundingBoxes(); instance->getAnimator()->updateBoneMatrices(); @@ -49,6 +49,7 @@ namespace thermion std::unique_ptr sceneAssetInstance = std::make_unique( instance, _engine, + _ncm, materialInstances, materialInstanceCount ); diff --git a/thermion_dart/native/src/scene/SceneManager.cpp b/thermion_dart/native/src/scene/SceneManager.cpp index 4280cb42..25fad074 100644 --- a/thermion_dart/native/src/scene/SceneManager.cpp +++ b/thermion_dart/native/src/scene/SceneManager.cpp @@ -285,7 +285,9 @@ namespace thermion auto sceneAsset = std::make_unique( asset, _assetLoader, - _engine); + _engine, + _ncm + ); auto filamentInstance = asset->getInstance(); size_t entityCount = filamentInstance->getEntityCount(); @@ -372,7 +374,9 @@ namespace thermion auto sceneAsset = std::make_unique( asset, _assetLoader, - _engine); + _engine, + _ncm + ); auto sceneAssetInstance = sceneAsset->createInstance(); if(addToScene) {