Unity3d《Shader篇》绘制圆角图片

Pass
        {
            CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
            
            #pragma fragment frag
            #include "UnityCG.cginc"
            
            sampler2D _MainTex;
            float4 _MainColor;
            float _RoundRadius;
            
            struct texCoord
            {
                float2 inputTexCoord : TEXCOORD0;
            };
            
            
            float4 frag(texCoord input) : COLOR
            {
                float x = input.inputTexCoord.x;
                float y = input.inputTexCoord.y;
                float4 col = tex2D(_MainTex,input.inputTexCoord)*_MainColor;
                
                float radius = _RoundRadius;
                
                if(x<radius && y<radius)
                {
                    if(pow(x-radius,2)+pow(y-radius,2)>radius*radius)
                    {
                        discard;
                    }
                }
                else if(x>1-radius && y>1-radius)
                {
                    if(pow(x-(1-radius),2)+pow(y-(1-radius),2)>radius*radius)
                    {
                        discard;
                    }
                }
                else if(x<radius && y>1-radius)
                {
                    if(pow(x-radius,2)+pow(y-(1-radius),2)>radius*radius)
                    {
                        discard;
                    }
                }
                
                else if(x>1-radius && y<radius)
                {
                    if(pow(x-(1-radius),2)+pow(y-radius,2)>radius*radius)
                    {
                        discard;
                    }
                }
                
                return col;
            }
            
            ENDCG
        }

效果图

posted @ 2015-06-19 16:24  MrBlue  阅读(1575)  评论(0编辑  收藏  举报