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

Commit b426406

Browse files
committed
Merge remote-tracking branch 'refs/remotes/Unity-Technologies/master'
2 parents 82f0ab9 + b9d3a97 commit b426406

File tree

6 files changed

+155
-129
lines changed

6 files changed

+155
-129
lines changed

PostProcessing/Resources/Shaders/DepthOfField.cginc

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ half4 FragPrefilter(VaryingsDOF i) : SV_Target
8989
// Output CoC = average of CoCs
9090
half coc = dot(cocs, 0.25);
9191

92+
// Premultiply CoC again.
93+
avg *= smoothstep(0, _MainTex_TexelSize.y * 2, abs(coc));
94+
9295
#if defined(UNITY_COLORSPACE_GAMMA)
9396
avg = GammaToLinearSpace(avg);
9497
#endif
@@ -144,8 +147,13 @@ half4 FragBlur(VaryingsDOF i) : SV_Target
144147

145148
// Compare the CoC to the sample distance.
146149
// Add a small margin to smooth out.
147-
half bgWeight = saturate((bgCoC - dist + 0.005) / 0.01);
148-
half fgWeight = saturate((-samp.a - dist + 0.005) / 0.01);
150+
const half margin = _MainTex_TexelSize.y * 2;
151+
half bgWeight = saturate((bgCoC - dist + margin) / margin);
152+
half fgWeight = saturate((-samp.a - dist + margin) / margin);
153+
154+
// Cut influence from focused areas because they're darkened by CoC
155+
// premultiplying. This is only needed for near field.
156+
fgWeight *= step(_MainTex_TexelSize.y, -samp.a);
149157

150158
// Accumulation
151159
bgAcc += half4(samp.rgb, 1.0) * bgWeight;
@@ -174,4 +182,26 @@ half4 FragBlur(VaryingsDOF i) : SV_Target
174182
return half4(rgb, alpha);
175183
}
176184

185+
// Postfilter blur
186+
half4 FragPostBlur(VaryingsDOF i) : SV_Target
187+
{
188+
// 9-tap tent filter
189+
float4 duv = _MainTex_TexelSize.xyxy * float4(1, 1, -1, 0);
190+
half4 acc;
191+
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);
195+
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;
199+
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);
203+
204+
return acc / 16;
205+
}
206+
177207
#endif // __DEPTH_OF_FIELD__

PostProcessing/Resources/Shaders/DepthOfField.shader

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ Shader "Hidden/Post FX/Depth Of Field"
8585
#include "DepthOfField.cginc"
8686
ENDCG
8787
}
88+
89+
// (7) Postfilter blur
90+
Pass
91+
{
92+
CGPROGRAM
93+
#pragma vertex VertDOF
94+
#pragma fragment FragPostBlur
95+
#include "DepthOfField.cginc"
96+
ENDCG
97+
}
8898
}
8999

90100
FallBack Off

PostProcessing/Resources/Shaders/DiskKernels.cginc

Lines changed: 102 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ static const float2 kDiskKernel[1] = { float2(0, 0) };
1111

1212
#if defined(KERNEL_SMALL)
1313

14-
// rings = 3
14+
// rings = 2
1515
// points per ring = 5
1616
static const int kSampleCount = 16;
1717
static const float2 kDiskKernel[kSampleCount] = {
1818
float2(0,0),
19-
float2(0.375,0),
20-
float2(0.11588137,0.3566462),
21-
float2(-0.30338138,0.22041944),
22-
float2(-0.30338135,-0.22041951),
23-
float2(0.11588142,-0.35664618),
24-
float2(0.6875,0),
25-
float2(0.5561992,0.40410236),
26-
float2(0.21244916,0.6538514),
27-
float2(-0.21244921,0.65385133),
28-
float2(-0.55619925,0.40410233),
29-
float2(-0.6875,-0.00000006010316),
30-
float2(-0.55619913,-0.40410244),
31-
float2(-0.21244894,-0.6538514),
32-
float2(0.21244927,-0.65385133),
33-
float2(0.55619913,-0.40410239),
19+
float2(0.54545456,0),
20+
float2(0.16855472,0.5187581),
21+
float2(-0.44128203,0.3206101),
22+
float2(-0.44128197,-0.3206102),
23+
float2(0.1685548,-0.5187581),
24+
float2(1,0),
25+
float2(0.809017,0.58778524),
26+
float2(0.30901697,0.95105654),
27+
float2(-0.30901703,0.9510565),
28+
float2(-0.80901706,0.5877852),
29+
float2(-1,0),
30+
float2(-0.80901694,-0.58778536),
31+
float2(-0.30901664,-0.9510566),
32+
float2(0.30901712,-0.9510565),
33+
float2(0.80901694,-0.5877853),
3434
};
3535

