ZED_Blur.shader 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. //Blurs with predefined kernel and weights
  3. Shader "ZED/ZED Blur"
  4. {
  5. Properties
  6. {
  7. _MainTex("Texture", 2D) = "white" {}
  8. }
  9. SubShader
  10. {
  11. Tags { "RenderType" = "Opaque" }
  12. ZWrite Off
  13. ZTest Always
  14. Cull Off
  15. //0 - Blur based on alpha
  16. Pass
  17. {
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma target 4.0
  22. #include "UnityCG.cginc"
  23. struct appdata
  24. {
  25. float4 vertex : POSITION;
  26. float2 uv : TEXCOORD0;
  27. };
  28. struct v2f
  29. {
  30. float2 uv : TEXCOORD0;
  31. float4 vertex : SV_POSITION;
  32. };
  33. sampler2D _MainTex;
  34. float4 _MainTex_ST;
  35. v2f vert(appdata v)
  36. {
  37. v2f o;
  38. o.vertex = UnityObjectToClipPos(v.vertex);
  39. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  40. return o;
  41. }
  42. float4 _MainTex_TexelSize;
  43. uniform int horizontal;
  44. uniform float weights[5];
  45. uniform float offset[5];
  46. float4 frag(v2f vi) : SV_Target
  47. {
  48. float2 uv = vi.uv;
  49. float result = tex2D(_MainTex, uv).a * weights[0]; // current fragment's contribution
  50. int i = 0;
  51. if (horizontal == 1)
  52. {
  53. result += tex2D(_MainTex, uv + float2(offset[1] * _MainTex_TexelSize.x, 0.0)).a * weights[1];
  54. result += tex2D(_MainTex, uv - float2(offset[1] * _MainTex_TexelSize.x, 0.0)).a * weights[1];
  55. result += tex2D(_MainTex, uv + float2(offset[2] * _MainTex_TexelSize.x, 0.0)).a * weights[2];
  56. result += tex2D(_MainTex, uv - float2(offset[2] * _MainTex_TexelSize.x, 0.0)).a * weights[2];
  57. }
  58. else
  59. {
  60. result += tex2D(_MainTex, uv + float2(0.0, offset[1] * _MainTex_TexelSize.y)).a * weights[1];
  61. result += tex2D(_MainTex, uv - float2(0.0, offset[1] * _MainTex_TexelSize.y)).a * weights[1];
  62. result += tex2D(_MainTex, uv + float2(0.0, offset[2] * _MainTex_TexelSize.y)).a * weights[2];
  63. result += tex2D(_MainTex, uv - float2(0.0, offset[2] * _MainTex_TexelSize.y)).a * weights[2];
  64. }
  65. return float4(tex2D(_MainTex, uv).rgb, result);
  66. }
  67. ENDCG
  68. }
  69. // 1 - Blur based on RGB, R unchanged, GB blured
  70. Pass
  71. {
  72. CGPROGRAM
  73. #pragma vertex vert
  74. #pragma fragment frag
  75. #pragma target 4.0
  76. #include "UnityCG.cginc"
  77. struct appdata
  78. {
  79. float4 vertex : POSITION;
  80. float2 uv : TEXCOORD0;
  81. };
  82. struct v2f
  83. {
  84. float2 uv : TEXCOORD0;
  85. float4 vertex : SV_POSITION;
  86. };
  87. sampler2D _MainTex;
  88. float4 _MainTex_ST;
  89. v2f vert(appdata v)
  90. {
  91. v2f o;
  92. o.vertex = UnityObjectToClipPos(v.vertex);
  93. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  94. return o;
  95. }
  96. float4 _MainTex_TexelSize;
  97. uniform int horizontal;
  98. uniform float weights2[5];
  99. uniform float offset2[5];
  100. float4 frag(v2f vi) : SV_Target
  101. {
  102. float2 uv = vi.uv;
  103. float3 result = tex2D(_MainTex, uv).rgb * weights2[0]; // current fragment's contribution
  104. result.r = tex2D(_MainTex, uv).r;
  105. int i = 0;
  106. if (horizontal == 1)
  107. {
  108. result.gb += tex2D(_MainTex, uv + float2(offset2[1] * _MainTex_TexelSize.x, 0.0)).gb * weights2[1];
  109. result.gb += tex2D(_MainTex, uv - float2(offset2[1] * _MainTex_TexelSize.x, 0.0)).gb * weights2[1];
  110. result.gb += tex2D(_MainTex, uv + float2(offset2[2] * _MainTex_TexelSize.x, 0.0)).gb * weights2[2];
  111. result.gb += tex2D(_MainTex, uv - float2(offset2[2] * _MainTex_TexelSize.x, 0.0)).gb * weights2[2];
  112. }
  113. else
  114. {
  115. result.gb += tex2D(_MainTex, uv + float2(0.0, offset2[1] * _MainTex_TexelSize.y)).gb * weights2[1];
  116. result.gb += tex2D(_MainTex, uv - float2(0.0, offset2[1] * _MainTex_TexelSize.y)).gb * weights2[1];
  117. result.gb += tex2D(_MainTex, uv + float2(0.0, offset2[2] * _MainTex_TexelSize.y)).gb * weights2[2];
  118. result.gb += tex2D(_MainTex, uv - float2(0.0, offset2[2] * _MainTex_TexelSize.y)).gb * weights2[2];
  119. }
  120. return float4(result.x, result.y, result.z, 1);
  121. }
  122. ENDCG
  123. }
  124. // 2 - Blur based with mask
  125. Pass
  126. {
  127. CGPROGRAM
  128. #pragma vertex vert
  129. #pragma fragment frag
  130. #pragma target 4.0
  131. #include "UnityCG.cginc"
  132. struct appdata
  133. {
  134. float4 vertex : POSITION;
  135. float2 uv : TEXCOORD0;
  136. };
  137. struct v2f
  138. {
  139. float2 uv : TEXCOORD0;
  140. float4 vertex : SV_POSITION;
  141. };
  142. sampler2D _MainTex;
  143. sampler2D _Mask;
  144. float4 _MainTex_ST;
  145. v2f vert(appdata v)
  146. {
  147. v2f o;
  148. o.vertex = UnityObjectToClipPos(v.vertex);
  149. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  150. return o;
  151. }
  152. float4 _MainTex_TexelSize;
  153. uniform int horizontal;
  154. uniform float weights[5];
  155. uniform float offset[5];
  156. float4 frag(v2f vi) : SV_Target
  157. {
  158. //TODO issue : The blur will shrink the virtual due to the mask
  159. float2 uv = vi.uv;
  160. float3 result = tex2D(_MainTex, uv).rgb;
  161. if (tex2D(_Mask, uv).r != 0) {
  162. result *= weights[0]; // current fragment's contribution
  163. int i = 0;
  164. if (horizontal == 1)
  165. {
  166. result += tex2D(_MainTex, uv + float2(offset[1] * _MainTex_TexelSize.x, 0.0)).rgb * weights[1];
  167. result += tex2D(_MainTex, uv - float2(offset[1] * _MainTex_TexelSize.x, 0.0)).rgb * weights[1];
  168. result += tex2D(_MainTex, uv + float2(offset[2] * _MainTex_TexelSize.x, 0.0)).rgb * weights[2];
  169. result += tex2D(_MainTex, uv - float2(offset[2] * _MainTex_TexelSize.x, 0.0)).rgb * weights[2];
  170. }
  171. else
  172. {
  173. result += tex2D(_MainTex, uv + float2(0.0, offset[1] * _MainTex_TexelSize.y)).rgb * weights[1];
  174. result += tex2D(_MainTex, uv - float2(0.0, offset[1] * _MainTex_TexelSize.y)).rgb * weights[1];
  175. result += tex2D(_MainTex, uv + float2(0.0, offset[2] * _MainTex_TexelSize.y)).rgb * weights[2];
  176. result += tex2D(_MainTex, uv - float2(0.0, offset[2] * _MainTex_TexelSize.y)).rgb * weights[2];
  177. }
  178. }
  179. return float4(result, 1);
  180. }
  181. ENDCG
  182. }
  183. //3 - Blur based on R
  184. Pass
  185. {
  186. CGPROGRAM
  187. #pragma vertex vert
  188. #pragma fragment frag
  189. #pragma target 4.0
  190. #include "UnityCG.cginc"
  191. struct appdata
  192. {
  193. float4 vertex : POSITION;
  194. float2 uv : TEXCOORD0;
  195. };
  196. struct v2f
  197. {
  198. float2 uv : TEXCOORD0;
  199. float4 vertex : SV_POSITION;
  200. };
  201. sampler2D _MainTex;
  202. float4 _MainTex_ST;
  203. v2f vert(appdata v)
  204. {
  205. v2f o;
  206. o.vertex = UnityObjectToClipPos(v.vertex);
  207. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  208. return o;
  209. }
  210. float4 _MainTex_TexelSize;
  211. uniform int horizontal;
  212. uniform float weights[5];
  213. uniform float offset[5];
  214. float4 frag(v2f vi) : SV_Target
  215. {
  216. float2 uv = vi.uv;
  217. float result = tex2D(_MainTex, uv).r * weights[0]; // current fragment's contribution
  218. int i = 0;
  219. if (horizontal == 1)
  220. {
  221. result += tex2D(_MainTex, uv + float2(offset[1] * _MainTex_TexelSize.x, 0.0)).r * weights[1];
  222. result += tex2D(_MainTex, uv - float2(offset[1] * _MainTex_TexelSize.x, 0.0)).r * weights[1];
  223. result += tex2D(_MainTex, uv + float2(offset[2] * _MainTex_TexelSize.x, 0.0)).r * weights[2];
  224. result += tex2D(_MainTex, uv - float2(offset[2] * _MainTex_TexelSize.x, 0.0)).r * weights[2];
  225. }
  226. else
  227. {
  228. result += tex2D(_MainTex, uv + float2(0.0, offset[1] * _MainTex_TexelSize.y)).r * weights[1];
  229. result += tex2D(_MainTex, uv - float2(0.0, offset[1] * _MainTex_TexelSize.y)).r * weights[1];
  230. result += tex2D(_MainTex, uv + float2(0.0, offset[2] * _MainTex_TexelSize.y)).r * weights[2];
  231. result += tex2D(_MainTex, uv - float2(0.0, offset[2] * _MainTex_TexelSize.y)).r * weights[2];
  232. }
  233. return float4(result, result, result, result);
  234. }
  235. ENDCG
  236. }
  237. }
  238. }