Skip to content

Commit

Permalink
Fixed joint attachment component and skeleton visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
jankrassnigg committed Feb 25, 2021
1 parent a563dce commit eb6c184
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ class EZ_GAMEENGINE_DLL ezAnimatedMeshComponent : public ezSkinnedMeshComponent
void InitializeAnimationPose();

ezTransform m_RootTransform;
ezSkeletonResourceHandle m_hSkeleton;
ezSkinningSpaceAnimationPose m_SkinningSpacePose;
};
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,15 @@ void ezAnimatedMeshComponent::InitializeAnimationPose()
if (pMesh.GetAcquireResult() != ezResourceAcquireResult::Final)
return;

m_hSkeleton = pMesh->m_hDefaultSkeleton;
const auto hSkeleton = pMesh->m_hDefaultSkeleton;

if (!m_hSkeleton.IsValid())
if (!hSkeleton.IsValid())
return;

ezResourceLock<ezSkeletonResource> pSkeleton(m_hSkeleton, ezResourceAcquireMode::BlockTillLoaded);
ezResourceLock<ezSkeletonResource> pSkeleton(hSkeleton, ezResourceAcquireMode::BlockTillLoaded);
if (pSkeleton.GetAcquireResult() != ezResourceAcquireResult::Final)
return;

const ezSkeleton& skeleton = pSkeleton->GetDescriptor().m_Skeleton;

m_RootTransform = pSkeleton->GetDescriptor().m_RootTransform;

{
const ozz::animation::Skeleton* pOzzSkeleton = &pSkeleton->GetDescriptor().m_Skeleton.GetOzzSkeleton();
const ezUInt32 uiNumSkeletonJoints = pOzzSkeleton->num_joints();
Expand All @@ -107,37 +103,12 @@ void ezAnimatedMeshComponent::InitializeAnimationPose()

ezMsgAnimationPoseUpdated msg;
msg.m_ModelTransforms = pPoseMatrices;
msg.m_pRootTransform = &pSkeleton->GetDescriptor().m_RootTransform;
msg.m_pSkeleton = &pSkeleton->GetDescriptor().m_Skeleton;

OnAnimationPoseUpdated(msg);
}

// for (auto itBone : pMesh->m_Bones)
//{
// const ezUInt16 uiJointIdx = skeleton.FindJointByName(ezTempHashedString(itBone.Key().GetData()));
// const ezMat4 modelSpaceTransform = m_SkinningSpacePose.m_Transforms[uiJointIdx];

// m_SkinningSpacePose.m_Transforms[itBone.Value().m_uiBoneIndex] = modelSpaceTransform * itBone.Value().m_GlobalInverseBindPoseMatrix;

// ezUInt16 uiParentJointIdx = skeleton.GetJointByIndex(uiJointIdx).GetParentIndex();
// while (uiParentJointIdx != ezInvalidJointIndex)
// {
// auto parentName = skeleton.GetJointByIndex(uiParentJointIdx).GetName();

// auto itParent = pMesh->m_Bones.Find(parentName);

// if (itParent.IsValid())
// {
// auto& l = m_Lines.ExpandAndGetRef();
// l.m_start = itBone.Value().m_GlobalInverseBindPoseMatrix.GetInverse().GetTranslationVector();
// l.m_end = itParent.Value().m_GlobalInverseBindPoseMatrix.GetInverse().GetTranslationVector();
// break;
// }

// uiParentJointIdx = skeleton.GetJointByIndex(uiParentJointIdx).GetParentIndex();
// }
//}

// Create the buffer for the skinning matrices
CreateSkinningTransformBuffer(m_SkinningSpacePose.m_Transforms);
}
Expand All @@ -161,9 +132,11 @@ ezMeshRenderData* ezAnimatedMeshComponent::CreateRenderData() const

