鸟语天空
模型淡入淡出
post by:追风剑情 2021-12-29 18:40

Shader

Shader "Custom/HalfMask"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", Color) = (1, 1, 0, 0)
        _Height("Height", range(0, 1)) = 1
        _Fade ("Fade", range(0.1, 1.0)) = 1
    }
    SubShader
    {
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
        LOD 100

        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha

            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;
                float3 worldPos : TEXCOORD1;
                float3 pivotPos : TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            fixed _Height;
            fixed _Fade;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                //模型顶点坐标转世界坐标
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                //模型原点(中心点)坐标转世界坐标
                o.pivotPos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                //y=[-1, 1]
                float y = i.worldPos.y - i.pivotPos.y;
                //y=[-1, 0]
                y =  (y - 1) / 2;
                //y=[0, 1]
                y += _Height;
                //当_Height从0增加到1时,模型将从上到下逐渐显示全身
                clip(y);//y为负数的像素会被裁剪掉
                fixed4 col = tex2D(_MainTex, i.uv);
                //颜色渐变(淡出)
                col = lerp(_Color, col, saturate(y / _Fade));
                return col;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

效果

111111111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容