Added skeletal animation 1D blending

This commit is contained in:
antopilo
2024-12-01 02:10:19 -05:00
parent 6a96ce497c
commit c8aa609ab1
5 changed files with 198 additions and 11 deletions

View File

@@ -0,0 +1,132 @@
#pragma once
#include <src/Core/Core.h>
#include "ComponentPanel.h"
#include "ModelResourceInspector.h"
#include "../Misc/PopupHelper.h"
#include <src/Scene/Entities/ImGuiHelper.h>
#include <src/Scene/Components/SkinnedModelComponent.h>
#include <src/Resource/ResourceLoader.h>
#include <src/Resource/ResourceManager.h>
#include <src/Core/String.h>
#include <src/Resource/ModelLoader.h>
class SkinnedMeshPanel : ComponentPanel
{
private:
Scope<ModelResourceInspector> _modelInspector;
bool _expanded = false;
std::string _importedPathMesh;
public:
SkinnedMeshPanel()
{
CreateScope<ModelResourceInspector>();
}
void Draw(Nuake::Entity& entity, entt::meta_any& componentInstance)
{
using namespace Nuake;
Nuake::SkinnedModelComponent* componentPtr = componentInstance.try_cast<Nuake::SkinnedModelComponent>();
if (componentPtr == nullptr)
{
return;
}
Nuake::SkinnedModelComponent& component = *componentPtr;
BeginComponentTable(SKINNED MESH, SkinnedModelComponent);
{
ImGui::Text("Model");
ImGui::TableNextColumn();
std::string label = "None";
const bool isModelNone = component.ModelResource == nullptr;
if (!isModelNone)
{
label = std::to_string(component.ModelResource->ID);
}
if (ImGui::Button(label.c_str(), ImVec2(ImGui::GetContentRegionAvail().x, 0)))
{
if (!isModelNone)
{
if (!_expanded)
{
//_modelInspector = CreateScope<ModelResourceInspector>(component.ModelResource);
}
_expanded = !_expanded;
}
}
if (_expanded)
{
//_modelInspector->Draw();
}
bool shouldConvert = false;
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("_Model"))
{
char* file = (char*)payload->Data;
std::string fullPath = std::string(file, 256);
fullPath = Nuake::FileSystem::AbsoluteToRelative(fullPath);
if (Nuake::String::EndsWith(fullPath, ".mesh"))
{
//component.ModelPath = fullPath;
//component.ModelResource = ResourceLoader::LoadModel(fullPath);
}
else
{
// Convert to .Model
Ref<Nuake::File> resourceFile = FileSystem::GetFile(fullPath);
component.ModelPath = resourceFile;
component.LoadModel((entt::entity)entity.GetHandle(), entity.GetScene());
_importedPathMesh = fullPath;
auto loader = ModelLoader();
auto modelResource = loader.LoadModel(fullPath);
shouldConvert = true;
}
}
ImGui::EndDragDropTarget();
}
//if (PopupHelper::DefineConfirmationDialog("##ConvertAsset", "Convert Asset"))
//{
// // Convert to disk
// auto loader = ModelLoader();
// Ref<Model> modelResource = loader.LoadModel(_importedPathMesh);
// json serializedData = modelResource->SerializeData();
// const std::string exportedMeshPath = _importedPathMesh + ".mesh";
// FileSystem::BeginWriteFile(exportedMeshPath);
// FileSystem::WriteLine(serializedData.dump());
// FileSystem::EndWriteFile();
// ResourceManager::RegisterResource(modelResource);
// // Update component
// component.ModelPath = exportedMeshPath;
// component.ModelResource = modelResource;
//}
//if (shouldConvert)
//{
// PopupHelper::OpenPopup("##ConvertAsset");
//}
ImGui::TableNextColumn();
//ComponentTableReset(component.ModelPath, "");
}
EndComponentTable();
}
};

View File

@@ -4,6 +4,11 @@
namespace Nuake
{
void SkinnedModelComponent::SetModel(ResourceFile file)
{
ModelPath = file;
}
void SkinnedModelComponent::LoadModel(entt::entity e, Scene* scene)
{
ModelLoader loader = ModelLoader();
@@ -70,6 +75,9 @@ namespace Nuake
{
SERIALIZE_OBJECT(ModelResource);
}
j["CurrentAnimationIndex"] = animationList.index;
END_SERIALIZE();
}
@@ -87,6 +95,15 @@ namespace Nuake
RegenerateAnimationList();
if (j.contains("CurrentAnimationIndex"))
{
int animIndex = j["CurrentAnimationIndex"];
if (animIndex > 0 && animIndex < animationList.items.size())
{
SetAnimationList(animIndex);
}
}
return true;
}
}

View File

