SimpleGrabPassBlur.shader 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Custom/SimpleGrabPassBlur" {
  3. Properties {
  4. _Color ("Main Color", Color) = (1,1,1,1)
  5. _BumpAmt ("Distortion", Range (0,128)) = 10
  6. _MainTex ("Tint Color (RGB)", 2D) = "white" {}
  7. _BumpMap ("Normalmap", 2D) = "bump" {}
  8. _Size ("Size", Range(0, 20)) = 1
  9. }
  10. Category {
  11. // We must be transparent, so other objects are drawn before this one.
  12. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
  13. SubShader {
  14. // Horizontal blur
  15. GrabPass {
  16. Tags { "LightMode" = "Always" }
  17. }
  18. Pass {
  19. Tags { "LightMode" = "Always" }
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma fragment frag
  23. #pragma fragmentoption ARB_precision_hint_fastest
  24. #include "UnityCG.cginc"
  25. struct appdata_t {
  26. float4 vertex : POSITION;
  27. float2 texcoord: TEXCOORD0;
  28. };
  29. struct v2f {
  30. float4 vertex : POSITION;
  31. float4 uvgrab : TEXCOORD0;
  32. };
  33. v2f vert (appdata_t v) {
  34. v2f o;
  35. o.vertex = UnityObjectToClipPos(v.vertex);
  36. #if UNITY_UV_STARTS_AT_TOP
  37. float scale = -1.0;
  38. #else
  39. float scale = 1.0;
  40. #endif
  41. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  42. o.uvgrab.zw = o.vertex.zw;
  43. return o;
  44. }
  45. sampler2D _GrabTexture;
  46. float4 _GrabTexture_TexelSize;
  47. float _Size;
  48. half4 frag( v2f i ) : COLOR {
  49. // half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  50. // return col;
  51. half4 sum = half4(0,0,0,0);
  52. #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
  53. sum += GRABPIXEL(0.05, -4.0);
  54. sum += GRABPIXEL(0.09, -3.0);
  55. sum += GRABPIXEL(0.12, -2.0);
  56. sum += GRABPIXEL(0.15, -1.0);
  57. sum += GRABPIXEL(0.18, 0.0);
  58. sum += GRABPIXEL(0.15, +1.0);
  59. sum += GRABPIXEL(0.12, +2.0);
  60. sum += GRABPIXEL(0.09, +3.0);
  61. sum += GRABPIXEL(0.05, +4.0);
  62. return sum;
  63. }
  64. ENDCG
  65. }
  66. // Vertical blur
  67. GrabPass {
  68. Tags { "LightMode" = "Always" }
  69. }
  70. Pass {
  71. Tags { "LightMode" = "Always" }
  72. CGPROGRAM
  73. #pragma vertex vert
  74. #pragma fragment frag
  75. #pragma fragmentoption ARB_precision_hint_fastest
  76. #include "UnityCG.cginc"
  77. struct appdata_t {
  78. float4 vertex : POSITION;
  79. float2 texcoord: TEXCOORD0;
  80. };
  81. struct v2f {
  82. float4 vertex : POSITION;
  83. float4 uvgrab : TEXCOORD0;
  84. };
  85. v2f vert (appdata_t v) {
  86. v2f o;
  87. o.vertex = UnityObjectToClipPos(v.vertex);
  88. #if UNITY_UV_STARTS_AT_TOP
  89. float scale = -1.0;
  90. #else
  91. float scale = 1.0;
  92. #endif
  93. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  94. o.uvgrab.zw = o.vertex.zw;
  95. return o;
  96. }
  97. sampler2D _GrabTexture;
  98. float4 _GrabTexture_TexelSize;
  99. float _Size;
  100. half4 frag( v2f i ) : COLOR {
  101. // half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  102. // return col;
  103. half4 sum = half4(0,0,0,0);
  104. #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight
  105. //G(X) = (1/(sqrt(2*PI*deviation*deviation))) * exp(-(x*x / (2*deviation*deviation)))
  106. sum += GRABPIXEL(0.05, -4.0);
  107. sum += GRABPIXEL(0.09, -3.0);
  108. sum += GRABPIXEL(0.12, -2.0);
  109. sum += GRABPIXEL(0.15, -1.0);
  110. sum += GRABPIXEL(0.18, 0.0);
  111. sum += GRABPIXEL(0.15, +1.0);
  112. sum += GRABPIXEL(0.12, +2.0);
  113. sum += GRABPIXEL(0.09, +3.0);
  114. sum += GRABPIXEL(0.05, +4.0);
  115. return sum;
  116. }
  117. ENDCG
  118. }
  119. // Distortion
  120. GrabPass {
  121. Tags { "LightMode" = "Always" }
  122. }
  123. Pass {
  124. Tags { "LightMode" = "Always" }
  125. CGPROGRAM
  126. #pragma vertex vert
  127. #pragma fragment frag
  128. #pragma fragmentoption ARB_precision_hint_fastest
  129. #include "UnityCG.cginc"
  130. struct appdata_t {
  131. float4 vertex : POSITION;
  132. float2 texcoord: TEXCOORD0;
  133. };
  134. struct v2f {
  135. float4 vertex : POSITION;
  136. float4 uvgrab : TEXCOORD0;
  137. float2 uvbump : TEXCOORD1;
  138. float2 uvmain : TEXCOORD2;
  139. };
  140. float _BumpAmt;
  141. float4 _BumpMap_ST;
  142. float4 _MainTex_ST;
  143. v2f vert (appdata_t v) {
  144. v2f o;
  145. o.vertex = UnityObjectToClipPos(v.vertex);
  146. #if UNITY_UV_STARTS_AT_TOP
  147. float scale = -1.0;
  148. #else
  149. float scale = 1.0;
  150. #endif
  151. o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
  152. o.uvgrab.zw = o.vertex.zw;
  153. o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
  154. o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
  155. return o;
  156. }
  157. fixed4 _Color;
  158. sampler2D _GrabTexture;
  159. float4 _GrabTexture_TexelSize;
  160. sampler2D _BumpMap;
  161. sampler2D _MainTex;
  162. half4 frag( v2f i ) : COLOR {
  163. // calculate perturbed coordinates
  164. half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x y without reconstructing the Z
  165. float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
  166. i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
  167. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  168. half4 tint = tex2D( _MainTex, i.uvmain ) * _Color;
  169. return col * tint;
  170. }
  171. ENDCG
  172. }
  173. }
  174. }
  175. }