美文网首页
Shader学习20——常用透明AC,AB,AD

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

作者: ShawnWeasley | 来源:发表于2021-03-24 14:08 被阅读0次

    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
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Shader学习20——常用透明AC,AB,AD

          本文链接:https://www.haomeiwen.com/subject/iathhltx.html