Skip to content

Commit

Permalink
add skeleton resource
Browse files Browse the repository at this point in the history
  • Loading branch information
VTui22 committed Apr 25, 2024
1 parent 2d6e17c commit 867f06b
Show file tree
Hide file tree
Showing 27 changed files with 982 additions and 128 deletions.
12 changes: 12 additions & 0 deletions Engine/BuiltInShaders/shaders/vs_skeleton.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$input a_position, a_indices

#include "../common/common.sh"

uniform mat4 u_boneMatrices[128];

void main()
{
mat4 boneTransform = u_boneMatrices[a_indices[0]];
vec4 localPosition = mul(boneTransform, vec4(a_position, 1.0));
gl_Position = mul(u_modelViewProj, localPosition);
}
54 changes: 51 additions & 3 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include "Rendering/RenderContext.h"
#include "Rendering/Resources/MeshResource.h"
#include "Rendering/Resources/ResourceContext.h"
#include "Rendering/Resources/SkeletonResource.h"
#include "Rendering/Resources/TextureResource.h"
#include "Rendering/ShaderFeature.h"
#include "Resources/ResourceBuilder.h"
#include "Resources/ResourceLoader.h"
#include "Resources/ShaderBuilder.h"
#include "Rendering/Utility/VertexLayoutUtility.h"
#include "Scene/SceneDatabase.h"

#include <algorithm>
Expand Down Expand Up @@ -58,12 +60,14 @@ void ECWorldConsumer::Execute(const cd::SceneDatabase* pSceneDatabase)
if (hasSkin)
{
engine::MaterialType* pMaterialType = m_pSceneWorld->GetAnimationMaterialType();
AddSkinMesh(meshEntity, mesh, pMaterialType->GetRequiredVertexFormat());
AddSkinMesh(meshEntity, mesh, pMaterialType->GetRequiredVertexFormat(), pSceneDatabase);

// TODO : Use a standalone .cdanim file to play animation.
// Currently, we assume that imported SkinMesh will play animation automatically for testing.
//TODO : depens on skeleton to match animation
AddAnimation(meshEntity, pSceneDatabase->GetAnimation(0), pSceneDatabase);
AddMaterial(meshEntity, nullptr, pMaterialType, pSceneDatabase);
AddSkeleton(meshEntity, pSceneDatabase);
}
else
{
Expand Down Expand Up @@ -185,9 +189,39 @@ void ECWorldConsumer::AddStaticMesh(engine::Entity entity, const cd::Mesh& mesh,
staticMeshComponent.SetMeshResource(pMeshResource);
}

void ECWorldConsumer::AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat)
void ECWorldConsumer::AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat, const cd::SceneDatabase* pSceneDatabase)
{
AddStaticMesh(entity, mesh, vertexFormat);
assert(mesh.GetVertexCount() > 0 && mesh.GetPolygonCount() > 0);

engine::World* pWorld = m_pSceneWorld->GetWorld();
auto& nameComponent = pWorld->CreateComponent<engine::NameComponent>(entity);
std::string meshName(mesh.GetName());
engine::StringCrc meshNameCrc(meshName);
nameComponent.SetName(cd::MoveTemp(meshName));

auto& collisionMeshComponent = pWorld->CreateComponent<engine::CollisionMeshComponent>(entity);
collisionMeshComponent.SetType(engine::CollisonMeshType::AABB);
collisionMeshComponent.SetAABB(mesh.GetAABB());
collisionMeshComponent.Build();

auto& staticMeshComponent = pWorld->CreateComponent<engine::StaticMeshComponent>(entity);
engine::MeshResource* pMeshResource = m_pResourceContext->AddMeshResource(meshNameCrc);
pMeshResource->SetMeshAsset(&mesh);

//auto& skinMeshComponent = pWorld->CreateComponent<engine::SkinMeshComponent>(entity);
for (auto skinID : mesh.GetSkinIDs())
{
pMeshResource->SetSkinAsset(&pSceneDatabase->GetSkin(skinID.Data()));
auto skeletonID = pSceneDatabase->GetSkin(skinID.Data()).GetSkeletonID();
auto& skeleton = pSceneDatabase->GetSkeleton(skeletonID.Data());
for (auto boneID : skeleton.GetBoneIDs())
{
pMeshResource->AddBonesAsset(pSceneDatabase->GetBone(boneID.Data()));
}
}

pMeshResource->UpdateVertexFormat(vertexFormat);
staticMeshComponent.SetMeshResource(pMeshResource);
}

