SaliencyBlur.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Custom/SaliencyBlur"
  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[121]; //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. float _brightnessThreshold;
  42. float _redThreshold;
  43. float _greenThreshold;
  44. float _blueThreshold;
  45. float _darkSaliency;
  46. float4 gridOverPixel(sampler2D tex, float2 uv, float4 size) //average values surrounding pixel together and return the result
  47. {
  48. float4 currentColor = tex2D(tex,uv+float2(size.x * 0, size.y * 0));
  49. if(_darkSaliency == 1){
  50. if(dot(currentColor, float4(1,1,1,0)) <= _brightnessThreshold){
  51. return currentColor;
  52. }
  53. if(dot(currentColor, float4(1,0,0,0)) <= _redThreshold){
  54. return currentColor;
  55. }
  56. if(dot(currentColor, float4(0,1,0,0)) <= _greenThreshold){
  57. return currentColor;
  58. }
  59. if(dot(currentColor, float4(0,0,1,0)) <= _blueThreshold){
  60. return currentColor;
  61. }
  62. }else{
  63. if(dot(currentColor, float4(1,1,1,0)) >= _brightnessThreshold){
  64. return currentColor;
  65. }
  66. if(dot(currentColor, float4(1,0,0,0)) >= _redThreshold){
  67. return currentColor;
  68. }
  69. if(dot(currentColor, float4(0,1,0,0)) >= _greenThreshold){
  70. return currentColor;
  71. }
  72. if(dot(currentColor, float4(0,0,1,0)) >= _blueThreshold){
  73. return currentColor;
  74. }
  75. }
  76. float4 newFragColor = 0;
  77. // float newFragColor = 0;
  78. // newFragColor += tex2D(tex, uv+float2(-size.x*5, size.y*5)) * _kernel[0];
  79. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -5)) *_kernel[0];
  80. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -5)) *_kernel[1];
  81. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -5)) *_kernel[2];
  82. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -5)) *_kernel[3];
  83. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -5)) *_kernel[4];
  84. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -5)) *_kernel[5];
  85. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -5)) *_kernel[6];
  86. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -5)) *_kernel[7];
  87. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -5)) *_kernel[8];
  88. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -5)) *_kernel[9];
  89. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -5)) *_kernel[10];
  90. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -4)) *_kernel[11];
  91. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -4)) *_kernel[12];
  92. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -4)) *_kernel[13];
  93. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -4)) *_kernel[14];
  94. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -4)) *_kernel[15];
  95. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -4)) *_kernel[16];
  96. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -4)) *_kernel[17];
  97. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -4)) *_kernel[18];
  98. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -4)) *_kernel[19];
  99. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -4)) *_kernel[20];
  100. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -4)) *_kernel[21];
  101. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -3)) *_kernel[22];
  102. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -3)) *_kernel[23];
  103. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -3)) *_kernel[24];
  104. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -3)) *_kernel[25];
  105. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -3)) *_kernel[26];
  106. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -3)) *_kernel[27];
  107. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -3)) *_kernel[28];
  108. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -3)) *_kernel[29];
  109. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -3)) *_kernel[30];
  110. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -3)) *_kernel[31];
  111. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -3)) *_kernel[32];
  112. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -2)) *_kernel[33];
  113. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -2)) *_kernel[34];
  114. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -2)) *_kernel[35];
  115. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -2)) *_kernel[36];
  116. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -2)) *_kernel[37];
  117. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -2)) *_kernel[38];
  118. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -2)) *_kernel[39];
  119. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -2)) *_kernel[40];
  120. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -2)) *_kernel[41];
  121. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -2)) *_kernel[42];
  122. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -2)) *_kernel[43];
  123. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * -1)) *_kernel[44];
  124. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * -1)) *_kernel[45];
  125. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * -1)) *_kernel[46];
  126. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * -1)) *_kernel[47];
  127. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * -1)) *_kernel[48];
  128. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * -1)) *_kernel[49];
  129. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * -1)) *_kernel[50];
  130. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * -1)) *_kernel[51];
  131. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * -1)) *_kernel[52];
  132. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * -1)) *_kernel[53];
  133. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * -1)) *_kernel[54];
  134. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 0)) *_kernel[55];
  135. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 0)) *_kernel[56];
  136. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 0)) *_kernel[57];
  137. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 0)) *_kernel[58];
  138. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 0)) *_kernel[59];
  139. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 0)) *_kernel[60];
  140. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 0)) *_kernel[61];
  141. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 0)) *_kernel[62];
  142. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 0)) *_kernel[63];
  143. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 0)) *_kernel[64];
  144. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 0)) *_kernel[65];
  145. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 1)) *_kernel[66];
  146. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 1)) *_kernel[67];
  147. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 1)) *_kernel[68];
  148. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 1)) *_kernel[69];
  149. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 1)) *_kernel[70];
  150. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 1)) *_kernel[71];
  151. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 1)) *_kernel[72];
  152. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 1)) *_kernel[73];
  153. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 1)) *_kernel[74];
  154. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 1)) *_kernel[75];
  155. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 1)) *_kernel[76];
  156. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 2)) *_kernel[77];
  157. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 2)) *_kernel[78];
  158. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 2)) *_kernel[79];
  159. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 2)) *_kernel[80];
  160. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 2)) *_kernel[81];
  161. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 2)) *_kernel[82];
  162. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 2)) *_kernel[83];
  163. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 2)) *_kernel[84];
  164. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 2)) *_kernel[85];
  165. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 2)) *_kernel[86];
  166. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 2)) *_kernel[87];
  167. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 3)) *_kernel[88];
  168. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 3)) *_kernel[89];
  169. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 3)) *_kernel[90];
  170. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 3)) *_kernel[91];
  171. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 3)) *_kernel[92];
  172. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 3)) *_kernel[93];
  173. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 3)) *_kernel[94];
  174. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 3)) *_kernel[95];
  175. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 3)) *_kernel[96];
  176. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 3)) *_kernel[97];
  177. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 3)) *_kernel[98];
  178. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 4)) *_kernel[99];
  179. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 4)) *_kernel[100];
  180. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 4)) *_kernel[101];
  181. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 4)) *_kernel[102];
  182. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 4)) *_kernel[103];
  183. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 4)) *_kernel[104];
  184. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 4)) *_kernel[105];
  185. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 4)) *_kernel[106];
  186. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 4)) *_kernel[107];
  187. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 4)) *_kernel[108];
  188. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 4)) *_kernel[109];
  189. newFragColor += tex2D(tex,uv+float2(size.x * -5, size.y * 5)) *_kernel[110];
  190. newFragColor += tex2D(tex,uv+float2(size.x * -4, size.y * 5)) *_kernel[111];
  191. newFragColor += tex2D(tex,uv+float2(size.x * -3, size.y * 5)) *_kernel[112];
  192. newFragColor += tex2D(tex,uv+float2(size.x * -2, size.y * 5)) *_kernel[113];
  193. newFragColor += tex2D(tex,uv+float2(size.x * -1, size.y * 5)) *_kernel[114];
  194. newFragColor += tex2D(tex,uv+float2(size.x * 0, size.y * 5)) *_kernel[115];
  195. newFragColor += tex2D(tex,uv+float2(size.x * 1, size.y * 5)) *_kernel[116];
  196. newFragColor += tex2D(tex,uv+float2(size.x * 2, size.y * 5)) *_kernel[117];
  197. newFragColor += tex2D(tex,uv+float2(size.x * 3, size.y * 5)) *_kernel[118];
  198. newFragColor += tex2D(tex,uv+float2(size.x * 4, size.y * 5)) *_kernel[119];
  199. newFragColor += tex2D(tex,uv+float2(size.x * 5, size.y * 5)) *_kernel[120];
  200. return newFragColor;
  201. }
  202. //returns color in float 4 variable given our v2f
  203. float4 giveColor (v2f i) : SV_Target
  204. {
  205. float4 col = gridOverPixel(_MainTex, i.uv, _MainTex_TexelSize);
  206. return col;
  207. }
  208. ENDCG
  209. }
  210. }
  211. }