shader 2d 影子

shader 简单应用 2d 影子

uv 翻转 v 后,采样目标纹理,舍弃透明部分,非透明部分按设定颜色输出。

image

Shader "Custom/ImageEffect/Shadow2d"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _ShadowColor("ShadowColor",Color)=(1,1,1,1)
    }
    SubShader
    {
        Cull Off ZWrite Off ZTest Always
        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;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv =float2(v.uv.x,1-v.uv.y);
                return o;
            }

            sampler2D _MainTex;
            fixed4 _ShadowColor;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                clip(col.a-0.1);
                return _ShadowColor;
            }
            ENDCG
        }
    }
}

posted @ 2025-07-22 14:15  晓叔  阅读(6)  评论(0)    收藏  举报