void ECWorldConsumer::AddAnimation(engine::Entity entity, const cd::Animation& animation, const cd::SceneDatabase* pSceneDatabase)
Expand Down Expand Up @@ -434,4 +468,18 @@ void ECWorldConsumer::AddParticleEmitter(engine::Entity entity, const cd::Mesh&
particleEmitterComponent.Build();
}

void ECWorldConsumer::AddSkeleton(engine::Entity entity, const cd::SceneDatabase* pSceneDatabase)
{
engine::World* pWorld = m_pSceneWorld->GetWorld();
auto& SkeletonComponent = pWorld->CreateComponent<engine::SkeletonComponent>(entity);

auto& skeleton = pSceneDatabase->GetSkeleton(0);
std::string meshName(skeleton.GetName());
engine::StringCrc meshNameCrc(meshName);
engine::SkeletonResource* pSkeletonResource = m_pResourceContext->AddSkeletonResource(meshNameCrc);
pSkeletonResource->SetSceneDataBase(pSceneDatabase);
SkeletonComponent.SetSkeletonAsset(pSkeletonResource);

}

}
3 changes: 2 additions & 1 deletion Engine/Source/Editor/ECWorld/ECWorldConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class ECWorldConsumer final : public cdtools::IConsumer
void AddLight(engine::Entity entity, const cd::Light& light);
void AddTransform(engine::Entity entity, const cd::Transform& transform);
void AddStaticMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat);
void AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat);
void AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat, const cd::SceneDatabase* pSceneDatabase);
void AddSkeleton(engine::Entity entity, const cd::SceneDatabase* pSceneDatabase);
void AddAnimation(engine::Entity entity, const cd::Animation& animation, const cd::SceneDatabase* pSceneDatabase);
void AddMaterial(engine::Entity entity, const cd::Material* pMaterial, engine::MaterialType* pMaterialType, const cd::SceneDatabase* pSceneDatabase);
void AddBlendShape(engine::Entity entity, const cd::Mesh* pMesh, const cd::BlendShape& blendShape, const cd::SceneDatabase* pSceneDatabase);
Expand Down
16 changes: 8 additions & 8 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ void EditorApp::InitEngineRenderers()
AddEngineRenderer(cd::MoveTemp(pPBRSkyRenderer));
}

auto pSkeletonRenderer = std::make_unique<engine::SkeletonRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pSkeletonRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pSkeletonRenderer));

auto pAnimationRenderer = std::make_unique<engine::AnimationRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pAnimationRenderer));

auto pSceneRenderer = std::make_unique<engine::WorldRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pSceneRenderer = pSceneRenderer.get();
pSceneRenderer->SetSceneWorld(m_pSceneWorld.get());
Expand All @@ -516,14 +524,6 @@ void EditorApp::InitEngineRenderers()
pTerrainRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pTerrainRenderer));

auto pSkeletonRenderer = std::make_unique<engine::SkeletonRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pSkeletonRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pSkeletonRenderer));

auto pAnimationRenderer = std::make_unique<engine::AnimationRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pAnimationRenderer));

auto pWhiteModelRenderer = std::make_unique<engine::WhiteModelRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pWhiteModelRenderer = pWhiteModelRenderer.get();
pWhiteModelRenderer->SetSceneWorld(m_pSceneWorld.get());
Expand Down
1 change: 0 additions & 1 deletion Engine/Source/Editor/UILayers/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,6 @@ void AssetBrowser::ImportModelFile(const char* pFilePath)
// Step 2 : Process generated cd::SceneDatabase
ProcessSceneDatabase(pSceneDatabase, m_importOptions.ImportMesh, m_importOptions.ImportMaterial, m_importOptions.ImportTexture,
m_importOptions.ImportCamera, m_importOptions.ImportLight);

// Step 3 : Convert cd::SceneDatabase to entities and components
{
ECWorldConsumer ecConsumer(pSceneWorld, pCurrentRenderContext);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Editor/UILayers/AssetBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct AssetImportOptions
bool ImportMaterial = true;
bool ImportMesh = true;
bool ImportTexture = true;
bool ImportAnimation = false;
bool ImportAnimation = true;
};

struct AssetExportOptions
Expand Down
27 changes: 27 additions & 0 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,32 @@ void UpdateComponentWidget<engine::ParticleForceFieldComponent>(engine::SceneWor
ImGui::PopStyleVar();
}