void ezAnimatedMeshComponent::OnAnimationPoseUpdated(ezMsgAnimationPoseUpdated& msg)
{
if (!m_hSkeleton.IsValid() || !m_hMesh.IsValid())
if (!m_hMesh.IsValid())
return;

m_RootTransform = *msg.m_pRootTransform;

ezResourceLock<ezMeshResource> pMesh(m_hMesh, ezResourceAcquireMode::BlockTillLoaded);

m_SkinningSpacePose.MapModelSpacePoseToSkinningSpace(pMesh->m_Bones, *msg.m_pSkeleton, msg.m_ModelTransforms);
Expand All @@ -174,7 +147,12 @@ void ezAnimatedMeshComponent::OnQueryAnimationSkeleton(ezMsgQueryAnimationSkelet
if (!msg.m_hSkeleton.IsValid())
{
// only overwrite, if no one else had a better skeleton (e.g. the ezSkeletonComponent)
msg.m_hSkeleton = m_hSkeleton;

ezResourceLock<ezMeshResource> pMesh(m_hMesh, ezResourceAcquireMode::BlockTillLoaded);
if (pMesh.GetAcquireResult() == ezResourceAcquireResult::Final)
{
msg.m_hSkeleton = pMesh->m_hDefaultSkeleton;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <GameEngine/Animation/Skeletal/JointAttachmentComponent.h>
#include <RendererCore/AnimationSystem/AnimationPose.h>
#include <RendererCore/AnimationSystem/Skeleton.h>
#include <RendererCore/Debug/DebugRenderer.h>

// clang-format off
EZ_BEGIN_COMPONENT_TYPE(ezJointAttachmentComponent, 1, ezComponentMode::Dynamic);
Expand Down Expand Up @@ -79,14 +80,27 @@ void ezJointAttachmentComponent::OnAnimationPoseUpdated(ezMsgAnimationPoseUpdate
if (m_uiJointIndex == ezInvalidJointIndex)
return;

ezTransform t;
t.SetFromMat4(msg.m_ModelTransforms[m_uiJointIndex]);
const ezMat4 bone = msg.m_pRootTransform->GetAsMat4() * msg.m_ModelTransforms[m_uiJointIndex];
ezQuat boneRot;

const ezQuat rot = t.m_qRotation * m_vLocalRotationOffset;
// the bone might contain (non-uniform) scaling and mirroring, which the quaternion can't represent
// so reconstruct a representable rotation matrix
{
const ezVec3 x = bone.TransformDirection(ezVec3(1, 0, 0)).GetNormalized();
const ezVec3 y = bone.TransformDirection(ezVec3(0, 1, 0)).GetNormalized();
const ezVec3 z = x.CrossRH(y);

ezMat3 m;
m.SetColumn(0, x);
m.SetColumn(1, y);
m.SetColumn(2, z);

boneRot.SetFromMat3(m);
}

ezGameObject* pOwner = GetOwner();
pOwner->SetLocalPosition(t.m_vPosition + rot * m_vLocalPositionOffset);
pOwner->SetLocalRotation(rot);
pOwner->SetLocalPosition(bone.GetTranslationVector() + bone.TransformDirection(m_vLocalPositionOffset));
pOwner->SetLocalRotation(boneRot * m_vLocalRotationOffset);
}

EZ_STATICLINK_FILE(GameEngine, GameEngine_Animation_Skeletal_Implementation_JointAttachmentComponent);
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ void ezSimpleAnimationComponent::Update()
// inform child nodes/components that a new pose is available
{
ezMsgAnimationPoseUpdated msg;
msg.m_pRootTransform = &pSkeleton->GetDescriptor().m_RootTransform;
msg.m_pSkeleton = &pSkeleton->GetDescriptor().m_Skeleton;
msg.m_ModelTransforms = pPoseMatrices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void ezAnimGraph::SendResultTo(ezGameObject* pObject)
Finalize(pSkeleton.GetPointer());

ezMsgAnimationPoseUpdated msg;
msg.m_pRootTransform = &pSkeleton->GetDescriptor().m_RootTransform;
msg.m_pSkeleton = &pSkeleton->GetDescriptor().m_Skeleton;
msg.m_ModelTransforms = m_ModelSpaceTransforms;

Expand Down
1 change: 1 addition & 0 deletions Code/Engine/RendererCore/AnimationSystem/Declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct EZ_RENDERERCORE_DLL ezMsgAnimationPoseUpdated : public ezMessage
{
EZ_DECLARE_MESSAGE_TYPE(ezMsgAnimationPoseUpdated, ezMessage);

const ezTransform* m_pRootTransform = nullptr;
const ezSkeleton* m_pSkeleton = nullptr;
ezArrayPtr<const ezMat4> m_ModelTransforms;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ void ezSkeletonComponent::OnAnimationPoseUpdated(ezMsgAnimationPoseUpdated& msg)
if (parentBone == ozz::animation::Skeleton::kNoParent)
return;

const ezVec3 v0 = msg.m_ModelTransforms[parentBone].GetTranslationVector();
const ezVec3 v1 = msg.m_ModelTransforms[currentBone].GetTranslationVector();
const ezVec3 v0 = *msg.m_pRootTransform * msg.m_ModelTransforms[parentBone].GetTranslationVector();
const ezVec3 v1 = *msg.m_pRootTransform * msg.m_ModelTransforms[currentBone].GetTranslationVector();

bsphere.ExpandToInclude(v0);

Expand Down Expand Up @@ -181,6 +181,7 @@ void ezSkeletonComponent::UpdateSkeletonVis()
}

ezMsgAnimationPoseUpdated msg;
msg.m_pRootTransform = &pSkeleton->GetDescriptor().m_RootTransform;
msg.m_pSkeleton = &pSkeleton->GetDescriptor().m_Skeleton;
msg.m_ModelTransforms = ezArrayPtr<const ezMat4>(reinterpret_cast<const ezMat4*>(&modelTransforms[0]), (ezUInt32)modelTransforms.size());

Expand Down

0 comments on commit eb6c184

Please sign in to comment.