diff --git a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp index e7e54ef9..7ea7f790 100644 --- a/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp +++ b/Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp @@ -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(¤tDataPtr[vertexOffset], translate.Begin(), posDataSize); vertexOffset += posDataSize; @@ -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); } } @@ -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(vertexBuffer.size())), vertexLayout).idx; diff --git a/Engine/Source/Editor/UILayers/ImGuizmoView.cpp b/Engine/Source/Editor/UILayers/ImGuizmoView.cpp index 07a23e46..ed7de1ad 100644 --- a/Engine/Source/Editor/UILayers/ImGuizmoView.cpp +++ b/Engine/Source/Editor/UILayers/ImGuizmoView.cpp @@ -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) @@ -89,11 +96,6 @@ void ImGuizmoView::Update() } pTransformComponent->Build(); - if (pSkinMeshComponent) - { - //pSkinMeshComponent->SetBoneMatrix(selectedBoneID.Data(), worldMatrix); - pSkinMeshComponent->SetBoneChangeMatrix(selectedBoneID.Data(), deltaMatrix); - } } } diff --git a/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp b/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp index 2d5f4f1d..a794c1a1 100644 --- a/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp +++ b/Engine/Source/Runtime/Rendering/SkeletonRenderer.cpp @@ -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); } } @@ -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(pSkinMeshComponent->GetBoneChangeMatrices().size())); bgfx::setVertexBuffer(0, bgfx::VertexBufferHandle{ pSkinMeshComponent->GetBoneVBH() }); bgfx::setIndexBuffer(bgfx::IndexBufferHandle{ pSkinMeshComponent->GetBoneIBH() });