Skip to content

Commit da75792

Browse files
GLTF Viewer: added motion vector field visualization
1 parent a7cdbd4 commit da75792

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

Samples/GLTFViewer/src/GLTFViewer.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "CommandLineParser.hpp"
4444
#include "GraphicsAccessories.hpp"
4545
#include "EnvMapRenderer.hpp"
46+
#include "VectorFieldRenderer.hpp"
4647
#include "Utilities/include/DiligentFXShaderSourceStreamFactory.hpp"
4748
#include "ShaderSourceFactoryUtils.h"
4849

@@ -368,7 +369,7 @@ struct PSOutput
368369
}
369370
# endif
370371
371-
PSOut.MotionVec = float4(0.0, 0.0, 0.0, 1.0);
372+
PSOut.MotionVec = float4(MotionVector, 0.0, 1.0);
372373
return PSOut;
373374
)";
374375

@@ -498,6 +499,16 @@ void GLTFViewer::CrateEnvMapRenderer()
498499
m_EnvMapRenderer = std::make_unique<EnvMapRenderer>(EnvMapRendererCI);
499500
}
500501

502+
void GLTFViewer::CreateVectorFieldRenderer()
503+
{
504+
VectorFieldRenderer::CreateInfo CI;
505+
CI.pDevice = m_pDevice;
506+
CI.NumRenderTargets = 1;
507+
CI.RTVFormats[0] = m_pSwapChain->GetDesc().ColorBufferFormat;
508+
509+
m_VectorFieldRenderer = std::make_unique<VectorFieldRenderer>(CI);
510+
}
511+
501512
void GLTFViewer::Initialize(const SampleInitInfo& InitInfo)
502513
{
503514
SampleBase::Initialize(InitInfo);
@@ -534,6 +545,7 @@ void GLTFViewer::Initialize(const SampleInitInfo& InitInfo)
534545

535546
CreateGLTFRenderer();
536547
CrateEnvMapRenderer();
548+
CreateVectorFieldRenderer();
537549

538550
RefCntAutoPtr<IRenderStateNotationParser> pRSNParser;
539551
{
@@ -724,7 +736,13 @@ void GLTFViewer::UpdateUI()
724736
ImGui::SetNextItemOpen(true, ImGuiCond_FirstUseEver);
725737
if (ImGui::TreeNode("Animation"))
726738
{
727-
ImGui::Checkbox("Play", &m_PlayAnimation);
739+
if (ImGui::Checkbox("Play", &m_PlayAnimation))
740+
{
741+
if (!m_PlayAnimation)
742+
{
743+
m_Transforms[(m_CurrentFrameNumber + 1) & 0x01] = m_Transforms[m_CurrentFrameNumber & 0x01];
744+
}
745+
}
728746
std::vector<const char*> Animations(m_Model->Animations.size());
729747
for (size_t i = 0; i < m_Model->Animations.size(); ++i)
730748
Animations[i] = m_Model->Animations[i].Name.c_str();
@@ -773,7 +791,7 @@ void GLTFViewer::UpdateUI()
773791
{GLTF_PBR_Renderer::DebugViewType::ShadingNormal, "Shading normal"},
774792
{GLTF_PBR_Renderer::DebugViewType::MotionVectors, "Motion Vectors"},
775793
{GLTF_PBR_Renderer::DebugViewType::NdotV, "n*v"},
776-
{PBR_Renderer::DebugViewType::PunctualLighting, "Punctual Lighting"},
794+
{GLTF_PBR_Renderer::DebugViewType::PunctualLighting, "Punctual Lighting"},
777795
{GLTF_PBR_Renderer::DebugViewType::DiffuseIBL, "Diffuse IBL"},
778796
{GLTF_PBR_Renderer::DebugViewType::SpecularIBL, "Specular IBL"},
779797
{GLTF_PBR_Renderer::DebugViewType::ClearCoat, "Clear Coat"},
@@ -933,8 +951,9 @@ GLTFViewer::~GLTFViewer()
933951
// Render a frame
934952
void GLTFViewer::Render()
935953
{
936-
auto* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
937-
auto* pDSV = m_pSwapChain->GetDepthBufferDSV();
954+
ITextureView* pRTV = m_pSwapChain->GetCurrentBackBufferRTV();
955+
ITextureView* pDSV = m_pSwapChain->GetDepthBufferDSV();
956+
const SwapChainDesc& SCDesc = m_pSwapChain->GetDesc();
938957

939958
if (m_bEnablePostProcessing)
940959
{
@@ -943,7 +962,6 @@ void GLTFViewer::Render()
943962
m_ApplyPostFX.Initialize(m_pDevice, m_pSwapChain->GetDesc().ColorBufferFormat, m_FrameAttribsCB);
944963
}
945964

946-
const auto& SCDesc = m_pSwapChain->GetDesc();
947965
m_GBuffer->Resize(m_pDevice, SCDesc.Width, SCDesc.Height);
948966
m_GBuffer->Bind(m_pImmediateContext, GBUFFER_RT_FLAG_ALL, nullptr, GBUFFER_RT_FLAG_ALL);
949967
}
@@ -1077,6 +1095,26 @@ void GLTFViewer::Render()
10771095
m_ApplyPostFX.ptex2DColorVar->Set(m_GBuffer->GetBuffer(GBUFFER_RT_COLOR)->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE));
10781096
m_pImmediateContext->CommitShaderResources(m_ApplyPostFX.pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
10791097
m_pImmediateContext->Draw({3, DRAW_FLAG_VERIFY_ALL});
1098+
1099+
if (m_VectorFieldRenderer &&
1100+
(m_RenderParams.DebugView == GLTF_PBR_Renderer::DebugViewType::MotionVectors) &&
1101+
(m_RenderParams.Flags & GLTF_PBR_Renderer::PSO_FLAG_COMPUTE_MOTION_VECTORS) != 0)
1102+
{
1103+
VectorFieldRenderer::RenderAttribs Attribs;
1104+
Attribs.pContext = m_pImmediateContext;
1105+
Attribs.GridSize = {SCDesc.Width / 20, SCDesc.Height / 20};
1106+
// Render motion vectors in the opposite direction
1107+
Attribs.Scale = float2{-0.05f} / std::max(m_ElapsedTime, 0.001f);
1108+
Attribs.StartColor = float4{1};
1109+
Attribs.EndColor = float4{0.5};
1110+
Attribs.ConvertOutputToSRGB = (SCDesc.ColorBufferFormat == TEX_FORMAT_RGBA8_UNORM || SCDesc.ColorBufferFormat == TEX_FORMAT_BGRA8_UNORM);
1111+
1112+
StateTransitionDesc Barrier{m_GBuffer->GetBuffer(GBUFFER_RT_MOTION_VECTORS), RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_SHADER_RESOURCE, STATE_TRANSITION_FLAG_UPDATE_STATE};
1113+
m_pImmediateContext->TransitionResourceStates(1, &Barrier);
1114+
1115+
Attribs.pVectorField = m_GBuffer->GetBuffer(GBUFFER_RT_MOTION_VECTORS)->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
1116+
m_VectorFieldRenderer->Render(Attribs);
1117+
}
10801118
}
10811119
}
10821120

@@ -1090,6 +1128,8 @@ void GLTFViewer::Update(double CurrTime, double ElapsedTime)
10901128
SampleBase::Update(CurrTime, ElapsedTime);
10911129
UpdateUI();
10921130

1131+
m_ElapsedTime = static_cast<float>(ElapsedTime);
1132+
10931133
float YFov = PI_F / 4.0f;
10941134
float ZNear = 0.1f;
10951135
float ZFar = 100.f;

Samples/GLTFViewer/src/GLTFViewer.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct CameraAttribs;
4848
}
4949

