RadialBlurShader.shader 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. Shader "PostEffect/RadialBlurShader"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _IterationNumber("IterationNumber", Int) = 16
  7. _Intensity("Intensity", float) = 0.1
  8. _OffsetX("OffsetX", float) = 0.5
  9. _OffsetY("OffsetY", float) = 0.5
  10. }
  11. CGINCLUDE
  12. uniform sampler2D _MainTex;
  13. uniform float _Intensity;
  14. uniform float _OffsetX;
  15. uniform float _OffsetY;
  16. uniform int _IterationNumber;
  17. //uniform float _BlurFactor; //模糊强度(0-0.05)
  18. //uniform float4 _BlurCenter; //模糊中心点xy值(0-1)屏幕空间
  19. #include "UnityCG.cginc"
  20. //#define SAMPLE_COUNT 16 //迭代次数
  21. fixed4 frag(v2f_img i) : SV_Target
  22. {
  23. ////模糊方向为模糊中点指向边缘(当前像素点),而越边缘该值越大,越模糊
  24. //float2 dir = i.uv - _BlurCenter.xy;
  25. //float4 outColor = 0;
  26. ////采样SAMPLE_COUNT次
  27. //for (int j = 0; j < SAMPLE_COUNT; ++j)
  28. //{
  29. // //计算采样uv值:正常uv值+从中间向边缘逐渐增加的采样距离
  30. // float2 uv = i.uv - _BlurFactor * dir * j;
  31. // outColor += tex2D(_MainTex, uv);
  32. //}
  33. ////取平均值
  34. //outColor /= SAMPLE_COUNT;
  35. //return outColor;
  36. float2 center = float2(_OffsetX, _OffsetY);
  37. float2 uv = i.uv;
  38. uv -= center;
  39. float4 color = float4(0.0, 0.0, 0.0, 0.0);
  40. _Intensity *= 0.085;
  41. float scale = 1;
  42. for (int j = 1; j < _IterationNumber; ++j)
  43. {
  44. color += tex2D(_MainTex, uv * scale + center);
  45. scale = 1 + (float(j * _Intensity));
  46. }
  47. color /= (float)_IterationNumber;
  48. return color;
  49. }
  50. ENDCG
  51. SubShader
  52. {
  53. Pass
  54. {
  55. ZTest Always
  56. Cull Off
  57. ZWrite Off
  58. Fog{ Mode off }
  59. //调用CG函数
  60. CGPROGRAM
  61. //使效率更高的编译宏
  62. #pragma fragmentoption ARB_precision_hint_fastest
  63. //vert_img是在UnityCG.cginc中定义好的,当后处理vert阶段计算常规,可以直接使用自带的vert_img
  64. #pragma vertex vert_img
  65. #pragma fragment frag
  66. ENDCG
  67. }
  68. }
  69. Fallback off
  70. }