五彩斑斓的黑+高斯模糊

  1 Shader "xShader/UGUIImage"
  2 {
  3  Properties
  4     {
  5         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
  6         _Color ("Tint", Color) = (1,1,1,1)
  7 
  8         _StencilComp ("Stencil Comparison", Float) = 8
  9         _Stencil ("Stencil ID", Float) = 0
 10         _StencilOp ("Stencil Operation", Float) = 0
 11         _StencilWriteMask ("Stencil Write Mask", Float) = 255
 12         _StencilReadMask ("Stencil Read Mask", Float) = 255
 13 
 14         _ColorMask ("Color Mask", Float) = 15
 15 
 16         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
 17 
 18         // Gaussian Blur
 19         _TextureSize ("_TextureSize",Float) = 256
 20         _BlurRadius ("_BlurRadius",Range(0,4) ) = 3
 21         // Gray
 22         _Gray ("_Gray",Range(0,1) ) = 0.2
 23     }
 24 
 25     SubShader
 26     {
 27         Tags
 28         {
 29             "Queue"="Transparent"
 30             "IgnoreProjector"="True"
 31             "RenderType"="Transparent"
 32             "PreviewType"="Plane"
 33             "CanUseSpriteAtlas"="True"
 34         }
 35 
 36         Stencil
 37         {
 38             Ref [_Stencil]
 39             Comp [_StencilComp]
 40             Pass [_StencilOp]
 41             ReadMask [_StencilReadMask]
 42             WriteMask [_StencilWriteMask]
 43         }
 44 
 45         Cull Off
 46         Lighting Off
 47         ZWrite Off
 48         ZTest [unity_GUIZTestMode]
 49         Blend SrcAlpha OneMinusSrcAlpha
 50         ColorMask [_ColorMask]
 51 
 52         Pass
 53         {
 54             Name "Default"
 55         CGPROGRAM
 56             #pragma vertex vert
 57             #pragma fragment frag
 58             #pragma target 2.0
 59 
 60             #include "UnityCG.cginc"
 61             #include "UnityUI.cginc"
 62 
 63             #pragma multi_compile __ UNITY_UI_CLIP_RECT
 64             #pragma multi_compile __ UNITY_UI_ALPHACLIP
 65 
 66             struct appdata_t
 67             {
 68                 float4 vertex   : POSITION;
 69                 float4 color    : COLOR;
 70                 float2 texcoord : TEXCOORD0;
 71                 UNITY_VERTEX_INPUT_INSTANCE_ID
 72             };
 73 
 74             struct v2f
 75             {
 76                 float4 vertex   : SV_POSITION;
 77                 fixed4 color    : COLOR;
 78                 float2 texcoord  : TEXCOORD0;
 79                 float4 worldPosition : TEXCOORD1;
 80                 UNITY_VERTEX_OUTPUT_STEREO
 81             };
 82 
 83             sampler2D _MainTex;
 84             fixed4 _Color;
 85             fixed4 _TextureSampleAdd;
 86             float4 _ClipRect;
 87             float4 _MainTex_ST;
 88 
 89             float4 _MainTex_TexelSize;
 90 
 91             // Gaussian Blur
 92             float _TextureSize;
 93             float _BlurRadius;
 94 
 95             //Gray
 96             float _Gray;
 97 
 98             float4 GetBlurColor( float2 uv,fixed4 inColor)
 99             {
100                 float space = 1.0/_TextureSize; //算出一个像素的空间
101                 int count = _BlurRadius * 2  + 1; //取值范围
102                 count *= count;
103                 //将以自己为中心,周围半径的所有颜色相加,然后除以总数,求得平均值
104                 float4 colorTmp = float4(0,0,0,0);
105                 for( int x = -_BlurRadius ; x <= _BlurRadius ; x++ )
106                 {
107                     for( int y = -_BlurRadius ; y <= _BlurRadius ; y++ )
108                     {
109                         // float4 color = tex2D(_MainTex,uv + float2(x * space,y * space));
110                         half4 color = (tex2D(_MainTex,uv + float2(x * space,y * space))) * inColor;
111                         colorTmp += color;
112                     }  
113                 }
114                 return colorTmp/count;
115             }
116 
117 
118             v2f vert(appdata_t v)
119             {
120                 v2f OUT;
121                 UNITY_SETUP_INSTANCE_ID(v);
122                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
123                 OUT.worldPosition = v.vertex;
124                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
125 
126                 OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
127 
128                 OUT.color = v.color * _Color;
129                 return OUT;
130             }
131 
132 
133             fixed4 frag(v2f IN) : SV_Target
134             {
135 
136                 half4 color;
137 
138                 if(_BlurRadius == 0) {
139                     color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
140                 }else {
141                     // Gaussian Blur
142                     color = GetBlurColor(IN.texcoord,IN.color);
143                 }
144 
145                 // 五彩斑斓的灰
146                 if(_Gray>0) {
147                     float baseGray = (color.r * 0.299f + color.g * 0.578f + color.b * 0.114f);
148                     color.r = baseGray * (1.0f -_Gray) + color.r * _Gray ;
149                     color.g = baseGray * (1.0f - _Gray) + color.g * _Gray ;
150                     color.b = baseGray * (1.0f - _Gray)  + color.b * _Gray ;
151                 }
152 
153                 #ifdef UNITY_UI_CLIP_RECT
154                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
155                 #endif
156 
157                 #ifdef UNITY_UI_ALPHACLIP
158                 clip (color.a - 0.001);
159                 #endif
160 
161                 return color;
162             }
163         ENDCG
164         }
165     }
166 }

 

posted @ 2022-02-22 17:46  czjone  阅读(53)  评论(0编辑  收藏  举报