Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
VTui22 committed Sep 13, 2023
1 parent b05ce97 commit 1a5f2d7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
27 changes: 21 additions & 6 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,35 @@ constexpr size_t indexTypeSize = sizeof(uint16_t);
cd::Vec3f CalculateBoneTranslate(const cd::Bone& bone, cd::Vec3f& translate, const cd::SceneDatabase* pSceneDatabase)
{
const cd::Bone& parentBone = pSceneDatabase->GetBone(bone.GetParentID().Data());
translate += parentBone.GetTransform().GetTranslation();
translate += parentBone.GetOffset().GetTranslation();
if (0U != bone.GetParentID().Data())
{
CalculateBoneTranslate(parentBone, translate, pSceneDatabase);
//CalculateBoneTranslate(parentBone, translate, pSceneDatabase);
}
return translate;
}

void CalculateTranslate(const cd::Bone& bone, const cd::SceneDatabase* pSceneDatabase, engine::SkinMeshComponent& skinmeshComponent)
{
for (cd::BoneID boneID : bone.GetChildIDs())
{
const cd::Bone& childBone = pSceneDatabase->GetBone(boneID.Data());
cd::Matrix4x4 matrix = skinmeshComponent.GetBoneMatrix(boneID.Data()).Inverse() * skinmeshComponent.GetBoneChangeMatrix(boneID.Data());
skinmeshComponent.SetBoneChangeMatrix(boneID.Data(), matrix);
CalculateTranslate(childBone, pSceneDatabase, skinmeshComponent);
}
}

void TraverseBone(const cd::Bone& bone, const cd::SceneDatabase* pSceneDatabase, engine::SkinMeshComponent& skinmeshComponent, std::byte* currentDataPtr,
std::byte* currentIndexPtr, uint32_t& vertexOffset, uint32_t& indexOffset)
{
for (auto& child : bone.GetChildIDs())
{
const cd::Bone& currBone = pSceneDatabase->GetBone(child.Data());
const cd::Bone& parent = pSceneDatabase->GetBone(currBone.GetParentID().Data());
cd::Vec3f translate = currBone.GetOffset().GetTranslation();
//const cd::Vec3f position = Detail::CalculateBoneTranslate(currBone, translate, pSceneDatabase);

//const cd::Vec3f position = details::CalculateBoneTranslate(currBone, translate, pSceneDatabase);

uint16_t parentID = currBone.GetParentID().Data();
uint16_t parentID = bone.GetID().Data();
uint16_t currBoneID = currBone.GetID().Data();
std::memcpy(&currentDataPtr[vertexOffset], translate.Begin(), posDataSize);
vertexOffset += posDataSize;
Expand All @@ -84,6 +93,9 @@ void TraverseBone(const cd::Bone& bone, const cd::SceneDatabase* pSceneDatabase,
const cd::Matrix4x4& boneMatrix = currBone.GetOffset();
skinmeshComponent.SetBoneMatrix(currBoneID, boneMatrix);

const cd::Transform& boneTransform = currBone.GetTransform();
skinmeshComponent.SetBoneChangeMatrix(currBoneID, boneTransform.GetMatrix());

TraverseBone(currBone, pSceneDatabase, skinmeshComponent, currentDataPtr, currentIndexPtr, vertexOffset, indexOffset);
}
}
Expand Down Expand Up @@ -464,9 +476,12 @@ void ECWorldConsumer::AddSkeleton(engine::Entity entity, const cd::SceneDatabase
currentVertexOffset += Detail::indexDataSize;

const cd::Matrix4x4& boneMatrix = firstBone.GetOffset();
const cd::Transform& boneTransform = firstBone.GetTransform();
skinmeshComponent.SetBoneMatrix(BoneID, boneMatrix);
skinmeshComponent.SetBoneChangeMatrix(BoneID, boneTransform.GetMatrix());

Detail::TraverseBone(firstBone, pSceneDatabase, skinmeshComponent, vertexBuffer.data(), indexBuffer.data(), currentVertexOffset, currentIndexOffset);
//Detail::CalculateTranslate(firstBone, pSceneDatabase, skinmeshComponent);
bgfx::VertexLayout vertexLayout;
engine::VertexLayoutUtility::CreateVertexLayout(vertexLayout, vertexFormat.GetVertexLayout());
uint16_t boneVBH = bgfx::createVertexBuffer(bgfx::makeRef(vertexBuffer.data(), static_cast<uint32_t>(vertexBuffer.size())), vertexLayout).idx;
Expand Down
12 changes: 7 additions & 5 deletions Engine/Source/Editor/UILayers/ImGuizmoView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ void ImGuizmoView::Update()
pTransformComponent->GetTransform().SetTranslation(worldMatrix.GetTranslation());
pTransformComponent->Dirty();
}

if (pSkinMeshComponent)
{
pSkinMeshComponent->SetBoneChangeMatrix(selectedBoneID.Data(), deltaMatrix);
pSkinMeshComponent->SetBoneMatrix(selectedBoneID.Data(), worldMatrix);
}

}

