ImageBlur.shader 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 
  2. Shader "FT/Blur/Back"
  3. {
  4. Properties
  5. {
  6. _Color ("Main Color", Color) = (1,1,1,1)
  7. _Size ("Size", Range(0, 20)) = 1
  8. }
  9. Category {
  10. // We must be transparent, so other objects are drawn before this one.
  11. Tags {
  12. "Queue"="Transparent"
  13. "IgnoreProjector"="True"
  14. "RenderType"="Transparent"
  15. "PreviewType" = "Plane"
  16. "CanUseSpriteAtlas" = "True"
  17. }
  18. SubShader {
  19. // Horizontal blur
  20. GrabPass
  21. {
  22. Tags { "LightMode" = "Always" }
  23. }
  24. Pass {
  25. Tags { "LightMode" = "Always" }
  26. Name "BackBlurHor"
  27. CGPROGRAM
  28. #pragma vertex vert
  29. #pragma fragment frag
  30. #pragma fragmentoption ARB_precision_hint_fastest
  31. #include "UnityCG.cginc"
  32. struct appdata_t {
  33. float4 vertex : POSITION;
  34. float2 texcoord : TEXCOORD0;
  35. float4 color : COLOR;
  36. };
  37. struct v2f {
  38. float4 vertex : POSITION;
  39. float4 uvgrab : TEXCOORD0;
  40. float4 color : COLOR;
  41. };
  42. v2f vert (appdata_t v) {
  43. v2f o;
  44. o.vertex = UnityObjectToClipPos(v.vertex);
  45. #if UNITY_UV_STARTS_AT_TOP
  46. float scale = -1.0;
  47. #else
  48. float scale = 1.0;
  49. #endif
  50. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  51. o.uvgrab.zw = o.vertex.zw;
  52. o.color = v.color;
  53. return o;
  54. }
  55. sampler2D _GrabTexture;
  56. float4 _GrabTexture_TexelSize;
  57. float4 _MainTex_TexelSize;
  58. float _Size;
  59. uniform float4 _Color;
  60. half4 GrabPixel(v2f i, float weight, float kernel){
  61. kernel = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y))) * kernel;
  62. return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernel * _Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight;
  63. }
  64. half4 frag( v2f i ) : COLOR {
  65. half4 sum = half4(0,0,0,0);
  66. sum += GrabPixel(i, 0.05, -4.0);
  67. sum += GrabPixel(i, 0.09, -3.0);
  68. sum += GrabPixel(i, 0.12, -2.0);
  69. sum += GrabPixel(i, 0.15, -1.0);
  70. sum += GrabPixel(i, 0.18, 0.0);
  71. sum += GrabPixel(i, 0.15, +1.0);
  72. sum += GrabPixel(i, 0.12, +2.0);
  73. sum += GrabPixel(i, 0.09, +3.0);
  74. sum += GrabPixel(i, 0.05, +4.0);
  75. float4 col5 = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  76. fixed decayFactor = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y)));
  77. sum = lerp(col5, sum, decayFactor) * i.color * _Color;
  78. return sum;
  79. }
  80. ENDCG
  81. }
  82. // Vertical blur
  83. GrabPass {
  84. Tags { "LightMode" = "Always" }
  85. }
  86. Pass {
  87. Tags { "LightMode" = "Always" }
  88. Name "BackBlurVer"
  89. CGPROGRAM
  90. #pragma vertex vert
  91. #pragma fragment frag
  92. #pragma fragmentoption ARB_precision_hint_fastest
  93. #include "UnityCG.cginc"
  94. struct appdata_t {
  95. float4 vertex : POSITION;
  96. float2 texcoord: TEXCOORD0;
  97. float4 color : COLOR;
  98. };
  99. struct v2f {
  100. float4 vertex : POSITION;
  101. float4 uvgrab : TEXCOORD0;
  102. float4 color : COLOR;
  103. };
  104. v2f vert (appdata_t v) {
  105. v2f o;
  106. o.vertex = UnityObjectToClipPos(v.vertex);
  107. #if UNITY_UV_STARTS_AT_TOP
  108. float scale = -1.0;
  109. #else
  110. float scale = 1.0;
  111. #endif
  112. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  113. o.uvgrab.zw = o.vertex.zw;
  114. o.color = v.color;
  115. return o;
  116. }
  117. sampler2D _GrabTexture;
  118. float4 _GrabTexture_TexelSize;
  119. float _Size;
  120. uniform float4 _Color;
  121. half4 GrabPixel(v2f i, float weight, float kernel){
  122. kernel = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y))) * kernel;
  123. return tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernel * _Size, i.uvgrab.z, i.uvgrab.w))) * weight;
  124. }
  125. half4 frag( v2f i ) : COLOR
  126. {
  127. half4 sum = half4(0,0,0,0);
  128. sum += GrabPixel(i, 0.05, -4.0);
  129. sum += GrabPixel(i, 0.09, -3.0);
  130. sum += GrabPixel(i, 0.12, -2.0);
  131. sum += GrabPixel(i, 0.15, -1.0);
  132. sum += GrabPixel(i, 0.18, 0.0);
  133. sum += GrabPixel(i, 0.15, +1.0);
  134. sum += GrabPixel(i, 0.12, +2.0);
  135. sum += GrabPixel(i, 0.09, +3.0);
  136. sum += GrabPixel(i, 0.05, +4.0);
  137. float4 col5 = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  138. fixed decayFactor = sign(sign(abs(i.uvgrab.x)) + sign(abs(i.uvgrab.y)));
  139. sum = lerp(col5, sum, decayFactor) * i.color * _Color;
  140. return sum;
  141. }
  142. ENDCG
  143. }
  144. }
  145. }
  146. }