1
2 Shader "Hidden/FastBlur" {
3 Properties {
4 _MainTex ("Base (RGB)", 2D) = "white" {}
5 _Bloom ("Bloom (RGB)", 2D) = "black" {}
6 }
7
8 CGINCLUDE
9
10 #include "UnityCG.cginc"
11
12 sampler2D _MainTex;
13 sampler2D _Bloom;
14
15 uniform half4 _MainTex_TexelSize;
16 uniform half4 _Parameter;
17
18 struct v2f_tap
19 {
20 float4 pos : SV_POSITION;
21 half2 uv20 : TEXCOORD0;
22 half2 uv21 : TEXCOORD1;
23 half2 uv22 : TEXCOORD2;
24 half2 uv23 : TEXCOORD3;
25 };
26
27 v2f_tap vert4Tap ( appdata_img v )
28 {
29 v2f_tap o;
30
31 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
32 o.uv20 = v.texcoord + _MainTex_TexelSize.xy;
33 o.uv21 = v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,-0.5h);
34 o.uv22 = v.texcoord + _MainTex_TexelSize.xy * half2(0.5h,-0.5h);
35 o.uv23 = v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,0.5h);
36
37 return o;
38 }
39
40 fixed4 fragDownsample ( v2f_tap i ) : SV_Target
41 {
42 fixed4 color = tex2D (_MainTex, i.uv20);
43 color += tex2D (_MainTex, i.uv21);
44 color += tex2D (_MainTex, i.uv22);
45 color += tex2D (_MainTex, i.uv23);
46 return color / 4;
47 }
48
49 // weight curves
50
51 static const half curve[7] = { 0.0205, 0.0855, 0.232, 0.324, 0.232, 0.0855, 0.0205 }; // gauss'ish blur weights
52
53 static const half4 curve4[7] = { half4(0.0205,0.0205,0.0205,0), half4(0.0855,0.0855,0.0855,0), half4(0.232,0.232,0.232,0),
54 half4(0.324,0.324,0.324,1), half4(0.232,0.232,0.232,0), half4(0.0855,0.0855,0.0855,0), half4(0.0205,0.0205,0.0205,0) };
55
56 struct v2f_withBlurCoords8
57 {
58 float4 pos : SV_POSITION;
59 half4 uv : TEXCOORD0;
60 half2 offs : TEXCOORD1;
61 };
62
63 struct v2f_withBlurCoordsSGX
64 {
65 float4 pos : SV_POSITION;
66 half2 uv : TEXCOORD0;
67 half4 offs[3] : TEXCOORD1;
68 };
69
70 v2f_withBlurCoords8 vertBlurHorizontal (appdata_img v)
71 {
72 v2f_withBlurCoords8 o;
73 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
74
75 o.uv = half4(v.texcoord.xy,1,1);
76 o.offs = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
77
78 return o;
79 }
80
81 v2f_withBlurCoords8 vertBlurVertical (appdata_img v)
82 {
83 v2f_withBlurCoords8 o;
84 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
85
86 o.uv = half4(v.texcoord.xy,1,1);
87 o.offs = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
88
89 return o;
90 }
91
92 half4 fragBlur8 ( v2f_withBlurCoords8 i ) : SV_Target
93 {
94 half2 uv = i.uv.xy;
95 half2 netFilterWidth = i.offs;
96 half2 coords = uv - netFilterWidth * 3.0;
97
98 half4 color = 0;
99 for( int l = 0; l < 7; l++ )
100 {
101 half4 tap = tex2D(_MainTex, coords);
102 color += tap * curve4[l];
103 coords += netFilterWidth;
104 }
105 return color;
106 }
107
108
109 v2f_withBlurCoordsSGX vertBlurHorizontalSGX (appdata_img v)
110 {
111 v2f_withBlurCoordsSGX o;
112 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
113
114 o.uv = v.texcoord.xy;
115 half2 netFilterWidth = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
116 half4 coords = -netFilterWidth.xyxy * 3.0;
117
118 o.offs[0] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
119 coords += netFilterWidth.xyxy;
120 o.offs[1] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
121 coords += netFilterWidth.xyxy;
122 o.offs[2] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
123
124 return o;
125 }
126
127 v2f_withBlurCoordsSGX vertBlurVerticalSGX (appdata_img v)
128 {
129 v2f_withBlurCoordsSGX o;
130 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
131
132 o.uv = half4(v.texcoord.xy,1,1);
133 half2 netFilterWidth = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
134 half4 coords = -netFilterWidth.xyxy * 3.0;
135
136 o.offs[0] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
137 coords += netFilterWidth.xyxy;
138 o.offs[1] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
139 coords += netFilterWidth.xyxy;
140 o.offs[2] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
141
142 return o;
143 }
144
145 half4 fragBlurSGX ( v2f_withBlurCoordsSGX i ) : SV_Target
146 {
147 half2 uv = i.uv.xy;
148
149 half4 color = tex2D(_MainTex, i.uv) * curve4[3];
150
151 for( int l = 0; l < 3; l++ )
152 {
153 half4 tapA = tex2D(_MainTex, i.offs[l].xy);
154 half4 tapB = tex2D(_MainTex, i.offs[l].zw);
155 color += (tapA + tapB) * curve4[l];
156 }
157
158 return color;
159
160 }
161
162 ENDCG
163
164 SubShader {
165 ZTest Off Cull Off ZWrite Off Blend Off
166
167 // 0
168 Pass {
169
170 CGPROGRAM
171
172 #pragma vertex vert4Tap
173 #pragma fragment fragDownsample
174
175 ENDCG
176
177 }
178
179 // 1
180 Pass {
181 ZTest Always
182 Cull Off
183
184 CGPROGRAM
185
186 #pragma vertex vertBlurVertical
187 #pragma fragment fragBlur8
188
189 ENDCG
190 }
191
192 // 2
193 Pass {
194 ZTest Always
195 Cull Off
196
197 CGPROGRAM
198
199 #pragma vertex vertBlurHorizontal
200 #pragma fragment fragBlur8
201
202 ENDCG
203 }
204
205 // alternate blur
206 // 3
207 Pass {
208 ZTest Always
209 Cull Off
210
211 CGPROGRAM
212
213 #pragma vertex vertBlurVerticalSGX
214 #pragma fragment fragBlurSGX
215
216 ENDCG
217 }
218
219 // 4
220 Pass {
221 ZTest Always
222 Cull Off
223
224 CGPROGRAM
225
226 #pragma vertex vertBlurHorizontalSGX
227 #pragma fragment fragBlurSGX
228
229 ENDCG
230 }
231 }
232
233 FallBack Off
234 }