Fixed support for new animation structure, not done yet
This commit is contained in:
@@ -62,7 +62,6 @@ public:
|
||||
int Index = 0;
|
||||
double Time = 0.0;
|
||||
BoneProperty BoneProperties;
|
||||
//Keyframe::BoneProperty boneProperty;
|
||||
};
|
||||
std::string Name;
|
||||
double Duration;
|
||||
@@ -87,8 +86,6 @@ public:
|
||||
|
||||
const Animation* GetAnimation(std::string name);
|
||||
std::vector<glm::mat4> GetFrameBones(const Animation* animation, double time, bool noRootMotion = false);
|
||||
void AccumulateBoneTransforms(bool noRootMotion, const Animation::Keyframe& currentFrame, const Animation::Keyframe& nextFrame, float progress, std::map<int, glm::mat4>& boneMatrices, const Bone* bone, glm::mat4 parentMatrix);
|
||||
|
||||
void AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, float time, std::map<int, glm::mat4>& boneMatrices, const Bone* bone, glm::mat4 parentMatrix);
|
||||
|
||||
void PrintSkeleton();
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<Components>
|
||||
<c:Model>
|
||||
<Resource>Models/Core/UnitPlane.mesh</Resource>
|
||||
<Visible>false</Visible>
|
||||
<Color A="1" B="0.392156869" G="0.392156869" R="0.392156869"/>
|
||||
</c:Model>
|
||||
<c:Transform>
|
||||
<Position X="0" Y="-1.245" Z="0"/>
|
||||
@@ -35,8 +35,8 @@
|
||||
<Entity>
|
||||
<Components>
|
||||
<c:Animation>
|
||||
<AnimationName>Ru</AnimationName>
|
||||
<Time>0.68893688300324918</Time>
|
||||
<AnimationName>Run</AnimationName>
|
||||
<Time>0.71089640059930681</Time>
|
||||
<Speed>1</Speed>
|
||||
</c:Animation>
|
||||
<c:Model>
|
||||
|
||||
@@ -20,6 +20,15 @@ void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& a
|
||||
return;
|
||||
}
|
||||
|
||||
ImGui::SliderFloat("Angle", &angle, -100.f, 100.f);
|
||||
{
|
||||
int id = skeleton->GetBoneID("Spine_3");
|
||||
auto it = skeleton->Bones.find(id);
|
||||
if (it != skeleton->Bones.end()) {
|
||||
it->second->ModificationMatrix = glm::mat4(glm::quat(glm::vec3(glm::radians(angle), 0.f, 0.f)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const Skeleton::Animation* animation = skeleton->GetAnimation(animationComponent["AnimationName"]);
|
||||
|
||||
@@ -51,55 +60,10 @@ void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& a
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
ImGui::SliderFloat("Angle", &angle, -180.f, 180.f);
|
||||
|
||||
int id = skeleton->GetBoneID("Spine_2");
|
||||
auto it = skeleton->Bones.find(id);
|
||||
if (it != skeleton->Bones.end()) {
|
||||
int currentKeyframeIndex = skeleton->GetKeyframe(*animation, entity["Animation"]["Time"]);
|
||||
|
||||
const Skeleton::Animation::Keyframe& currentFrame = animation->Keyframes[currentKeyframeIndex];
|
||||
const Skeleton::Animation::Keyframe& nextFrame = animation->Keyframes[(currentKeyframeIndex + 1) % animation->Keyframes.size()];
|
||||
float alpha = ((double)entity["Animation"]["Time"] - currentFrame.Time) / (nextFrame.Time - currentFrame.Time);
|
||||
|
||||
glm::mat4 parentBoneTransform = skeleton->GetBoneTransform(it->second->Parent, currentFrame, nextFrame, alpha, glm::mat4(1));
|
||||
|
||||
|
||||
|
||||
glm::vec3 scale;
|
||||
glm::quat rotation;
|
||||
glm::vec3 translation;
|
||||
glm::vec3 skew;
|
||||
glm::vec4 perspective;
|
||||
glm::decompose(parentBoneTransform, scale, rotation, translation, skew, perspective);
|
||||
|
||||
|
||||
|
||||
glm::vec3 rot;
|
||||
rot = glm::vec3(1, 0, 0);
|
||||
rot = glm::normalize(rot) * glm::radians(angle);
|
||||
glm::mat4 modmat = glm::mat4(glm::quat(rot)) * glm::inverse(glm::mat4(rotation));
|
||||
|
||||
|
||||
it->second->ModificationMatrix = modmat;
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
{
|
||||
int id = skeleton->GetBoneID("Neck");
|
||||
auto it = skeleton->Bones.find(id);
|
||||
if (it != skeleton->Bones.end()) {
|
||||
it->second->ModificationMatrix = glm::mat4(glm::quat(glm::vec3(glm::radians(angle/2.f), 0.f, 0.f)));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
/* {
|
||||
int id = skeleton->GetBoneID("Spine_2");
|
||||
auto it = skeleton->Bones.find(id);
|
||||
if (it != skeleton->Bones.end()) {
|
||||
@@ -119,8 +83,9 @@ void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& a
|
||||
if (it != skeleton->Bones.end()) {
|
||||
it->second->ModificationMatrix = glm::mat4(glm::quat(glm::vec3(glm::radians(angle/2.f), 0.f, 0.f)));
|
||||
}
|
||||
}*
|
||||
}*/
|
||||
|
||||
/*
|
||||
if (entity.HasComponent("Player")) {
|
||||
EntityWrapper cameraEntity = entity.FirstChildByName("Camera");
|
||||
if (cameraEntity.Valid()) {
|
||||
@@ -128,7 +93,7 @@ void AnimationSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& a
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -41,17 +41,17 @@ const Skeleton::Animation* Skeleton::GetAnimation(std::string name)
|
||||
|
||||
std::vector<glm::mat4> Skeleton::GetFrameBones(const Animation* animation, double time, bool noRootMotion /*= false*/)
|
||||
{
|
||||
if(true){
|
||||
// if(animation == nullptr) {
|
||||
if(animation == nullptr) {
|
||||
std::vector<glm::mat4> finalMatrices;
|
||||
for(auto& b : Bones) {
|
||||
finalMatrices.push_back(glm::mat4(1));//b.second->OffsetMatrix);
|
||||
finalMatrices.push_back(b.second->ModificationMatrix);//b.second->OffsetMatrix);
|
||||
}
|
||||
return finalMatrices;
|
||||
}
|
||||
|
||||
|
||||
// HACK: Animation wrap-around
|
||||
|
||||
while (time < 0) {
|
||||
time += animation->Duration;
|
||||
}
|
||||
@@ -59,12 +59,6 @@ std::vector<glm::mat4> Skeleton::GetFrameBones(const Animation* animation, doubl
|
||||
time -= animation->Duration;
|
||||
}
|
||||
|
||||
int currentKeyframeIndex = GetKeyframe(*animation, time);
|
||||
|
||||
// const Animation::Keyframe& currentFrame = animation->Keyframes[currentKeyframeIndex];
|
||||
// const Animation::Keyframe& nextFrame = animation->Keyframes[(currentKeyframeIndex + 1) % animation->Keyframes.size()];
|
||||
// float alpha = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time);
|
||||
|
||||
//auto animationFrame = Animations[""].Keyframes[frame];
|
||||
std::map<int, glm::mat4> frameBones;
|
||||
AccumulateBoneTransforms(noRootMotion, animation, time, frameBones, RootBone, glm::mat4(1));
|
||||
@@ -79,86 +73,33 @@ std::vector<glm::mat4> Skeleton::GetFrameBones(const Animation* animation, doubl
|
||||
|
||||
|
||||
|
||||
void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation::Keyframe ¤tFrame, const Animation::Keyframe &nextFrame, float progress, std::map<int, glm::mat4> &boneMatrices, const Bone* bone, glm::mat4 parentMatrix)
|
||||
{
|
||||
/* glm::mat4 boneMatrix;
|
||||
|
||||
|
||||
|
||||
float alpha = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time);
|
||||
|
||||
|
||||
if (currentFrame.BoneProperties.find(bone->ID) != currentFrame.BoneProperties.end() && nextFrame.BoneProperties.find(bone->ID) != nextFrame.BoneProperties.end()) {
|
||||
|
||||
|
||||
Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties.at(bone->ID);
|
||||
Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties.at(bone->ID);
|
||||
|
||||
glm::vec3 positionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress;
|
||||
glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress);
|
||||
glm::vec3 scaleInterp = currentBoneProperty.Scale * (1.f - progress) + nextBoneProperty.Scale * progress;
|
||||
|
||||
|
||||
// Flag for no root motion
|
||||
if (bone == RootBone && noRootMotion) {
|
||||
positionInterp.x = 0;
|
||||
positionInterp.z = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
boneMatrix = parentMatrix * bone->ModificationMatrix *(glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp));
|
||||
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
|
||||
} else {
|
||||
if (bone->Parent) {
|
||||
boneMatrix = parentMatrix;// *glm::inverse(bone->OffsetMatrix);
|
||||
}
|
||||
boneMatrices[bone->ID] = boneMatrix;// *bone->OffsetMatrix;
|
||||
}
|
||||
|
||||
for (auto &child : bone->Children) {
|
||||
std::string name = child->Name;
|
||||
AccumulateBoneTransforms(noRootMotion, currentFrame, nextFrame, progress, boneMatrices, child, boneMatrix);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, float time, std::map<int, glm::mat4>& boneMatrices, const Bone* bone, glm::mat4 parentMatrix)
|
||||
{
|
||||
glm::mat4 boneMatrix;// = glm::mat4(1);
|
||||
|
||||
/*
|
||||
glm::mat4 boneMatrix = glm::mat4(1);
|
||||
Animation::Keyframe currentFrame;
|
||||
Animation::Keyframe nextFrame;
|
||||
|
||||
Animation::Keyframe* currentFrame = nullptr;
|
||||
Animation::Keyframe* nextFrame = nullptr;
|
||||
|
||||
if(animation->BoneKeyFrames.find(bone->ID) != animation->BoneKeyFrames.end()) { // find the bone keyframes that surrounds the current frame
|
||||
std::list<Animation::Keyframe> boneKeyFrames = animation->BoneKeyFrames.at(bone->ID);
|
||||
for (auto frame : boneKeyFrames) {
|
||||
if(frame.Time <= time) {
|
||||
currentFrame = &frame;
|
||||
} else if (frame.Time > time) {
|
||||
nextFrame = &frame;
|
||||
if(animation->JointAnimations.find(bone->ID) != animation->JointAnimations.end()) { // find the bone keyframes that surrounds the current frame
|
||||
std::vector<Animation::Keyframe> boneKeyFrames = animation->JointAnimations.at(bone->ID);
|
||||
|
||||
if(boneKeyFrames.size() > 1) { // 2+ keyframes for the current bone
|
||||
for (int index = boneKeyFrames.size()-1; index >= 0; index--) {
|
||||
if (time >= boneKeyFrames.at(index).Time) {
|
||||
currentFrame = boneKeyFrames.at(index);
|
||||
nextFrame = boneKeyFrames.at((index + 1) % boneKeyFrames.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//if(currentFrame == nullptr) cant happen
|
||||
|
||||
if (nextFrame == nullptr) {
|
||||
std::list<Animation::Keyframe> boneKeyFrames = animation->BoneKeyFrames.at(bone->ID);
|
||||
nextFrame = &boneKeyFrames.front();
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (currentFrame != nextFrame) {
|
||||
|
||||
float progress = (time - currentFrame->Time) / (nextFrame->Time - currentFrame->Time);
|
||||
|
||||
Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame->boneProperty;
|
||||
Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame->boneProperty;
|
||||
float progress = (time - currentFrame.Time) / (nextFrame.Time - currentFrame.Time); // fix loopinguuuu
|
||||
if(progress > 1.0f || progress < 0.0f) {
|
||||
LOG_INFO("Progress %f", progress);
|
||||
progress = 0.f;
|
||||
}
|
||||
Animation::Keyframe::BoneProperty currentBoneProperty = currentFrame.BoneProperties;
|
||||
Animation::Keyframe::BoneProperty nextBoneProperty = nextFrame.BoneProperties;
|
||||
|
||||
glm::vec3 positionInterp = currentBoneProperty.Position * (1.f - progress) + nextBoneProperty.Position * progress;
|
||||
glm::quat rotationInterp = glm::slerp(currentBoneProperty.Rotation, nextBoneProperty.Rotation, progress);
|
||||
@@ -171,26 +112,24 @@ void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation* anim
|
||||
}
|
||||
|
||||
boneMatrix = parentMatrix * bone->ModificationMatrix * (glm::translate(positionInterp) * glm::toMat4(rotationInterp) * glm::scale(scaleInterp));
|
||||
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
|
||||
|
||||
} else {// if (currentFrame == nextFrame) {
|
||||
boneMatrix = parentMatrix * bone->ModificationMatrix * (glm::translate(currentFrame->boneProperty.Position) * glm::toMat4(currentFrame->boneProperty.Rotation) * glm::scale(currentFrame->boneProperty.Scale));
|
||||
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
|
||||
}
|
||||
} else {
|
||||
// if (bone->Parent) { // dont think this is needed
|
||||
// boneMatrix = parentMatrix * bone->ModificationMatrix;
|
||||
// }
|
||||
boneMatrices[bone->ID] = boneMatrix *bone->OffsetMatrix;
|
||||
|
||||
} else { // 1 keyframes for the current bone
|
||||
currentFrame = boneKeyFrames.at(0);
|
||||
boneMatrix = parentMatrix * bone->ModificationMatrix *(glm::translate(currentFrame.BoneProperties.Position) * glm::toMat4(currentFrame.BoneProperties.Rotation) * glm::scale(currentFrame.BoneProperties.Scale));
|
||||
boneMatrices[bone->ID] = boneMatrix *bone->OffsetMatrix;
|
||||
}
|
||||
} else { // 0 keyframes for the current bone
|
||||
boneMatrix = parentMatrix * bone->ModificationMatrix;
|
||||
boneMatrices[bone->ID] = boneMatrix * bone->OffsetMatrix;
|
||||
|
||||
|
||||
boneMatrix = parentMatrix * bone->ModificationMatrix;
|
||||
boneMatrices[bone->ID] = boneMatrix;// *bone->OffsetMatrix;
|
||||
// }
|
||||
}
|
||||
|
||||
for (auto &child : bone->Children) {
|
||||
std::string name = child->Name;
|
||||
AccumulateBoneTransforms(noRootMotion, animation, time, boneMatrices, child, boneMatrix);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
glm::mat4 Skeleton::GetBoneTransform(const Bone* bone, const Animation::Keyframe& currentFrame, const Animation::Keyframe& nextFrame, float progress, glm::mat4 parentMatrix)
|
||||
@@ -254,12 +193,13 @@ void Skeleton::PrintSkeleton(const Bone* bone, int depthCount)
|
||||
|
||||
int Skeleton::GetKeyframe(const Animation& animation, double time)
|
||||
{
|
||||
|
||||
/*
|
||||
if (time < 0) {
|
||||
time = 0;
|
||||
}
|
||||
if (time >= animation.Duration) {
|
||||
return animation.Keyframes.size() - 1;
|
||||
return animation..size() - 1;
|
||||
}
|
||||
|
||||
for (int keyframe = 0; keyframe < animation.Keyframes.size(); ++keyframe) {
|
||||
@@ -269,5 +209,6 @@ int Skeleton::GetKeyframe(const Animation& animation, double time)
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user