Advertisement
Guest User

MobileBloom

a guest
Feb 2nd, 2020
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2.  
  3.  
  4. Shader "Hidden/FastBloom" {
  5. Properties {
  6. _MainTex ("Base (RGB)", 2D) = "white" {}
  7. _Bloom ("Bloom (RGB)", 2D) = "black" {}
  8. }
  9.  
  10. CGINCLUDE
  11.  
  12. #include "UnityCG.cginc"
  13.  
  14. sampler2D _MainTex;
  15. sampler2D _Bloom;
  16.  
  17. uniform half4 _MainTex_TexelSize;
  18.  
  19. uniform half4 _Parameter;
  20. uniform half4 _OffsetsA;
  21. uniform half4 _OffsetsB;
  22.  
  23. #define ONE_MINUS_THRESHHOLD_TIMES_INTENSITY _Parameter.w
  24. #define THRESHHOLD _Parameter.z
  25.  
  26. struct v2f_simple
  27. {
  28. float4 pos : SV_POSITION;
  29. half2 uv : TEXCOORD0;
  30.  
  31. #if UNITY_UV_STARTS_AT_TOP
  32. half2 uv2 : TEXCOORD1;
  33. #endif
  34. };
  35.  
  36. v2f_simple vertBloom ( appdata_img v )
  37. {
  38. v2f_simple o;
  39.  
  40. o.pos = UnityObjectToClipPos (v.vertex);
  41. o.uv = v.texcoord;
  42.  
  43. #if UNITY_UV_STARTS_AT_TOP
  44. o.uv2 = v.texcoord;
  45. if (_MainTex_TexelSize.y < 0.0)
  46. o.uv.y = 1.0 - o.uv.y;
  47. #endif
  48.  
  49. return o;
  50. }
  51.  
  52. struct v2f_tap
  53. {
  54. float4 pos : SV_POSITION;
  55. half2 uv20 : TEXCOORD0;
  56. half2 uv21 : TEXCOORD1;
  57. half2 uv22 : TEXCOORD2;
  58. half2 uv23 : TEXCOORD3;
  59. };
  60.  
  61. v2f_tap vert4Tap ( appdata_img v )
  62. {
  63. v2f_tap o;
  64.  
  65. o.pos = UnityObjectToClipPos (v.vertex);
  66. o.uv20 = v.texcoord + _MainTex_TexelSize.xy;
  67. o.uv21 = v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,-0.5h);
  68. o.uv22 = v.texcoord + _MainTex_TexelSize.xy * half2(0.5h,-0.5h);
  69. o.uv23 = v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,0.5h);
  70.  
  71. return o;
  72. }
  73.  
  74. fixed4 fragBloom ( v2f_simple i ) : SV_Target
  75. {
  76. #if UNITY_UV_STARTS_AT_TOP
  77.  
  78. fixed4 color = tex2D(_MainTex, i.uv2);
  79. return color + tex2D(_Bloom, i.uv);
  80.  
  81. #else
  82.  
  83. fixed4 color = tex2D(_MainTex, i.uv);
  84. return color + tex2D(_Bloom, i.uv);
  85.  
  86. #endif
  87. }
  88.  
  89. fixed4 fragDownsample ( v2f_tap i ) : SV_Target
  90. {
  91. fixed4 color = tex2D (_MainTex, i.uv20);
  92. color += tex2D (_MainTex, i.uv21);
  93. color += tex2D (_MainTex, i.uv22);
  94. color += tex2D (_MainTex, i.uv23);
  95. return max(color/4 - THRESHHOLD, 0) * ONE_MINUS_THRESHHOLD_TIMES_INTENSITY;
  96. }
  97.  
  98. // weight curves
  99.  
  100. static const half curve[7] = { 0.0205, 0.0855, 0.232, 0.324, 0.232, 0.0855, 0.0205 }; // gauss'ish blur weights
  101.  
  102. 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),
  103. 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) };
  104.  
  105. struct v2f_withBlurCoords8
  106. {
  107. float4 pos : SV_POSITION;
  108. half4 uv : TEXCOORD0;
  109. half2 offs : TEXCOORD1;
  110. };
  111.  
  112. struct v2f_withBlurCoordsSGX
  113. {
  114. float4 pos : SV_POSITION;
  115. half2 uv : TEXCOORD0;
  116. half4 offs[3] : TEXCOORD1;
  117. };
  118.  
  119. v2f_withBlurCoords8 vertBlurHorizontal (appdata_img v)
  120. {
  121. v2f_withBlurCoords8 o;
  122. o.pos = UnityObjectToClipPos (v.vertex);
  123.  
  124. o.uv = half4(v.texcoord.xy,1,1);
  125. o.offs = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
  126.  
  127. return o;
  128. }
  129.  
  130. v2f_withBlurCoords8 vertBlurVertical (appdata_img v)
  131. {
  132. v2f_withBlurCoords8 o;
  133. o.pos = UnityObjectToClipPos (v.vertex);
  134.  
  135. o.uv = half4(v.texcoord.xy,1,1);
  136. o.offs = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
  137.  
  138. return o;
  139. }
  140.  
  141. half4 fragBlur8 ( v2f_withBlurCoords8 i ) : SV_Target
  142. {
  143. half2 uv = i.uv.xy;
  144. half2 netFilterWidth = i.offs;
  145. half2 coords = uv - netFilterWidth * 3.0;
  146.  
  147. half4 color = 0;
  148. for( int l = 0; l < 7; l++ )
  149. {
  150. half4 tap = tex2D(_MainTex, coords);
  151. color += tap * curve4[l];
  152. coords += netFilterWidth;
  153. }
  154. return color;
  155. }
  156.  
  157.  
  158. v2f_withBlurCoordsSGX vertBlurHorizontalSGX (appdata_img v)
  159. {
  160. v2f_withBlurCoordsSGX o;
  161. o.pos = UnityObjectToClipPos (v.vertex);
  162.  
  163. o.uv = v.texcoord.xy;
  164. half2 netFilterWidth = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
  165. half4 coords = -netFilterWidth.xyxy * 3.0;
  166.  
  167. o.offs[0] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
  168. coords += netFilterWidth.xyxy;
  169. o.offs[1] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
  170. coords += netFilterWidth.xyxy;
  171. o.offs[2] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
  172.  
  173. return o;
  174. }
  175.  
  176. v2f_withBlurCoordsSGX vertBlurVerticalSGX (appdata_img v)
  177. {
  178. v2f_withBlurCoordsSGX o;
  179. o.pos = UnityObjectToClipPos (v.vertex);
  180.  
  181. o.uv = half4(v.texcoord.xy,1,1);
  182. half2 netFilterWidth = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
  183. half4 coords = -netFilterWidth.xyxy * 3.0;
  184.  
  185. o.offs[0] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
  186. coords += netFilterWidth.xyxy;
  187. o.offs[1] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
  188. coords += netFilterWidth.xyxy;
  189. o.offs[2] = v.texcoord.xyxy + coords * half4(1.0h,1.0h,-1.0h,-1.0h);
  190.  
  191. return o;
  192. }
  193.  
  194. half4 fragBlurSGX ( v2f_withBlurCoordsSGX i ) : SV_Target
  195. {
  196. half2 uv = i.uv.xy;
  197.  
  198. half4 color = tex2D(_MainTex, i.uv) * curve4[3];
  199.  
  200. for( int l = 0; l < 3; l++ )
  201. {
  202. half4 tapA = tex2D(_MainTex, i.offs[l].xy);
  203. half4 tapB = tex2D(_MainTex, i.offs[l].zw);
  204. color += (tapA + tapB) * curve4[l];
  205. }
  206.  
  207. return color;
  208.  
  209. }
  210.  
  211. ENDCG
  212.  
  213. SubShader {
  214. ZTest Off Cull Off ZWrite Off Blend Off
  215.  
  216. // 0
  217. Pass {
  218.  
  219. CGPROGRAM
  220. #pragma vertex vertBloom
  221. #pragma fragment fragBloom
  222.  
  223. ENDCG
  224.  
  225. }
  226.  
  227. // 1
  228. Pass {
  229.  
  230. CGPROGRAM
  231.  
  232. #pragma vertex vert4Tap
  233. #pragma fragment fragDownsample
  234.  
  235. ENDCG
  236.  
  237. }
  238.  
  239. // 2
  240. Pass {
  241. ZTest Always
  242. Cull Off
  243.  
  244. CGPROGRAM
  245.  
  246. #pragma vertex vertBlurVertical
  247. #pragma fragment fragBlur8
  248.  
  249. ENDCG
  250. }
  251.  
  252. // 3
  253. Pass {
  254. ZTest Always
  255. Cull Off
  256.  
  257. CGPROGRAM
  258.  
  259. #pragma vertex vertBlurHorizontal
  260. #pragma fragment fragBlur8
  261.  
  262. ENDCG
  263. }
  264.  
  265. // alternate blur
  266. // 4
  267. Pass {
  268. ZTest Always
  269. Cull Off
  270.  
  271. CGPROGRAM
  272.  
  273. #pragma vertex vertBlurVerticalSGX
  274. #pragma fragment fragBlurSGX
  275.  
  276. ENDCG
  277. }
  278.  
  279. // 5
  280. Pass {
  281. ZTest Always
  282. Cull Off
  283.  
  284. CGPROGRAM
  285.  
  286. #pragma vertex vertBlurHorizontalSGX
  287. #pragma fragment fragBlurSGX
  288.  
  289. ENDCG
  290. }
  291. }
  292.  
  293. FallBack Off
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement