【UnityShader】透明度測試

透明度度測試缺點

透明度度測試結果,要麼完全透明,要麼完全不透明;而且,得到的透明度效果在邊緣處往往參差不齊,有鋸齒,這是因爲在邊界處紋理的透明度的變化精度問題。

透明度測試基本原理:

只要一個片元的透明度不滿足條件(通常是小於某個閾值),那麼它對應的片元就會被捨棄。被捨棄的片元將不會再進行任何處理,也不會對顏色緩衝產生任何影響;否則,就會按照普通的不透明物體的處理方法來處理它。

透明度測試不需要關閉深度寫入

可以利用深度緩衝按逐像素的粒度進行深度排序,保證渲染的正確性。

通常使用 Clip 函數來進行透明度測試

函數:void clip(float4 x);void clip(float3 x);void clip(float2 x);void clip(float x)
參數:裁剪時使用的標量或矢量條件
描述:如果給定參數的任何一個分量是負數,就會捨棄當前像素的輸出顏色。等同於

void clip(float4 x)
{
	if(any(x<0))
		discard;
}
Tags

通常,使用了透明度測試的 Shader 都應該在SubShader 中設置這三個標籤
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True"}
說明:
"Queue"="AlphaTest"開啓透明度測試
"RenderType"="TransparentCutout"可以讓Unity把這個shader歸入到提前定義的組(這裏就是TransparentCutout組),以指明該shader是一個使用了透明測試的Shader。RenderType標籤通常被用於着色器替換功能。
"IgnoreProjector"="True"這個shader忽略投影器(Projectors)的影響。

FallBack

FallBack "Transparent/Cutout/VertexLit"
由於 Transparent/Cutout/VertexLit中計算透明度測試時,使用了 _Cutoff 的屬性來進行透明度測試,因此,這要求我們的shader中必須提供名爲 _Cutoff 的屬性,否則同樣無法得到正確的結果。

注意

處理透明度測試時,實際上對待普通的不透明物體幾乎是一樣的,只是在片元着色器中增加了對透明度判斷並裁剪片元的代碼。

Shader "zjt/Shader8_1_test"
{
    Properties
    {
        _Color("Color", COlor) = (1,1,1,1)
        _MainTex("Maint Tex", 2D) = "white"{}
        _Cutoff("Alpha Cutoff", Range(0, 1)) = 0.5 // 注意,FallBack中 Transparent/Cutout/VertexLit 計算透明度時,使用了 _Cutoff 屬性
    }
    SubShader
    {
        // 通常,使用了透明度測試的 Shader 都應該在SubShader 中設置這三個標籤
        // "Queue"="AlphaTest" 開啓透明度測試
        // "RenderType"="TransparentCutout" 可以讓Unity把這個shader歸入到提前定義的組(這裏就是TransparentCutout組),
        // 以指明該shader是一個使用了透明測試的Shader。RenderType標籤通常被用於着色器替換功能。
        // "IgnoreProjector"="True" 這個shader忽略投影器(Projectors)的影響。
        Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True"}
        LOD 200
        Pass
        {
            Tags{"LightMode"="ForwardBase" }

            CGPROGRAM
            #pragma vertex vert 
            #pragma fragment frag 
            #include "Lighting.cginc"

            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Cutoff;

            struct a2v
            {
                float4 position:POSITION;
                float4 texcoord:TEXCOORD0;
                float3 normal:NORMAL;
            };
            struct v2f
            {
                float4 pos:SV_POSITION;
                float2 uv:TEXCOORD0;
                float3 worldNor:TEXCOORD1;
                float3 worldPos:TEXCOORD2;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.position);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.worldNor = UnityObjectToWorldNormal(v.position);
                o.worldPos = mul(unity_ObjectToWorld, v.position).xyz;
                return o;
            }

            fixed4 frag(v2f IN):SV_Target
            {
             	fixed4 texColor = tex2D(_MainTex, IN.uv);
                clip(texColor.a - _Cutoff); // 當 texColor.a - _Cutoff 爲負時,捨棄該片元的輸出,該片元產生完全透明的效果

                fixed3 worldNor = normalize(IN.worldNor);
                fixed3 worldLight = normalize(UnityWorldSpaceLightDir(IN.worldPos));

                fixed3 albedo = texColor.rgb * _Color.rgb;
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
                fixed3 diffuse = _LightColor0.rgb * albedo * (0.5 * dot(worldNor, worldLight) + 0.5);
                return fixed4(ambient + diffuse, 1);
            }
            ENDCG
        }
    }
    FallBack "Transparent/Cutout/VertexLit"
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章