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

Commit ff6a9ac

Browse files
committed
DoF: Tweaked the prefilter/postfilter to reduce artifacts.
Tweaked the prefilter and postfilter to hide unblurred areas that usually appear on boundaries between focused and unfocused objects. This could be one of the supposed solutions for issue #40 (DoF doesn't handle alpha cutout very well) but possibly adds noticeable dark seams.
1 parent c276434 commit ff6a9ac

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
@@ -87,7 +87,9 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
8787
avg /= dot(weights, 1.0);
8888

8989
// Output CoC = average of CoCs
90-
half coc = dot(cocs, 0.25);
90+
half cocmin = Min4(cocs);
91+
half cocmax = Max4(cocs);
92+
half coc = -cocmin > cocmax ? cocmin : cocmax;
9193

9294
// Premultiply CoC again.
9395
avg *= smoothstep(0, _MainTex_TexelSize.y * 2, abs(coc));
@@ -187,21 +189,35 @@ half4 FragPostBlur(VaryingsDOF i) : SV_Target
187189
{
188190
// 9-tap tent filter
189191
float4 duv = _MainTex_TexelSize.xyxy * float4(1, 1, -1, 0);
190-
half4 acc;
191192

192-
acc = tex2D(_MainTex, i.uv - duv.xy);
193-
acc += tex2D(_MainTex, i.uv - duv.wy) * 2;
194-
acc += tex2D(_MainTex, i.uv - duv.zy);
193+
half4 c0 = tex2D(_MainTex, i.uv - duv.xy);
194+
half4 c1 = tex2D(_MainTex, i.uv - duv.wy);
195+
half4 c2 = tex2D(_MainTex, i.uv - duv.zy);
195196

196-
acc += tex2D(_MainTex, i.uv + duv.zw) * 2;
197-
acc += tex2D(_MainTex, i.uv ) * 4;
198-
acc += tex2D(_MainTex, i.uv + duv.xw) * 2;
197+
half4 c3 = tex2D(_MainTex, i.uv + duv.zw);
198+
half4 c4 = tex2D(_MainTex, i.uv );
199+
half4 c5 = tex2D(_MainTex, i.uv + duv.xw);
199200

200-
acc += tex2D(_MainTex, i.uv + duv.zy);
201-
acc += tex2D(_MainTex, i.uv + duv.wy) * 2;
202-
acc += tex2D(_MainTex, i.uv + duv.xy);
201+
half4 c6 = tex2D(_MainTex, i.uv + duv.zy);
202+
half4 c7 = tex2D(_MainTex, i.uv + duv.wy);
203+
half4 c8 = tex2D(_MainTex, i.uv + duv.xy);
203204

204-
return acc / 16;
205+
half4 acc = c0 * 1 + c1 * 2 + c2 * 1 +
206+
c3 * 2 + c4 * 4 + c5 * 2 +
207+
c6 * 1 + c7 * 2 + c8 * 1;
208+
209+
half aa =
210+
c0.a * c0.a * 1 + c1.a * c1.a * 2 + c2.a * c2.a * 1 +
211+
c3.a * c3.a * 2 + c4.a * c4.a * 4 + c5.a * c5.a * 2 +
212+
c6.a * c6.a * 1 + c7.a * c7.a * 2 + c8.a * c8.a * 1;
213+
214+
half wb = 1.2;
215+
half a = (wb * acc.a - aa) / (wb * 16 - acc.a);
216+
217+
acc /= 16;
218+
219+
half3 rgb = acc.rgb * (1 + saturate(acc.a - a));
220+
return half4(rgb, a);
205221
}
206222

207223
#endif // __DEPTH_OF_FIELD__

0 commit comments

Comments
 (0)