Unity Shader--Alpha Test
阿新 • • 發佈:2018-12-28
Alpha測試:
符合條件的alpha畫素顯示出來,不符合的丟棄掉
AlphaTest Greater 0.5
AlphaTest Always 0.5表示關閉測試,總是通過
AlphaTest Never 0.5 表示總是不通過,不會渲染出來
變化引數
_AlphaCut("Alpha cut", float) = 0.5
AlphaTest Greater [_AlphaCut]
shader1.0中Alpha Test程式碼在如下:
Shader "Unlit/AlphaShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _AlphaCut("Alpha cut", float) = 0.5 } SubShader { //開啟AlphaTest //AlphaTest Greater 0.5 //AlphaTest Never 0.5 AlphaTest Greater [_AlphaCut] Pass { SetTexture[_MainTex] { combine texture } } } }
執行效果圖:
AlphaTest在shader2.0中不起作用,因為2.0中可以對紋理進行取樣變成,所以不需要進行AlphaTest,就可以直接獲取到alpha值
程式碼如下:
Shader "Unlit/AlphaTest2.0shader" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { AlphaTest Greater 0.5 Blend SrcAlpha OneMinusSrcAlpha 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; }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.uv; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); if (col.a < 0.5) { return fixed4(0, 0, 0, 0); } else { return col; } return col; } ENDCG } } }
執行效果圖: