diff --git a/Jusin154_Team3D/Client/Private/Broom.cpp b/Jusin154_Team3D/Client/Private/Broom.cpp index c33467dbe..a0d336b21 100644 --- a/Jusin154_Team3D/Client/Private/Broom.cpp +++ b/Jusin154_Team3D/Client/Private/Broom.cpp @@ -38,9 +38,19 @@ void CBroom::Priority_Update(_float fTimeDelta) void CBroom::Update(_float fTimeDelta) { - m_pModelCom->Play_Animation(fTimeDelta, m_pTransformCom); + m_pModelCom->ComputeAnimation(); - //m_pModelCom->ComputeSkinning(); + static _uint m_iIndex = 0; + + if (m_pGameInstance->Key_Down(DIK_1)) + m_iIndex++; + if (m_pGameInstance->Key_Down(DIK_2)) + m_iIndex--; + + m_pModelCom->Set_AnimationIndex(m_iIndex); + + + m_pModelCom->Play_Animation(fTimeDelta, m_pTransformCom); } void CBroom::Late_Update(_float fTimeDelta) @@ -61,6 +71,9 @@ HRESULT CBroom::Render() for (_uint i = 0; i < iNumMeshes; i++) { + if (FAILED(m_pModelCom->Bind_BoneMatrices(i, m_pShaderCom, "g_BoneMatrices"))) { + return E_FAIL; + } if (FAILED(m_pModelCom->Bind_Material(i, m_pShaderCom))) { return E_FAIL; @@ -69,9 +82,6 @@ HRESULT CBroom::Render() return E_FAIL; } - //if (FAILED(m_pModelCom->Bind_CS_Output(5, i))) - // return E_FAIL; - if (FAILED(m_pModelCom->Render(i))) { return E_FAIL; diff --git a/Jusin154_Team3D/Client/Private/Level_GamePlay.cpp b/Jusin154_Team3D/Client/Private/Level_GamePlay.cpp index a3201bcad..0b170fda7 100644 --- a/Jusin154_Team3D/Client/Private/Level_GamePlay.cpp +++ b/Jusin154_Team3D/Client/Private/Level_GamePlay.cpp @@ -140,8 +140,8 @@ HRESULT CLevel_GamePlay::Ready_Layer_Player(const _wstring& strLayerTag) if (FAILED(m_pGameInstance->Add_GameObject_ToLayer(g_iStaticLevel, NEXT_LEVEL, strLayerTag))) return E_FAIL; - if (FAILED(m_pGameInstance->Add_GameObject_ToLayer(g_iStaticLevel, NEXT_LEVEL, strLayerTag))) - return E_FAIL; + /*if (FAILED(m_pGameInstance->Add_GameObject_ToLayer(g_iStaticLevel, NEXT_LEVEL, strLayerTag))) + return E_FAIL;*/ return S_OK; } diff --git a/Jusin154_Team3D/Client/Private/Player.cpp b/Jusin154_Team3D/Client/Private/Player.cpp index 5723d6a1e..9e7779f58 100644 --- a/Jusin154_Team3D/Client/Private/Player.cpp +++ b/Jusin154_Team3D/Client/Private/Player.cpp @@ -71,13 +71,14 @@ HRESULT CPlayer::Initialize(void* pArg) void CPlayer::Priority_Update(_float fTimeDelta) { m_pTransformCom->RewindMomentum(); - m_pTransformCom->Set_State(STATE::POSITION, m_pCharacter_Controller->Get_Position()); + __super::Priority_Update(fTimeDelta); } void CPlayer::Update(_float fTimeDelta) { - //m_pModelCom->ComputeSkinning(); + m_pModelCom->ComputeAnimation(); + IsSprint(); IsWalk(); @@ -87,6 +88,8 @@ void CPlayer::Update(_float fTimeDelta) m_pModelCom->Play_Animation(fTimeDelta, m_pTransformCom); + + __super::Update(fTimeDelta); Describe_Entity(); m_pCharacter_Controller->Move(fTimeDelta); @@ -94,6 +97,9 @@ void CPlayer::Update(_float fTimeDelta) void CPlayer::Late_Update(_float fTimeDelta) { + + m_pTransformCom->Set_State(STATE::POSITION, m_pCharacter_Controller->Get_Position()); + m_pGameInstance->Add_RenderGroup(RENDER::BLEND, this); __super::Late_Update(fTimeDelta); @@ -120,9 +126,6 @@ HRESULT CPlayer::Render() return E_FAIL; } - //if (FAILED(m_pModelCom->Bind_CS_Output(5, i))) - // return E_FAIL; - if (FAILED(m_pModelCom->Render(i))) { return E_FAIL; } diff --git a/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl b/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl index 31276135c..645fd6d0c 100644 --- a/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl +++ b/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl @@ -1,64 +1,151 @@ float PI = 3.141592; -struct Mesh +struct KeyFrame { - float3 vPosition; - float3 vNormal; - float3 vTangent; - float3 vBinormal; - float2 vTexcoord; - - uint4 vBlendIndex; - float4 vBlendWeight; + float3 vScale; + float4 vRotation; + float3 vTranslation; + float fTrackPosition; }; -struct SkinngMesh +struct Channel { - float3 vPosition; - float3 vNormal; - float3 vTangent; - float3 vBinormal; - float2 vTexcoord; - + uint StartIndex; + uint KeyCount; + uint BoneIndex; }; -StructuredBuffer g_MeshBufferInput : register(t0); -RWStructuredBuffer gSkinngMeshOutput : register(u0); - -cbuffer g_ConstantBuffer : register(b0) +struct LocalPos { - row_major float4x4 BoneMatrix[512]; + float3 Scale; + float pad0; + + float4 Rotation; + + float3 Translation; + float pad1; }; -//[numthreads(256, 1, 1)] -[numthreads(256, 1, 1)] -void CS_MAIN(uint3 DispatchThreadID : SV_DispatchThreadID) + +StructuredBuffer g_KeyFrameBuffer : register(t0); +StructuredBuffer g_ChannelBuffer : register(t1); + +RWStructuredBuffer g_LocalPosOutput : register(u0); + + +cbuffer AnimCB : register(b0) { - uint iIndex = DispatchThreadID.x; + float CurrentTime; + float Duration; + float Speed; + int BoneCount; + + row_major float4x4 PreTransformMatrix; +}; - Mesh mesh = g_MeshBufferInput[iIndex]; +float4 quatSlerp(float4 q1, float4 q2, float t) +{ + float cosTheta = dot(q1, q2); - float fWeightW = 1.f - - (mesh.vBlendWeight.x + mesh.vBlendWeight.y + mesh.vBlendWeight.z); + if (cosTheta < 0.0f) + { + q2 = -q2; + cosTheta = -cosTheta; + } - matrix SkinMatrix = - BoneMatrix[mesh.vBlendIndex.x] * mesh.vBlendWeight.x + - BoneMatrix[mesh.vBlendIndex.y] * mesh.vBlendWeight.y + - BoneMatrix[mesh.vBlendIndex.z] * mesh.vBlendWeight.z + - BoneMatrix[mesh.vBlendIndex.w] * fWeightW; + if (cosTheta > 0.9995f) + return normalize(lerp(q1, q2, t)); - float4 vPosition = mul(float4(mesh.vPosition, 1), SkinMatrix); - float4 vNormal = mul(float4(mesh.vNormal, 0), SkinMatrix); - float4 vBinormal = mul(float4(mesh.vBinormal, 0), SkinMatrix); - float4 vTangent = mul(float4(mesh.vTangent, 0), SkinMatrix); + float theta = acos(cosTheta); + float sinTheta = sin(theta); + float w1 = sin((1.0f - t) * theta) / sinTheta; + float w2 = sin(t * theta) / sinTheta; - SkinngMesh outv; - outv.vPosition = vPosition.xyz; - outv.vNormal = vNormal.xyz; - outv.vTangent = vTangent.xyz; - outv.vBinormal = vBinormal.xyz; - outv.vTexcoord = mesh.vTexcoord; - - gSkinngMeshOutput[iIndex] = outv; + return q1 * w1 + q2 * w2; +} + + +[numthreads(256, 1, 1)] +void CS_LocalSRT(uint3 DTID : SV_DispatchThreadID) +{ + uint boneIndex = DTID.x; + if (boneIndex >= (uint) BoneCount) + return; + + float time = CurrentTime; + if (Duration > 0) + time = min(time, Duration); + + Channel channel = g_ChannelBuffer[boneIndex]; + + LocalPos result; + + if (channel.KeyCount == 0) + { + result.Scale = float3(1, 1, 1); + result.Rotation = float4(0, 0, 0, 1); + result.Translation = float3(0, 0, 0); + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + + uint firstIndex = channel.StartIndex; + uint lastIndex = channel.StartIndex + channel.KeyCount - 1; + + KeyFrame keyframe1 = g_KeyFrameBuffer[firstIndex]; + KeyFrame keyframe2 = g_KeyFrameBuffer[lastIndex]; + + if (time <= keyframe1.fTrackPosition) + { + result.Scale = keyframe1.vScale; + result.Rotation = keyframe1.vRotation; + result.Translation = keyframe1.vTranslation; + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + + if (time >= keyframe2.fTrackPosition) + { + result.Scale = keyframe2.vScale; + result.Rotation = keyframe2.vRotation; + result.Translation = keyframe2.vTranslation; + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + + for (uint i = 0; i < channel.KeyCount - 1; ++i) + { + uint A = channel.StartIndex + i; + uint B = channel.StartIndex + i + 1; + + KeyFrame ka = g_KeyFrameBuffer[A]; + KeyFrame kb = g_KeyFrameBuffer[B]; + + if (time >= ka.fTrackPosition && time <= kb.fTrackPosition) + { + float t = (time - ka.fTrackPosition) / (kb.fTrackPosition - ka.fTrackPosition); + + result.Scale = lerp(ka.vScale, kb.vScale, t); + result.Rotation = quatSlerp(ka.vRotation, kb.vRotation, t); + result.Translation = lerp(ka.vTranslation, kb.vTranslation, t); + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + } + + result.Scale = keyframe2.vScale; + result.Rotation = keyframe2.vRotation; + result.Translation = keyframe2.vTranslation; + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; } diff --git a/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_VtxAnimMesh.hlsl b/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_VtxAnimMesh.hlsl index 06f94131b..5b21faa0f 100644 --- a/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_VtxAnimMesh.hlsl +++ b/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_VtxAnimMesh.hlsl @@ -1,7 +1,5 @@ -#include "Engine_Shader_Defines.hlsli" - - +#include "Engine_Shader_Defines.hlsli" matrix g_WorldMatrix, g_ViewMatrix, g_ProjMatrix; @@ -16,11 +14,11 @@ float3 g_vRimColor; float4 g_TestColor; vector g_vCamPosition; -Texture2D g_DiffuseTexture : register(t0); -Texture2D g_NormalTexture : register(t1); -Texture2D g_DAOTexture : register(t2); -Texture2D g_THVTexture : register(t3); -Texture2D g_SurfaceParamsTexture : register(t4); +Texture2D g_DiffuseTexture; +Texture2D g_NormalTexture; +Texture2D g_DAOTexture; +Texture2D g_THVTexture; +Texture2D g_SurfaceParamsTexture; float3 g_RootColor; @@ -57,23 +55,26 @@ struct VS_OUT float4 vWorldPos : TEXCOORD1; float4 vProjPos : TEXCOORD2; }; + VS_OUT VS_MAIN(VS_IN In) { VS_OUT Out; float fWeightW = 1.f - (In.vBlendWeight.x + In.vBlendWeight.y + In.vBlendWeight.z); + matrix BoneMatrix = - mul(g_BoneMatrices[In.vBlendIndex.x], In.vBlendWeight.x) + - mul(g_BoneMatrices[In.vBlendIndex.y], In.vBlendWeight.y) + - mul(g_BoneMatrices[In.vBlendIndex.z], In.vBlendWeight.z) + - mul(g_BoneMatrices[In.vBlendIndex.w], fWeightW); + mul(g_BoneMatrices[In.vBlendIndex.x], In.vBlendWeight.x) + + mul(g_BoneMatrices[In.vBlendIndex.y], In.vBlendWeight.y) + + mul(g_BoneMatrices[In.vBlendIndex.z], In.vBlendWeight.z) + + mul(g_BoneMatrices[In.vBlendIndex.w], fWeightW); + vector vPosition = mul(vector(In.vPosition, 1.f), BoneMatrix); vector vNormal = mul(vector(In.vNormal, 0.f), BoneMatrix); vector vBinormal = mul(vector(In.vBinormal, 0.f), BoneMatrix); vector vTangent = mul(vector(In.vTangent, 0.f), BoneMatrix); + matrix matWV, matWVP; - matWV = mul(g_WorldMatrix, g_ViewMatrix); matWVP = mul(matWV, g_ProjMatrix); @@ -82,7 +83,6 @@ VS_OUT VS_MAIN(VS_IN In) Out.vBinormal = normalize(mul(vBinormal, g_WorldMatrix)).xyz; Out.vTangent = normalize(mul(vTangent, g_WorldMatrix)).xyz; Out.vTexcoord = In.vTexcoord; - //In.vTexcoord; Out.vWorldPos = mul(vPosition, g_WorldMatrix); Out.vProjPos = Out.vPosition; return Out; @@ -106,10 +106,10 @@ VS_OUT_CAPTUREDMODEL VS_CAPTUREDMODEL(VS_IN In) float fWeightW = 1.f - (In.vBlendWeight.x + In.vBlendWeight.y + In.vBlendWeight.z); - matrix BoneMatrix = - mul(g_BoneMatrices[In.vBlendIndex.x], In.vBlendWeight.x ) + - mul(g_BoneMatrices[In.vBlendIndex.y], In.vBlendWeight.y ) + - mul(g_BoneMatrices[In.vBlendIndex.z], In.vBlendWeight.z ) + + matrix BoneMatrix = + mul(g_BoneMatrices[In.vBlendIndex.x], In.vBlendWeight.x) + + mul(g_BoneMatrices[In.vBlendIndex.y], In.vBlendWeight.y) + + mul(g_BoneMatrices[In.vBlendIndex.z], In.vBlendWeight.z) + mul(g_BoneMatrices[In.vBlendIndex.w], fWeightW); vector vPosition = mul(vector(In.vPosition, 1.f), BoneMatrix); @@ -201,10 +201,13 @@ struct PS_OUT PS_OUT PS_MAIN(PS_IN In) { PS_OUT Out; - + vector vMtrlDiffuse = g_DiffuseTexture.Sample(DefaultSampler, In.vTexcoord); vector vSurface = g_SurfaceParamsTexture.Sample(DefaultSampler, In.vTexcoord); - if (vMtrlDiffuse.a < 0.2f) { discard; } + if (vMtrlDiffuse.a < 0.2f) + { + discard; + } float3 vNormalDecoded = DecodeNormalFromRG(g_NormalTexture, DefaultSampler, In.vTexcoord); float3x3 WorldMatrix = float3x3(In.vTangent, In.vBinormal * -1.f, In.vNormal); diff --git a/Jusin154_Team3D/Editor/Private/Broom.cpp b/Jusin154_Team3D/Editor/Private/Broom.cpp index d31202546..76e8578f3 100644 --- a/Jusin154_Team3D/Editor/Private/Broom.cpp +++ b/Jusin154_Team3D/Editor/Private/Broom.cpp @@ -66,9 +66,6 @@ HRESULT CBroom::Render() return E_FAIL; } - if (FAILED(m_pModelCom->Bind_CS_Output(5, i))) - return E_FAIL; - if (FAILED(m_pModelCom->Render(i))) { return E_FAIL; } diff --git a/Jusin154_Team3D/Editor/Private/DummyObject.cpp b/Jusin154_Team3D/Editor/Private/DummyObject.cpp index 006d8b704..72c59ab06 100644 --- a/Jusin154_Team3D/Editor/Private/DummyObject.cpp +++ b/Jusin154_Team3D/Editor/Private/DummyObject.cpp @@ -44,7 +44,7 @@ void CDummyObject::Priority_Update(_float fTimeDelta) void CDummyObject::Update(_float fTimeDelta) { - m_pModelCom->ComputeSkinning(); + m_pModelCom->ComputeAnimation(); m_pModelCom->Play_Animation(fTimeDelta,m_pTransformCom); @@ -77,9 +77,6 @@ HRESULT CDummyObject::Render() if (FAILED(m_pShaderCom->Begin(ENUM_CLASS(SHADER_PASS_ANIM::DEFAULT)))) { return E_FAIL; } - - if (FAILED(m_pModelCom->Bind_CS_Output(5, i))) - return E_FAIL; if (FAILED(m_pModelCom->Render(i))) { return E_FAIL; diff --git a/Jusin154_Team3D/Editor/Private/Goblin.cpp b/Jusin154_Team3D/Editor/Private/Goblin.cpp index 5756bd09d..a520dae57 100644 --- a/Jusin154_Team3D/Editor/Private/Goblin.cpp +++ b/Jusin154_Team3D/Editor/Private/Goblin.cpp @@ -41,7 +41,7 @@ void CGoblin::Update(_float fTimeDelta) { m_pModelCom->Play_Animation(fTimeDelta, m_pTransformCom); - //m_pModelCom->ComputeSkinning(); + m_pModelCom->ComputeAnimation(); } void CGoblin::Late_Update(_float fTimeDelta) @@ -71,9 +71,6 @@ HRESULT CGoblin::Render() return E_FAIL; } - //if (FAILED(m_pModelCom->Bind_CS_Output(5, 0))) - // return E_FAIL; - if (FAILED(m_pModelCom->Render(i))) { return E_FAIL; } diff --git a/Jusin154_Team3D/Editor/Private/Level_ObjectViewer.cpp b/Jusin154_Team3D/Editor/Private/Level_ObjectViewer.cpp index 42781e230..2d1795709 100644 --- a/Jusin154_Team3D/Editor/Private/Level_ObjectViewer.cpp +++ b/Jusin154_Team3D/Editor/Private/Level_ObjectViewer.cpp @@ -30,10 +30,10 @@ HRESULT CLevel_ObjectViewer::Initialize() return E_FAIL; } - /*if (FAILED(Ready_Layer_Camera(TEXT("Layer_Camera")))) + if (FAILED(Ready_Layer_Camera(TEXT("Layer_Camera")))) { return E_FAIL; - }*/ + } if (FAILED(Ready_Layer_Dummy(TEXT("Layer_Dummy")))) { @@ -58,7 +58,7 @@ void CLevel_ObjectViewer::Update(_float fTimeDelta) Find_Anim(); - m_Test->Describe_Entity(); + //m_Test->Describe_Entity(); } HRESULT CLevel_ObjectViewer::Render() @@ -702,8 +702,8 @@ HRESULT CLevel_ObjectViewer::Ready_Layer_Dummy(const _wstring& strLayerTag) - if (FAILED(m_pGameInstance->Add_GameObject_ToLayer(g_iStaticLevel, NEXT_LEVEL, strLayerTag))) - return E_FAIL; + //if (FAILED(m_pGameInstance->Add_GameObject_ToLayer(g_iStaticLevel, NEXT_LEVEL, strLayerTag))) + // return E_FAIL; /*if (FAILED(m_pGameInstance->Add_GameObject_ToLayer(g_iStaticLevel, NEXT_LEVEL, strLayerTag))) return E_FAIL;*/ diff --git a/Jusin154_Team3D/Editor/Private/Player.cpp b/Jusin154_Team3D/Editor/Private/Player.cpp index eb4cb6166..bfd2c7aec 100644 --- a/Jusin154_Team3D/Editor/Private/Player.cpp +++ b/Jusin154_Team3D/Editor/Private/Player.cpp @@ -69,10 +69,7 @@ void CPlayer::Priority_Update(_float fTimeDelta) void CPlayer::Update(_float fTimeDelta) { - //m_pModelCom->ComputeSkinning(); - - IsSprint(); - IsWalk(); + m_pModelCom->ComputeAnimation(); m_pFSM->Update_State(fTimeDelta); @@ -109,8 +106,6 @@ HRESULT CPlayer::Render() return E_FAIL; } - //if (FAILED(m_pModelCom->Bind_CS_Output(5, i))) - // return E_FAIL; if (FAILED(m_pModelCom->Render(i))) { return E_FAIL; diff --git a/Jusin154_Team3D/Editor/Private/RootModelPart.cpp b/Jusin154_Team3D/Editor/Private/RootModelPart.cpp index a9fda49f4..61acedece 100644 --- a/Jusin154_Team3D/Editor/Private/RootModelPart.cpp +++ b/Jusin154_Team3D/Editor/Private/RootModelPart.cpp @@ -71,6 +71,7 @@ void CRootModelPart::Update_Anim(_float fTimeDelta) { pModel->Set_PlayAnim(m_pMainModel->Get_PlayAnim()); pModel->Set_AnimSpeed(m_pMainModel->Get_AnimSpeed()); + pModel->ComputeAnimation(); pModel->Play_Animation(fTimeDelta,m_pTransformCom); } } @@ -98,7 +99,8 @@ void CRootModelPart::Change_Model(_uint iIndex) CModel* pModel = m_ModelParts[iIndex]->Get_Component(); pModel->Set_AnimationIndex(m_iAnimIndex); - pModel->Set_CurrentTrackPosition(m_fTrackPosition); + pModel->Set_CurrentTrackPosition(m_fTrackPosition); + pModel->ComputeAnimation(); pModel->Play_Animation(0.f, m_pTransformCom); if (iIndex == ENUM_CLASS(PARTSTYPE::BODY)) @@ -112,6 +114,7 @@ void CRootModelPart::Set_Animation(_uint iAnimIndex) { m_pMainModel->Set_AnimationIndex(iAnimIndex); m_pMainModel->Set_CurrentTrackPosition(0.f); + m_pMainModel->ComputeAnimation(); m_pMainModel->Play_Animation(0.f, m_pTransformCom); for (auto& PartObject : m_ModelParts) { @@ -120,6 +123,7 @@ void CRootModelPart::Set_Animation(_uint iAnimIndex) { pModel->Set_AnimationIndex(m_pMainModel->Get_AnimIndex()); pModel->Set_CurrentTrackPosition(m_pMainModel->Get_CurrentTrackPosition()); + pModel->ComputeAnimation(); pModel->Play_Animation(0.f, m_pTransformCom); } } diff --git a/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl b/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl index 31276135c..645fd6d0c 100644 --- a/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl +++ b/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl @@ -1,64 +1,151 @@ float PI = 3.141592; -struct Mesh +struct KeyFrame { - float3 vPosition; - float3 vNormal; - float3 vTangent; - float3 vBinormal; - float2 vTexcoord; - - uint4 vBlendIndex; - float4 vBlendWeight; + float3 vScale; + float4 vRotation; + float3 vTranslation; + float fTrackPosition; }; -struct SkinngMesh +struct Channel { - float3 vPosition; - float3 vNormal; - float3 vTangent; - float3 vBinormal; - float2 vTexcoord; - + uint StartIndex; + uint KeyCount; + uint BoneIndex; }; -StructuredBuffer g_MeshBufferInput : register(t0); -RWStructuredBuffer gSkinngMeshOutput : register(u0); - -cbuffer g_ConstantBuffer : register(b0) +struct LocalPos { - row_major float4x4 BoneMatrix[512]; + float3 Scale; + float pad0; + + float4 Rotation; + + float3 Translation; + float pad1; }; -//[numthreads(256, 1, 1)] -[numthreads(256, 1, 1)] -void CS_MAIN(uint3 DispatchThreadID : SV_DispatchThreadID) + +StructuredBuffer g_KeyFrameBuffer : register(t0); +StructuredBuffer g_ChannelBuffer : register(t1); + +RWStructuredBuffer g_LocalPosOutput : register(u0); + + +cbuffer AnimCB : register(b0) { - uint iIndex = DispatchThreadID.x; + float CurrentTime; + float Duration; + float Speed; + int BoneCount; + + row_major float4x4 PreTransformMatrix; +}; - Mesh mesh = g_MeshBufferInput[iIndex]; +float4 quatSlerp(float4 q1, float4 q2, float t) +{ + float cosTheta = dot(q1, q2); - float fWeightW = 1.f - - (mesh.vBlendWeight.x + mesh.vBlendWeight.y + mesh.vBlendWeight.z); + if (cosTheta < 0.0f) + { + q2 = -q2; + cosTheta = -cosTheta; + } - matrix SkinMatrix = - BoneMatrix[mesh.vBlendIndex.x] * mesh.vBlendWeight.x + - BoneMatrix[mesh.vBlendIndex.y] * mesh.vBlendWeight.y + - BoneMatrix[mesh.vBlendIndex.z] * mesh.vBlendWeight.z + - BoneMatrix[mesh.vBlendIndex.w] * fWeightW; + if (cosTheta > 0.9995f) + return normalize(lerp(q1, q2, t)); - float4 vPosition = mul(float4(mesh.vPosition, 1), SkinMatrix); - float4 vNormal = mul(float4(mesh.vNormal, 0), SkinMatrix); - float4 vBinormal = mul(float4(mesh.vBinormal, 0), SkinMatrix); - float4 vTangent = mul(float4(mesh.vTangent, 0), SkinMatrix); + float theta = acos(cosTheta); + float sinTheta = sin(theta); + float w1 = sin((1.0f - t) * theta) / sinTheta; + float w2 = sin(t * theta) / sinTheta; - SkinngMesh outv; - outv.vPosition = vPosition.xyz; - outv.vNormal = vNormal.xyz; - outv.vTangent = vTangent.xyz; - outv.vBinormal = vBinormal.xyz; - outv.vTexcoord = mesh.vTexcoord; - - gSkinngMeshOutput[iIndex] = outv; + return q1 * w1 + q2 * w2; +} + + +[numthreads(256, 1, 1)] +void CS_LocalSRT(uint3 DTID : SV_DispatchThreadID) +{ + uint boneIndex = DTID.x; + if (boneIndex >= (uint) BoneCount) + return; + + float time = CurrentTime; + if (Duration > 0) + time = min(time, Duration); + + Channel channel = g_ChannelBuffer[boneIndex]; + + LocalPos result; + + if (channel.KeyCount == 0) + { + result.Scale = float3(1, 1, 1); + result.Rotation = float4(0, 0, 0, 1); + result.Translation = float3(0, 0, 0); + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + + uint firstIndex = channel.StartIndex; + uint lastIndex = channel.StartIndex + channel.KeyCount - 1; + + KeyFrame keyframe1 = g_KeyFrameBuffer[firstIndex]; + KeyFrame keyframe2 = g_KeyFrameBuffer[lastIndex]; + + if (time <= keyframe1.fTrackPosition) + { + result.Scale = keyframe1.vScale; + result.Rotation = keyframe1.vRotation; + result.Translation = keyframe1.vTranslation; + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + + if (time >= keyframe2.fTrackPosition) + { + result.Scale = keyframe2.vScale; + result.Rotation = keyframe2.vRotation; + result.Translation = keyframe2.vTranslation; + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + + for (uint i = 0; i < channel.KeyCount - 1; ++i) + { + uint A = channel.StartIndex + i; + uint B = channel.StartIndex + i + 1; + + KeyFrame ka = g_KeyFrameBuffer[A]; + KeyFrame kb = g_KeyFrameBuffer[B]; + + if (time >= ka.fTrackPosition && time <= kb.fTrackPosition) + { + float t = (time - ka.fTrackPosition) / (kb.fTrackPosition - ka.fTrackPosition); + + result.Scale = lerp(ka.vScale, kb.vScale, t); + result.Rotation = quatSlerp(ka.vRotation, kb.vRotation, t); + result.Translation = lerp(ka.vTranslation, kb.vTranslation, t); + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; + return; + } + } + + result.Scale = keyframe2.vScale; + result.Rotation = keyframe2.vRotation; + result.Translation = keyframe2.vTranslation; + result.pad0 = result.pad1 = 0.0f; + + g_LocalPosOutput[boneIndex] = result; } diff --git a/Jusin154_Team3D/Engine/Private/Animation.cpp b/Jusin154_Team3D/Engine/Private/Animation.cpp index 91a1376ef..27c4cac77 100644 --- a/Jusin154_Team3D/Engine/Private/Animation.cpp +++ b/Jusin154_Team3D/Engine/Private/Animation.cpp @@ -23,6 +23,7 @@ CAnimation::CAnimation(const CAnimation& rhs) , m_fTickPerSecond{ rhs.m_fTickPerSecond } , m_pSaveAnim{ rhs.m_pSaveAnim } , m_fAnimSpeed{rhs.m_fAnimSpeed } + ,m_iBoneCount{rhs.m_iBoneCount} { memcpy_s(m_TickPerSeconds, sizeof(_float) * 2, rhs.m_TickPerSeconds, sizeof(_float) * 2); @@ -32,7 +33,7 @@ CAnimation::CAnimation(const CAnimation& rhs) SAFE_ADDREF(pChannel); } } -_bool CAnimation::Update_TransformationMatrices(const vector& Bones, _bool bIsLoop, _float fTimeDelta,CTransform*pTransform) +_bool CAnimation::Update_TransformationMatrices(const vector& Bones, const LOCALPOS_DESC* pLocalPosArray, _bool bIsLoop, _float fTimeDelta,CTransform*pTransform) { //m_fCurrentTrackPosition += m_TickPerSeconds[m_bPause] * fTimeDelta; m_fCurrentTrackPosition += m_fTickPerSecond * fTimeDelta * m_fAnimSpeed; @@ -52,7 +53,7 @@ _bool CAnimation::Update_TransformationMatrices(const vector& Bone if (m_vBoneTransformationMatrix.size() > 0) m_vBoneTransformationMatrix.clear(); for (auto& pChannel : m_Channels) { - pChannel->Update_TransformationMatirx(Bones, m_fCurrentTrackPosition, &m_CurrentKeyFrameIndices[iIndex++],pTransform); + pChannel->Update_TransformationMatirx(Bones, pLocalPosArray, m_fCurrentTrackPosition, &m_CurrentKeyFrameIndices[iIndex++],pTransform); m_vBoneTransformationMatrix.push_back(pChannel->Get_BoneTransformationMatrix()); } @@ -184,9 +185,36 @@ void CAnimation::InterpAnim(CAnimation* pPreAnim, vector& Bones, float f } } +void CAnimation::CreateGPUData(ID3D11Device* pDevice) +{ + vector channels; + vector keyframes; + + channels.resize(m_iBoneCount); + + for (auto& pChannel : m_Channels) + { + CHANNEL_DESC desc = pChannel->Fill_GPU_ChannelDesc(); + desc.StartIndex = (_uint)keyframes.size(); + + pChannel->Fill_GPU_Keyframes(keyframes); + + channels[desc.BoneIndex] = desc; + } + + m_iChannelCount = (_uint)channels.size(); + m_iKeyframeCount = (_uint)keyframes.size(); + + CreateKeyFrameBuffer(pDevice, keyframes); + CreateChannelBuffer(pDevice, channels); + +} + #ifdef EDITOR_PROJECT + + HRESULT CAnimation::Initialize(const vector& Bones, const aiAnimation* pAIAnimation) { m_strName = pAIAnimation->mName.data; @@ -216,6 +244,8 @@ HRESULT CAnimation::Initialize(const vector& Bones, const aiAnimation* p if (FAILED(Combined_Initialize())) { return E_FAIL; } + + return S_OK; } HRESULT CAnimation::SaveAsBinary(HANDLE hFile, DWORD& dwByte) @@ -274,7 +304,7 @@ HRESULT CAnimation::Initialize(HANDLE hFile, DWORD& dwByte) return S_OK; } -HRESULT CAnimation::Initialize(const CModel* pModel, SaveAnimation* pSaveAnimation) +HRESULT CAnimation::Initialize(const vector& Bones, const CModel* pModel, SaveAnimation* pSaveAnimation) { m_pSaveAnim = pSaveAnimation; @@ -286,6 +316,8 @@ HRESULT CAnimation::Initialize(const CModel* pModel, SaveAnimation* pSaveAnimati m_CurrentKeyFrameIndices.resize(m_iNumChannels); + m_iBoneCount = (_uint)Bones.size(); + for (size_t i = 0; i < m_iNumChannels; i++) { CChannel* pChannel = CChannel::Create(pModel, &pSaveAnimation->Channels[i]); @@ -313,6 +345,64 @@ _uint CAnimation::Get_BoneIndex(const char* pChannelName) return _uint(); } +HRESULT CAnimation::CreateKeyFrameBuffer(ID3D11Device* pDevice,vector keyframe) +{ + D3D11_BUFFER_DESC desc{}; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.ByteWidth = (_int)sizeof(KEYFRAME_DESC) * m_iKeyframeCount; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + desc.StructureByteStride = (_int)sizeof(KEYFRAME_DESC); + desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + + D3D11_SUBRESOURCE_DATA init{}; + init.pSysMem = keyframe.data(); + + pDevice->CreateBuffer(&desc, &init, &m_pKeyFrameBuffer); + + + + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX; + srvDesc.Format = DXGI_FORMAT_UNKNOWN; + srvDesc.Buffer.FirstElement = 0; + srvDesc.Buffer.NumElements = m_iKeyframeCount; + + if (FAILED(pDevice->CreateShaderResourceView(m_pKeyFrameBuffer, &srvDesc, &m_pKeyFrameSrv))) + return E_FAIL; + + + return S_OK; +} + +HRESULT CAnimation::CreateChannelBuffer(ID3D11Device* pDevice, vector channel) +{ + D3D11_BUFFER_DESC desc{}; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.ByteWidth = (_int)sizeof(CHANNEL_DESC) * m_iChannelCount; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + desc.StructureByteStride = (_int)sizeof(CHANNEL_DESC); + desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + + D3D11_SUBRESOURCE_DATA init{}; + init.pSysMem = channel.data(); + + pDevice->CreateBuffer(&desc, &init, &m_pChannelBuffer); + + + D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; + srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX; + srvDesc.Format = DXGI_FORMAT_UNKNOWN; + srvDesc.Buffer.FirstElement = 0; + srvDesc.Buffer.NumElements = m_iChannelCount; + + if (FAILED(pDevice->CreateShaderResourceView(m_pChannelBuffer, &srvDesc, &m_pChannelSrv))) + return E_FAIL; + + return S_OK; +} + CAnimation* CAnimation::Create(HANDLE hFile, DWORD& dwByte) { CAnimation* pInstance = new CAnimation(); @@ -326,11 +416,11 @@ CAnimation* CAnimation::Create(HANDLE hFile, DWORD& dwByte) return pInstance; } -CAnimation* CAnimation::Create(const CModel* pModel, SaveAnimation* pSaveAnimation) +CAnimation* CAnimation::Create(const vector& Bones, const CModel* pModel, SaveAnimation* pSaveAnimation) { CAnimation* pInstance = new CAnimation(); - if (FAILED(pInstance->Initialize(pModel, pSaveAnimation))) + if (FAILED(pInstance->Initialize(Bones,pModel, pSaveAnimation))) { MSG_BOX("Failed to Created : CAnimation"); SAFE_RELEASE(pInstance); @@ -354,6 +444,18 @@ void CAnimation::Free() m_Channels.clear(); m_CurrentKeyFrameIndices.clear(); + + m_KeyFrameDesc.clear(); + m_ChannelDesc.clear(); + + if (m_pKeyFrameSrv&& m_pChannelSrv) + { + SAFE_RELEASE(m_pKeyFrameBuffer); + SAFE_RELEASE(m_pChannelBuffer); + SAFE_RELEASE(m_pKeyFrameSrv); + SAFE_RELEASE(m_pChannelSrv); + } + } ; void CAnimation::Describe_Entity() diff --git a/Jusin154_Team3D/Engine/Private/Channel.cpp b/Jusin154_Team3D/Engine/Private/Channel.cpp index b60d9c6a1..4b3a29790 100644 --- a/Jusin154_Team3D/Engine/Private/Channel.cpp +++ b/Jusin154_Team3D/Engine/Private/Channel.cpp @@ -60,6 +60,34 @@ HRESULT CChannel::Initialize(const vector& Bones, const aiNodeAnim* pAIC return S_OK; } +void CChannel::Fill_GPU_Keyframes(vector& outKeyframes) +{ + for (_uint i = 0; i < m_iNumKeyFrames; i++) + { + const KEYFRAME& src = m_KeyFrames[i]; + + KEYFRAME_DESC dst{}; + dst.vScale = src.vScale; + dst.vRotation = src.vRotation; + dst.vTranslation = src.vTranslation; + dst.fTrackPosition = src.fTrackPosition; + + outKeyframes.push_back(dst); + } +} + +CHANNEL_DESC CChannel::Fill_GPU_ChannelDesc() +{ + CHANNEL_DESC desc{}; + desc.StartIndex = 0; + desc.KeyCount = m_iNumKeyFrames; + desc.BoneIndex = m_iBoneIndex; + + return desc; +} + + + HRESULT CChannel::SaveAsBinary(HANDLE hFile, DWORD& dwByte) { string strName = m_szName; @@ -100,84 +128,104 @@ HRESULT CChannel::Initialize(const vector& Bones, _uint iIndex) } -void CChannel::Update_TransformationMatirx(const vector& Bones, _float fCurrentTrackPosition, _uint* pCurrentKeyFrameIndex, CTransform* pTransform) - +void CChannel::Update_TransformationMatirx(const vector& Bones, const LOCALPOS_DESC* pLocalPosArray, _float fCurrentTrackPosition, _uint* pCurrentKeyFrameIndex, CTransform* pTransform) { - if (0.f == fCurrentTrackPosition) { - *pCurrentKeyFrameIndex = 0; - } - KEYFRAME LastKeyFrame = m_KeyFrames.back(); - - _vector vScale = {}; - _vector vRotation = {}; - _vector vTranslation = {}; - - if (fCurrentTrackPosition >= LastKeyFrame.fTrackPosition) - { - vScale = XMLoadFloat3(&LastKeyFrame.vScale); - vRotation = XMLoadFloat4(&LastKeyFrame.vRotation); - vTranslation = XMVectorSetW(XMLoadFloat3(&LastKeyFrame.vTranslation), 1.f); - } - else { - while (fCurrentTrackPosition >= m_KeyFrames[*pCurrentKeyFrameIndex + 1].fTrackPosition) { - ++*pCurrentKeyFrameIndex; - } - - _float3 vSrcScale = {}, vDstScale = {}; - _float4 vSrcRotation = {}, vDstRotation = {}; - _float3 vSrcTranslation = {}, vDstTranslation = {}; - - vSrcScale = m_KeyFrames[*pCurrentKeyFrameIndex].vScale; - vDstScale = m_KeyFrames[*pCurrentKeyFrameIndex + 1].vScale; - - vSrcRotation = m_KeyFrames[*pCurrentKeyFrameIndex].vRotation; - vDstRotation = m_KeyFrames[*pCurrentKeyFrameIndex + 1].vRotation; - - vSrcTranslation = m_KeyFrames[*pCurrentKeyFrameIndex].vTranslation; - vDstTranslation = m_KeyFrames[*pCurrentKeyFrameIndex + 1].vTranslation; - - _float fRatio = (fCurrentTrackPosition - m_KeyFrames[*pCurrentKeyFrameIndex].fTrackPosition) / - (m_KeyFrames[*pCurrentKeyFrameIndex + 1].fTrackPosition - m_KeyFrames[*pCurrentKeyFrameIndex].fTrackPosition); - - vScale = XMVectorLerp(XMLoadFloat3(&vSrcScale), XMLoadFloat3(&vDstScale), fRatio); - vRotation = XMQuaternionSlerp(XMLoadFloat4(&vSrcRotation), XMLoadFloat4(&vDstRotation), fRatio); - vTranslation = XMVectorSetW(XMVectorLerp(XMLoadFloat3(&vSrcTranslation), XMLoadFloat3(&vDstTranslation), fRatio), 1.f); - } + //if (0.f == fCurrentTrackPosition) { + // *pCurrentKeyFrameIndex = 0; + //} + //KEYFRAME LastKeyFrame = m_KeyFrames.back(); + + //_vector vScale = {}; + //_vector vRotation = {}; + //_vector vTranslation = {}; + + //if (fCurrentTrackPosition >= LastKeyFrame.fTrackPosition) + //{ + // vScale = XMLoadFloat3(&LastKeyFrame.vScale); + // vRotation = XMLoadFloat4(&LastKeyFrame.vRotation); + // vTranslation = XMVectorSetW(XMLoadFloat3(&LastKeyFrame.vTranslation), 1.f); + //} + //else { + // while (fCurrentTrackPosition >= m_KeyFrames[*pCurrentKeyFrameIndex + 1].fTrackPosition) { + // ++*pCurrentKeyFrameIndex; + // } + + // _float3 vSrcScale = {}, vDstScale = {}; + // _float4 vSrcRotation = {}, vDstRotation = {}; + // _float3 vSrcTranslation = {}, vDstTranslation = {}; + + // vSrcScale = m_KeyFrames[*pCurrentKeyFrameIndex].vScale; + // vDstScale = m_KeyFrames[*pCurrentKeyFrameIndex + 1].vScale; + + // vSrcRotation = m_KeyFrames[*pCurrentKeyFrameIndex].vRotation; + // vDstRotation = m_KeyFrames[*pCurrentKeyFrameIndex + 1].vRotation; + + // vSrcTranslation = m_KeyFrames[*pCurrentKeyFrameIndex].vTranslation; + // vDstTranslation = m_KeyFrames[*pCurrentKeyFrameIndex + 1].vTranslation; + + // _float fRatio = (fCurrentTrackPosition - m_KeyFrames[*pCurrentKeyFrameIndex].fTrackPosition) / + // (m_KeyFrames[*pCurrentKeyFrameIndex + 1].fTrackPosition - m_KeyFrames[*pCurrentKeyFrameIndex].fTrackPosition); + + // vScale = XMVectorLerp(XMLoadFloat3(&vSrcScale), XMLoadFloat3(&vDstScale), fRatio); + // vRotation = XMQuaternionSlerp(XMLoadFloat4(&vSrcRotation), XMLoadFloat4(&vDstRotation), fRatio); + // vTranslation = XMVectorSetW(XMVectorLerp(XMLoadFloat3(&vSrcTranslation), XMLoadFloat3(&vDstTranslation), fRatio), 1.f); + //} + + + + const LOCALPOS_DESC& LocalPos = pLocalPosArray[m_iBoneIndex]; + + _vector vScale = XMLoadFloat3(&LocalPos.Scale); + _vector vRotation = XMLoadFloat4(&LocalPos.Rotation); + _vector vTranslation = XMVectorSet( + LocalPos.Translation.x, + LocalPos.Translation.y, + LocalPos.Translation.z, + 1.f); + if (Bones[m_iBoneIndex]->Compare_Name("Reference") && pTransform != nullptr) { _float3 vCurRootPos; XMStoreFloat3(&vCurRootPos, vTranslation); - _vector vDeltaLocal = XMLoadFloat3(&vCurRootPos) - XMLoadFloat3(&m_vPrevRootPos); - - _vector vDeltaAdjusted = {}; - XMMATRIX pre = XMLoadFloat4x4(&m_PreTransformMatrix); - vDeltaAdjusted = XMVector3TransformNormal(vDeltaLocal, pre); + + if (!m_bInitialRootPos) + { + m_bInitialRootPos = true; + vTranslation = XMVectorZero(); + } + else + { + _vector vDeltaLocal = XMLoadFloat3(&vCurRootPos) - XMLoadFloat3(&m_vPrevRootPos); - _vector vRight = pTransform->Get_State(STATE::RIGHT); - _vector vUp = pTransform->Get_State(STATE::UP); - _vector vLook = pTransform->Get_State(STATE::LOOK); + _vector vDeltaAdjusted = {}; - _float dx = XMVectorGetX(vDeltaAdjusted); - _float dy = XMVectorGetY(vDeltaAdjusted); - _float dz = XMVectorGetZ(vDeltaAdjusted); + vDeltaAdjusted = XMVector3TransformNormal(vDeltaLocal, pre); - _vector vDeltaWorld = vRight * dx + (-vUp * dz) + vLook * dy; + _vector vRight = pTransform->Get_State(STATE::RIGHT); + _vector vUp = pTransform->Get_State(STATE::UP); + _vector vLook = pTransform->Get_State(STATE::LOOK); - vDeltaWorld *= 0.01f; + _float dx = XMVectorGetX(vDeltaAdjusted); + _float dy = XMVectorGetY(vDeltaAdjusted); + _float dz = XMVectorGetZ(vDeltaAdjusted); - _vector vCurrentPos = pTransform->Get_State(STATE::POSITION); - _vector vTargetPos = vCurrentPos + vDeltaWorld; + _vector vDeltaWorld = vRight * dx + (-vUp * dz) + vLook * dy; - //pTransform->Set_State(STATE::POSITION, vTargetPos); - pTransform->AccumulateMomentum(vDeltaWorld); + vDeltaWorld *= 0.01f; - m_vPrevRootPos = vCurRootPos; - vTranslation = XMVectorZero(); + _vector vCurrentPos = pTransform->Get_State(STATE::POSITION); + _vector vTargetPos = vCurrentPos + vDeltaWorld; - ///////////////////////////////////// + pTransform->Set_State(STATE::POSITION, vTargetPos); + //pTransform->AccumulateMomentum(vDeltaWorld); + + m_vPrevRootPos = vCurRootPos; + vTranslation = XMVectorZero(); + } + /////////////////////////////////// _float4 curRotF4; XMStoreFloat4(&curRotF4, vRotation); @@ -301,6 +349,7 @@ HRESULT CChannel::Initialize(const CModel* pModel, SaveChannel* pSaveChannel) KeyFrame.vRotation = vRotation; KeyFrame.vTranslation = vTranslation; + m_KeyFrames.push_back(KeyFrame); } @@ -351,4 +400,6 @@ void CChannel::Free() __super::Free(); m_KeyFrames.clear(); + + m_KeyFrameDesc.clear(); } diff --git a/Jusin154_Team3D/Engine/Private/ComputeShader.cpp b/Jusin154_Team3D/Engine/Private/ComputeShader.cpp index 7d46182a5..eebd01e99 100644 --- a/Jusin154_Team3D/Engine/Private/ComputeShader.cpp +++ b/Jusin154_Team3D/Engine/Private/ComputeShader.cpp @@ -83,9 +83,6 @@ vector CComputeShader::Dispatch(_uint iSRVIndex, _uint if (SUCCEEDED(m_pContext->Map(m_pOutputStagingBuffer[i], 0, D3D11_MAP_READ, 0, &StagingSubResource))) { - - CInstance_Model::CS_PARTICLE_VALUE_DESC* pValueDesc = static_cast(StagingSubResource.pData); - StagingSubResources.push_back(StagingSubResource); m_pContext->Unmap(m_pOutputStagingBuffer[i], 0); @@ -124,6 +121,37 @@ void CComputeShader::Bind_OutPut_SRV(_uint iIndex, _uint iBufferIndex) } +ID3D11UnorderedAccessView* CComputeShader::GetOutputUAV(_uint iIndex) const +{ + return m_pOutputUAV[iIndex]; +} + +vector CComputeShader::ReadBackOutputs() +{ + std::vector StagingSubResources; + StagingSubResources.reserve(m_iNumOutputBuffer); + + for (_uint i = 0; i < m_iNumOutputBuffer; ++i) + { + if (m_pOutputStagingBuffer[i] == nullptr || m_pOutputBuffer[i] == nullptr) + continue; + + m_pContext->CopyResource(m_pOutputStagingBuffer[i], m_pOutputBuffer[i]); + + D3D11_MAPPED_SUBRESOURCE mapped = {}; + if (SUCCEEDED(m_pContext->Map(m_pOutputStagingBuffer[i], 0, + D3D11_MAP_READ, 0, &mapped))) + { + StagingSubResources.push_back(mapped); + + m_pContext->Unmap(m_pOutputStagingBuffer[i], 0); + } + } + + return StagingSubResources; + +} + void CComputeShader::Reset() { //다음 랜더링 패스에 연관되지 않도록 초기화 diff --git a/Jusin154_Team3D/Engine/Private/GameInstance.cpp b/Jusin154_Team3D/Engine/Private/GameInstance.cpp index 52fc982e6..e2387d636 100644 --- a/Jusin154_Team3D/Engine/Private/GameInstance.cpp +++ b/Jusin154_Team3D/Engine/Private/GameInstance.cpp @@ -126,11 +126,13 @@ void CGameInstance::Update_Engine(_float fTimeDelta) m_pObject_Manager->Update(fTimeDelta); + m_pPhysX_Manager->Update(fTimeDelta); + m_pObject_Manager->Late_Update(fTimeDelta); m_pLevel_Manager->Update(fTimeDelta); m_pObject_Manager->Clear_DeadObj(); - m_pPhysX_Manager->Update(fTimeDelta); + //m_pObstacle_Manager->Refresh_Region(); } diff --git a/Jusin154_Team3D/Engine/Private/LerpAnim.cpp b/Jusin154_Team3D/Engine/Private/LerpAnim.cpp index e4b4aa8c3..ed36a6c1d 100644 --- a/Jusin154_Team3D/Engine/Private/LerpAnim.cpp +++ b/Jusin154_Team3D/Engine/Private/LerpAnim.cpp @@ -1,4 +1,4 @@ -#include "pch.h" +#include "pch.h" #include "LerpAnim.h" #include "Channel.h" #include "Bone.h" @@ -17,13 +17,13 @@ _bool CLerpAnim::Update_TransformationMatrices(const vector& Bones, _flo return false; } - _uint iIndex = {}; + /*_uint iIndex = {}; for (auto& pChannel : m_Channels) { if (true == m_Movable[iIndex]) { pChannel->Update_TransformationMatirx(Bones, m_fCurrentTrackPosition, &m_CurrentKeyFrameIndices[iIndex]); } iIndex++; - } + }*/ return true; } @@ -50,9 +50,9 @@ HRESULT CLerpAnim::Initialize(_uint iNumBone, _float fTickPerSecond, const vecto } void CLerpAnim::Begin(const vector& Bones, vector& StartFrames, vector& EndFrames, _float fDuration, _uint iRootBoneIndex) { - { // Ãʱâȭ + { // 초기화 - //KEYFRAME kf = { // Ç׵îÇà·ÄÀÇ ŰÇÁ·¹ÀÓ + //KEYFRAME kf = { // 항등행렬의 키프레임 // { 1.f, 1.f, 1.f }, // { 0.f, 0.f, 0.f, 1.f }, // { 0.f, 0.f, 0.f}, @@ -61,19 +61,19 @@ void CLerpAnim::Begin(const vector& Bones, vector& Start m_fDuration = fDuration; m_fCurrentTrackPosition = 0.f; for (_uint i = 0; i < m_iNumChannels; ++i) { - m_Channels[i]->Set_Frame(1, m_InitialFrames[i]); // ³¡ÇÁ·¹ÀÓÀ» Ãʱâ °ªÀ¸·Î ä¿ì±â + m_Channels[i]->Set_Frame(1, m_InitialFrames[i]); // 끝프레임을 초기 값으로 채우기 } fill(m_CurrentKeyFrameIndices.begin(), m_CurrentKeyFrameIndices.end(), 0); fill(m_Movable.begin(), m_Movable.end(), false); } - { // ½ÇÁ¦ °ª ä¿ì±â - for (LERPDESC& Desc : EndFrames) // ³¡ÇÁ·¹ÀÓÀ» ½ÇÁ¦ °ªÀ¸·Î ä¿ì±â + { // 실제 값 채우기 + for (LERPDESC& Desc : EndFrames) // 끝프레임을 실제 값으로 채우기 { _int iIndex = Desc.iSlot; Desc.tagKeyFrame.fTrackPosition = m_fDuration; m_Channels[iIndex]->Set_Frame(1, Desc.tagKeyFrame); } - for (_uint i = 0; i < StartFrames.size(); ++i) { // ½ÃÀÛ ÇÁ·¹ÀÓÀ» ÇöÀç º» »óÅ ŰÇÁ·¹ÀÓÀ¸·Î ä¿ì±â + for (_uint i = 0; i < StartFrames.size(); ++i) { // 시작 프레임을 현재 본 상태 키프레임으로 채우기 _int iIndex = StartFrames[i].iSlot; m_Movable[iIndex] = true; m_Channels[iIndex]->Set_Frame(0, StartFrames[i].tagKeyFrame); @@ -101,4 +101,4 @@ void CLerpAnim::Free() m_CurrentKeyFrameIndices.clear(); } -; \ No newline at end of file +; diff --git a/Jusin154_Team3D/Engine/Private/Mesh.cpp b/Jusin154_Team3D/Engine/Private/Mesh.cpp index d36182e10..4b950894a 100644 --- a/Jusin154_Team3D/Engine/Private/Mesh.cpp +++ b/Jusin154_Team3D/Engine/Private/Mesh.cpp @@ -289,7 +289,7 @@ HRESULT CMesh::Initialize_Prototype(MODEL eType, const CModel* pModel, SaveMesh* #pragma endregion - Create_RawVB(m_RawVertices); + //Create_RawVB(m_RawVertices); return S_OK; } @@ -507,20 +507,6 @@ HRESULT CMesh::Ready_VertexBuffer_For_Anim(const CModel* pModel, SaveMesh* _Save if (FAILED(m_pDevice->CreateBuffer(&VBDesc, &InitialVBData, &m_pVB))) return E_FAIL; - - m_RawVertices.resize(m_iNumVertices); - - for (_uint i = 0; i < m_iNumVertices; i++) - { - m_RawVertices[i].vPosition = pVertices[i].vPosition; - m_RawVertices[i].vNormal = pVertices[i].vNormal; - m_RawVertices[i].vTangent = pVertices[i].vTangent; - m_RawVertices[i].vBinormal = pVertices[i].vBinormal; - m_RawVertices[i].vTexcoord = pVertices[i].vTexcoord; - - m_RawVertices[i].vBlendIndex = pVertices[i].vBlendIndex; - m_RawVertices[i].vBlendWeight = pVertices[i].vBlendWeight; - } Safe_Delete_Array(pVertices); @@ -528,61 +514,6 @@ HRESULT CMesh::Ready_VertexBuffer_For_Anim(const CModel* pModel, SaveMesh* _Save } -HRESULT CMesh::Create_RawVB(vector& RawVertices) -{ - m_iRawVertexStride = sizeof(MESH_DESC); - - D3D11_BUFFER_DESC desc{}; - desc.Usage = D3D11_USAGE_DEFAULT; - desc.ByteWidth = m_iRawVertexStride *m_iNumVertices; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; - desc.CPUAccessFlags = 0; - desc.StructureByteStride = m_iRawVertexStride; - desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; - - D3D11_SUBRESOURCE_DATA init{}; - init.pSysMem = RawVertices.data(); - - m_pDevice->CreateBuffer(&desc, &init, &m_pVBMesh); - - - return S_OK; -} - - -void CMesh::ComputSkinning(CComputeShader* ComputeShader, ID3D11Buffer* ConstantBuffer) -{ - if (ComputeShader == nullptr) - return; - - _uint iGroupCountX = (m_iNumVertices + 255) / 256; - - ID3D11Buffer* CSBuffers[] = { - m_pVBMesh - }; - - vector OutSubResources = {}; - D3D11_MAPPED_SUBRESOURCE VBInstanceResource = {}; - - OutSubResources = ComputeShader->Dispatch(0, 0, _float3((_float)iGroupCountX, 1.f, 1.f), CSBuffers, ConstantBuffer); -} - -void CMesh::FillBoneDesc(const vector<_float4x4>& CombinedMatrices, BONE_DESC* pOut) -{ - for (size_t i = 0; i < m_iNumBones; ++i) - { - const _float4x4& offset = m_offsetMatrices[i]; - const _float4x4& combined = CombinedMatrices[m_BoneIndices[i]]; - - _matrix matOffset = XMLoadFloat4x4(&offset); - _matrix matCombined = XMLoadFloat4x4(&combined); - - XMStoreFloat4x4(&pOut->BoneMatrix[i], matOffset * matCombined); - } -} - - - HRESULT CMesh::Initialize(void* pArg) { return S_OK; @@ -891,7 +822,6 @@ void CMesh::Free() __super::Free(); Safe_Delete_Array(m_pBoneMatrices); - SAFE_RELEASE(m_pVBMesh); } void CMesh::Describe_Entity() diff --git a/Jusin154_Team3D/Engine/Private/Model.cpp b/Jusin154_Team3D/Engine/Private/Model.cpp index 6995fb73e..5ae671060 100644 --- a/Jusin154_Team3D/Engine/Private/Model.cpp +++ b/Jusin154_Team3D/Engine/Private/Model.cpp @@ -160,7 +160,7 @@ _bool CModel::Play_Animation(_float fTimeDelta,CTransform* pTransform) _float fRatio = m_fBlendTime / m_fBlendDuration; fRatio = min(fRatio, 1.f); - m_bIsFinishedAnim = m_Animations[m_iCurrentAnimIndex]->Update_TransformationMatrices(m_Bones, m_bIsLoop, fTimeDelta, pTransform); + m_bIsFinishedAnim = m_Animations[m_iCurrentAnimIndex]->Update_TransformationMatrices(m_Bones, m_pLocalPos, m_bIsLoop, fTimeDelta, pTransform); m_Animations[m_iCurrentAnimIndex]->InterpAnim(m_Animations[m_iPreAnimIndex], m_Bones, fRatio); if (fRatio >= 1.f) @@ -171,38 +171,20 @@ _bool CModel::Play_Animation(_float fTimeDelta,CTransform* pTransform) } else { - m_bIsFinishedAnim = m_Animations[m_iCurrentAnimIndex]->Update_TransformationMatrices(m_Bones, m_bIsLoop, fTimeDelta, pTransform); + m_bIsFinishedAnim = m_Animations[m_iCurrentAnimIndex]->Update_TransformationMatrices(m_Bones, m_pLocalPos, m_bIsLoop, fTimeDelta, pTransform); m_iPreAnimIndex = m_iCurrentAnimIndex; } - for (_int i = 0; i < m_Bones.size(); ++i) + for (auto& pBone : m_Bones) { - m_Bones[i]->Update_CombinedTransformationMatrix(m_Bones, XMLoadFloat4x4(&m_PreTransformMatrix)); - - if (i == m_iRootBoneIndex) { // 루트본의 매트릭스를 갱신했다면 애니메이션에 맞게 이동량을 루트본에서 트랜스폼으로 보정시킴 - if (true == m_pLerpAnim->IsLerping()) { // 럴프중이었으면 이동량 반영 x - _matrix currAnimMatrix = Get_BoneMatrix(m_iRootBoneIndex); - currAnimMatrix.r[3] = XMVectorSet(0.f, 0.f, 0.f, 1.f); - m_Bones[i]->Set_CombinedTransformationMatrixPtr(currAnimMatrix); - } - else { - _matrix currAnimMatrix = Get_BoneMatrix(m_iRootBoneIndex); // 현재 루트본의 컴바인드 트랜스폼을 꺼내오고 - _matrix deltaAnimMatrix = XMMatrixMultiply(XMMatrixInverse(nullptr, m_PrevAnimationMatrix), currAnimMatrix); // 이전 트랜스폼이랑 비교해서 델타매트릭스를 꺼냄 - _vector momentum = XMMatrixMultiply(deltaAnimMatrix, XMMatrixRotationQuaternion(m_pTransform->Get_QuarternionVector())).r[3]; - deltaAnimMatrix.r[3] = XMVectorSetW(momentum, 0.f); - - m_pTransform->AccumulateMomentum(deltaAnimMatrix.r[3]); // 모체 트랜스폼에 이동량 반영 - //m_pTransform->Set_State(STATE::POSITION, m_pTransform->Get_State(STATE::POSITION) + deltaAnimMatrix.r[3]); - - m_PrevAnimationMatrix = currAnimMatrix; - currAnimMatrix.r[3] = XMVectorSet(0.f, 0.f, 0.f, 1.f); - m_Bones[i]->Set_CombinedTransformationMatrixPtr(currAnimMatrix); // 루트본의 이동량 소거 - } - } + pBone->Update_CombinedTransformationMatrix(m_Bones, XMLoadFloat4x4(&m_PreTransformMatrix)); } - if(m_bIsFinishedAnim) + if (m_bIsFinishedAnim) + { m_Animations[m_iCurrentAnimIndex]->ResetRootMotion(); + m_Animations[m_iCurrentAnimIndex]->Depart_Animation(); + } return m_bIsFinishedAnim; } @@ -902,79 +884,124 @@ HRESULT CModel::Assimp_Model_Load(const _char* pModelFilePath, MODEL eType, _fma HRESULT CModel::Create_CS() { - _uint vertexCount = 0; - _uint MaxvertexCount = 0; - for (_uint i = 0; i < m_Meshes.size(); ++i) - { - vertexCount = m_Meshes[i]->Get_Vertex(); - if (MaxvertexCount < vertexCount) - MaxvertexCount = vertexCount; - } - - _uint CS_InputStrides[] = { - sizeof(MESH_DESC) + _uint CS1_InputStrides[] = { + sizeof(KEYFRAME_DESC), + sizeof(CHANNEL_DESC), }; - size_t meshCount = m_Meshes.size(); - - _uint* CS_OutputStrides = new _uint[meshCount]; - - for (size_t i = 0; i < meshCount; i++) - { - CS_OutputStrides[i] = sizeof(SKINNG_MESH_DESC)*(_uint)meshCount; - } + _uint CS1_OutputStrides[] = { + sizeof(LOCALPOS_DESC), + }; - m_pComputeShader = CComputeShader::Create(m_pDevice, m_pContext, - L"../Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl", "CS_MAIN", MaxvertexCount, 1, (_uint)meshCount, CS_InputStrides,CS_OutputStrides); + m_pComputeShader = CComputeShader::Create(m_pDevice, m_pContext, L"../Bin/Resources/ShaderFiles/Shader_Mesh_Compute.hlsl", + "CS_LocalSRT", (_uint)m_Bones.size(), 0, 1, nullptr, CS1_OutputStrides + ); - if (m_pComputeShader == nullptr) + if (!m_pComputeShader) return E_FAIL; - Safe_Delete_Array(CS_OutputStrides); - return S_OK; } -void CModel::ComputeSkinning() +void CModel::Create_Temp() { - Get_BoneMatrix(); - for (_uint i = 0; i < m_iNumMeshes; ++i) + for (auto& anim : m_Animations) { - D3D11_MAPPED_SUBRESOURCE ConstantSubResource{}; - if (SUCCEEDED(m_pContext->Map(m_pConstantBuffer, 0, - D3D11_MAP_WRITE_DISCARD, 0, &ConstantSubResource))) - { - CMesh::BONE_DESC* pDesc = - static_cast(ConstantSubResource.pData); - - m_Meshes[i]->FillBoneDesc(m_BoneMatrix,pDesc); - - m_pContext->Unmap(m_pConstantBuffer, 0); - } - - m_Meshes[i]->ComputSkinning(m_pComputeShader, m_pConstantBuffer); + anim->CreateGPUData(m_pDevice); } } +void CModel::Create_LocalPosVB() +{ + _uint iCount = (_uint)m_Bones.size(); + _uint iStride = sizeof(LOCALPOS_DESC); + + D3D11_BUFFER_DESC desc = {}; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.ByteWidth = iStride * iCount; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS; + desc.CPUAccessFlags = 0; + desc.StructureByteStride = iStride; + desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + + HRESULT hr = m_pDevice->CreateBuffer(&desc, nullptr, &m_pLocalPosBuffer); + + return; +} + + void CModel::Create_Con() { D3D11_BUFFER_DESC cbDesc = {}; cbDesc.Usage = D3D11_USAGE_DYNAMIC; cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - cbDesc.ByteWidth = sizeof(BONE_DESC); + cbDesc.ByteWidth = sizeof(ANIMSTATE_DESC); if (FAILED(m_pDevice->CreateBuffer(&cbDesc, nullptr, &m_pConstantBuffer))) return; } -HRESULT CModel::Bind_CS_Output(_uint Index, _uint iBufferIndex) -{ - if (m_pComputeShader == nullptr) - return E_FAIL; - m_pComputeShader->Bind_OutPut_SRV(Index, iBufferIndex); +void CModel::UpdateAnimationCS() +{ + D3D11_MAPPED_SUBRESOURCE sub{}; + if (SUCCEEDED(m_pContext->Map(m_pConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sub))) + { + ANIMSTATE_DESC* pDesc = (ANIMSTATE_DESC*)sub.pData; + + CAnimation* pAnim = m_Animations[m_iCurrentAnimIndex]; + pDesc->CurrentTime = pAnim->Get_CurrentTrackPosition(); + pDesc->Duration = pAnim->Get_Duration(); + pDesc->Speed = pAnim->Get_AnimSpeed(); + pDesc->BoneCount = (_uint)m_Bones.size(); + pDesc->PreTransformMatrix = m_PreTransformMatrix; + m_pContext->Unmap(m_pConstantBuffer, 0); + } +} + +void CModel::ComputeAnimation() +{ + UpdateAnimationCS(); + + _uint iGroupCountX = ((_uint)m_Bones.size() + 255) / 256; + + CAnimation* pAnim = m_Animations[m_iCurrentAnimIndex]; + ID3D11ShaderResourceView* srvs[2] = { + pAnim->Get_KeyFrameSrv(), + pAnim->Get_ChannelSrv(), + }; + + m_pContext->CSSetShader(m_pComputeShader->Get_Compute(), nullptr, 0); + m_pContext->CSSetConstantBuffers(0, 1, &m_pConstantBuffer); + m_pContext->CSSetShaderResources(0, 2, srvs); + + ID3D11UnorderedAccessView* uavs[1] = { m_pComputeShader->GetOutputUAV(0) }; + m_pContext->CSSetUnorderedAccessViews(0, 1, uavs, nullptr); + + m_pContext->Dispatch(iGroupCountX, 1, 1); + + auto outSubs = m_pComputeShader->ReadBackOutputs(); + m_pLocalPos = static_cast(outSubs[0].pData); +} + + +HRESULT CModel::Create_ParentVB() +{ + D3D11_BUFFER_DESC desc{}; + desc.Usage = D3D11_USAGE_DEFAULT; + desc.ByteWidth = (_int)sizeof(PARENT_DESC) * (_uint)m_Bones.size(); + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + desc.StructureByteStride = (_int)sizeof(PARENT_DESC); + desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + + D3D11_SUBRESOURCE_DATA init{}; + init.pSysMem = m_Parent.data(); + + m_pDevice->CreateBuffer(&desc, &init, &m_pParentBuffer); + return S_OK; } @@ -988,8 +1015,6 @@ void CModel::Get_BoneMatrix() } } - - HRESULT CModel::Initialize_Prototype(MODEL eType, const _char* pModelFilePath, _fmatrix PreTransformMatrix) { m_eType = eType; @@ -1032,7 +1057,7 @@ HRESULT CModel::Initialize_Prototype(MODEL eType, const _char* pModelFilePath, _ return E_FAIL; } - if (FAILED(Ready_Animations())) { + if (FAILED(Ready_Animations(m_Bones))) { return E_FAIL; } return S_OK; @@ -1317,12 +1342,25 @@ HRESULT CModel::Initialize(void* pArg) m_pTransform = m_pOwner->Get_Component(); SAFE_ADDREF(m_pTransform); - /*Create_Con(); + + if (m_eType == MODEL::ANIM) + { + Create_Temp(); + m_Parent.resize(m_Bones.size()); + for (size_t i = 0; i < (_uint)m_Bones.size(); i++) + { + m_Parent[i].ParentIndex = m_Bones[i]->Get_ParentBoneIndex(); + } - if (FAILED(Create_CS())) - return E_FAIL;*/ + if (FAILED(Create_CS())) + return E_FAIL; + Create_Con(); + Create_LocalPosVB(); + Create_ParentVB(); + } + return S_OK; } @@ -1378,13 +1416,13 @@ HRESULT CModel::Ready_Bones(const std::vector& allNodes, _int currentI return S_OK; } -HRESULT CModel::Ready_Animations() +HRESULT CModel::Ready_Animations(const vector& Bones) { m_iNumAnimations = m_pSaveModel->AnimationCount; for (size_t i = 0; i < m_iNumAnimations; i++) { - CAnimation* pAnimation = CAnimation::Create(this, &m_pSaveModel->Animations[i]); + CAnimation* pAnimation = CAnimation::Create(Bones,this, &m_pSaveModel->Animations[i]); if (nullptr == pAnimation) return E_FAIL; @@ -1425,6 +1463,11 @@ void CModel::Free() { __super::Free(); + + SAFE_RELEASE(m_pComputeShader); + SAFE_RELEASE(m_pConstantBuffer); + SAFE_RELEASE(m_pParentBuffer); + SAFE_RELEASE(m_pLocalPosBuffer); for (auto& pAnimation : m_Animations) { SAFE_RELEASE(pAnimation); } @@ -1448,8 +1491,8 @@ void CModel::Free() m_SaveModel.clear(); - SAFE_RELEASE(m_pComputeShader); - SAFE_RELEASE(m_pConstantBuffer); + m_Parent.clear(); + m_AnimRanges.clear(); } void CModel::Describe_Entity() diff --git a/Jusin154_Team3D/Engine/Public/Animation.h b/Jusin154_Team3D/Engine/Public/Animation.h index 5d86dbdad..3140d3dd6 100644 --- a/Jusin154_Team3D/Engine/Public/Animation.h +++ b/Jusin154_Team3D/Engine/Public/Animation.h @@ -11,7 +11,7 @@ class ENGINE_DLL CAnimation : public CBase CAnimation(const CAnimation& rhs); virtual ~CAnimation() = default; public: - _bool Update_TransformationMatrices(const vector& Bones, _bool bIsLoop, _float fTimeDelta, class CTransform* pTransform = nullptr); + _bool Update_TransformationMatrices(const vector& Bones, const LOCALPOS_DESC* pLocalPosArray, _bool bIsLoop, _float fTimeDelta, class CTransform* pTransform = nullptr); void ResetRootMotion(); void Depart_Animation(); void Set_AnimPause(_bool bValue) { m_bPause = bValue; } @@ -21,6 +21,7 @@ class ENGINE_DLL CAnimation : public CBase void Set_AnimProgressPostion(const _char* pChannelName, _uint iPosition); vector<_int>* Capture_Bones(); void InterpAnim(CAnimation* pPreAnim, vector& Bones, float fRatio); + void CreateGPUData(ID3D11Device* pDevice); vector Get_StartFrameInformations() { return m_StartKeyFrames; } const _string& Get_Name() const { return m_strName; } @@ -29,6 +30,14 @@ class ENGINE_DLL CAnimation : public CBase _float Get_CurrentTrackPosition() { return m_fCurrentTrackPosition; } void Set_AnimSpeed(_float fSpeed) { m_fAnimSpeed = fSpeed; } _float Get_AnimSpeed() { return m_fAnimSpeed; } + + _uint Get_ChannelCount() { return m_iNumChannels; } + _float Get_Duration() { return m_fDuration; } + + ID3D11Buffer* Get_KeyFrameBuffer() { return m_pKeyFrameBuffer; } + ID3D11Buffer* Get_ChannelBuffer() { return m_pChannelBuffer; } + ID3D11ShaderResourceView* Get_KeyFrameSrv() { return m_pKeyFrameSrv; } + ID3D11ShaderResourceView* Get_ChannelSrv() { return m_pChannelSrv; } #ifdef EDITOR_PROJECT private: HRESULT Initialize(const vector& Bones, const aiAnimation* pAIAnimation); @@ -40,11 +49,16 @@ class ENGINE_DLL CAnimation : public CBase private: HRESULT Initialize(HANDLE hFile, DWORD& dwByte); // 바이너리 - HRESULT Initialize(const class CModel* pModel,SaveAnimation* pSaveAnimation); + HRESULT Initialize(const vector& Bones, const class CModel* pModel,SaveAnimation* pSaveAnimation); // HRESULT Combined_Initialize(); _uint Get_BoneIndex(const char* pChannelName); + HRESULT CreateKeyFrameBuffer(ID3D11Device* pDevice, vector keyframe); + HRESULT CreateChannelBuffer(ID3D11Device* pDevice, vector channel); + + + private: SaveAnimation* m_pSaveAnim = { nullptr }; _char m_szName[MAX_PATH] = {}; @@ -63,11 +77,22 @@ class ENGINE_DLL CAnimation : public CBase vector<_int> m_DestBones; // 이 애니메이션에서 영향을 받는 본들의 집합 vector m_StartKeyFrames; // 이 애니메이션의 영향을 받는 본들의 초기값 - vector<_matrix> m_vBoneTransformationMatrix = {}; + vector<_matrix> m_vBoneTransformationMatrix = {}; + + _uint m_iChannelCount; + _uint m_iKeyframeCount; + _uint m_iBoneCount; + vector m_KeyFrameDesc; + vector m_ChannelDesc; + + ID3D11Buffer* m_pKeyFrameBuffer; + ID3D11Buffer* m_pChannelBuffer; + ID3D11ShaderResourceView* m_pKeyFrameSrv = {}; + ID3D11ShaderResourceView* m_pChannelSrv = {}; public: static CAnimation* Create(HANDLE hFile, DWORD& dwByte); // 바이너리 - static CAnimation* Create(const class CModel* pModel, SaveAnimation* pSaveAnimation); + static CAnimation* Create(const vector& Bones, const class CModel* pModel, SaveAnimation* pSaveAnimation); // CAnimation* Clone(); virtual void Free() override; diff --git a/Jusin154_Team3D/Engine/Public/Bone.h b/Jusin154_Team3D/Engine/Public/Bone.h index 8543693b8..6343c5043 100644 --- a/Jusin154_Team3D/Engine/Public/Bone.h +++ b/Jusin154_Team3D/Engine/Public/Bone.h @@ -28,14 +28,13 @@ class ENGINE_DLL CBone final : public CBase _float3 Get_LocalPosition(); void Update_CombinedTransformationMatrix(const vector& Bones, _fmatrix PreTransformMatrix); void Set_TransformationMatrix(_fmatrix TransformationMatrix); // 애니메이션에서 현재 로컬 매트릭스를 변경할 때 사용된다 - + _int Get_ParentBoneIndex() { return m_iParentBoneIndex; } private: _char m_szName[MAX_PATH] = {}; // 본의 이름 _float4x4 m_TransformationMatrix = {}; // 본의 현재 로컬 매트릭스 _float4x4 m_CombinedTransformationMatrix = {}; // 본의 루트노드로부터 상대적인 복합매트릭스 ( 부모노드는 PreTranformMatrix와 월드좌표로부터 상대적임 ) _int m_iParentBoneIndex = { -1 }; // 부모본의 인덱스 ( 모델에 있는 본벡터의 인덱스 ) ( 부모는 인덱스 -1 고정 ) _float4x4 m_matInitial = {}; - private: HRESULT Initialize(HANDLE hFile, DWORD& dwByte); // 바이너리 diff --git a/Jusin154_Team3D/Engine/Public/Channel.h b/Jusin154_Team3D/Engine/Public/Channel.h index 745a95f1c..759f3718c 100644 --- a/Jusin154_Team3D/Engine/Public/Channel.h +++ b/Jusin154_Team3D/Engine/Public/Channel.h @@ -13,19 +13,23 @@ class ENGINE_DLL CChannel : public CBase #ifdef EDITOR_PROJECT private: HRESULT Initialize(const vector& Bones, const aiNodeAnim* pAIChannel); + public: HRESULT SaveAsBinary(HANDLE hFile, DWORD& dwByte); static CChannel* Create(const vector& Bones, const aiNodeAnim* pAIChannel); + void Fill_GPU_Keyframes(vector& outKeyframes); + CHANNEL_DESC Fill_GPU_ChannelDesc(); #endif // EDITOR_PROJECT public: - void Update_TransformationMatirx(const vector& Bones, _float fCurrentTrackPosition, _uint* pCurrentKeyFrameIndex,class CTransform* pTransform=nullptr); + void Update_TransformationMatirx(const vector& Bones, const LOCALPOS_DESC* pLocalPosArray, _float fCurrentTrackPosition, _uint* pCurrentKeyFrameIndex,class CTransform* pTransform=nullptr); _int Get_BoneIndex() { return { m_iBoneIndex }; } void Set_BoneIndex(_int iBoneIndex) { m_iBoneIndex = iBoneIndex; } void ResetRootMotion() { m_vPrevRootPos = { 0.f, 0.f, 0.f }; m_vPrevRootRot = { 0.f,0.f,0.f,0.f }; m_bInitialRootRotSaved = false; + m_bInitialRootPos = false; } _matrix Get_BoneTransformationMatrix() { return m_BoneTransformationMatrix; } @@ -53,7 +57,10 @@ class ENGINE_DLL CChannel : public CBase _matrix m_BoneTransformationMatrix = {}; _float m_fPrevYaw = 0.f; _bool m_bInitialRootRotSaved = {false}; -_float4 m_vInitialRootRot = {}; + _bool m_bInitialRootPos = { false }; + _float4 m_vInitialRootRot = {}; + + vector m_KeyFrameDesc; public: static CChannel* Create(const vector& Bones, _uint iIndex); // 럴프전용 static CChannel* Create(HANDLE hFile, DWORD& dwByte); diff --git a/Jusin154_Team3D/Engine/Public/ComputeShader.h b/Jusin154_Team3D/Engine/Public/ComputeShader.h index 89980d890..69e66bcff 100644 --- a/Jusin154_Team3D/Engine/Public/ComputeShader.h +++ b/Jusin154_Team3D/Engine/Public/ComputeShader.h @@ -17,6 +17,8 @@ class ENGINE_DLL CComputeShader final : public CBase vector Dispatch(_uint iSRVIndex, _uint iUAVIndex, _float3 vGroupCount, ID3D11Buffer** ppBuffers, ID3D11Buffer* pConstantBuffer = nullptr); void Bind_OutPut_SRV(_uint iIndex, _uint iBufferIndex); ID3D11ComputeShader* Get_Compute() { return m_pComputeShader; } + ID3D11UnorderedAccessView* GetOutputUAV(_uint iIndex) const; + vector ReadBackOutputs(); private: void Bind_SRV(_uint iIndex); void Bind_UAV(_uint iIndex); diff --git a/Jusin154_Team3D/Engine/Public/Engine_Struct.h b/Jusin154_Team3D/Engine/Public/Engine_Struct.h index d1363daca..9ad49e3c9 100644 --- a/Jusin154_Team3D/Engine/Public/Engine_Struct.h +++ b/Jusin154_Team3D/Engine/Public/Engine_Struct.h @@ -362,4 +362,42 @@ typedef struct tagOnCollsionInfo }ON_COLLISION_INFO; +typedef struct tagKeyFrameDesc +{ + _float3 vScale; + _float4 vRotation; + _float3 vTranslation; + _float fTrackPosition; + +}KEYFRAME_DESC; + +typedef struct tagChannelDesc +{ + _uint StartIndex; + _uint KeyCount; + _uint BoneIndex; + //_uint Padding; +}CHANNEL_DESC; + + +typedef struct tagParentDesc +{ + _int ParentIndex; +}PARENT_DESC; + +typedef struct tagLocalPos +{ + _float3 Scale; + _float pad0; + _float4 Rotation; + _float3 Translation; + _float pad1; +}LOCALPOS_DESC; + +typedef struct tagBoneDesc +{ + _float4x4 Combined; +}BONE_DESC; + + NS_END diff --git a/Jusin154_Team3D/Engine/Public/Mesh.h b/Jusin154_Team3D/Engine/Public/Mesh.h index 9c88c1dfb..5fadd3e78 100644 --- a/Jusin154_Team3D/Engine/Public/Mesh.h +++ b/Jusin154_Team3D/Engine/Public/Mesh.h @@ -6,36 +6,6 @@ NS_BEGIN(Engine) class ENGINE_DLL CMesh final : public CVIBuffer { -public: - typedef struct tagMeshDesc - { - _float3 vPosition = {}; - _float3 vNormal = {}; - _float3 vTangent = {}; - _float3 vBinormal = {}; - _float2 vTexcoord = {}; - - XMUINT4 vBlendIndex = {}; - _float4 vBlendWeight = {}; - - }MESH_DESC; - - typedef struct tagSkinngMeshDesc - { - _float3 vPosition = {}; - _float3 vNormal = {}; - _float3 vTangent = {}; - _float3 vBinormal = {}; - _float2 vTexcoord = {}; - - }SKINNG_MESH_DESC; - - typedef struct tagBoneDesc - { - _float4x4 BoneMatrix[512]; - }BONE_DESC; - - private: CMesh(ID3D11Device* pDevice, ID3D11DeviceContext* pContext); CMesh(const CMesh& rhs); @@ -66,10 +36,6 @@ class ENGINE_DLL CMesh final : public CVIBuffer virtual HRESULT Bind_Resources_Instance(ID3D11Buffer* pVBInstance, _uint iInstanceStride, _uint iBufferCount); virtual HRESULT Render_Instance(_uint iNumInstance); - void ComputSkinning(class CComputeShader* ComputeShader, ID3D11Buffer* ConstantBuffer); - - void FillBoneDesc(const vector<_float4x4>& CombinedMatrices, BONE_DESC* pOut); - _uint Get_Vertex() { return m_iNumVertices; } private: virtual HRESULT Initialize_Prototype(HANDLE hFile, DWORD& dwByte); @@ -81,7 +47,7 @@ class ENGINE_DLL CMesh final : public CVIBuffer HRESULT Ready_VertexBuffer_For_NonAnim(SaveMesh* _SaveMesh, _fmatrix PreTransformMatrix); HRESULT Ready_VertexBuffer_For_Anim(const class CModel* pModel, SaveMesh* _SaveMesh); - HRESULT Create_RawVB(vector& RawVertices); + // private: @@ -99,16 +65,9 @@ class ENGINE_DLL CMesh final : public CVIBuffer // private: - MESH_DESC m_MeshDesc = {}; - BONE_DESC m_BoneDesc = {}; _uint m_iNumBuffer = {}; _uint m_iNumMeshes = {}; _int m_iRootBoneIndex = { -1 }; - _int m_iRawVertexStride = {}; - - vector m_RawVertices; - - ID3D11Buffer* m_pVBMesh = { nullptr }; public: static CMesh* Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, HANDLE hFile, DWORD& dwByte); diff --git a/Jusin154_Team3D/Engine/Public/Model.h b/Jusin154_Team3D/Engine/Public/Model.h index 1bfe26d31..4f070c18e 100644 --- a/Jusin154_Team3D/Engine/Public/Model.h +++ b/Jusin154_Team3D/Engine/Public/Model.h @@ -7,32 +7,15 @@ NS_BEGIN(Engine) class ENGINE_DLL CModel final : public CComponent { public: - typedef struct tagMeshDesc + typedef struct tagAnimStateDesc { - _float3 vPosition = {}; - _float3 vNormal = {}; - _float3 vTangent = {}; - _float3 vBinormal = {}; - _float2 vTexcoord = {}; + _float CurrentTime; + _float Duration; + _float Speed; + _int BoneCount; + _float4x4 PreTransformMatrix; - XMUINT4 vBlendIndex = {}; - _float4 vBlendWeight = {}; - }MESH_DESC; - - typedef struct tagSkinngMeshDesc - { - _float3 vPosition = {}; - _float3 vNormal = {}; - _float3 vTangent = {}; - _float3 vBinormal = {}; - _float2 vTexcoord = {}; - - }SKINNG_MESH_DESC; - - typedef struct tagBoneDesc - { - _float4x4 BoneMatrix[512]; - }BONE_DESC; + }ANIMSTATE_DESC; private: CModel(ID3D11Device* pDevice, ID3D11DeviceContext* pContext); @@ -94,9 +77,11 @@ static _int Get_BoneIndex(const _char* pBoneName, vector Bones); / HRESULT Bind_Material(_uint iMeshIndex, class CShader* pShader); #pragma endregion - void ComputeSkinning(); + void Create_Con(); - HRESULT Bind_CS_Output(_uint Index, _uint iBufferIndex); + void UpdateAnimationCS(); + void ComputeAnimation(); + HRESULT Create_ParentVB(); void Get_BoneMatrix(); public: @@ -169,14 +154,23 @@ static _int Get_BoneIndex(const _char* pBoneName, vector Bones); / private: HRESULT Create_CS(); - MESH_DESC m_MeshDesc = {}; - BONE_DESC m_BoneDesc = {}; + void Create_Temp(); + + void Create_LocalPosVB(); + _uint m_iNumBuffer = {}; - class CComputeShader* m_pComputeShader = {}; - ID3D11Buffer* m_pConstantBuffer = { nullptr }; - _uint AccumulatedVertexCount = {}; + vector m_Parent = {}; + vector m_AnimRanges; + LOCALPOS_DESC* m_pLocalPos = { nullptr }; + + class CComputeShader* m_pComputeShader = nullptr; + + ID3D11Buffer* m_pConstantBuffer = { nullptr }; + + ID3D11Buffer* m_pParentBuffer = { nullptr }; + ID3D11Buffer* m_pLocalPosBuffer = { nullptr }; private: // 바이너리 virtual HRESULT Initialize_Prototype(MODEL eType, const _char* pModelFilePath, _fmatrix PreTransformMatrix); @@ -191,7 +185,7 @@ static _int Get_BoneIndex(const _char* pBoneName, vector Bones); / HRESULT Ready_Meshes(); HRESULT Ready_Materials(const _char* pModelFilePath); HRESULT Ready_Bones(const std::vector& allNodes, _int currentIndex, _int parentIndex); - HRESULT Ready_Animations(); + HRESULT Ready_Animations(const vector& Bones); // public: