From 7949c57f16de917e62ee3268ea4a68e866c2e9c8 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Tue, 30 Aug 2022 18:31:04 +0200 Subject: [PATCH] Utility: Add a better way to attach objects to joints --- examples/Showcase/main.cpp | 12 ++++---- .../Utility/Components/NodeComponent.hpp | 2 ++ .../Components/SharedSkeletonComponent.hpp | 3 +- .../Utility/Components/SkeletonComponent.hpp | 4 ++- .../Components/SkeletonComponentBase.hpp | 6 +++- .../Components/SkeletonComponentBase.inl | 10 +++++++ .../Utility/Components/NodeComponent.cpp | 30 +++++++++++++++++++ .../Components/SharedSkeletonComponent.cpp | 10 +++---- .../Utility/Components/SkeletonComponent.cpp | 10 +++---- 9 files changed, 67 insertions(+), 20 deletions(-) diff --git a/examples/Showcase/main.cpp b/examples/Showcase/main.cpp index 74231e310..678d16581 100644 --- a/examples/Showcase/main.cpp +++ b/examples/Showcase/main.cpp @@ -167,17 +167,17 @@ int main() } }*/ - entt::entity bobEntity = registry.create(); + entt::handle bobEntity = entt::handle(registry, registry.create()); { - auto& bobNode = registry.emplace(bobEntity); + auto& bobNode = bobEntity.emplace(); //bobNode.SetRotation(Nz::EulerAnglesf(-90.f, -90.f, 0.f)); //bobNode.SetScale(1.f / 40.f * 0.5f); //bobNode.SetPosition(bobNode.GetScale() * Nz::Vector3f(0.f, -bobAABB.height / 2.f + bobAABB.y, 0.f)); - auto& bobGfx = registry.emplace(bobEntity); + auto& bobGfx = bobEntity.emplace(); bobGfx.AttachRenderable(bobModel, 0xFFFFFFFF); - auto& sharedSkeleton = registry.emplace(bobEntity, skeleton); + auto& sharedSkeleton = bobEntity.emplace(skeleton); entt::entity sphereEntity = registry.create(); @@ -219,7 +219,7 @@ int main() auto& sphereNode = registry.emplace(sphereEntity); sphereNode.SetScale(0.1f); sphereNode.SetInheritScale(false); - sphereNode.SetParent(sharedSkeleton.GetAttachedJoint(83)); + sphereNode.SetParentJoint(bobEntity, "RightHand"); auto& sphereBody = registry.emplace(sphereEntity, &physSytem.GetPhysWorld()); sphereBody.SetGeom(std::make_shared(0.1f)); @@ -230,7 +230,7 @@ int main() entt::entity smallBobEntity = registry.create(); auto& smallBobNode = registry.emplace(smallBobEntity); - smallBobNode.SetParent(sharedSkeleton.GetAttachedJoint(59)); + smallBobNode.SetParentJoint(bobEntity, "LeftHand"); auto& smallBobGfx = registry.emplace(smallBobEntity); smallBobGfx.AttachRenderable(bobModel, 0xFFFFFFFF); diff --git a/include/Nazara/Utility/Components/NodeComponent.hpp b/include/Nazara/Utility/Components/NodeComponent.hpp index 3039a1063..7f0ea3a15 100644 --- a/include/Nazara/Utility/Components/NodeComponent.hpp +++ b/include/Nazara/Utility/Components/NodeComponent.hpp @@ -22,6 +22,8 @@ namespace Nz ~NodeComponent() = default; void SetParent(entt::handle entity, bool keepDerived = false); + void SetParentJoint(entt::handle entity, const std::string& jointName, bool keepDerived = false); + void SetParentJoint(entt::handle entity, std::size_t jointIndex, bool keepDerived = false); using Node::SetParent; NodeComponent& operator=(const NodeComponent&) = default; diff --git a/include/Nazara/Utility/Components/SharedSkeletonComponent.hpp b/include/Nazara/Utility/Components/SharedSkeletonComponent.hpp index 5a117b2ca..6ec9e050c 100644 --- a/include/Nazara/Utility/Components/SharedSkeletonComponent.hpp +++ b/include/Nazara/Utility/Components/SharedSkeletonComponent.hpp @@ -23,12 +23,11 @@ namespace Nz SharedSkeletonComponent(SharedSkeletonComponent&& sharedSkeletalComponent) noexcept; ~SharedSkeletonComponent() = default; - const Joint& GetAttachedJoint(std::size_t jointIndex) const override; - SharedSkeletonComponent& operator=(const SharedSkeletonComponent& sharedSkeletalComponent); SharedSkeletonComponent& operator=(SharedSkeletonComponent&& sharedSkeletalComponent) noexcept; private: + const Skeleton& GetAttachedSkeleton() const override; inline bool IsAttachedSkeletonOutdated() const; void OnReferenceJointsInvalidated(const Skeleton* skeleton); void SetSkeletonParent(Node* parent); diff --git a/include/Nazara/Utility/Components/SkeletonComponent.hpp b/include/Nazara/Utility/Components/SkeletonComponent.hpp index b31a5bd67..dcd512f89 100644 --- a/include/Nazara/Utility/Components/SkeletonComponent.hpp +++ b/include/Nazara/Utility/Components/SkeletonComponent.hpp @@ -23,11 +23,13 @@ namespace Nz SkeletonComponent(SkeletonComponent&& skeletalComponent) noexcept = default; ~SkeletonComponent() = default; - const Joint& GetAttachedJoint(std::size_t jointIndex) const override; Node* GetRootNode(); SkeletonComponent& operator=(const SkeletonComponent&) = delete; SkeletonComponent& operator=(SkeletonComponent&& skeletalComponent) noexcept = default; + + private: + const Skeleton& GetAttachedSkeleton() const override; }; } diff --git a/include/Nazara/Utility/Components/SkeletonComponentBase.hpp b/include/Nazara/Utility/Components/SkeletonComponentBase.hpp index 016e1b5d4..4d327e535 100644 --- a/include/Nazara/Utility/Components/SkeletonComponentBase.hpp +++ b/include/Nazara/Utility/Components/SkeletonComponentBase.hpp @@ -20,7 +20,9 @@ namespace Nz SkeletonComponentBase(SkeletonComponentBase&&) noexcept = default; ~SkeletonComponentBase() = default; - virtual const Joint& GetAttachedJoint(std::size_t jointIndex) const = 0; + inline std::size_t FindJointByName(const std::string& jointName) const; + + inline const Joint& GetAttachedJoint(std::size_t jointIndex) const; inline const std::shared_ptr& GetSkeleton() const; SkeletonComponentBase& operator=(const SkeletonComponentBase&) = default; @@ -29,6 +31,8 @@ namespace Nz protected: SkeletonComponentBase(std::shared_ptr skeleton); + virtual const Skeleton& GetAttachedSkeleton() const = 0; + std::shared_ptr m_referenceSkeleton; }; } diff --git a/include/Nazara/Utility/Components/SkeletonComponentBase.inl b/include/Nazara/Utility/Components/SkeletonComponentBase.inl index 48d93e30a..6b3aee4ca 100644 --- a/include/Nazara/Utility/Components/SkeletonComponentBase.inl +++ b/include/Nazara/Utility/Components/SkeletonComponentBase.inl @@ -12,6 +12,16 @@ namespace Nz { } + inline std::size_t SkeletonComponentBase::FindJointByName(const std::string& jointName) const + { + return m_referenceSkeleton->GetJointIndex(jointName); + } + + inline const Joint& SkeletonComponentBase::GetAttachedJoint(std::size_t jointIndex) const + { + return *GetAttachedSkeleton().GetJoint(jointIndex); + } + inline const std::shared_ptr& SkeletonComponentBase::GetSkeleton() const { return m_referenceSkeleton; diff --git a/src/Nazara/Utility/Components/NodeComponent.cpp b/src/Nazara/Utility/Components/NodeComponent.cpp index 24263b48a..6f766dd54 100644 --- a/src/Nazara/Utility/Components/NodeComponent.cpp +++ b/src/Nazara/Utility/Components/NodeComponent.cpp @@ -3,6 +3,8 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include +#include #include #include @@ -15,4 +17,32 @@ namespace Nz Node::SetParent(nodeComponent, keepDerived); } + + void NodeComponent::SetParentJoint(entt::handle entity, const std::string& jointName, bool keepDerived) + { + SkeletonComponentBase* skeletonComponent = entity.try_get(); + if (!skeletonComponent) + skeletonComponent = entity.try_get(); + + NazaraAssert(skeletonComponent, "entity doesn't have a SkeletonComponent nor a SharedSkeletonComponent"); + + std::size_t jointIndex = skeletonComponent->FindJointByName(jointName); + if (jointIndex == Skeleton::InvalidJointIndex) + { + NazaraError("skeleton has no joint \"" + jointName + "\""); + return; + } + + Node::SetParent(skeletonComponent->GetAttachedJoint(jointIndex), keepDerived); + } + + void NodeComponent::SetParentJoint(entt::handle entity, std::size_t jointIndex, bool keepDerived) + { + SkeletonComponentBase* skeletonComponent = entity.try_get(); + if (!skeletonComponent) + skeletonComponent = entity.try_get(); + + NazaraAssert(skeletonComponent, "entity doesn't have a SkeletonComponent nor a SharedSkeletonComponent"); + Node::SetParent(skeletonComponent->GetAttachedJoint(jointIndex), keepDerived); + } } diff --git a/src/Nazara/Utility/Components/SharedSkeletonComponent.cpp b/src/Nazara/Utility/Components/SharedSkeletonComponent.cpp index c33e61c9a..4822a2ff4 100644 --- a/src/Nazara/Utility/Components/SharedSkeletonComponent.cpp +++ b/src/Nazara/Utility/Components/SharedSkeletonComponent.cpp @@ -31,11 +31,6 @@ namespace Nz SetupSkeleton(); } - const Joint& SharedSkeletonComponent::GetAttachedJoint(std::size_t jointIndex) const - { - return *m_attachedSkeleton.GetJoint(jointIndex); - } - SharedSkeletonComponent& SharedSkeletonComponent::operator=(const SharedSkeletonComponent& sharedSkeletalComponent) { SkeletonComponentBase::operator=(sharedSkeletalComponent); @@ -58,6 +53,11 @@ namespace Nz return *this; } + const Skeleton& SharedSkeletonComponent::GetAttachedSkeleton() const + { + return m_attachedSkeleton; + } + void SharedSkeletonComponent::OnReferenceJointsInvalidated(const Skeleton* skeleton) { m_skeletonJointInvalidated = true; diff --git a/src/Nazara/Utility/Components/SkeletonComponent.cpp b/src/Nazara/Utility/Components/SkeletonComponent.cpp index f690e9660..276d2e07f 100644 --- a/src/Nazara/Utility/Components/SkeletonComponent.cpp +++ b/src/Nazara/Utility/Components/SkeletonComponent.cpp @@ -12,13 +12,13 @@ namespace Nz { } - const Joint& SkeletonComponent::GetAttachedJoint(std::size_t jointIndex) const - { - return *m_referenceSkeleton->GetJoint(jointIndex); - } - Node* SkeletonComponent::GetRootNode() { return m_referenceSkeleton->GetRootJoint(); } + + const Skeleton& SkeletonComponent::GetAttachedSkeleton() const + { + return *m_referenceSkeleton; + } }