UIBlur.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. /**
  3. * Gaussian Blur
  4. *
  5. * Author(s): Flying Banana
  6. * Date Created: 25-Jul-2015
  7. */
  8. /**
  9. * This is a two-pass blur shader. Performance-wise this gives the best result.
  10. */
  11. Shader "UI/Blur/UIBlur" {
  12. Properties {
  13. _TintColor ("Tint Color", Color) = (1, 1, 1, 0.2)
  14. _Size ("Spacing", Range(0, 10)) = 5.0
  15. _Vibrancy ("Vibrancy", Range(0, 2)) = 0.2
  16. [HideInInspector]
  17. _StencilComp ("Stencil Comparison", Float) = 8
  18. [HideInInspector]
  19. _Stencil ("Stencil ID", Float) = 0
  20. [HideInInspector]
  21. _StencilOp ("Stencil Operation", Float) = 0
  22. [HideInInspector]
  23. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  24. [HideInInspector]
  25. _StencilReadMask ("Stencil Read Mask", Float) = 255
  26. }
  27. Category {
  28. // We must be transparent, so other objects are drawn before this one.
  29. Tags {
  30. "Queue"="Transparent"
  31. "IgnoreProjector"="True"
  32. "RenderType"="Opaque"
  33. }
  34. Stencil {
  35. Ref [_Stencil]
  36. Comp [_StencilComp]
  37. Pass [_StencilOp]
  38. ReadMask [_StencilReadMask]
  39. WriteMask [_StencilWriteMask]
  40. }
  41. SubShader {
  42. GrabPass {
  43. Tags { "LightMode" = "Always" }
  44. }
  45. // Vertical blur
  46. Pass {
  47. Name "VERTICAL"
  48. Tags { "LightMode" = "Always" }
  49. CGPROGRAM
  50. #pragma vertex vert
  51. #pragma fragment frag
  52. #pragma fragmentoption ARB_precision_hint_fastest
  53. #include "UnityCG.cginc"
  54. struct appdata_t {
  55. float4 vertex : POSITION;
  56. float2 texcoord: TEXCOORD0;
  57. };
  58. struct v2f {
  59. float4 vertex : POSITION;
  60. float4 uvgrab : TEXCOORD0;
  61. };
  62. v2f vert (appdata_t v) {
  63. v2f o;
  64. o.vertex = UnityObjectToClipPos(v.vertex);
  65. #if UNITY_UV_STARTS_AT_TOP
  66. float scale = -1.0;
  67. #else
  68. float scale = 1.0;
  69. #endif
  70. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  71. o.uvgrab.zw = o.vertex.zw;
  72. return o;
  73. }
  74. sampler2D _GrabTexture;
  75. float4 _GrabTexture_TexelSize;
  76. float _Size;
  77. half4 frag( v2f i ) : COLOR {
  78. half4 sum = half4(0,0,0,0);
  79. #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely * _Size * 1.61, i.uvgrab.z, i.uvgrab.w))) * weight
  80. sum += GRABPIXEL(0.05, -4.0);
  81. sum += GRABPIXEL(0.09, -3.0);
  82. sum += GRABPIXEL(0.12, -2.0);
  83. sum += GRABPIXEL(0.15, -1.0);
  84. sum += GRABPIXEL(0.18, 0.0);
  85. sum += GRABPIXEL(0.15, +1.0);
  86. sum += GRABPIXEL(0.12, +2.0);
  87. sum += GRABPIXEL(0.09, +3.0);
  88. sum += GRABPIXEL(0.05, +4.0);
  89. return sum;
  90. }
  91. ENDCG
  92. }
  93. GrabPass {
  94. Tags { "LightMode" = "Always" }
  95. }
  96. // Horizontal blur
  97. Pass {
  98. Name "HORIZONTAL"
  99. Tags { "LightMode" = "Always" }
  100. CGPROGRAM
  101. #pragma vertex vert
  102. #pragma fragment frag
  103. #pragma fragmentoption ARB_precision_hint_fastest
  104. #include "UnityCG.cginc"
  105. struct appdata_t {
  106. float4 vertex : POSITION;
  107. float2 texcoord: TEXCOORD0;
  108. };
  109. struct v2f {
  110. float4 vertex : POSITION;
  111. float4 uvgrab : TEXCOORD0;
  112. };
  113. v2f vert (appdata_t v) {
  114. v2f o;
  115. o.vertex = UnityObjectToClipPos(v.vertex);
  116. #if UNITY_UV_STARTS_AT_TOP
  117. float scale = -1.0;
  118. #else
  119. float scale = 1.0;
  120. #endif
  121. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  122. o.uvgrab.zw = o.vertex.zw;
  123. return o;
  124. }
  125. sampler2D _GrabTexture;
  126. float4 _GrabTexture_TexelSize;
  127. float _Size;
  128. half4 frag( v2f i ) : COLOR {
  129. half4 sum = half4(0,0,0,0);
  130. #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx * _Size * 1.61, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
  131. sum += GRABPIXEL(0.05, -4.0);
  132. sum += GRABPIXEL(0.09, -3.0);
  133. sum += GRABPIXEL(0.12, -2.0);
  134. sum += GRABPIXEL(0.15, -1.0);
  135. sum += GRABPIXEL(0.18, 0.0);
  136. sum += GRABPIXEL(0.15, +1.0);
  137. sum += GRABPIXEL(0.12, +2.0);
  138. sum += GRABPIXEL(0.09, +3.0);
  139. sum += GRABPIXEL(0.05, +4.0);
  140. return sum;
  141. }
  142. ENDCG
  143. }
  144. GrabPass {
  145. Tags { "LightMode" = "Always" }
  146. }
  147. // Vertical blur
  148. Pass {
  149. Name "VERTICAL"
  150. Tags { "LightMode" = "Always" }
  151. CGPROGRAM
  152. #pragma vertex vert
  153. #pragma fragment frag
  154. #pragma fragmentoption ARB_precision_hint_fastest
  155. #include "UnityCG.cginc"
  156. struct appdata_t {
  157. float4 vertex : POSITION;
  158. float2 texcoord: TEXCOORD0;
  159. };
  160. struct v2f {
  161. float4 vertex : POSITION;
  162. float4 uvgrab : TEXCOORD0;
  163. };
  164. v2f vert (appdata_t v) {
  165. v2f o;
  166. o.vertex = UnityObjectToClipPos(v.vertex);
  167. #if UNITY_UV_STARTS_AT_TOP
  168. float scale = -1.0;
  169. #else
  170. float scale = 1.0;
  171. #endif
  172. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  173. o.uvgrab.zw = o.vertex.zw;
  174. return o;
  175. }
  176. sampler2D _GrabTexture;
  177. float4 _GrabTexture_TexelSize;
  178. float _Size;
  179. half4 frag( v2f i ) : COLOR {
  180. half4 sum = half4(0,0,0,0);
  181. #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely * _Size, i.uvgrab.z, i.uvgrab.w))) * weight
  182. sum += GRABPIXEL(0.05, -4.0);
  183. sum += GRABPIXEL(0.09, -3.0);
  184. sum += GRABPIXEL(0.12, -2.0);
  185. sum += GRABPIXEL(0.15, -1.0);
  186. sum += GRABPIXEL(0.18, 0.0);
  187. sum += GRABPIXEL(0.15, +1.0);
  188. sum += GRABPIXEL(0.12, +2.0);
  189. sum += GRABPIXEL(0.09, +3.0);
  190. sum += GRABPIXEL(0.05, +4.0);
  191. return sum;
  192. }
  193. ENDCG
  194. }
  195. GrabPass {
  196. Tags { "LightMode" = "Always" }
  197. }
  198. // Horizontal blur
  199. Pass {
  200. Name "HORIZONTAL"
  201. Tags { "LightMode" = "Always" }
  202. CGPROGRAM
  203. #pragma vertex vert
  204. #pragma fragment frag
  205. #pragma fragmentoption ARB_precision_hint_fastest
  206. #include "UnityCG.cginc"
  207. struct appdata_t {
  208. float4 vertex : POSITION;
  209. float2 texcoord: TEXCOORD0;
  210. };
  211. struct v2f {
  212. float4 vertex : POSITION;
  213. float4 uvgrab : TEXCOORD0;
  214. };
  215. v2f vert (appdata_t v) {
  216. v2f o;
  217. o.vertex = UnityObjectToClipPos(v.vertex);
  218. #if UNITY_UV_STARTS_AT_TOP
  219. float scale = -1.0;
  220. #else
  221. float scale = 1.0;
  222. #endif
  223. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  224. o.uvgrab.zw = o.vertex.zw;
  225. return o;
  226. }
  227. sampler2D _GrabTexture;
  228. float4 _GrabTexture_TexelSize;
  229. float _Size;
  230. half4 frag( v2f i ) : COLOR {
  231. half4 sum = half4(0,0,0,0);
  232. #define GRABPIXEL(weight,kernelx) tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx * _Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
  233. sum += GRABPIXEL(0.05, -4.0);
  234. sum += GRABPIXEL(0.09, -3.0);
  235. sum += GRABPIXEL(0.12, -2.0);
  236. sum += GRABPIXEL(0.15, -1.0);
  237. sum += GRABPIXEL(0.18, 0.0);
  238. sum += GRABPIXEL(0.15, +1.0);
  239. sum += GRABPIXEL(0.12, +2.0);
  240. sum += GRABPIXEL(0.09, +3.0);
  241. sum += GRABPIXEL(0.05, +4.0);
  242. return sum;
  243. }
  244. ENDCG
  245. }
  246. // Distortion
  247. GrabPass {
  248. Tags { "LightMode" = "Always" }
  249. }
  250. Pass {
  251. Tags { "LightMode" = "Always" }
  252. CGPROGRAM
  253. #pragma vertex vert
  254. #pragma fragment frag
  255. #pragma fragmentoption ARB_precision_hint_fastest
  256. #include "UnityCG.cginc"
  257. struct appdata_t {
  258. float4 vertex : POSITION;
  259. float2 texcoord: TEXCOORD0;
  260. };
  261. struct v2f {
  262. float4 vertex : POSITION;
  263. float4 uvgrab : TEXCOORD0;
  264. };
  265. v2f vert (appdata_t v) {
  266. v2f o;
  267. o.vertex = UnityObjectToClipPos(v.vertex);
  268. #if UNITY_UV_STARTS_AT_TOP
  269. float scale = -1.0;
  270. #else
  271. float scale = 1.0;
  272. #endif
  273. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  274. o.uvgrab.zw = o.vertex.zw;
  275. return o;
  276. }
  277. half4 _TintColor;
  278. float _Vibrancy;
  279. sampler2D _GrabTexture;
  280. float4 _GrabTexture_TexelSize;
  281. half4 frag( v2f i ) : COLOR {
  282. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  283. col.xyz *= 1 + _Vibrancy;
  284. col = lerp (col, _TintColor, _TintColor.w);
  285. return col;
  286. }
  287. ENDCG
  288. }
  289. }
  290. }
  291. }