Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit f506ee5

Browse files
committed
DoF, grain improvements, dejittered depth buffer (see description)
First version of DoF. Fixed luminance contribution on grain, it was reversed. Added a hacky depth buffer dejittering process when DoF and TAA are enabled at the same time. Quick code refactor for active effect states.
1 parent d600585 commit f506ee5

30 files changed

Lines changed: 854 additions & 236 deletions

PostProcessing/Editor/Models/AntialiasingModelEditor.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ public class AntialiasingModelEditor : PostProcessingModelEditor
1414
SerializedProperty m_FxaaPreset;
1515

1616
SerializedProperty m_TaaJitterSpread;
17-
SerializedProperty m_TaaSampleCount;
1817
SerializedProperty m_TaaSharpen;
1918
SerializedProperty m_TaaStationaryBlending;
2019
SerializedProperty m_TaaMotionBlending;
21-
SerializedProperty m_TaaQueue;
2220

2321
static string[] s_MethodNames =
2422
{
@@ -33,11 +31,9 @@ public override void OnEnable()
3331
m_FxaaPreset = FindSetting((Settings x) => x.fxaaSettings.preset);
3432

3533
m_TaaJitterSpread = FindSetting((Settings x) => x.taaSettings.jitterSpread);
36-
m_TaaSampleCount = FindSetting((Settings x) => x.taaSettings.sampleCount);
3734
m_TaaSharpen = FindSetting((Settings x) => x.taaSettings.sharpen);
3835
m_TaaStationaryBlending = FindSetting((Settings x) => x.taaSettings.stationaryBlending);
3936
m_TaaMotionBlending = FindSetting((Settings x) => x.taaSettings.motionBlending);
40-
m_TaaQueue = FindSetting((Settings x) => x.taaSettings.renderQueue);
4137
}
4238

4339
public override void OnInspectorGUI()
@@ -56,7 +52,6 @@ public override void OnInspectorGUI()
5652
EditorGUILayout.LabelField("Jitter", EditorStyles.boldLabel);
5753
EditorGUI.indentLevel++;
5854
EditorGUILayout.PropertyField(m_TaaJitterSpread, EditorGUIHelper.GetContent("Spread"));
59-
EditorGUILayout.PropertyField(m_TaaSampleCount);
6055
EditorGUI.indentLevel--;
6156

6257
EditorGUILayout.Space();
@@ -70,7 +65,6 @@ public override void OnInspectorGUI()
7065
EditorGUILayout.Space();
7166

7267
EditorGUILayout.PropertyField(m_TaaSharpen);
73-
EditorGUILayout.PropertyField(m_TaaQueue);
7468
}
7569
}
7670
}

PostProcessing/Editor/Models/DepthOfFieldModelEditor.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@ namespace UnityEditor.PostProcessing
77
[PostProcessingModelEditor(typeof(DepthOfFieldModel))]
88
public class DepthOfFieldModelEditor : PostProcessingModelEditor
99
{
10+
SerializedProperty m_FocusDistance;
11+
SerializedProperty m_Aperture;
12+
SerializedProperty m_FocalLength;
13+
SerializedProperty m_UseCameraFov;
14+
SerializedProperty m_KernelSize;
15+
1016
public override void OnEnable()
1117
{
18+
m_FocusDistance = FindSetting((Settings x) => x.focusDistance);
19+
m_Aperture = FindSetting((Settings x) => x.aperture);
20+
m_FocalLength = FindSetting((Settings x) => x.focalLength);
21+
m_UseCameraFov = FindSetting((Settings x) => x.useCameraFov);
22+
m_KernelSize = FindSetting((Settings x) => x.kernelSize);
1223
}
1324

1425
public override void OnInspectorGUI()
1526
{
16-
EditorGUILayout.HelpBox("Work in progress.", MessageType.Warning);
27+
EditorGUILayout.PropertyField(m_FocusDistance);
28+
EditorGUILayout.PropertyField(m_Aperture, EditorGUIHelper.GetContent("Aperture (f-stop)"));
29+
30+
EditorGUILayout.PropertyField(m_UseCameraFov, EditorGUIHelper.GetContent("Use Camera FOV"));
31+
if (!m_UseCameraFov.boolValue)
32+
EditorGUILayout.PropertyField(m_FocalLength, EditorGUIHelper.GetContent("Focal Length (mm)"));
33+
34+
EditorGUILayout.PropertyField(m_KernelSize);
1735
}
1836
}
1937
}

