GaussianBlur.shader 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Custom/GaussianBlur"
  3. {
  4. Properties
  5. {
  6. _MainTex ("Texture", 2D) = "white" {} //include a texture as a property
  7. }
  8. SubShader
  9. {
  10. // No culling or depth
  11. Cull Off ZWrite Off //ZTest Always
  12. Pass
  13. {
  14. CGPROGRAM
  15. #pragma vertex vertexToFragment
  16. #pragma fragment giveColor
  17. #include "UnityCG.cginc"
  18. float _kernel[25]; //this is our gaussian kernel
  19. float _kernelSum; //whatever to divide by
  20. //info from vertex on the mesh
  21. struct appdata
  22. {
  23. float4 vertex : POSITION;
  24. float2 uv : TEXCOORD0;
  25. };
  26. //info for fragment function
  27. struct v2f
  28. {
  29. float2 uv : TEXCOORD0;
  30. float4 vertex : SV_POSITION;
  31. };
  32. v2f vertexToFragment (appdata v)
  33. {
  34. v2f o;
  35. o.vertex = UnityObjectToClipPos(v.vertex);
  36. o.uv = v.uv;
  37. return o;
  38. }
  39. sampler2D _MainTex;
  40. float4 _MainTex_TexelSize;
  41. float4 gridOverPixel(sampler2D tex, float2 uv, float4 size) //average values surrounding pixel together and return the result
  42. {
  43. float4 newFragColor = 0;
  44. // float newFragColor = 0;
  45. // newFragColor += tex2D(tex, uv+float2(-size.x*5, size.y*5)) * _kernel[0];
  46. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -2)) *_kernel[0];
  47. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -2)) *_kernel[1];
  48. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -2)) *_kernel[2];
  49. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -2)) *_kernel[3];
  50. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -2)) *_kernel[4];
  51. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -1)) *_kernel[5];
  52. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -1)) *_kernel[6];
  53. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -1)) *_kernel[7];
  54. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -1)) *_kernel[8];
  55. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -1)) *_kernel[9];
  56. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 0)) *_kernel[10];
  57. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 0)) *_kernel[11];
  58. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 0)) *_kernel[12];
  59. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 0)) *_kernel[13];
  60. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 0)) *_kernel[14];
  61. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 1)) *_kernel[15];
  62. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 1)) *_kernel[16];
  63. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 1)) *_kernel[17];
  64. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 1)) *_kernel[18];
  65. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 1)) *_kernel[19];
  66. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 2)) *_kernel[20];
  67. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 2)) *_kernel[21];
  68. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 2)) *_kernel[22];
  69. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 2)) *_kernel[23];
  70. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 2)) *_kernel[24];
  71. //filter size 11
  72. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -5)) *_kernel[0];
  73. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -5)) *_kernel[1];
  74. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -5)) *_kernel[2];
  75. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -5)) *_kernel[3];
  76. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -5)) *_kernel[4];
  77. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -5)) *_kernel[5];
  78. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -5)) *_kernel[6];
  79. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -5)) *_kernel[7];
  80. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -5)) *_kernel[8];
  81. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -5)) *_kernel[9];
  82. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -5)) *_kernel[10];
  83. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -4)) *_kernel[11];
  84. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -4)) *_kernel[12];
  85. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -4)) *_kernel[13];
  86. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -4)) *_kernel[14];
  87. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -4)) *_kernel[15];
  88. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -4)) *_kernel[16];
  89. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -4)) *_kernel[17];
  90. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -4)) *_kernel[18];
  91. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -4)) *_kernel[19];
  92. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -4)) *_kernel[20];
  93. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -4)) *_kernel[21];
  94. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -3)) *_kernel[22];
  95. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -3)) *_kernel[23];
  96. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -3)) *_kernel[24];
  97. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -3)) *_kernel[25];
  98. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -3)) *_kernel[26];
  99. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -3)) *_kernel[27];
  100. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -3)) *_kernel[28];
  101. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -3)) *_kernel[29];
  102. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -3)) *_kernel[30];
  103. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -3)) *_kernel[31];
  104. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -3)) *_kernel[32];
  105. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -2)) *_kernel[33];
  106. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -2)) *_kernel[34];
  107. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -2)) *_kernel[35];
  108. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -2)) *_kernel[36];
  109. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -2)) *_kernel[37];
  110. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -2)) *_kernel[38];
  111. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -2)) *_kernel[39];
  112. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -2)) *_kernel[40];
  113. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -2)) *_kernel[41];
  114. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -2)) *_kernel[42];
  115. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -2)) *_kernel[43];
  116. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -1)) *_kernel[44];
  117. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -1)) *_kernel[45];
  118. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -1)) *_kernel[46];
  119. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -1)) *_kernel[47];
  120. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -1)) *_kernel[48];
  121. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -1)) *_kernel[49];
  122. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -1)) *_kernel[50];
  123. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -1)) *_kernel[51];
  124. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -1)) *_kernel[52];
  125. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -1)) *_kernel[53];
  126. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -1)) *_kernel[54];
  127. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 0)) *_kernel[55];
  128. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 0)) *_kernel[56];
  129. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 0)) *_kernel[57];
  130. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 0)) *_kernel[58];
  131. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 0)) *_kernel[59];
  132. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 0)) *_kernel[60];
  133. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 0)) *_kernel[61];
  134. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 0)) *_kernel[62];
  135. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 0)) *_kernel[63];
  136. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 0)) *_kernel[64];
  137. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 0)) *_kernel[65];
  138. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 1)) *_kernel[66];
  139. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 1)) *_kernel[67];
  140. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 1)) *_kernel[68];
  141. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 1)) *_kernel[69];
  142. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 1)) *_kernel[70];
  143. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 1)) *_kernel[71];
  144. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 1)) *_kernel[72];
  145. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 1)) *_kernel[73];
  146. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 1)) *_kernel[74];
  147. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 1)) *_kernel[75];
  148. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 1)) *_kernel[76];
  149. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 2)) *_kernel[77];
  150. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 2)) *_kernel[78];
  151. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 2)) *_kernel[79];
  152. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 2)) *_kernel[80];
  153. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 2)) *_kernel[81];
  154. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 2)) *_kernel[82];
  155. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 2)) *_kernel[83];
  156. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 2)) *_kernel[84];
  157. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 2)) *_kernel[85];
  158. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 2)) *_kernel[86];
  159. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 2)) *_kernel[87];
  160. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 3)) *_kernel[88];
  161. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 3)) *_kernel[89];
  162. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 3)) *_kernel[90];
  163. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 3)) *_kernel[91];
  164. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 3)) *_kernel[92];
  165. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 3)) *_kernel[93];
  166. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 3)) *_kernel[94];
  167. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 3)) *_kernel[95];
  168. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 3)) *_kernel[96];
  169. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 3)) *_kernel[97];
  170. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 3)) *_kernel[98];
  171. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 4)) *_kernel[99];
  172. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 4)) *_kernel[100];
  173. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 4)) *_kernel[101];
  174. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 4)) *_kernel[102];
  175. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 4)) *_kernel[103];
  176. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 4)) *_kernel[104];
  177. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 4)) *_kernel[105];
  178. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 4)) *_kernel[106];
  179. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 4)) *_kernel[107];
  180. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 4)) *_kernel[108];
  181. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 4)) *_kernel[109];
  182. // newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 5)) *_kernel[110];
  183. // newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 5)) *_kernel[111];
  184. // newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 5)) *_kernel[112];
  185. // newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 5)) *_kernel[113];
  186. // newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 5)) *_kernel[114];
  187. // newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 5)) *_kernel[115];
  188. // newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 5)) *_kernel[116];
  189. // newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 5)) *_kernel[117];
  190. // newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 5)) *_kernel[118];
  191. // newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 5)) *_kernel[119];
  192. // newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 5)) *_kernel[120];
  193. return newFragColor;
  194. }
  195. //returns color in float 4 variable given our v2f
  196. float4 giveColor (v2f i) : SV_Target
  197. {
  198. float4 col = gridOverPixel(_MainTex, i.uv, _MainTex_TexelSize);
  199. return col;
  200. }
  201. ENDCG
  202. }
  203. }
  204. }