#include "src/Resource/ModelLoader.h" #include "src/Resource/SkinnedModel.h" #include "src/Core/FileSystem.h" #include "src/Core/Logger.h" namespace Nuake { SkinnedModel::SkinnedModel(const std::string path) : m_Meshes(std::vector>()) { this->Path = path; } SkinnedModel::SkinnedModel() : m_Meshes(std::vector>()) {} SkinnedModel::~SkinnedModel() {} void SkinnedModel::AddMesh(Ref mesh) { m_Meshes.push_back(mesh); } std::vector>& SkinnedModel::GetMeshes() { return m_Meshes; } void SkinnedModel::SetAnimations(const std::vector> animations) { m_NumAnimation = static_cast(animations.size()); m_CurrentAnimation = 0; m_Animations = animations; } void SkinnedModel::AddAnimation(Ref animation) { m_NumAnimation++; m_Animations.push_back(std::move(animation)); } Ref SkinnedModel::GetCurrentAnimation() { if (m_CurrentAnimation < m_NumAnimation) { return m_Animations[m_CurrentAnimation]; } Logger::Log("Cannot get animation if no animation exists", "skinned model", WARNING); return nullptr; } void SkinnedModel::PlayAnimation(uint32_t animationId) { if (animationId >= m_NumAnimation) { Logger::Log("Cannot play animation, index out of range", "skinned model", CRITICAL); return; } GetCurrentAnimation()->SetCurrentTime(0.0f); // Reset previous animation m_CurrentAnimation = animationId; } json SkinnedModel::Serialize() { BEGIN_SERIALIZE(); if (this->Path != "") { j["Path"] = this->Path; } else { for (uint32_t i = 0; i < std::size(m_Meshes); i++) { j["Meshes"][i] = m_Meshes[i]->Serialize(); } j["m_CurrentAnimation"] = m_CurrentAnimation; j["m_NumAnimation"] = m_NumAnimation; uint32_t a = 0; for (auto& animation : m_Animations) { j["m_Animations"][a] = animation->Serialize(); } } END_SERIALIZE(); } bool SkinnedModel::Deserialize(const json& j) { if (j.contains("Path")) { this->IsEmbedded = true; ModelLoader loader; auto otherModel = loader.LoadSkinnedModel(j["Path"], false); m_Meshes = otherModel->GetMeshes(); m_Animations = otherModel->GetAnimations(); m_SkeletonRoot = otherModel->GetSkeletonRootNode(); m_NumAnimation = m_Animations.size(); m_CurrentAnimation = 0; this->Path = j["Path"]; } else { for (auto& m : j["Meshes"]) { auto mesh = CreateRef(); mesh->Deserialize(m); m_Meshes.push_back(mesh); } } return true; } }