BoneAttachment working

This commit is contained in:
viktorljung
2016-02-17 15:48:45 +01:00
parent db8bc612bd
commit debb1881bf
3 changed files with 68 additions and 54 deletions
+1 -1
View File
@@ -108,7 +108,7 @@ public:
void AccumulateBoneTransforms(bool noRootMotion, std::vector<AnimationData> animations, std::map<int, glm::mat4>& frameBones, const Bone* bone, glm::mat4 parentMatrix); void AccumulateBoneTransforms(bool noRootMotion, std::vector<AnimationData> animations, std::map<int, glm::mat4>& frameBones, const Bone* bone, glm::mat4 parentMatrix);
void AccumulateBoneTransforms(bool noRootMotion, std::vector<AnimationData> animations, AnimationOffset animationOffset, std::map<int, glm::mat4>& frameBones, const Bone* bone, glm::mat4 parentMatrix); void AccumulateBoneTransforms(bool noRootMotion, std::vector<AnimationData> animations, AnimationOffset animationOffset, std::map<int, glm::mat4>& frameBones, const Bone* bone, glm::mat4 parentMatrix);
glm::mat4 AdditiveBlend(glm::mat4 differencePose, glm::mat4 targetPose); glm::mat4 AdditiveBlend(const Bone* bone, AnimationOffset animationOffset, glm::mat4 targetPose);
void PrintSkeleton(); void PrintSkeleton();
void PrintSkeleton(const Bone* parent, int depthCount); void PrintSkeleton(const Bone* parent, int depthCount);
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
<Components>
<c:BoneAttachment>
<BoneName>R_Arm_Weapon_Joint</BoneName>
</c:BoneAttachment>
<c:Model>
<Resource>Models/Weapons/Blue/AssaultWeaponBlue.mesh</Resource>
</c:Model>
<c:Transform>
<Position X="0.136586398" Y="0.92942369" Z="-0.139760047"/>
<Orientation X="-0.601206124" Y="0.132410824" Z="-0.129014626"/>
</c:Transform>
</Components>
<Children/>
</Entity>
+48 -53
View File
@@ -276,7 +276,11 @@ void Skeleton::AccumulateBoneTransforms(bool noRootMotion, std::vector<Animation
if (JointTransforms.size() == 0) { if (JointTransforms.size() == 0) {
if (bone->Parent) { if (bone->Parent) {
boneMatrix = parentMatrix * (glm::inverse(bone->OffsetMatrix) * bone->Parent->OffsetMatrix);
glm::mat4 jointPose = (glm::inverse(bone->OffsetMatrix) * bone->Parent->OffsetMatrix);
glm::mat4 boneTransform = AdditiveBlend(bone, animationOffset, jointPose);
boneMatrix = parentMatrix * boneTransform;
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix; boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
} else { } else {
boneMatrix = glm::inverse(bone->OffsetMatrix); boneMatrix = glm::inverse(bone->OffsetMatrix);
@@ -308,22 +312,8 @@ void Skeleton::AccumulateBoneTransforms(bool noRootMotion, std::vector<Animation
} }
JointFrameTransform offsetTransform = GetOffsetTransform(bone, animationOffset);
AnimationOffset ao = animationOffset;
ao.time = 0.5f;
JointFrameTransform offsetTransform0 = GetOffsetTransform(bone, ao);
glm::mat4 rPose = (glm::translate(offsetTransform.Position) * glm::toMat4(offsetTransform.Rotation) * glm::scale(offsetTransform.Scale));
glm::mat4 sPose = (glm::translate(offsetTransform0.Position) * glm::toMat4(offsetTransform0.Rotation) * glm::scale(offsetTransform0.Scale));
glm::mat4 jointPose = (glm::translate(jointFinalTransform.Position) * glm::toMat4(jointFinalTransform.Rotation) * glm::scale(jointFinalTransform.Scale)); glm::mat4 jointPose = (glm::translate(jointFinalTransform.Position) * glm::toMat4(jointFinalTransform.Rotation) * glm::scale(jointFinalTransform.Scale));
glm::mat4 boneTransform = AdditiveBlend(bone, animationOffset, jointPose);
glm::mat4 diff = rPose * glm::inverse(sPose);
glm::mat4 boneTransform = AdditiveBlend(diff, jointPose);
boneMatrix = parentMatrix * boneTransform; boneMatrix = parentMatrix * boneTransform;
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix; boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
@@ -335,10 +325,19 @@ void Skeleton::AccumulateBoneTransforms(bool noRootMotion, std::vector<Animation
} }
glm::mat4 Skeleton::AdditiveBlend(glm::mat4 differencePose, glm::mat4 targetPose) glm::mat4 Skeleton::AdditiveBlend(const Bone* bone, AnimationOffset animationOffset, glm::mat4 targetPose)
{ {
glm::mat4 finalMat = differencePose * targetPose; AnimationOffset refOffset = animationOffset;
return finalMat; refOffset.time = 0.5f; // reference pose is at 0.5s for now
JointFrameTransform refOffsetTransform = GetOffsetTransform(bone, refOffset);
JointFrameTransform srcOffsetTransform = GetOffsetTransform(bone, animationOffset);
glm::mat4 srcPose = (glm::translate(srcOffsetTransform.Position) * glm::toMat4(srcOffsetTransform.Rotation) * glm::scale(srcOffsetTransform.Scale));
glm::mat4 refPose = (glm::translate(refOffsetTransform.Position) * glm::toMat4(refOffsetTransform.Rotation) * glm::scale(refOffsetTransform.Scale));
glm::mat4 differencePose = srcPose * glm::inverse(refPose);
glm::mat4 finalPose = differencePose * targetPose;
return finalPose;
} }
Skeleton::JointFrameTransform Skeleton::GetOffsetTransform(const Bone* bone, AnimationOffset animationOffset) Skeleton::JointFrameTransform Skeleton::GetOffsetTransform(const Bone* bone, AnimationOffset animationOffset)
@@ -402,7 +401,6 @@ Skeleton::JointFrameTransform Skeleton::GetOffsetTransform(const Bone* bone, Ani
return jointTransform; return jointTransform;
} }
glm::mat4 Skeleton::GetBoneTransform(const Bone* bone, const Animation* animation, float time, glm::mat4 childMatrix) glm::mat4 Skeleton::GetBoneTransform(const Bone* bone, const Animation* animation, float time, glm::mat4 childMatrix)
{ {
glm::mat4 boneMatrix; glm::mat4 boneMatrix;
@@ -468,7 +466,7 @@ glm::mat4 Skeleton::GetBoneTransform(const Bone* bone, const Animation* animatio
glm::mat4 Skeleton::GetBoneTransform(bool noRootMotion, const Bone* bone, std::vector<AnimationData> animations, AnimationOffset animationOffset, glm::mat4 childMatrix) glm::mat4 Skeleton::GetBoneTransform(bool noRootMotion, const Bone* bone, std::vector<AnimationData> animations, AnimationOffset animationOffset, glm::mat4 childMatrix)
{ {
glm::mat4 boneMatrix; glm::mat4 boneMatrix;
/*
std::vector<JointFrameTransform> JointTransforms; std::vector<JointFrameTransform> JointTransforms;
for (const AnimationData animationData : animations) { for (const AnimationData animationData : animations) {
@@ -511,23 +509,23 @@ glm::mat4 Skeleton::GetBoneTransform(bool noRootMotion, const Bone* bone, std::v
Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties; Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties;
Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties; Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties;
jointTransform.PositionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress; jointTransform.Position = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress;
jointTransform.RotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress); jointTransform.Rotation = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress);
jointTransform.ScaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress; jointTransform.Scale = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress;
// Flag for no root motion // Flag for no root motion
if (bone == RootBone && noRootMotion) { if (bone == RootBone && noRootMotion) {
jointTransform.PositionInterp.x = 0; jointTransform.Position.x = 0;
jointTransform.PositionInterp.z = 0; jointTransform.Position.z = 0;
} }
JointTransforms.push_back(jointTransform); JointTransforms.push_back(jointTransform);
} else { // 1 keyframes for the current bone } else { // 1 keyframes for the current bone
currentFrame = boneKeyFrames.at(0); currentFrame = boneKeyFrames.at(0);
jointTransform.PositionInterp = currentFrame.BoneProperties.Position; jointTransform.Position = currentFrame.BoneProperties.Position;
jointTransform.RotationInterp = currentFrame.BoneProperties.Rotation; jointTransform.Rotation = currentFrame.BoneProperties.Rotation;
jointTransform.ScaleInterp = currentFrame.BoneProperties.Scale; jointTransform.Scale = currentFrame.BoneProperties.Scale;
JointTransforms.push_back(jointTransform); JointTransforms.push_back(jointTransform);
} }
@@ -538,23 +536,20 @@ glm::mat4 Skeleton::GetBoneTransform(bool noRootMotion, const Bone* bone, std::v
} }
glm::mat4 offset = GetOffsetTransform(bone, animationOffset);
if (JointTransforms.size() == 0) { if (JointTransforms.size() == 0) {
if (bone->Parent) { if (bone->Parent) {
if (offset != glm::mat4(1)) {
boneMatrix = offset * childMatrix;// *((glm::inverse(bone->OffsetMatrix) * bone->Parent->OffsetMatrix)); glm::mat4 jointPose = (glm::inverse(bone->OffsetMatrix) * bone->Parent->OffsetMatrix);
} else { glm::mat4 boneTransform = AdditiveBlend(bone, animationOffset, jointPose);
boneMatrix = ((glm::inverse(bone->OffsetMatrix) * bone->Parent->OffsetMatrix)) * childMatrix;
} boneMatrix = boneTransform * childMatrix;
} else { } else {
boneMatrix = offset * glm::inverse(bone->OffsetMatrix); boneMatrix = glm::inverse(bone->OffsetMatrix) * childMatrix;
} }
} else { } else {
glm::vec3 finalPosInterp; JointFrameTransform jointFinalTransform;
glm::quat finalRotInterp;
glm::vec3 finalScaleInterp;
float totalWeight = 0; float totalWeight = 0;
for (JointFrameTransform jointTransform : JointTransforms) { for (JointFrameTransform jointTransform : JointTransforms) {
@@ -564,30 +559,30 @@ glm::mat4 Skeleton::GetBoneTransform(bool noRootMotion, const Bone* bone, std::v
for (JointFrameTransform jointTransform : JointTransforms) { for (JointFrameTransform jointTransform : JointTransforms) {
if (jointTransform.Weight == 1.0f) { if (jointTransform.Weight == 1.0f) {
finalPosInterp = jointTransform.PositionInterp; jointFinalTransform.Position = jointTransform.Position;
finalRotInterp = jointTransform.RotationInterp; jointFinalTransform.Rotation = jointTransform.Rotation;
finalScaleInterp = jointTransform.ScaleInterp; jointFinalTransform.Scale = jointTransform.Scale;
break; break;
} else { } else {
finalPosInterp += jointTransform.PositionInterp * (jointTransform.Weight/totalWeight); jointFinalTransform.Position += jointTransform.Position * (jointTransform.Weight/totalWeight);
finalRotInterp *= glm::slerp(glm::quat(), jointTransform.RotationInterp, (jointTransform.Weight/totalWeight)); jointFinalTransform.Rotation *= glm::slerp(glm::quat(), jointTransform.Rotation, (jointTransform.Weight/totalWeight));
finalScaleInterp += jointTransform.ScaleInterp * (jointTransform.Weight/totalWeight); jointFinalTransform.Scale += jointTransform.Scale * (jointTransform.Weight/totalWeight);
} }
} }
if (offset != glm::mat4(1)) {
boneMatrix = ((glm::translate(finalPosInterp) * glm::toMat4(finalRotInterp) * glm::scale(finalScaleInterp)) + offset) * childMatrix; glm::mat4 jointPose = (glm::translate(jointFinalTransform.Position) * glm::toMat4(jointFinalTransform.Rotation) * glm::scale(jointFinalTransform.Scale));
} else { glm::mat4 boneTransform = AdditiveBlend(bone, animationOffset, jointPose);
boneMatrix = (glm::translate(finalPosInterp) * glm::toMat4(finalRotInterp) * glm::scale(finalScaleInterp)) * childMatrix;
} boneMatrix = boneTransform * childMatrix;
} }
if (bone->Parent != nullptr) { if (bone->Parent != nullptr) {
return GetBoneTransform(noRootMotion, bone->Parent, animations, animationOffset, boneMatrix); return GetBoneTransform(noRootMotion, bone->Parent, animations, animationOffset, boneMatrix);
} else {*/ } else {
return boneMatrix; return boneMatrix;
// } }
} }