Unity里用shader让物体显示出边框效果

其实有个很简单的办法,不过仅对尽量凸的物体很有效,不管是2d,3d,

因为unity里使用的object其自身坐标系的原点在其形式中心,所以可以直接

沿着vertex的方向进行扩放,下面这个shader是我试验的一个2d物体Quad,


Shader "Custom/guangquan" {
    Properties {
        _Color ("Surround Color"Color) = (0.3,0.3,0.3,1)
        _MainTex ("Base (RGB)"2D) = "white" {}
        _Range("Range",Range(0.1,0.3))=0.15
    }
    SubShader {
    Tags{"RenderType"="Opaque"}
        //Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
        LOD 100
        
        Pass{
        //Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        //Blend One Zero
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            uniform float _Range;
            uniform float4 _Color;
            struct appdata_t
            {
                float4 vertex : POSITION;
                //half4 color : COLOR;
                //float2 texcoord : TEXCOORD0;
            };
            struct v2f
            {
                float4 vertex : POSITION;
                half4 color : COLOR;
                //float2 texcoord : TEXCOORD0;
                //float2 worldPos : TEXCOORD1;
            };

            v2f vert (appdata_t v)
            {
                v2f o;
                v.vertex.xyz+=_Range*v.vertex.xyz;
                o.vertex = mul(UNITY_MATRIX_MVPv.vertex);
                o.color = _Color;//v.color;
                //o.texcoord = v.texcoord;
                //o.worldPos = TRANSFORM_TEX(v.vertex.xy_MainTex);
                return o;
            }

            half4 frag (v2f IN) : COLOR
            {
                return IN.color;
                // Sample the texture
        //        half4 col = IN.color;
        //        col.a *= tex2D(_MainTexIN.texcoord).a;

        //        float2 factor = abs(IN.worldPos);
        //        float val = 1.0 - max(factor.xfactor.y);

                // Option 1: 'ifstatement
        //        if (val < 0.0col.a = 0.0;

                // Option 2no 'ifstatement -- may be faster on some devices
                //col.a *= ceil(clamp(val0.01.0));

        //        return col;
            } 
            ENDCG
        }
        
        Pass{
            Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
            Blend One Zero
            Offset -1,-1
            ColorMaterial AmbientAndDiffuse
            
            SetTexture [_MainTex]
            {
                Combine Texture
            }
        }
    
    // third part
    } 
    //FallBack "Diffuse"
}

你可能感兴趣的:(Unity里用shader让物体显示出边框效果)