1 Shader "Hidden/BlurAndFlares" {
2 Properties {
3 _MainTex ("Base (RGB)", 2D) = "" {}
4 _NonBlurredTex ("Base (RGB)", 2D) = "" {}
5 }
6
7 CGINCLUDE
8
9 #include "UnityCG.cginc"
10
11 struct v2f {
12 half4 pos : SV_POSITION;
13 half2 uv : TEXCOORD0;
14 };
15
16 struct v2f_opts {
17 half4 pos : SV_POSITION;
18 half2 uv[7] : TEXCOORD0;
19 };
20
21 struct v2f_blur {
22 half4 pos : SV_POSITION;
23 half2 uv : TEXCOORD0;
24 half4 uv01 : TEXCOORD1;
25 half4 uv23 : TEXCOORD2;
26 half4 uv45 : TEXCOORD3;
27 half4 uv67 : TEXCOORD4;
28 };
29
30 half4 _Offsets;
31 half4 _TintColor;
32
33 half _StretchWidth;
34 half2 _Threshhold;
35 half _Saturation;
36
37 half4 _MainTex_TexelSize;
38
39 sampler2D _MainTex;
40 sampler2D _NonBlurredTex;
41
42 v2f vert (appdata_img v) {
43 v2f o;
44 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
45 o.uv = v.texcoord.xy;
46 return o;
47 }
48
49 v2f_blur vertWithMultiCoords2 (appdata_img v) {
50 v2f_blur o;
51 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
52 o.uv.xy = v.texcoord.xy;
53 o.uv01 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1);
54 o.uv23 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 2.0;
55 o.uv45 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 3.0;
56 o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 4.0;
57 o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 5.0;
58 return o;
59 }
60
61 v2f_opts vertStretch (appdata_img v) {
62 v2f_opts o;
63 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
64 half b = _StretchWidth;
65 o.uv[0] = v.texcoord.xy;
66 o.uv[1] = v.texcoord.xy + b * 2.0 * _Offsets.xy;
67 o.uv[2] = v.texcoord.xy - b * 2.0 * _Offsets.xy;
68 o.uv[3] = v.texcoord.xy + b * 4.0 * _Offsets.xy;
69 o.uv[4] = v.texcoord.xy - b * 4.0 * _Offsets.xy;
70 o.uv[5] = v.texcoord.xy + b * 6.0 * _Offsets.xy;
71 o.uv[6] = v.texcoord.xy - b * 6.0 * _Offsets.xy;
72 return o;
73 }
74
75 v2f_opts vertWithMultiCoords (appdata_img v) {
76 v2f_opts o;
77 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
78 o.uv[0] = v.texcoord.xy;
79 o.uv[1] = v.texcoord.xy + 0.5 * _MainTex_TexelSize.xy * _Offsets.xy;
80 o.uv[2] = v.texcoord.xy - 0.5 * _MainTex_TexelSize.xy * _Offsets.xy;
81 o.uv[3] = v.texcoord.xy + 1.5 * _MainTex_TexelSize.xy * _Offsets.xy;
82 o.uv[4] = v.texcoord.xy - 1.5 * _MainTex_TexelSize.xy * _Offsets.xy;
83 o.uv[5] = v.texcoord.xy + 2.5 * _MainTex_TexelSize.xy * _Offsets.xy;
84 o.uv[6] = v.texcoord.xy - 2.5 * _MainTex_TexelSize.xy * _Offsets.xy;
85 return o;
86 }
87
88 half4 fragPostNoBlur (v2f i) : SV_Target {
89 half4 color = tex2D (_MainTex, i.uv);
90 return color * 1.0/(1.0 + Luminance(color.rgb) + 0.5); // this also makes it a little noisy
91 }
92
93 half4 fragGaussBlur (v2f_blur i) : SV_Target {
94 half4 color = half4 (0,0,0,0);
95 color += 0.225 * tex2D (_MainTex, i.uv);
96 color += 0.150 * tex2D (_MainTex, i.uv01.xy);
97 color += 0.150 * tex2D (_MainTex, i.uv01.zw);
98 color += 0.110 * tex2D (_MainTex, i.uv23.xy);
99 color += 0.110 * tex2D (_MainTex, i.uv23.zw);
100 color += 0.075 * tex2D (_MainTex, i.uv45.xy);
101 color += 0.075 * tex2D (_MainTex, i.uv45.zw);
102 color += 0.0525 * tex2D (_MainTex, i.uv67.xy);
103 color += 0.0525 * tex2D (_MainTex, i.uv67.zw);
104 return color;
105 }
106
107 half4 fragPreAndCut (v2f_opts i) : SV_Target {
108 half4 color = tex2D (_MainTex, i.uv[0]);
109 color += tex2D (_MainTex, i.uv[1]);
110 color += tex2D (_MainTex, i.uv[2]);
111 color += tex2D (_MainTex, i.uv[3]);
112 color += tex2D (_MainTex, i.uv[4]);
113 color += tex2D (_MainTex, i.uv[5]);
114 color += tex2D (_MainTex, i.uv[6]);
115 color = max(color / 7.0 - _Threshhold.xxxx, float4(0,0,0,0));
116 half lum = Luminance(color.rgb);
117 color.rgb = lerp(half3(lum,lum,lum), color.rgb, _Saturation) * _TintColor.rgb;
118 return color;
119 }
120
121 half4 fragStretch (v2f_opts i) : SV_Target {
122 half4 color = tex2D (_MainTex, i.uv[0]);
123 color = max (color, tex2D (_MainTex, i.uv[1]));
124 color = max (color, tex2D (_MainTex, i.uv[2]));
125 color = max (color, tex2D (_MainTex, i.uv[3]));
126 color = max (color, tex2D (_MainTex, i.uv[4]));
127 color = max (color, tex2D (_MainTex, i.uv[5]));
128 color = max (color, tex2D (_MainTex, i.uv[6]));
129 return color;
130 }
131
132 half4 fragPost (v2f_opts i) : SV_Target {
133 half4 color = tex2D (_MainTex, i.uv[0]);
134 color += tex2D (_MainTex, i.uv[1]);
135 color += tex2D (_MainTex, i.uv[2]);
136 color += tex2D (_MainTex, i.uv[3]);
137 color += tex2D (_MainTex, i.uv[4]);
138 color += tex2D (_MainTex, i.uv[5]);
139 color += tex2D (_MainTex, i.uv[6]);
140 return color * 1.0/(7.0 + Luminance(color.rgb) + 0.5); // this also makes it a little noisy
141 }
142
143 ENDCG
144
145 Subshader {
146 ZTest Always Cull Off ZWrite Off
147 Pass {
148
149 CGPROGRAM
150
151 #pragma vertex vert
152 #pragma fragment fragPostNoBlur
153
154 ENDCG
155 }
156
157 Pass {
158
159 CGPROGRAM
160
161 #pragma vertex vertStretch
162 #pragma fragment fragStretch
163
164 ENDCG
165 }
166
167 // 2
168 Pass {
169
170 CGPROGRAM
171
172 #pragma vertex vertWithMultiCoords
173 #pragma fragment fragPreAndCut
174
175 ENDCG
176 }
177
178 // 3
179 Pass {
180
181 CGPROGRAM
182
183 #pragma vertex vertWithMultiCoords
184 #pragma fragment fragPost
185
186 ENDCG
187 }
188 // 4
189 Pass {
190
191 CGPROGRAM
192
193 #pragma vertex vertWithMultiCoords2
194 #pragma fragment fragGaussBlur
195
196 ENDCG
197 }
198 }
199
200 Fallback off
201
202 }