BlurShader.shader 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Shader "Custom/Blur"{
  2. //show values to edit in inspector
  3. Properties{
  4. [HideInInspector]_MainTex ("Texture", 2D) = "white" {}
  5. _BlurSize("Blur Size", Range(0,0.5)) = 0
  6. [KeywordEnum(Low, Medium, High)] _Samples ("Sample amount", Float) = 0
  7. [Toggle(GAUSS)] _Gauss ("Gaussian Blur", float) = 0
  8. [PowerSlider(3)]_StandardDeviation("Standard Deviation (Gauss only)", Range(0.00, 0.3)) = 0.02
  9. }
  10. SubShader{
  11. // markers that specify that we don't need culling
  12. // or reading/writing to the depth buffer
  13. Cull Off
  14. ZWrite Off
  15. ZTest Always
  16. //Vertical Blur
  17. Pass{
  18. CGPROGRAM
  19. //include useful shader functions
  20. #include "UnityCG.cginc"
  21. //define vertex and fragment shader
  22. #pragma vertex vert
  23. #pragma fragment frag
  24. #pragma multi_compile _SAMPLES_LOW _SAMPLES_MEDIUM _SAMPLES_HIGH
  25. #pragma shader_feature GAUSS
  26. //texture and transforms of the texture
  27. sampler2D _MainTex;
  28. float _BlurSize;
  29. float _StandardDeviation;
  30. #define PI 3.14159265359
  31. #define E 2.71828182846
  32. #if _SAMPLES_LOW
  33. #define SAMPLES 10
  34. #elif _SAMPLES_MEDIUM
  35. #define SAMPLES 30
  36. #else
  37. #define SAMPLES 100
  38. #endif
  39. //the object data that's put into the vertex shader
  40. struct appdata{
  41. float4 vertex : POSITION;
  42. float2 uv : TEXCOORD0;
  43. };
  44. //the data that's used to generate fragments and can be read by the fragment shader
  45. struct v2f{
  46. float4 position : SV_POSITION;
  47. float2 uv : TEXCOORD0;
  48. };
  49. //the vertex shader
  50. v2f vert(appdata v){
  51. v2f o;
  52. //convert the vertex positions from object space to clip space so they can be rendered
  53. o.position = UnityObjectToClipPos(v.vertex);
  54. o.uv = v.uv;
  55. return o;
  56. }
  57. //the fragment shader
  58. fixed4 frag(v2f i) : SV_TARGET{
  59. #if GAUSS
  60. //failsafe so we can use turn off the blur by setting the deviation to 0
  61. if(_StandardDeviation == 0)
  62. return tex2D(_MainTex, i.uv);
  63. #endif
  64. //init color variable
  65. float4 col = 0;
  66. #if GAUSS
  67. float sum = 0;
  68. #else
  69. float sum = SAMPLES;
  70. #endif
  71. //iterate over blur samples
  72. for(float index = 0; index < SAMPLES; index++){
  73. //get the offset of the sample
  74. float offset = (index/(SAMPLES-1) - 0.5) * _BlurSize;
  75. //get uv coordinate of sample
  76. float2 uv = i.uv + float2(0, offset);
  77. #if !GAUSS
  78. //simply add the color if we don't have a gaussian blur (box)
  79. col += tex2D(_MainTex, uv);
  80. #else
  81. //calculate the result of the gaussian function
  82. float stDevSquared = _StandardDeviation*_StandardDeviation;
  83. float gauss = (1 / sqrt(2*PI*stDevSquared)) * pow(E, -((offset*offset)/(2*stDevSquared)));
  84. //add result to sum
  85. sum += gauss;
  86. //multiply color with influence from gaussian function and add it to sum color
  87. col += tex2D(_MainTex, uv) * gauss;
  88. #endif
  89. }
  90. //divide the sum of values by the amount of samples
  91. col = col / sum;
  92. return col;
  93. }
  94. ENDCG
  95. }
  96. //Horizontal Blur
  97. Pass{
  98. CGPROGRAM
  99. //include useful shader functions
  100. #include "UnityCG.cginc"
  101. #pragma multi_compile _SAMPLES_LOW _SAMPLES_MEDIUM _SAMPLES_HIGH
  102. #pragma shader_feature GAUSS
  103. //define vertex and fragment shader
  104. #pragma vertex vert
  105. #pragma fragment frag
  106. //texture and transforms of the texture
  107. sampler2D _MainTex;
  108. float _BlurSize;
  109. float _StandardDeviation;
  110. #define PI 3.14159265359
  111. #define E 2.71828182846
  112. #if _SAMPLES_LOW
  113. #define SAMPLES 10
  114. #elif _SAMPLES_MEDIUM
  115. #define SAMPLES 30
  116. #else
  117. #define SAMPLES 100
  118. #endif
  119. //the object data that's put into the vertex shader
  120. struct appdata{
  121. float4 vertex : POSITION;
  122. float2 uv : TEXCOORD0;
  123. };
  124. //the data that's used to generate fragments and can be read by the fragment shader
  125. struct v2f{
  126. float4 position : SV_POSITION;
  127. float2 uv : TEXCOORD0;
  128. };
  129. //the vertex shader
  130. v2f vert(appdata v){
  131. v2f o;
  132. //convert the vertex positions from object space to clip space so they can be rendered
  133. o.position = UnityObjectToClipPos(v.vertex);
  134. o.uv = v.uv;
  135. return o;
  136. }
  137. //the fragment shader
  138. fixed4 frag(v2f i) : SV_TARGET{
  139. #if GAUSS
  140. //failsafe so we can use turn off the blur by setting the deviation to 0
  141. if(_StandardDeviation == 0)
  142. return tex2D(_MainTex, i.uv);
  143. #endif
  144. //calculate aspect ratio
  145. float invAspect = _ScreenParams.y / _ScreenParams.x;
  146. //init color variable
  147. float4 col = 0;
  148. #if GAUSS
  149. float sum = 0;
  150. #else
  151. float sum = SAMPLES;
  152. #endif
  153. //iterate over blur samples
  154. for(float index = 0; index < SAMPLES; index++){
  155. //get the offset of the sample
  156. float offset = (index/(SAMPLES-1) - 0.5) * _BlurSize * invAspect;
  157. //get uv coordinate of sample
  158. float2 uv = i.uv + float2(offset, 0);
  159. #if !GAUSS
  160. //simply add the color if we don't have a gaussian blur (box)
  161. col += tex2D(_MainTex, uv);
  162. #else
  163. //calculate the result of the gaussian function
  164. float stDevSquared = _StandardDeviation*_StandardDeviation;
  165. float gauss = (1 / sqrt(2*PI*stDevSquared)) * pow(E, -((offset*offset)/(2*stDevSquared)));
  166. //add result to sum
  167. sum += gauss;
  168. //multiply color with influence from gaussian function and add it to sum color
  169. col += tex2D(_MainTex, uv) * gauss;
  170. #endif
  171. }
  172. //divide the sum of values by the amount of samples
  173. col = col / sum;
  174. return col;
  175. }
  176. ENDCG
  177. }
  178. }
  179. }