Unity creates a border effect for a rectangular area based on Cube

Without further ado, let’s take a look at the renderings first

 code show as below:

Shader "Hidden/CubeEdge"
{
    Properties
    {
        _Color("Color",Color) = (1,1,1,1)                 //颜色
    }
    SubShader
    {
        Cull Off ZWrite OFF ZTest Less                    //关闭剔除 关闭深度写入 深度比较模式为Less
        Blend SrcAlpha OneMinusSrcAlpha                   //设置混合模式

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float4 color: COLOR;                       //声明一个顶点颜色
            };

            fixed4 _Color;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                float a = step(v.vertex.y, 0);                     //这里用于筛选Cube顶点 顶点坐标小于0的 alpha设置为1 大于0的设置为0
                a *= (sin(_Time.y * 5) + 1) * 0.5;                 //通过sin函数 将alpha设置为周期性变化 且把值域限定到0-1
                o.color = fixed4(_Color.rgb, a);                   //设置顶点颜色
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return i.color;                                    //直接输出顶点颜色 通过逐像素自动插值 达到渐变过度效果
            }
            ENDCG
        }
    }
}

Notice:

After generating the material ball through the shader and assigning it to the Cube, you may see something like this. At this time, you can get the desired effect by adjusting the Position and Scale of the Cube and placing the bottom under other models.

 

Guess you like

Origin blog.csdn.net/chooselove/article/details/127156682