diff --git a/include/Engine/Rendering/Skeleton.h b/include/Engine/Rendering/Skeleton.h index a428b7f3..f1ef22c7 100644 --- a/include/Engine/Rendering/Skeleton.h +++ b/include/Engine/Rendering/Skeleton.h @@ -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 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& boneMatrices, const Bone* bone, glm::mat4 parentMatrix); - void AccumulateBoneTransforms(bool noRootMotion, const Animation* animation, float time, std::map& boneMatrices, const Bone* bone, glm::mat4 parentMatrix); void PrintSkeleton(); diff --git a/resources/Schema/Entities/AnimationTests2.xml b/resources/Schema/Entities/AnimationTests2.xml index baff7413..a116803f 100644 --- a/resources/Schema/Entities/AnimationTests2.xml +++ b/resources/Schema/Entities/AnimationTests2.xml @@ -10,7 +10,7 @@ Models/Core/UnitPlane.mesh - false + @@ -35,8 +35,8 @@ - Ru - + Run + 1 diff --git a/src/Engine/Rendering/AnimationSystem.cpp b/src/Engine/Rendering/AnimationSystem.cpp index 517fbf5c..c78aafd9 100644 --- a/src/Engine/Rendering/AnimationSystem.cpp +++ b/src/Engine/Rendering/AnimationSystem.cpp @@ -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 } - } - */ + }*/ + } diff --git a/src/Engine/Rendering/Skeleton.cpp b/src/Engine/Rendering/Skeleton.cpp index 86d1cb20..5ae6a264 100644 --- a/src/Engine/Rendering/Skeleton.cpp +++ b/src/Engine/Rendering/Skeleton.cpp @@ -41,17 +41,17 @@ const Skeleton::Animation* Skeleton::GetAnimation(std::string name) std::vector Skeleton::GetFrameBones(const Animation* animation, double time, bool noRootMotion /*= false*/) { - if(true){ - // if(animation == nullptr) { + if(animation == nullptr) { std::vector 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 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 frameBones; AccumulateBoneTransforms(noRootMotion, animation, time, frameBones, RootBone, glm::mat4(1)); @@ -79,86 +73,33 @@ std::vector Skeleton::GetFrameBones(const Animation* animation, doubl -void Skeleton::AccumulateBoneTransforms(bool noRootMotion, const Animation::Keyframe ¤tFrame, const Animation::Keyframe &nextFrame, float progress, std::map &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& 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 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 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 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; }