Shader学习20——常用透明AC,AB,AD

AC是指Alpha Cutout(又叫Alpha Test)即透明裁剪。通过clip函数直接移除<某个值的像素点。AC轮廓清晰,如果物体有排序问题通常使用AC,缺点是性能差。用于这种模型边缘复杂,仅通过贴图来做边缘的物体,可以看到树叶的前后关系都是正确的:

image.png
image.png

AB是指Alpha Blend即透明混合,边缘更柔和,但物体前后顺序错误,一般特效打底用。

image.png

AD是指Addtive 与AB类似,起到提亮作用,大多数特效使用此方法。

image.png

AC代码:

Shader "Unlit/AC"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Cutoff("透切阈值",Range(0.0,1.0))=0.5
    }
    SubShader
    {
        Tags{
            "RenderType"="TransparentCutout"
            "ForceNoShadowCasting"="True"
            "IgnoreProjector"="True"
        }

        Cull Off
        Pass
        {
            Tags{
                "LightMode"="ForwardBase"
            }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma muti_compile_fwdbase_fullshadows
            #pragma target 3.0

            #include "UnityCG.cginc"

            sampler2D _MainTex;float4 _MainTex_ST;

            half _Cutoff;

            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 = TRANSFORM_TEX(v.uv,_MainTex);
                return o;
            }


            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                half opacity=col.a;
                clip(opacity-_Cutoff);
                return float4(col.rgb,1.0);
            }
            ENDCG
        }
    }
}

AB代码:

Shader "Unlit/AB"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags{
            "Queue"="Transparent"
            "RenderType"="TransparentCutout"
            "ForceNoShadowCasting"="True"
            "IgnoreProjector"="True"
        }

        // Cull Off
        Pass
        {
            Tags{
                "LightMode"="ForwardBase"
            }
            //Src表示源,OneMinus表示1-,OneMinusSrcAlpha表示1-源的alpha
            //混合模式
            //源:One 或者 SrcAlpha
            //目标:OneMinusSrcAlpha
            Blend One OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma muti_compile_fwdbase_fullshadows
            #pragma target 3.0

            #include "UnityCG.cginc"

            sampler2D _MainTex;float4 _MainTex_ST;

            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 = TRANSFORM_TEX(v.uv,_MainTex);
                return o;
            }

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

AD代码:

Shader "Unlit/AD"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags{
            "Queue"="Transparent"
            "RenderType"="TransparentCutout"
            "ForceNoShadowCasting"="True"
            "IgnoreProjector"="True"
        }

        // Cull Off
        Pass
        {
            Tags{
                "LightMode"="ForwardBase"
            }
            //Src表示源,OneMinus表示1-,OneMinusSrcAlpha表示1-源的alpha
            //混合模式
            //源:One
            //目标:One
            Blend One One

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma muti_compile_fwdbase_fullshadows
            #pragma target 3.0

            #include "UnityCG.cginc"

            sampler2D _MainTex;float4 _MainTex_ST;

            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 = TRANSFORM_TEX(v.uv,_MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                return col;
            }
            ENDCG
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值