-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathFlatNormalResolution.compute
More file actions
92 lines (76 loc) · 3.63 KB
/
FlatNormalResolution.compute
File metadata and controls
92 lines (76 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal glcore ps5
#pragma kernel ResolveFlatNormals
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/SurfaceCache/Common.hlsl"
Texture2D<float> _ScreenDepths;
RWTexture2D<float3> _ScreenFlatNormals;
float4x4 _ClipToWorldTransform;
[numthreads(8, 8, 1)]
void ResolveFlatNormals(uint2 centerPixelPos : SV_DispatchThreadID)
{
// This is inspired by https://wickedengine.net/2019/09/improved-normal-reconstruction-from-depth/.
// In addition to the center sample, we take 4 depth samples in a cross around the center.
// Of these 4, we pick the best in the horizontal direction and the best in the vertical direction
// and use these to approximate the triangle surface orientation.
uint2 screenSize;
_ScreenDepths.GetDimensions(screenSize.x, screenSize.y);
if (any(screenSize <= centerPixelPos))
return;
const float centerNdcDepth = LoadNdcDepth(_ScreenDepths, centerPixelPos);
if (centerNdcDepth == invalidNdcDepth)
return;
const float2 reciprocalScreenSize = rcp(float2(screenSize));
const float2 centerUvPos = PixelPosToUvPos(centerPixelPos, reciprocalScreenSize);
const float3 centerWorldPos = ComputeWorldSpacePosition(centerUvPos, centerNdcDepth, _ClipToWorldTransform);
bool handednessFlip = false; // Account for handedness and winding order.
float3 ddxPos;
{
const uint2 rightPixelPos = centerPixelPos + int2(1, 0);
const float rightNdcDepth = LoadNdcDepth(_ScreenDepths, rightPixelPos);
const uint2 leftPixelPos = centerPixelPos + int2(-1, 0);
const float leftNdcDepth = LoadNdcDepth(_ScreenDepths, leftPixelPos);
uint2 horizontalPixelPos;
float horizontalNdcDepth;
if (abs(centerNdcDepth - leftNdcDepth) < abs(centerNdcDepth - rightNdcDepth))
{
horizontalPixelPos = leftPixelPos;
horizontalNdcDepth = leftNdcDepth;
handednessFlip = !handednessFlip;
}
else
{
horizontalPixelPos = rightPixelPos;
horizontalNdcDepth = rightNdcDepth;
}
const float2 horizontalUvPos = PixelPosToUvPos(horizontalPixelPos, reciprocalScreenSize);
const float3 horizontalWorldPos = ComputeWorldSpacePosition(horizontalUvPos, horizontalNdcDepth, _ClipToWorldTransform);
ddxPos = horizontalWorldPos - centerWorldPos;
}
float3 ddyPos;
{
const uint2 upPixelPos = centerPixelPos + int2(0, 1);
const float upNdcDepth = LoadNdcDepth(_ScreenDepths, upPixelPos);
const uint2 downPixelPos = centerPixelPos + int2(0, -1);
const float downNdcDepth = LoadNdcDepth(_ScreenDepths, downPixelPos);
uint2 verticalPixelPos;
float verticalNdcDepth;
if (abs(centerNdcDepth - downNdcDepth) < abs(centerNdcDepth - upNdcDepth))
{
verticalPixelPos = downPixelPos;
verticalNdcDepth = downNdcDepth;
}
else
{
verticalPixelPos = upPixelPos;
verticalNdcDepth = upNdcDepth;
handednessFlip = !handednessFlip;
}
const float2 verticalUvPos = PixelPosToUvPos(verticalPixelPos, reciprocalScreenSize);
const float3 verticalWorldPos = ComputeWorldSpacePosition(verticalUvPos, verticalNdcDepth, _ClipToWorldTransform);
ddyPos = verticalWorldPos - centerWorldPos;
}
float3 flatNormal = normalize(cross(ddxPos, ddyPos));
if (handednessFlip)
flatNormal = -flatNormal;
_ScreenFlatNormals[centerPixelPos] = flatNormal;
}