PostProcessing/Editor/Models/MotionBlurModelEditor.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class MotionBlurModelEditor : PostProcessingModelEditor
1010
{
1111
SerializedProperty m_ShutterAngle;
1212
SerializedProperty m_SampleCount;
13-
SerializedProperty m_MaxBlurRadius;
1413
SerializedProperty m_FrameBlending;
1514

1615
GraphDrawer m_GraphDrawer;
@@ -165,7 +164,6 @@ public override void OnEnable()
165164
{
166165
m_ShutterAngle = FindSetting((Settings x) => x.shutterAngle);
167166
m_SampleCount = FindSetting((Settings x) => x.sampleCount);
168-
m_MaxBlurRadius = FindSetting((Settings x) => x.maxBlurRadius);
169167
m_FrameBlending = FindSetting((Settings x) => x.frameBlending);
170168
}
171169

@@ -179,7 +177,6 @@ public override void OnInspectorGUI()
179177
m_GraphDrawer.DrawShutterGraph(m_ShutterAngle.floatValue);
180178
EditorGUILayout.PropertyField(m_ShutterAngle);
181179
EditorGUILayout.PropertyField(m_SampleCount);
182-
EditorGUILayout.PropertyField(m_MaxBlurRadius);
183180
EditorGUI.indentLevel--;
184181
EditorGUILayout.Space();
185182

PostProcessing/Resources/Shaders/Common.cginc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ inline half4 SafeHDR(half4 c) { return min(c, HALF_MAX); }
9090
#if (SHADER_TARGET < 50 && !defined(SHADER_API_PSSL))
9191
float rcp(float value)
9292
{
93-
return 1. / value;
93+
return 1.0 / value;
9494
}
9595
#endif
9696

PostProcessing/Resources/Shaders/DepthOfField.cginc

Lines changed: 149 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,157 @@
33

44
#include "UnityCG.cginc"
55
#include "Common.cginc"
6+
#include "DiskKernels.cginc"
67

7-
half4 Frag(VaryingsDefault i) : SV_Target
8+
#pragma target 3.0
9+
10+
sampler2D_float _CameraDepthTexture;
11+
sampler2D_float _DejitteredDepth;
12+
13+
// Camera parameters
14+
float _Distance;
15+
float _LensCoeff; // f^2 / (N * (S1 - f) * film_width * 2)
16+
float _MaxCoC;
17+
float _RcpMaxCoC;
18+
float _RcpAspect;
19+
20+
struct VaryingsDOF
21+
{
22+
float4 pos : SV_POSITION;
23+
half2 uv : TEXCOORD0;
24+
half2 uvAlt : TEXCOORD1;
25+
};
26+
27+
// Common vertex shader with single pass stereo rendering support
28+
VaryingsDOF VertDOF(AttributesDefault v)
29+
{
30+
half2 uvAlt = v.texcoord;
31+
#if UNITY_UV_STARTS_AT_TOP
32+
if (_MainTex_TexelSize.y < 0.0) uvAlt.y = 1.0 - uvAlt.y;
33+
#endif
34+
35+
VaryingsDOF o;
36+
#if defined(UNITY_SINGLE_PASS_STEREO)
37+
o.pos = UnityObjectToClipPos(v.vertex);
38+
o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord, _MainTex_ST);
39+
o.uvAlt = UnityStereoScreenSpaceUVAdjust(uvAlt, _MainTex_ST);
40+
#else
41+
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
42+
o.uv = v.texcoord;
43+
o.uvAlt = uvAlt;
44+
#endif
45+
46+
return o;
47+
}
48+
49+
// Downsampling, prefiltering and CoC calculation
50+
half4 FragPrefilter(VaryingsDOF i) : SV_Target
851
{
9-
return tex2D(_MainTex, i.uv);
52+
float3 duv = _MainTex_TexelSize.xyx * float3(0.5, 0.5, -0.5);
53+
54+
// Sample source colors.
55+
half3 c0 = tex2D(_MainTex, i.uv - duv.xy).rgb;
56+
half3 c1 = tex2D(_MainTex, i.uv - duv.zy).rgb;
57+
half3 c2 = tex2D(_MainTex, i.uv + duv.zy).rgb;
58+
half3 c3 = tex2D(_MainTex, i.uv + duv.xy).rgb;
59+
60+
// Sample linear depths.
61+
#if DEJITTER_DEPTH
62+
float d0 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt - duv.xy).x);
63+
float d1 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt - duv.zy).x);
64+
float d2 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt + duv.zy).x);
65+
float d3 = LinearEyeDepth(tex2D(_DejitteredDepth, i.uvAlt + duv.xy).x);
66+
#else
67+
float d0 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt - duv.xy));
68+
float d1 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt - duv.zy));
69+
float d2 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt + duv.zy));
70+
float d3 = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uvAlt + duv.xy));
71+
#endif
72+
73+
float4 depths = float4(d0, d1, d2, d3);
74+
75+
// Calculate the radiuses of CoCs at these sample points.
76+
float4 cocs = (depths - _Distance) * _LensCoeff / depths;
77+
cocs = clamp(cocs, -_MaxCoC, _MaxCoC);
78+
79+
// Premultiply CoC to reduce background bleeding.
80+
float4 weights = saturate(abs(cocs) * _RcpMaxCoC);
81+
82+
#if defined(PREFILTER_LUMA_WEIGHT)
83+
// Apply luma weights to reduce flickering.
84+
// References:
85+
// http://gpuopen.com/optimized-reversible-tonemapper-for-resolve/
86+
// http://graphicrants.blogspot.fr/2013/12/tone-mapping.html
87+
weights.x *= 1.0 / (Max3(c0) + 1.0);
88+
weights.y *= 1.0 / (Max3(c1) + 1.0);
89+
weights.z *= 1.0 / (Max3(c2) + 1.0);
90+
weights.w *= 1.0 / (Max3(c3) + 1.0);
91+
#endif
92+
93+
// Weighted average of the color samples
94+
half3 avg = c0 * weights.x + c1 * weights.y + c2 * weights.z + c3 * weights.w;
95+
avg /= dot(weights, 1.0);
96+
97+
// Output CoC = average of CoCs
98+
half coc = dot(cocs, 0.25);
99+
100+
#if defined(UNITY_COLORSPACE_GAMMA)
101+
avg = GammaToLinearSpace(avg);
102+
#endif
103+
104+
return half4(avg, coc);
105+
}
106+
107+
// Bokeh filter with disk-shaped kernels
108+
half4 FragBlur(VaryingsDOF i) : SV_Target
109+
{
110+
half4 samp0 = tex2D(_MainTex, i.uv);
111+
112+
half4 bgAcc = 0.0; // Background: far field bokeh
113+
half4 fgAcc = 0.0; // Foreground: near field bokeh
114+
115+
UNITY_LOOP for (int si = 0; si < kSampleCount; si++)
116+
{
117+
float2 disp = kDiskKernel[si] * _MaxCoC;
118+
float dist = length(disp);
119+
120+
float2 duv = float2(disp.x * _RcpAspect, disp.y);
121+
half4 samp = tex2D(_MainTex, i.uv + duv);
122+
123+
// BG: Compare CoC of the current sample and the center sample
124+
// and select smaller one.
125+
half bgCoC = max(min(samp0.a, samp.a), 0.0);
126+
127+
// Compare the CoC to the sample distance.
128+
// Add a small margin to smooth out.
129+
half bgWeight = saturate((bgCoC - dist + 0.005) / 0.01);
130+
half fgWeight = saturate((-samp.a - dist + 0.005) / 0.01);
131+
132+
// Accumulation
133+
bgAcc += half4(samp.rgb, 1.0) * bgWeight;
134+
fgAcc += half4(samp.rgb, 1.0) * fgWeight;
135+
}
136+
137+
// Get the weighted average.
138+
bgAcc.rgb /= bgAcc.a + (bgAcc.a == 0.0); // zero-div guard
139+
fgAcc.rgb /= fgAcc.a + (fgAcc.a == 0.0);
140+
141+
// BG: Calculate the alpha value only based on the center CoC.
142+
// This is a rather aggressive approximation but provides stable results.
143+
bgAcc.a = smoothstep(_MainTex_TexelSize.y, _MainTex_TexelSize.y * 2.0, samp0.a);
144+
145+
// FG: Normalize the total of the weights.
146+
fgAcc.a *= UNITY_PI / kSampleCount;
147+
148+
// Alpha premultiplying
149+
half3 rgb = 0.0;
150+
rgb = lerp(rgb, bgAcc.rgb, saturate(bgAcc.a));
151+
rgb = lerp(rgb, fgAcc.rgb, saturate(fgAcc.a));
152+
153+
// Combined alpha value
154+
half alpha = (1.0 - saturate(bgAcc.a)) * (1.0 - saturate(fgAcc.a));
155+
156+
return half4(rgb, alpha);
10157
}
11158

