From 92ba9e57cfaf0a4fef4132e4bf7a810595324527 Mon Sep 17 00:00:00 2001 From: Sebastian Fieber Date: Sun, 5 Jan 2025 20:22:29 +0100 Subject: [PATCH] addMesh lua calls --- apps/openmw/mwlua/animationbindings.cpp | 29 +++++++++++++++++++++++++ apps/openmw/mwrender/animation.cpp | 25 +++++++++++++++++++++ apps/openmw/mwrender/animation.hpp | 6 +++++ 3 files changed, 60 insertions(+) diff --git a/apps/openmw/mwlua/animationbindings.cpp b/apps/openmw/mwlua/animationbindings.cpp index 1af7acea36..ae9eece744 100644 --- a/apps/openmw/mwlua/animationbindings.cpp +++ b/apps/openmw/mwlua/animationbindings.cpp @@ -293,6 +293,35 @@ namespace MWLua "removeVfxAction"); }; + api["addMesh"] = [context](const SelfObject& object, std::string_view model, std::string_view bonename, + std::string_view meshId) { + context.mLuaManager->addAction( + [object = Object(object), model = std::string(model), bonename = std::string(bonename), + meshId = std::string(meshId)] { + MWRender::Animation* anim = getMutableAnimationOrThrow(object); + anim->addCustomMesh(model, bonename, meshId); + }, + "addMeshAction"); + }; + + api["removeMesh"] = [context](const SelfObject& object, std::string_view meshId) { + context.mLuaManager->addAction( + [object = Object(object), meshId = std::string(meshId)] { + MWRender::Animation* anim = getMutableAnimationOrThrow(object); + anim->removeCustomMesh(meshId); + }, + "removeMeshAction"); + }; + + api["removeAllMeshes"] = [context](const SelfObject& object) { + context.mLuaManager->addAction( + [object = Object(object)] { + MWRender::Animation* anim = getMutableAnimationOrThrow(object); + anim->removeCustomMeshes(); + }, + "removeAllMeshesAction"); + }; + return LuaUtil::makeReadOnly(api); } diff --git a/apps/openmw/mwrender/animation.cpp b/apps/openmw/mwrender/animation.cpp index d22e12ff01..5e80b7c24b 100644 --- a/apps/openmw/mwrender/animation.cpp +++ b/apps/openmw/mwrender/animation.cpp @@ -1710,6 +1710,31 @@ namespace MWRender mExtraLightSource->setActorFade(mAlpha); } + void Animation::removeCustomMesh(std::string_view meshId) + { + mCustomMeshesMap.erase(std::string(meshId)); + } + + void Animation::removeCustomMeshes() + { + mCustomMeshesMap.clear(); + } + + void Animation::addCustomMesh(std::string_view model, std::string_view bonename, std::string_view meshId) + { + SceneUtil::FindByNameVisitor findVisitor(bonename); + mObjectRoot->accept(findVisitor); + osg::Group* parent = findVisitor.mFoundNode; + + if (!parent) + return; + + VFS::Path::Normalized mesh(model); + osg::ref_ptr instance = mResourceSystem->getSceneManager()->getInstance(mesh, parent); + PartHolderPtr holder = std::make_unique(instance); + mCustomMeshesMap.insert(std::pair(meshId, std::move(holder))); + } + void Animation::addEffect(std::string_view model, std::string_view effectId, bool loop, std::string_view bonename, std::string_view texture, bool useAmbientLight) { diff --git a/apps/openmw/mwrender/animation.hpp b/apps/openmw/mwrender/animation.hpp index 1b5bc6b660..7875890a83 100644 --- a/apps/openmw/mwrender/animation.hpp +++ b/apps/openmw/mwrender/animation.hpp @@ -210,6 +210,8 @@ namespace MWRender mutable NodeMap mNodeMap; mutable bool mNodeMapCreated; + mutable std::unordered_map + mCustomMeshesMap; MWWorld::Ptr mPtr; @@ -350,6 +352,10 @@ namespace MWRender void addEffect(std::string_view model, std::string_view effectId, bool loop = false, std::string_view bonename = {}, std::string_view texture = {}, bool useAmbientLight = true); + void addCustomMesh(std::string_view model, std::string_view bonename, std::string_view meshId); + void removeCustomMesh(std::string_view meshId); + void removeCustomMeshes(); + void removeEffect(std::string_view effectId); void removeEffects(); std::vector getLoopingEffects() const;