MobileBlur.shader 5.0 KB

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