shader學習之路 透明度測試

2021-10-21 03:50:36 字數 1513 閱讀 9566

透明度測試:它採用一種「霸道極端」的機制,只要乙個片元的透明度不滿足條件(通常是小於某個閾值),那麼它對應的片元就會被捨棄。被捨棄的片元將不會再進行任何處理,也不會對顏色緩衝產生任何影響;否則,就會按照普通的不透明物體的處理方式來處理它,即進行深度測試、深度寫入等。也就是說,透明度測試是不需要關閉深度寫入的,他和其他不透明物體最大的不同就是他會根據透明度來捨棄一些片元。雖然建安,但是它產生的效果也很極端,要麼完全透明,即看不到,要麼完全不透明,就像不透明物體那樣。

通常,我們會在片元著色器中使用clip函式來進行透明度測試。clip是cg中的要給函式,他的定義如下   void clip(float4 x);void clip(flaot3 x);void clip(flaot2 x);void clip(flaot x);  引數:裁剪是使用的標量或者向量條件

描述:如果給定引數的任何乙個分量是負數,就會捨棄當前畫素的輸出顏色。它等同於下面的**

void clip(float4 x)

_cutoff ("alpha cutoff", range(0,1)) = 0.5

//用於決定我們呼叫clip進行透明度測試時使用的判斷條件。範圍[0,1]因為透明紋理畫素的透明度就在此範圍內

} subshader

//"rendertype" = "transparentcurout" rendertype標籤可以讓unity把這個shader歸入到提前定義的組 這裡是「transparentcurout」

//rendertype 通常被用於著色器替換功能 ignoreprojector 設定為true代表這個shader不會受投影器(projectors)的影響

pass

cgprogram

#pragma vertex vert

#pragma fragment frag

#include "lighting.cginc"

fixed4 _color;

fixed _cutoff; //由於_cutoff範圍在[0,1]我們用fixed儲存它

sampler2d _maintex;

float4 _maintex_st;

struct a2v;

struct v2f;

v2f vert(a2v v)

fixed4 frag(v2f i):sv_target

fixed3 albedo = texcolor.rgb * _color.rgb;

fixed3 ambient = unity_lightmodel_ambient.xyz * albedo;//unity_lightmode_ambient獲得環境光資訊

fixed3 diffuse = _lightcolor0.rgb * albedo * max(0,dot(worldnormal,worldlightdir));

return fixed4(ambient + diffuse ,1.0);

} endcg

} } fallback "transparent/cutout/vertexlit"

}

shader 透明度混合

透明度混合是實現了真正的半透效果。它會以當前片元透明度作為混合因子,與已經儲存在顏色緩衝區中的顏色進行混合,得到新的顏色。同時要關掉深度寫入,小心物體的渲染順序。用blend srcfactor dstfactor來進行混合。這個命令在設定混合因子的同時也開啟了混合模式,否則不會有混合效果,因為開啟...

透明度測試

shader custom testshader cutoff alpha cutoff range 0,1 0.5 subshader pass cgprogram pragma vertex vert pragma fragment frag include lighting.cginc fix...

透明度測試

透明度測試 只要乙個片元的透明度不滿足條件 小於某個閾值 它對應的片元就會被捨棄。被捨棄的片元不會再進行任何處理,也不會對顏色緩衝產生影響。否則,就會按照普通的不透明物體處理方式來處理它。使用函式 clip,如果給定的引數的任何乙個分量是負數,就會捨棄當前畫素顏色的輸出 如下 shader alph...