12159
#endif // __DEPTH_OF_FIELD__

PostProcessing/Resources/Shaders/DepthOfField.shader

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,55 @@ Shader "Hidden/Post FX/Depth Of Field"
99
{
1010
Cull Off ZWrite Off ZTest Always
1111

12+
// (0) Downsampling, prefiltering & CoC
1213
Pass
1314
{
1415
CGPROGRAM
15-
#pragma vertex VertDefault
16-
#pragma fragment Frag
16+
#pragma vertex VertDOF
17+
#pragma fragment FragPrefilter
1718
#pragma target 3.0
19+
#pragma multi_compile __ DEJITTER_DEPTH
20+
#include "DepthOfField.cginc"
21+
ENDCG
22+
}
23+
24+
// (1-4) Bokeh filter with disk-shaped kernels
25+
Pass
26+
{
27+
CGPROGRAM
28+
#pragma vertex VertDOF
29+
#pragma fragment FragBlur
30+
#define KERNEL_SMALL
31+
#include "DepthOfField.cginc"
32+
ENDCG
33+
}
34+
35+
Pass
36+
{
37+
CGPROGRAM
38+
#pragma vertex VertDOF
39+
#pragma fragment FragBlur
40+
#define KERNEL_MEDIUM
41+
#include "DepthOfField.cginc"
42+
ENDCG
43+
}
44+
45+
Pass
46+
{
47+
CGPROGRAM
48+
#pragma vertex VertDOF
49+
#pragma fragment FragBlur
50+
#define KERNEL_LARGE
51+
#include "DepthOfField.cginc"
52+
ENDCG
53+
}
54+
55+
Pass
56+
{
57+
CGPROGRAM
58+
#pragma vertex VertDOF
59+
#pragma fragment FragBlur
60+
#define KERNEL_VERYLARGE
1861
#include "DepthOfField.cginc"
1962
ENDCG
2063
}

0 commit comments

Comments
 (0)