template<>
void UpdateComponentWidget<engine::AnimationComponent>(engine::SceneWorld* pSceneWorld, engine::Entity entity)
{
auto* pAnimationComponent = pSceneWorld->GetAnimationComponent(entity);
if (!pAnimationComponent)
{
return;
}

bool isOpen = ImGui::CollapsingHeader("Animation Component", ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGui::Separator();

if (isOpen)
{
ImGui::Separator();
ImGuiUtils::ImGuiBoolProperty("play", pAnimationComponent->GetIsPlaying());
ImGuiUtils::ImGuiEnumProperty("AnimationClip", pAnimationComponent->GetAnimationClip());
ImGuiUtils::ImGuiFloatProperty("Factor", pAnimationComponent->GetBlendFactor(), cd::Unit::None, 0.0f, 1.0f, false, 0.01f);
ImGuiUtils::ImGuiFloatProperty("Time", pAnimationComponent->GetAnimationPlayTime(), cd::Unit::None);
ImGuiUtils::ImGuiFloatProperty("PlayBackSpeed", pAnimationComponent->GetPlayBackSpeed(), cd::Unit::None, 0.0f, 10.0f, false, 0.01f);
}
ImGui::Separator();
ImGui::PopStyleVar();
}

}

namespace editor
Expand Down Expand Up @@ -766,6 +792,7 @@ void Inspector::Update()
details::UpdateComponentWidget<engine::ParticleForceFieldComponent>(pSceneWorld, m_lastSelectedEntity);
details::UpdateComponentWidget<engine::CollisionMeshComponent>(pSceneWorld, m_lastSelectedEntity);
details::UpdateComponentWidget<engine::BlendShapeComponent>(pSceneWorld, m_lastSelectedEntity);
details::UpdateComponentWidget<engine::AnimationComponent>(pSceneWorld, m_lastSelectedEntity);

if (IsOpenFileBrowser())
{
Expand Down
17 changes: 9 additions & 8 deletions Engine/Source/Editor/UILayers/SkeletonView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ void SkeletonView::DrawBone(cd::SceneDatabase* pSceneDatabase, const cd::Bone& B
void SkeletonView::DrawSkeleton(engine::SceneWorld* pSceneWorld)
{
cd::SceneDatabase* pSceneDatabase = pSceneWorld->GetSceneDatabase();
if (0 == pSceneDatabase->GetBoneCount())
if (0 == pSceneDatabase->GetSkeletonCount())
{
return;
}
const cd::Bone& rootBone = pSceneDatabase->GetBone(0);
DrawBone(pSceneDatabase, rootBone);
for (uint32_t skeletonIndex = 0; skeletonIndex < pSceneDatabase->GetSkeletonCount(); skeletonIndex++)
{
const cd::Skeleton& skeleton = pSceneDatabase->GetSkeleton(skeletonIndex);
auto rootBoneID = skeleton.GetRootBoneID();
const cd::Bone& rootBone = pSceneDatabase->GetBone(rootBoneID.Data());
DrawBone(pSceneDatabase, rootBone);
}
}

void SkeletonView::Update()
Expand All @@ -63,11 +68,7 @@ void SkeletonView::Update()
ImGui::End();
return;
}
engine::AnimationComponent* pAnimationConponent = pSceneWorld->GetAnimationComponent(selectedEntity);
if (pAnimationConponent)
{
DrawSkeleton(pSceneWorld);
}
DrawSkeleton(pSceneWorld);

ImGui::End();
}
Expand Down
1 change: 1 addition & 0 deletions Engine/Source/Editor/UILayers/SkeletonView.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Scene/Bone.h"
#include "Scene/SceneDatabase.h"
#include "Scene/Skeleton.h"

