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

Commit 5e0e734

Browse files
authored
Merge pull request #53 from keijiro/dof-weight-bias
DoF: Tweaked the prefilter/postfilter to reduce artifacts.
2 parents da16cfb + ff6a9ac commit 5e0e734

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

PostProcessing/Resources/Shaders/Common.cginc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ inline half Min3(half x, half y, half z) { return min(x, min(y, z)); }
5858
inline half Max3(half3 x) { return max(x.x, max(x.y, x.z)); }
5959
inline half Max3(half x, half y, half z) { return max(x, max(y, z)); }
6060

61+
inline half Min4(half4 x) { return min(x.x, min(x.y, min(x.z, x.w))); }
62+
inline half Min4(half x, half y, half z, half w) { return min(x, min(y, min(z, w))); }
63+
64+
inline half Max4(half4 x) { return max(x.x, max(x.y, max(x.z, x.w))); }
65+
inline half Max4(half x, half y, half z, half w) { return max(x, max(y, min(z, w))); }
66+
6167
inline half Pow2(half x) { return x * x; }
6268
inline half2 Pow2(half2 x) { return x * x; }
6369
inline half3 Pow2(half3 x) { return x * x; }

PostProcessing/Resources/Shaders/DepthOfField.cginc

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ PrefilterOutput FragPrefilter(VaryingsDOF i) PrefilterSemantics
113113
avg /= dot(weights, 1.0);
114114

115115
// Output CoC = average of CoCs
116-
half coc = dot(cocs, 0.25);
116+
half cocmin = Min4(cocs);
117+
half cocmax = Max4(cocs);
118+
half coc = -cocmin > cocmax ? cocmin : cocmax;
117119

118120
// Premultiply CoC again.
119121
avg *= smoothstep(0, _MainTex_TexelSize.y * 2, abs(coc));
@@ -194,21 +196,35 @@ half4 FragPostBlur(VaryingsDOF i) : SV_Target
194196
{
195197
// 9-tap tent filter
196198
float4 duv = _MainTex_TexelSize.xyxy * float4(1, 1, -1, 0);
197-
half4 acc;
198199

199-
acc = tex2D(_MainTex, i.uv - duv.xy);
200-
acc += tex2D(_MainTex, i.uv - duv.wy) * 2;
201-
acc += tex2D(_MainTex, i.uv - duv.zy);
200+
half4 c0 = tex2D(_MainTex, i.uv - duv.xy);
201+
half4 c1 = tex2D(_MainTex, i.uv - duv.wy);
202+
half4 c2 = tex2D(_MainTex, i.uv - duv.zy);
202203

203-
acc += tex2D(_MainTex, i.uv + duv.zw) * 2;
204-
acc += tex2D(_MainTex, i.uv ) * 4;
205-
acc += tex2D(_MainTex, i.uv + duv.xw) * 2;
204+
half4 c3 = tex2D(_MainTex, i.uv + duv.zw);
205+
half4 c4 = tex2D(_MainTex, i.uv );
206+
half4 c5 = tex2D(_MainTex, i.uv + duv.xw);
206207

207-
acc += tex2D(_MainTex, i.uv + duv.zy);
208-
acc += tex2D(_MainTex, i.uv + duv.wy) * 2;
209-
acc += tex2D(_MainTex, i.uv + duv.xy);
208+
half4 c6 = tex2D(_MainTex, i.uv + duv.zy);
209+
half4 c7 = tex2D(_MainTex, i.uv + duv.wy);
210+
half4 c8 = tex2D(_MainTex, i.uv + duv.xy);
210211

211-
return acc / 16;
212+
half4 acc = c0 * 1 + c1 * 2 + c2 * 1 +
213+
c3 * 2 + c4 * 4 + c5 * 2 +
214+
c6 * 1 + c7 * 2 + c8 * 1;
215+
216+
half aa =
217+
c0.a * c0.a * 1 + c1.a * c1.a * 2 + c2.a * c2.a * 1 +
218+
c3.a * c3.a * 2 + c4.a * c4.a * 4 + c5.a * c5.a * 2 +
219+
c6.a * c6.a * 1 + c7.a * c7.a * 2 + c8.a * c8.a * 1;
220+
221+
half wb = 1.2;
222+
half a = (wb * acc.a - aa) / (wb * 16 - acc.a);
223+
224+
acc /= 16;
225+
226+
half3 rgb = acc.rgb * (1 + saturate(acc.a - a));
227+
return half4(rgb, a);
212228
}
213229

214230
#endif // __DEPTH_OF_FIELD__

0 commit comments

Comments
 (0)