Skip to content

Commit 26474a4

Browse files
Hydrogent: added edge map view mode
1 parent 14bc4e4 commit 26474a4

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

Hydrogent/interface/HnTypes.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ enum HN_VIEW_MODE : Uint8
147147
/// Display scene depth.
148148
HN_VIEW_MODE_SCENE_DEPTH,
149149

150+
/// Display edge map.
151+
HN_VIEW_MODE_EDGE_MAP,
152+
150153
/// The total number of view modes.
151154
HN_VIEW_MODE_COUNT
152155
};

Hydrogent/shaders/HnPostProcess.psh

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,83 @@ Texture2D g_BaseColor;
3737
Texture2D g_PreintegratedGGX;
3838
SamplerState g_PreintegratedGGX_sampler;
3939

40+
#if VIEW_MODE == VIEW_MODE_EDGE_MAP
41+
float ComputeColorEdge(int2 Pos)
42+
{
43+
int Width, Height;
44+
g_ColorBuffer.GetDimensions(Width, Height);
45+
46+
// t * * *
47+
// * * *
48+
// b * * *
49+
// l r
50+
int l = max(Pos.x - 1, 0);
51+
int b = max(Pos.y - 1, 0);
52+
int r = min(Pos.x + 1, Width - 1);
53+
int t = min(Pos.y + 1, Height - 1);
54+
55+
float3 LB = g_ColorBuffer.Load(int3(l, b, 0)).rgb;
56+
float3 RB = g_ColorBuffer.Load(int3(r, b, 0)).rgb;
57+
float3 LT = g_ColorBuffer.Load(int3(l, t, 0)).rgb;
58+
float3 RT = g_ColorBuffer.Load(int3(r, t, 0)).rgb;
59+
60+
float3 LL = g_ColorBuffer.Load(int3(l, Pos.y, 0)).rgb;
61+
float3 RR = g_ColorBuffer.Load(int3(r, Pos.y, 0)).rgb;
62+
float3 BB = g_ColorBuffer.Load(int3(Pos.x, b, 0)).rgb;
63+
float3 TT = g_ColorBuffer.Load(int3(Pos.x, t, 0)).rgb;
64+
65+
float3 H = (RR - LL) * 0.5;
66+
float3 V = (TT - BB) * 0.5;
67+
float3 D0 = (RT - LB) * 0.5;
68+
float3 D1 = (LT - RB) * 0.5;
69+
70+
float MaxGrad = max(max(length(H), length(V)), max(length(D0), length(D1)));
71+
72+
float Threshold = 0.2;
73+
float Scale = 5.0;
74+
return saturate((MaxGrad - Threshold) / (1.0 - Threshold) * Scale);
75+
}
76+
77+
float ComputeDepthEdge(int2 Pos, float Depth)
78+
{
79+
int Width, Height;
80+
g_Depth.GetDimensions(Width, Height);
81+
82+
// t * * *
83+
// * * *
84+
// b * * *
85+
// l r
86+
int l = max(Pos.x - 1, 0);
87+
int b = max(Pos.y - 1, 0);
88+
int r = min(Pos.x + 1, Width - 1);
89+
int t = min(Pos.y + 1, Height - 1);
90+
91+
float Z = DepthToCameraZ(Depth, g_Frame.Camera.mProj);
92+
93+
float LB = DepthToCameraZ(g_Depth.Load(int3(l, b, 0)).r, g_Frame.Camera.mProj);
94+
float RB = DepthToCameraZ(g_Depth.Load(int3(r, b, 0)).r, g_Frame.Camera.mProj);
95+
float LT = DepthToCameraZ(g_Depth.Load(int3(l, t, 0)).r, g_Frame.Camera.mProj);
96+
float RT = DepthToCameraZ(g_Depth.Load(int3(r, t, 0)).r, g_Frame.Camera.mProj);
97+
98+
float LL = DepthToCameraZ(g_Depth.Load(int3(l, Pos.y, 0)).r, g_Frame.Camera.mProj);
99+
float RR = DepthToCameraZ(g_Depth.Load(int3(r, Pos.y, 0)).r, g_Frame.Camera.mProj);
100+
float BB = DepthToCameraZ(g_Depth.Load(int3(Pos.x, b, 0)).r, g_Frame.Camera.mProj);
101+
float TT = DepthToCameraZ(g_Depth.Load(int3(Pos.x, t, 0)).r, g_Frame.Camera.mProj);
102+
103+
float H = abs(RR - LL) * 0.5;
104+
float V = abs(TT - BB) * 0.5;
105+
float D0 = abs(RT - LB) * 0.5;
106+
float D1 = abs(LT - RB) * 0.5;
107+
108+
float MaxGrad = max(max(H, V), max(D0, D1));
109+
110+
// Note: lower threshold makes planes at steep angles detected as edges
111+
float Threshold = 0.1;
112+
float Scale = 10.0;
113+
return saturate((abs(MaxGrad / Z) - Threshold) / (1.0 - Threshold) * Scale);
114+
}
115+
#endif
116+
40117
void main(in FullScreenTriangleVSOutput VSOut,
41118
out float4 Color : SV_Target0)
42119
{
@@ -100,8 +177,24 @@ void main(in FullScreenTriangleVSOutput VSOut,
100177
Color.rgb = ToneMap(Color.rgb, g_Attribs.ToneMapping, g_Attribs.AverageLogLum * exp2(-g_Frame.Camera.fExposure));
101178
#endif
102179

180+
float Depth = g_Depth.Load(int3(Pos.xy, 0)).r;
181+
182+
#if VIEW_MODE == VIEW_MODE_EDGE_MAP
183+
{
184+
// Note that in the edge map mode, the scene is rendered with mesh normal debug view.
185+
float ColorEdgeFactor = ComputeColorEdge(int2(Pos.xy));
186+
if (Depth == g_Frame.Camera.fFarPlaneDepth)
187+
{
188+
// Don't detect color edges on the background
189+
ColorEdgeFactor = 0.0;
190+
}
191+
float DepthEdgeFactor = ComputeDepthEdge(int2(Pos.xy), Depth);
192+
float EdgeFactor = min(ColorEdgeFactor + DepthEdgeFactor, 1.0);
193+
Color.rgb = float3(EdgeFactor, EdgeFactor, EdgeFactor);
194+
}
195+
#endif
196+
103197
float SelectionDepth = g_SelectionDepth.Load(int3(Pos.xy, 0)).r;
104-
float Depth = g_Depth.Load(int3(Pos.xy, 0)).r;
105198
bool IsSelected = Depth != g_Attribs.ClearDepth && SelectionDepth == Depth;
106199

107200
// Desaturate all unselected pixels

Hydrogent/src/HnTypeConversions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ const pxr::TfToken& PBRTextureAttribIdToPxrName(PBR_Renderer::TEXTURE_ATTRIB_ID
346346

347347
PBR_Renderer::DebugViewType HnViewModeToDebugViewType(HN_VIEW_MODE ViewMode)
348348
{
349-
static_assert(HN_VIEW_MODE_COUNT == 25, "Please handle the new view mode below");
349+
static_assert(HN_VIEW_MODE_COUNT == 26, "Please handle the new view mode below");
350350

351351
static const std::array<PBR_Renderer::DebugViewType, HN_VIEW_MODE_COUNT> ViewModeToDebugViewType = []() {
352352
std::array<PBR_Renderer::DebugViewType, HN_VIEW_MODE_COUNT> DebugViewTypes;
@@ -376,6 +376,7 @@ PBR_Renderer::DebugViewType HnViewModeToDebugViewType(HN_VIEW_MODE ViewMode)
376376
DebugViewTypes[HN_VIEW_MODE_CLEARCOAT_ROUGHNESS] = PBR_Renderer::DebugViewType::ClearCoatRoughness;
377377
DebugViewTypes[HN_VIEW_MODE_CLEARCOAT_NORMAL] = PBR_Renderer::DebugViewType::ClearCoatNormal;
378378
DebugViewTypes[HN_VIEW_MODE_SCENE_DEPTH] = PBR_Renderer::DebugViewType::SceneDepth;
379+
DebugViewTypes[HN_VIEW_MODE_EDGE_MAP] = PBR_Renderer::DebugViewType::MeshNormal;
379380
return DebugViewTypes;
380381
}();
381382
if (ViewMode < 0 || ViewMode >= HN_VIEW_MODE_COUNT)

Hydrogent/src/Tasks/HnPostProcessTask.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void HnPostProcessTask::PostProcessingTechnique::PreparePSO(TEXTURE_FORMAT RTVFo
186186
}
187187

188188
{
189-
if (_ViewMode != HN_VIEW_MODE_SCENE_DEPTH)
189+
if (_ViewMode != HN_VIEW_MODE_SCENE_DEPTH && _ViewMode != HN_VIEW_MODE_EDGE_MAP)
190190
_ViewMode = HN_VIEW_MODE_SHADED;
191191

192192
if (ViewMode != _ViewMode)
@@ -213,8 +213,11 @@ void HnPostProcessTask::PostProcessingTechnique::PreparePSO(TEXTURE_FORMAT RTVFo
213213
ShaderMacroHelper Macros;
214214
Macros.Add("CONVERT_OUTPUT_TO_SRGB", ConvertOutputToSRGB);
215215
Macros.Add("TONE_MAPPING_MODE", ToneMappingMode);
216+
216217
Macros.Add("VIEW_MODE_SCENE_DEPTH", static_cast<int>(HN_VIEW_MODE_SCENE_DEPTH));
218+
Macros.Add("VIEW_MODE_EDGE_MAP", static_cast<int>(HN_VIEW_MODE_EDGE_MAP));
217219
Macros.Add("VIEW_MODE", static_cast<int>(ViewMode));
220+
218221
if (GridFeatureFlags != CoordinateGridRenderer::FEATURE_FLAG_NONE)
219222
{
220223
Macros.Add("ENABLE_GRID", 1);

0 commit comments

Comments
 (0)