namespace editor
{
Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Runtime/ECWorld/AllComponentsHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "ECWorld/NameComponent.h"
#include "ECWorld/SkyComponent.h"
#include "ECWorld/StaticMeshComponent.h"
#include "ECWorld/SkeletonComponent.h"
#include "ECWorld/SkinMeshComponent.h"
#include "ECWorld/TerrainComponent.h"
#include "ECWorld/TransformComponent.h"
#include "ECWorld/ParticleEmitterComponent.h"
Expand Down
39 changes: 34 additions & 5 deletions Engine/Source/Runtime/ECWorld/AnimationComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ class Track;
namespace engine
{

enum class AnimationClip
{
Idel,
Walking,
Running,

Blend,
Switch,
Count,
};

class AnimationComponent final
{
public:
Expand All @@ -35,7 +46,7 @@ class AnimationComponent final

const cd::Animation* GetAnimationData() const { return m_pAnimation; }
void SetAnimationData(const cd::Animation* pAnimation) { m_pAnimation = pAnimation; }

// TODO : use std::span to present pointer array.
const cd::Track* GetTrackData() const { return m_pTrack; }
void SetTrackData(const cd::Track* pTrack) { m_pTrack = pTrack; }
Expand All @@ -49,14 +60,32 @@ class AnimationComponent final
void SetBoneMatricesUniform(uint16_t uniform) { m_boneMatricesUniform = uniform; }
uint16_t GetBoneMatrixsUniform() const { return m_boneMatricesUniform; }

void SetBoneMatrices(std::vector<cd::Matrix4x4> boneMatrices) { m_boneMatrices = cd::MoveTemp(boneMatrices); }
std::vector<cd::Matrix4x4>& GetBoneMatrices() { return m_boneMatrices; }
const std::vector<cd::Matrix4x4>& GetBoneMatrices() const { return m_boneMatrices; }
void SetAnimationPlayTime(float time) { m_animationPlayTime = time; }
float& GetAnimationPlayTime() { return m_animationPlayTime; }

void SetPlayBackSpeed(float time) { m_playBackSpeed = time; }
float& GetPlayBackSpeed() { return m_playBackSpeed; }

void SetAnimationClip(AnimationClip crtClip) { m_clip = crtClip; }
AnimationClip& GetAnimationClip() { return m_clip; }
const AnimationClip& GeAnimationClip() const { return m_clip; }

void SetBlendFactor(float factor) { m_blendFactor = factor; }
float& GetBlendFactor() { return m_blendFactor; }
const float GetBlendFactor() const { return m_blendFactor; }

bool& GetIsPlaying() { return m_playAnimation; }

private:
AnimationClip m_clip = AnimationClip::Idel;
const cd::Animation* m_pAnimation = nullptr;
const cd::Track* m_pTrack = nullptr;


float m_blendFactor = 0.0f;
float m_playBackSpeed = 1.0f;
bool m_playAnimation = false;

float m_animationPlayTime;
float m_duration;
float m_ticksPerSecond;
uint16_t m_boneMatricesUniform;
Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Runtime/ECWorld/SceneWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ SceneWorld::SceneWorld()
m_pNameComponentStorage = m_pWorld->Register<engine::NameComponent>();
m_pSkyComponentStorage = m_pWorld->Register<engine::SkyComponent>();
m_pStaticMeshComponentStorage = m_pWorld->Register<engine::StaticMeshComponent>();
m_pSkinMeshComponentStorage = m_pWorld->Register<engine::SkinMeshComponent>();
m_pSkeletonComponentStorage = m_pWorld->Register<engine::SkeletonComponent>();
m_pParticleEmitterComponentStorage = m_pWorld->Register<engine::ParticleEmitterComponent>();
m_pParticleForceFieldComponentStorage = m_pWorld->Register<engine::ParticleForceFieldComponent>();
m_pTerrainComponentStorage = m_pWorld->Register<engine::TerrainComponent>();
Expand Down
4 changes: 4 additions & 0 deletions Engine/Source/Runtime/ECWorld/SceneWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class SceneWorld
DEFINE_COMPONENT_STORAGE_WITH_APIS(Light);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Material);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Name);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Skeleton);
DEFINE_COMPONENT_STORAGE_WITH_APIS(SkinMesh);
DEFINE_COMPONENT_STORAGE_WITH_APIS(Sky);
DEFINE_COMPONENT_STORAGE_WITH_APIS(StaticMesh);
DEFINE_COMPONENT_STORAGE_WITH_APIS(ParticleEmitter);
Expand Down Expand Up @@ -96,6 +98,8 @@ class SceneWorld
DeleteLightComponent(entity);
DeleteMaterialComponent(entity);
DeleteNameComponent(entity);
DeleteSkeletonComponent(entity);
DeleteSkinMeshComponent(entity);
DeleteSkyComponent(entity);
DeleteStaticMeshComponent(entity);
DeleteParticleEmitterComponent(entity);
Expand Down
Loading

0 comments on commit 867f06b

Please sign in to comment.