@@ -21,7 +21,10 @@ namespace Nuake
static void InitializeComponentClass()
{
BindComponentField<&SkinnedModelComponent::ModelPath>("ModelPath", "Model Path");
//BindComponentField<&SkinnedModelComponent::ModelPath>("ModelPath", "Model Path");
BindComponentProperty<&SkinnedModelComponent::SetModel, &SkinnedModelComponent::GetModel>("ModelPath", "Model");
ResourceFileRestriction("_Model");
BindComponentProperty<&SkinnedModelComponent::SetPlaying, &SkinnedModelComponent::GetIsPlaying>("Playing", "Playing");
SetFlags(ComponentFieldTrait::Transient);
@@ -33,6 +36,12 @@ namespace Nuake
ResourceFile ModelPath;
DynamicItemList animationList;
void SetModel(ResourceFile file);
ResourceFile GetModel()
{
return ModelPath;
}
void LoadModel(entt::entity e, Scene* scene);
void SetPlaying(bool play);

View File

@@ -46,20 +46,28 @@ namespace Nuake
continue;
}
float animationTime = 0.0f;
Ref<SkeletalAnimation> animation = model->GetCurrentAnimation();
if (animation && model->IsPlaying)
float animTime = 0.0f;
float prevAnimTime = 0.0f;
Ref<SkeletalAnimation> anim = model->GetCurrentAnimation();
Ref<SkeletalAnimation> prevAnim = model->GetPreviousAnimation();
float blendWeight = model->GetCurrentBlendTime();
if (anim && model->IsPlaying)
{
animationTime = animation->GetCurrentTime() + (ts * animation->GetTicksPerSecond());
animation->SetCurrentTime(animationTime);
animTime = anim->GetCurrentTime() + (ts * anim->GetTicksPerSecond());
anim->SetCurrentTime(animTime);
model->SetCurrentBlendTime(blendWeight - ts);
Logger::Log("Blend time: " + std::to_string(model->GetCurrentBlendTime()));
prevAnimTime = prevAnim->GetCurrentTime() + (ts * prevAnim->GetTicksPerSecond());
prevAnim->SetCurrentTime(prevAnimTime);
}
auto& rootBone = model->GetSkeletonRootNode();
UpdateBonePositionTraversal(rootBone, animation, animationTime, model->IsPlaying);
UpdateBonePositionTraversal(rootBone, anim, prevAnim, animTime, prevAnimTime, model->IsPlaying, model->GetCurrentBlendTime());
}
}
void AnimationSystem::UpdateBonePositionTraversal(SkeletonNode& bone, Ref<SkeletalAnimation> animation, float time, bool isPlaying)
void AnimationSystem::UpdateBonePositionTraversal(SkeletonNode& bone, Ref<SkeletalAnimation> animation, Ref<SkeletalAnimation> prevAnimation, float time, float prevTime, bool isPlaying, float blendTime)
{
if (Entity boneEnt = m_Scene->GetEntityByID(bone.EntityHandle);
boneEnt.IsValid())
@@ -83,10 +91,31 @@ namespace Nuake
Vector3 localScale;
Decompose(finalTransform, localPosition, localRotation, localScale);
if (auto& prevAnimTrack = prevAnimation->GetTrack(bone.Name); !prevAnimTrack.IsEmpty())
{
prevAnimTrack.Update(prevTime);
Matrix4 prevTransform = prevAnimTrack.GetFinalTransform();
Vector3 prevLocalPosition;
Quat prevLocalRotation;
Vector3 prevLocalScale;
Decompose(prevTransform, prevLocalPosition, prevLocalRotation, prevLocalScale);
localPosition = glm::mix(localPosition, prevLocalPosition, blendTime);
localRotation = glm::slerp(localRotation, prevLocalRotation, blendTime);
prevLocalScale = glm::mix(localScale, prevLocalScale, blendTime);
}
Matrix4 finalLocalTransform = Matrix4(1.0f);
auto translateMatrix = glm::translate(Matrix4(1.0), localPosition);
auto RotationMatrix = glm::mat4_cast(localRotation);
auto scaleMatrix = glm::scale(Matrix4(1.0f), localScale);
auto finalMatrix = translateMatrix * RotationMatrix * scaleMatrix;
transformComponent.SetLocalPosition(localPosition);
transformComponent.SetLocalRotation(localRotation);
transformComponent.SetLocalScale(localScale);
transformComponent.SetLocalTransform(finalTransform);
transformComponent.SetLocalTransform(finalMatrix);
}
}
@@ -94,7 +123,7 @@ namespace Nuake
for (auto& childBone : bone.Children)
{
UpdateBonePositionTraversal(childBone, animation, time, isPlaying);
UpdateBonePositionTraversal(childBone, animation, prevAnimation, time, prevTime, isPlaying, blendTime);
}
}

View File

@@ -19,6 +19,6 @@ namespace Nuake
void Exit() override;
private:
void UpdateBonePositionTraversal(SkeletonNode& bone, Ref<SkeletalAnimation> animation, float time, bool isPlaying);
void UpdateBonePositionTraversal(SkeletonNode& bone, Ref<SkeletalAnimation> animation, Ref<SkeletalAnimation> prevAnimation, float time, float prevTime, bool isPlaying, float blendTime);
};
}