if (ImGuizmo::OPERATION::ROTATE & operation)
Expand All @@ -89,11 +96,6 @@ void ImGuizmoView::Update()
}

pTransformComponent->Build();
if (pSkinMeshComponent)
{
//pSkinMeshComponent->SetBoneMatrix(selectedBoneID.Data(), worldMatrix);
pSkinMeshComponent->SetBoneChangeMatrix(selectedBoneID.Data(), deltaMatrix);
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ namespace engine
namespace details
{

void CalculateBoneTranslate(const cd::Bone& bone, const cd::Matrix4x4& changeMatrix, const cd::SceneDatabase* pSceneDatabase, engine::SkinMeshComponent* pSkinMeshComponent)
void CalculateChangeTranslate(const cd::Bone& bone, const cd::Matrix4x4& changeMatrix, const cd::SceneDatabase* pSceneDatabase, engine::SkinMeshComponent* pSkinMeshComponent)
{
const uint32_t parentBoneID = bone.GetParentID().Data();
for (auto& boneChild : bone.GetChildIDs())
{
pSkinMeshComponent->SetBoneChangeMatrix(boneChild.Data(), changeMatrix);
CalculateBoneTranslate(pSceneDatabase->GetBone(boneChild.Data()), changeMatrix, pSceneDatabase, pSkinMeshComponent);
CalculateChangeTranslate(pSceneDatabase->GetBone(boneChild.Data()), changeMatrix, pSceneDatabase, pSkinMeshComponent);
}
}

void CalculateTranslate(const cd::Bone& bone, const cd::SceneDatabase* pSceneDatabase, engine::SkinMeshComponent* pSkinMeshComponent)
{
for (cd::BoneID boneID : bone.GetChildIDs())
{
const cd::Bone& childBone = pSceneDatabase->GetBone(boneID.Data());
cd::Matrix4x4 matrix = pSkinMeshComponent->GetBoneMatrix(boneID.Data()).Inverse() * pSkinMeshComponent->GetBoneChangeMatrix(boneID.Data());
pSkinMeshComponent->SetBoneChangeMatrix(childBone.GetID().Data(), matrix);
CalculateTranslate(childBone, pSceneDatabase, pSkinMeshComponent);
}
}

Expand Down Expand Up @@ -52,9 +63,10 @@ void SkeletonRenderer::Render(float delataTime)
const uint32_t changeBoneIndex = pSkinMeshComponent->GetChangeBoneIndex();
const cd::Bone& bone = pSceneDatabase->GetBone(changeBoneIndex);
const cd::Matrix4x4& changeBoneMatrix = pSkinMeshComponent->GetBoneChangeMatrix(changeBoneIndex);
details::CalculateBoneTranslate(bone, changeBoneMatrix, pSceneDatabase, pSkinMeshComponent);
pSkinMeshComponent->ResetChangeBoneIndex();
//details::CalculateChangeTranslate(bone, changeBoneMatrix, pSceneDatabase, pSkinMeshComponent);
//pSkinMeshComponent->ResetChangeBoneIndex();
}
//details::CalculateTranslate(pSceneDatabase->GetBone(0), pSceneDatabase, pSkinMeshComponent);
bgfx::setUniform(bgfx::UniformHandle{ pSkinMeshComponent->GetBoneMatrixsUniform() }, pSkinMeshComponent->GetBoneChangeMatrices().data(), static_cast<uint16_t>(pSkinMeshComponent->GetBoneChangeMatrices().size()));
bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pSkinMeshComponent->GetBoneVBH() });
bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pSkinMeshComponent->GetBoneIBH() });
Expand Down

0 comments on commit 1a5f2d7

Please sign in to comment.