5050
class EnvMapRenderer;
51+
class VectorFieldRenderer;
5152

5253
class GLTFViewer final : public SampleBase
5354
{
@@ -72,6 +73,7 @@ class GLTFViewer final : public SampleBase
7273
bool SetEnvironmentMap(ITextureView* pEnvMap);
7374
void CreateGLTFRenderer();
7475
void CrateEnvMapRenderer();
76+
void CreateVectorFieldRenderer();
7577

7678
enum class BackgroundMode : int
7779
{
@@ -123,6 +125,7 @@ class GLTFViewer final : public SampleBase
123125
bool m_PlayAnimation = false;
124126
int m_AnimationIndex = 0;
125127
std::vector<float> m_AnimationTimers;
128+
float m_ElapsedTime = 0.f;
126129

127130
std::unique_ptr<GLTF_PBR_Renderer> m_GLTFRenderer;
128131
std::unique_ptr<GLTF::Model> m_Model;
@@ -151,7 +154,8 @@ class GLTFViewer final : public SampleBase
151154
};
152155
ApplyPosteffects m_ApplyPostFX;
153156

154-
std::unique_ptr<EnvMapRenderer> m_EnvMapRenderer;
157+
std::unique_ptr<EnvMapRenderer> m_EnvMapRenderer;
158+
std::unique_ptr<VectorFieldRenderer> m_VectorFieldRenderer;
155159

156160
bool m_bUseResourceCache = false;
157161
RefCntAutoPtr<GLTF::ResourceManager> m_pResourceMgr;

0 commit comments

Comments
 (0)