3636
#endif
@@ -40,6 +40,38 @@ static const float2 kDiskKernel[kSampleCount] = {
4040
// rings = 3
4141
// points per ring = 7
4242
static const int kSampleCount = 22;
43+
static const float2 kDiskKernel[kSampleCount] = {
44+
float2(0,0),
45+
float2(0.53333336,0),
46+
float2(0.3325279,0.4169768),
47+
float2(-0.11867785,0.5199616),
48+
float2(-0.48051673,0.2314047),
49+
float2(-0.48051673,-0.23140468),
50+
float2(-0.11867763,-0.51996166),
51+
float2(0.33252785,-0.4169769),
52+
float2(1,0),
53+
float2(0.90096885,0.43388376),
54+
float2(0.6234898,0.7818315),
55+
float2(0.22252098,0.9749279),
56+
float2(-0.22252095,0.9749279),
57+
float2(-0.62349,0.7818314),
58+
float2(-0.90096885,0.43388382),
59+
float2(-1,0),
60+
float2(-0.90096885,-0.43388376),
61+
float2(-0.6234896,-0.7818316),
62+
float2(-0.22252055,-0.974928),
63+
float2(0.2225215,-0.9749278),
64+
float2(0.6234897,-0.7818316),
65+
float2(0.90096885,-0.43388376),
66+
};
67+
68+
#endif
69+
70+
#if defined(KERNEL_LARGE)
71+
72+
// rings = 4
73+
// points per ring = 7
74+
static const int kSampleCount = 43;
4375
static const float2 kDiskKernel[kSampleCount] = {
4476
float2(0,0),
4577
float2(0.36363637,0),
@@ -56,22 +88,43 @@ static const float2 kDiskKernel[kSampleCount] = {
5688
float2(-0.15171883,0.6647236),
5789
float2(-0.4251068,0.53306687),
5890
float2(-0.614297,0.29582986),
59-
float2(-0.6818182,-0.00000005960644),
91+
float2(-0.6818182,0),
6092
float2(-0.614297,-0.29582983),
6193
float2(-0.42510656,-0.53306705),
6294
float2(-0.15171856,-0.66472363),
6395
float2(0.1517192,-0.6647235),
6496
float2(0.4251066,-0.53306705),
6597
float2(0.614297,-0.29582983),
98+
float2(1,0),
99+
float2(0.9555728,0.2947552),
100+
float2(0.82623875,0.5633201),
101+
float2(0.6234898,0.7818315),
102+
float2(0.36534098,0.93087375),
103+
float2(0.07473,0.9972038),
104+
float2(-0.22252095,0.9749279),
105+
float2(-0.50000006,0.8660254),
106+
float2(-0.73305196,0.6801727),
107+
float2(-0.90096885,0.43388382),
108+
float2(-0.98883086,0.14904208),
109+
float2(-0.9888308,-0.14904249),
110+
float2(-0.90096885,-0.43388376),
111+
float2(-0.73305184,-0.6801728),
112+
float2(-0.4999999,-0.86602545),
113+
float2(-0.222521,-0.9749279),
114+
float2(0.07473029,-0.99720377),
115+
float2(0.36534148,-0.9308736),
116+
float2(0.6234897,-0.7818316),
117+
float2(0.8262388,-0.56332),
118+
float2(0.9555729,-0.29475483),
66119
};
67120

68121
#endif
69122

70-
#if defined(KERNEL_LARGE)
123+
#if defined(KERNEL_VERYLARGE)
71124

72-
// rings = 4
125+
// rings = 5
73126
// points per ring = 7
74-
static const int kSampleCount = 43;
127+
static const int kSampleCount = 71;
75128
static const float2 kDiskKernel[kSampleCount] = {
76129
float2(0,0),
77130
float2(0.2758621,0),
@@ -88,7 +141,7 @@ static const float2 kDiskKernel[kSampleCount] = {
88141
float2(-0.11509704,0.50427306),
89142
float2(-0.3224948,0.40439552),
90143
float2(-0.46601835,0.22442265),
91-
float2(-0.51724136,-0.000000045218677),
144+
float2(-0.51724136,0),
92145
float2(-0.46601835,-0.22442262),
93146
float2(-0.32249463,-0.40439564),
94147
float2(-0.11509683,-0.5042731),
@@ -116,87 +169,34 @@ static const float2 kDiskKernel[kSampleCount] = {
116169
float2(0.47299215,-0.59311366),
117170
float2(0.62680185,-0.4273462),
118171
float2(0.72491735,-0.22360711),
119-
};
120-
121-
#endif
122-
123-
#if defined(KERNEL_VERYLARGE)
124-
125-
// rings = 5
126-
// points per ring = 7
127-
static const int kSampleCount = 71;
128-
static const float2 kDiskKernel[kSampleCount] = {
129-
float2(0,0),
130-
float2(0.22222224,0),
131-
float2(0.13855329,0.17374034),
132-
float2(-0.049449105,0.21665066),
133-
float2(-0.20021531,0.096418634),
134-
float2(-0.20021531,-0.09641862),
135-
float2(-0.049449015,-0.2166507),
136-
float2(0.13855328,-0.17374037),
137-
float2(0.41666666,0),
138-
float2(0.37540367,0.1807849),
139-
float2(0.2597874,0.3257631),
140-
float2(0.092717074,0.40621996),
141-
float2(-0.09271706,0.40621996),
142-
float2(-0.25978747,0.32576308),
143-
float2(-0.37540367,0.18078493),
144-
float2(-0.41666666,-0.000000036426155),
145-
float2(-0.37540367,-0.1807849),
146-
float2(-0.25978732,-0.32576317),
147-
float2(-0.092716895,-0.40622),
148-
float2(0.09271729,-0.4062199),
149-
float2(0.25978735,-0.32576317),
150-
float2(0.37540367,-0.1807849),
151-
float2(0.6111111,0),
152-
float2(0.5839611,0.18012817),
153-
float2(0.5049237,0.34425116),
154-
float2(0.38102153,0.47778592),
155-
float2(0.22326393,0.56886727),
156-
float2(0.045668334,0.60940236),
157-
float2(-0.13598502,0.59578925),
158-
float2(-0.30555558,0.52923775),
159-
float2(-0.4479762,0.41566107),
160-
float2(-0.55059206,0.26515123),
161-
float2(-0.60428554,0.09108128),
162-
float2(-0.6042855,-0.09108152),
163-
float2(-0.55059206,-0.26515117),
164-
float2(-0.4479761,-0.41566116),
165-
float2(-0.3055555,-0.52923775),
166-
float2(-0.13598506,-0.59578925),
167-
float2(0.045668513,-0.6094023),
168-
float2(0.22326423,-0.5688672),
169-
float2(0.38102147,-0.47778597),
170-
float2(0.5049237,-0.3442511),
171-
float2(0.5839612,-0.18012795),
172-
float2(0.8055556,0),
173-
float2(0.7853586,0.17925298),
174-
float2(0.7257805,0.3495175),
175-
float2(0.6298087,0.5022557),
176-
float2(0.5022557,0.6298087),
177-
float2(0.34951738,0.72578055),
178-
float2(0.17925301,0.7853586),
179-
float2(-0.000000035211954,0.8055556),
180-
float2(-0.179253,0.7853586),
181-
float2(-0.34951755,0.7257805),
182-
float2(-0.50225586,0.62980866),
183-
float2(-0.6298089,0.5022555),
184-
float2(-0.7257805,0.34951752),
185-
float2(-0.7853586,0.17925298),
186-
float2(-0.8055556,-0.00000007042391),
187-
float2(-0.7853586,-0.17925294),
188-
float2(-0.7257805,-0.3495175),
189-
float2(-0.62980866,-0.5022558),
190-
float2(-0.50225556,-0.62980884),
191-
float2(-0.34951726,-0.7257806),
192-
float2(-0.17925267,-0.7853587),
193-
float2(0.000000393725,-0.8055556),
194-
float2(0.17925343,-0.7853585),
195-
float2(0.34951726,-0.7257806),
196-
float2(0.5022556,-0.62980884),
197-
float2(0.62980866,-0.50225574),
198-
float2(0.7257805,-0.3495175),
199-
float2(0.7853586,-0.17925292),
172+
float2(1,0),
173+
float2(0.9749279,0.22252093),
174+
float2(0.90096885,0.43388376),
175+
float2(0.7818315,0.6234898),
176+
float2(0.6234898,0.7818315),
177+
float2(0.43388364,0.9009689),
178+
float2(0.22252098,0.9749279),
179+
float2(0,1),
180+
float2(-0.22252095,0.9749279),
181+
float2(-0.43388385,0.90096885),
182+
float2(-0.62349,0.7818314),
183+
float2(-0.7818317,0.62348956),
184+
float2(-0.90096885,0.43388382),
185+
float2(-0.9749279,0.22252093),
186+
float2(-1,0),
187+
float2(-0.9749279,-0.22252087),
188+
float2(-0.90096885,-0.43388376),
189+
float2(-0.7818314,-0.6234899),
190+
float2(-0.6234896,-0.7818316),
191+
float2(-0.43388346,-0.900969),
192+
float2(-0.22252055,-0.974928),
193+
float2(0,-1),
194+
float2(0.2225215,-0.9749278),
195+
float2(0.4338835,-0.90096897),
196+
float2(0.6234897,-0.7818316),
197+
float2(0.78183144,-0.62348986),
198+
float2(0.90096885,-0.43388376),
199+
float2(0.9749279,-0.22252086),
200200
};
201201

202202
#endif

PostProcessing/Resources/Shaders/Uber.shader

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,8 @@ Shader "Hidden/Post FX/Uber Shader"
166166
// Depth of field
167167
#if DEPTH_OF_FIELD
168168
{
169-
// 9-tap tent filter
170-
float4 duv = _DepthOfFieldTex_TexelSize.xyxy * float4(1.0, 1.0, -1.0, 0.0);
171-
half4 acc;
172-
173-
acc = tex2D(_DepthOfFieldTex, i.uvFlippedSPR - duv.xy);
174-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR - duv.wy) * 2.0;
175-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR - duv.zy);
176-
177-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR + duv.zw) * 2.0;
178-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR) * 4.0;
179-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR + duv.xw) * 2.0;
180-
181-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR + duv.zy);
182-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR + duv.wy) * 2.0;
183-
acc += tex2D(_DepthOfFieldTex, i.uvFlippedSPR + duv.xy);
184-
185-
acc /= 16.0;
186-
187-
// Composite
188-
color = color * acc.a + acc.rgb * autoExposure;
169+
half4 dof = tex2D(_DepthOfFieldTex, i.uvFlippedSPR);
170+
color = color * dof.a + dof.rgb * autoExposure;
189171
}
190172
#elif DEPTH_OF_FIELD_COC_VIEW
191173
{

PostProcessing/Runtime/Components/DepthOfFieldComponent.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ float CalculateMaxCoCRadius(int screenHeight)
5858
{
5959
// Estimate the allowable maximum radius of CoC from the kernel
6060
// size (the equation below was empirically derived).
61-
float radiusInPixels = (float)model.settings.kernelSize * 4f + 10f;
61+
float radiusInPixels = (float)model.settings.kernelSize * 4f + 6f;
6262

6363
// Applying a 5% limit to the CoC radius to keep the size of
6464
// TileMax/NeighborMax small enough.
@@ -127,23 +127,25 @@ public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasC
127127
var rt2 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, RenderTextureFormat.ARGBHalf);
128128
Graphics.Blit(pass, rt2, material, 1 + (int)settings.kernelSize);
129129

130+
// Pass #4 - Postfilter blur
131+
Graphics.Blit(rt2, rt1, material, 7);
132+
130133
if (context.profile.debugViews.IsModeActive(DebugMode.FocusPlane))
131134
{
132-
uberMaterial.SetTexture(Uniforms._DepthOfFieldTex, rt1);
133135
uberMaterial.SetVector(Uniforms._DepthOfFieldParams, new Vector2(s1, coeff));
134136
uberMaterial.EnableKeyword("DEPTH_OF_FIELD_COC_VIEW");
135137
context.Interrupt();
136138
}
137139
else
138140
{
139-
uberMaterial.SetTexture(Uniforms._DepthOfFieldTex, rt2);
141+
uberMaterial.SetTexture(Uniforms._DepthOfFieldTex, rt1);
140142
uberMaterial.EnableKeyword("DEPTH_OF_FIELD");
141143
}
142144

143145
if (antialiasCoC)
144146
context.renderTextureFactory.Release(pass);
145147

146-
context.renderTextureFactory.Release(rt1);
148+
context.renderTextureFactory.Release(rt2);
147149
source.filterMode = FilterMode.Bilinear;
148150
}
149151

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Post-processing Stack (beta)
22

3-
Please read [the documentation](https://github.com/Unity-Technologies/PostProcessing/wiki).
3+
Need help ? Check out the [quick start guide](https://github.com/Unity-Technologies/PostProcessing/wiki) and [the official forums](https://forum.unity3d.com/forums/image-effects.96/) !
4+
5+
Found a bug ? Let us know and [post an issue](https://github.com/Unity-Technologies/PostProcessing/issues) !

0 commit comments

Comments
 (0)