Shader "BF/UI/UITutorial" { Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _Color ("Tint", Color) = (1,1,1,1) _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 _ColorMask ("Color Mask", Float) = 15 [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 //-------------------add---------------------- _Center ("Center", vector) = (0, 0, 0, 0) // _Blur ("Blur", Range(1,100)) = 25 // 边缘虚化 // _Radius ("Silder", Range(0,1000)) = 100 // 圆形半径 _Ellipse("_Ellipse", Range(0,100)) = 10 // 椭圆系数 _Rect_X ("Rect_X", Range(0,1500)) = 1500 _Rect_Y ("Rect_Y", Range(0,1500)) = 1500 //-------------------add---------------------- } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" } Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_ColorMask] Pass { Name "Default" CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 2.0 #include "UnityCG.cginc" #include "UnityUI.cginc" #pragma multi_compile __ UNITY_UI_ALPHACLIP struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; float4 worldPosition : TEXCOORD1; float2 grey : TEXCOORD2; }; sampler2D _MainTex; fixed4 _Color; fixed4 _TextureSampleAdd; float4 _ClipRect; float4 _MainTex_ST; //-------------------add---------------------- // float _Blur; // float _Radius; float2 _Center; float _Ellipse; float _Rect_X; float _Rect_Y; //-------------------add---------------------- v2f vert(appdata_t v) { v2f OUT; OUT.worldPosition = v.vertex; OUT.vertex = UnityObjectToClipPos(OUT.worldPosition); OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); OUT.color = _Color * v.color; int g_greyFactor = step(v.color.g, 0.001); int rb_greyFactor = step(1.999, v.color.r + v.color.b); int greyFactor = g_greyFactor * rb_greyFactor; OUT.grey = float2(greyFactor , 1 - greyFactor); return OUT; } fixed4 frag(v2f IN) : SV_Target { half4 texColor = tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd; half4 color = texColor * IN.color; float grey = dot(texColor.rgb, float3(0.299, 0.587, 0.114)); color.rgb = float3(grey * IN.grey.x + color.r * IN.grey.y, grey * IN.grey.x + color.g * IN.grey.y, grey * IN.grey.x + color.b * IN.grey.y); color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); #ifdef UNITY_UI_ALPHACLIP clip (color.a - 0.001); #endif //-------------------add---------------------- // float circle_dis = distance(IN.worldPosition.xy, _Center.xy); // fixed circle_tmp = step(circle_dis, _Radius); // float circle_blur = (clamp(circle_dis, _Radius - _Blur, _Radius) - (_Radius - _Blur)) / _Blur; // color.a *= (1 - circle_tmp) + circle_tmp * circle_blur; float rect_x_dis = abs(IN.worldPosition.x - _Center.x); float rect_y_dis = abs(IN.worldPosition.y - _Center.y); fixed rect_factor = clamp(pow(abs(rect_x_dis / _Rect_X), _Ellipse) + pow(abs(rect_y_dis / _Rect_Y), _Ellipse), 0.0, 1.0); fixed rect_tmp = step(rect_factor, 1.0f); color.a *= (1 - rect_tmp) + rect_tmp * rect_factor; color.rgb *= color.a; //-------------------add---------------------- return color; } ENDCG } } }