BlurCG.cginc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. #define GRABPIXELX(weight, kernelx) tex2Dproj(_SpriteBlurTextureH, UNITY_PROJ_COORD(half4(i.uvgrab.x + _SpriteBlurTextureH_TexelSize.x * kernelx * _Size * i.color.a, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
  3. #define GRABPIXELY(weight, kernely) tex2Dproj(_SpriteBlurTextureV, UNITY_PROJ_COORD(half4(i.uvgrab.x, i.uvgrab.y + _SpriteBlurTextureV_TexelSize.y * kernely * _Size * i.color.a, i.uvgrab.z, i.uvgrab.w))) * weight
  4. struct appdata_t {
  5. half4 vertex : POSITION;
  6. half2 texcoord: TEXCOORD0;
  7. half4 color : COLOR;
  8. };
  9. struct v2f {
  10. half4 vertex : POSITION;
  11. half4 uvgrab : TEXCOORD0;
  12. half2 uvmain : TEXCOORD1;
  13. half4 color : COLOR;
  14. };
  15. half4 _MainTex_ST;
  16. sampler2D _SpriteBlurTextureV;
  17. half4 _SpriteBlurTextureV_TexelSize;
  18. sampler2D _SpriteBlurTextureH;
  19. half4 _SpriteBlurTextureH_TexelSize;
  20. half _Size;
  21. sampler2D _MainTex;
  22. v2f vert(appdata_t v) {
  23. v2f o;
  24. o.vertex = UnityObjectToClipPos(v.vertex);
  25. #if UNITY_UV_STARTS_AT_TOP
  26. float scale = -1.0;
  27. #else
  28. float scale = 1.0;
  29. #endif
  30. o.uvgrab.xy = (half2(o.vertex.x, o.vertex.y * scale) + o.vertex.w) * 0.5;
  31. #ifdef UNITY_HALF_TEXEL_OFFSET
  32. o.vertex.xy += (_ScreenParams.zw - 1.0) * half2(-1, 1);
  33. o.uvgrab.xy += half2(0.0005, 0.0008);
  34. #else
  35. #endif
  36. o.uvgrab.zw = o.vertex.zw;
  37. o.color = v.color;
  38. o.uvmain = TRANSFORM_TEX(v.texcoord, _MainTex);
  39. return o;
  40. }
  41. half4 fragX(v2f i) : SV_Target {
  42. half4 sum = half4(0, 0, 0, 0);
  43. half4 tint = tex2D(_MainTex, i.uvmain);
  44. //half4 col = tex2Dproj(_SpriteBlurTexture, UNITY_PROJ_COORD(i.uvgrab));
  45. half4 col = tex2Dproj(_SpriteBlurTextureH, i.uvgrab);
  46. sum += GRABPIXELX(0.05, -4.0);
  47. sum += GRABPIXELX(0.09, -3.0);
  48. sum += GRABPIXELX(0.12, -2.0);
  49. sum += GRABPIXELX(0.15, -1.0);
  50. sum += GRABPIXELX(0.18, 0.0);
  51. sum += GRABPIXELX(0.15, +1.0);
  52. sum += GRABPIXELX(0.12, +2.0);
  53. sum += GRABPIXELX(0.09, +3.0);
  54. sum += GRABPIXELX(0.05, +4.0);
  55. sum.rgb *= i.color.r;
  56. return lerp(col, sum, tint.a);
  57. }
  58. half4 fragY(v2f i) : SV_Target {
  59. half4 sum = half4(0, 0, 0, 0);
  60. half4 tint = tex2D(_MainTex, i.uvmain);
  61. //half4 col = tex2Dproj(_SpriteBlurTexture, UNITY_PROJ_COORD(i.uvgrab));
  62. half4 col = tex2Dproj(_SpriteBlurTextureV, i.uvgrab);
  63. sum += GRABPIXELY(0.05, -4.0);
  64. sum += GRABPIXELY(0.09, -3.0);
  65. sum += GRABPIXELY(0.12, -2.0);
  66. sum += GRABPIXELY(0.15, -1.0);
  67. sum += GRABPIXELY(0.18, 0.0);
  68. sum += GRABPIXELY(0.15, +1.0);
  69. sum += GRABPIXELY(0.12, +2.0);
  70. sum += GRABPIXELY(0.09, +3.0);
  71. sum += GRABPIXELY(0.05, +4.0);
  72. sum.rgb *= i.color.r;
  73. return lerp(col, sum, tint.a);
  74. }