Changed animation system to support multiple roots

This commit is contained in:
Kah Wei 2023-03-20 14:53:03 +08:00
parent be68578ba0
commit 1476632220
3 changed files with 18 additions and 18 deletions

View File

@ -71,7 +71,7 @@ namespace SHADE
Play(); Play();
// Set to initial pose // Set to initial pose
if (rig && rig->GetRootNode()) if (rig && !rig->GetRootNodes().empty())
{ {
updateCurrentAnimatorState(currClip, 0.0f); updateCurrentAnimatorState(currClip, 0.0f);
} }
@ -124,7 +124,7 @@ namespace SHADE
std::fill(boneMatrices.begin(), boneMatrices.end(), SHMatrix::Identity); std::fill(boneMatrices.begin(), boneMatrices.end(), SHMatrix::Identity);
// Do not do anything if is not playing or there's nothing to animate // Do not do anything if is not playing or there's nothing to animate
if (!rig || !rig->GetRootNode()) if (!rig || rig->GetRootNodes().empty())
return; return;
// We want to still display a paused pose, so we only prevent progression // We want to still display a paused pose, so we only prevent progression
@ -274,7 +274,7 @@ namespace SHADE
void SHAnimatorComponent::updateCurrentAnimatorState(Handle<SHAnimationClip> clip, float playbackTime) void SHAnimatorComponent::updateCurrentAnimatorState(Handle<SHAnimationClip> clip, float playbackTime)
{ {
// Nothing to animate // Nothing to animate
if (!clip || !rig || !rig->GetRootNode()) if (!clip || !rig || rig->GetRootNodes().empty())
return; return;
// Check that we have animation data // Check that we have animation data
@ -286,7 +286,10 @@ namespace SHADE
} }
void SHAnimatorComponent::updatePoseWithClip(Handle<SHAnimationClip> clip, float poseTime) void SHAnimatorComponent::updatePoseWithClip(Handle<SHAnimationClip> clip, float poseTime)
{ {
updatePoseWithClip(poseTime, clip->GetRawAnimation(), rig->GetRootNode(), SHMatrix::Identity); for (auto rootNode : rig->GetRootNodes())
{
updatePoseWithClip(poseTime, clip->GetRawAnimation(), rootNode, SHMatrix::Identity);
}
} }
void SHAnimatorComponent::updatePoseWithClip(float poseTime, Handle<SHRawAnimation> rawAnimData, Handle<SHRigNode> node, const SHMatrix& parentMatrix) void SHAnimatorComponent::updatePoseWithClip(float poseTime, Handle<SHRawAnimation> rawAnimData, Handle<SHRigNode> node, const SHMatrix& parentMatrix)

View File

@ -33,22 +33,21 @@ namespace SHADE
} }
// Do a recursive depth first traversal to populate the rig // Do a recursive depth first traversal to populate the rig
rootNode = recurseCreateNode(asset, asset.root, nodeStore); auto rootNode = recurseCreateNode(asset, asset.root, nodeStore);
if (rootNode) if (rootNode)
{ {
globalInverseMatrix = SHMatrix::Inverse(rootNode->TransformMatrix); rootNodes.emplace_back(rootNode);
} }
} }
SHRig::SHRig(SHRig&& rhs) SHRig::SHRig(SHRig&& rhs)
: rootNode { rhs.rootNode } : rootNodes { std::move(rhs.rootNodes) }
, nodeNames { std::move(rhs.nodeNames) } , nodeNames { std::move(rhs.nodeNames) }
, nodesByName { std::move(rhs.nodesByName) } , nodesByName { std::move(rhs.nodesByName) }
, nodes { std::move(rhs.nodes) } , nodes { std::move(rhs.nodes) }
, nodeIndexMap { std::move(rhs.nodeIndexMap) } , nodeIndexMap { std::move(rhs.nodeIndexMap) }
, globalInverseMatrix { std::move(rhs.globalInverseMatrix) }
{ {
rhs.rootNode = {}; rhs.nodes = {};
} }
SHRig::~SHRig() SHRig::~SHRig()
{ {
@ -63,17 +62,17 @@ namespace SHADE
SHRig& SHRig::operator=(SHRig&& rhs) SHRig& SHRig::operator=(SHRig&& rhs)
{ {
rootNode = rhs.rootNode; rootNodes = std::move(rhs.rootNodes);
nodeNames = std::move(rhs.nodeNames); nodeNames = std::move(rhs.nodeNames);
nodesByName = std::move(rhs.nodesByName); nodesByName = std::move(rhs.nodesByName);
nodes = std::move(rhs.nodes); nodes = std::move(rhs.nodes);
nodeIndexMap = std::move(rhs.nodeIndexMap); nodeIndexMap = std::move(rhs.nodeIndexMap);
globalInverseMatrix = std::move(rhs.globalInverseMatrix);
rhs.rootNode = {}; rhs.rootNodes = {};
return *this; return *this;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Usage Functions */ /* Usage Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -104,11 +104,10 @@ namespace SHADE
/// </returns> /// </returns>
const std::string& GetName(Handle<SHRigNode> node) const noexcept; const std::string& GetName(Handle<SHRigNode> node) const noexcept;
/// <summary> /// <summary>
/// Retrieves the root node of the rig. /// Retrieves a read only reference to the root nodes of this rig.
/// </summary> /// </summary>
/// <returns>Handle to the root node of the rig.</returns> /// <returns>Vector of handles to the root node of the rig.</returns>
Handle<SHRigNode> GetRootNode() const noexcept { return rootNode; } const std::vector<Handle<SHRigNode>>& GetRootNodes() const noexcept { return rootNodes; }
const SHMatrix& GetGlobalInverseMatrix() const noexcept { return globalInverseMatrix; }
/// <summary> /// <summary>
/// Retrieves a node via name. /// Retrieves a node via name.
/// </summary> /// </summary>
@ -132,12 +131,11 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Handle<SHRigNode> rootNode; std::vector<Handle<SHRigNode>> rootNodes;
std::unordered_map<Handle<SHRigNode>, std::string> nodeNames; std::unordered_map<Handle<SHRigNode>, std::string> nodeNames;
std::unordered_map<std::string, Handle<SHRigNode>> nodesByName; std::unordered_map<std::string, Handle<SHRigNode>> nodesByName;
std::vector<Handle<SHRigNode>> nodes; std::vector<Handle<SHRigNode>> nodes;
std::unordered_map<Handle<SHRigNode>, int> nodeIndexMap; std::unordered_map<Handle<SHRigNode>, int> nodeIndexMap;
SHMatrix globalInverseMatrix;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */