表面著色器官方文檔:https://docs.unity3d.com/Manual/SL-SurfaceShaders.html
1、定義:對(duì)頂點(diǎn)著色器和片元著色器進(jìn)一層封裝,主要部分為兩個(gè)結(jié)構(gòu)體(Input、SurfaceOutput)和編譯指令。
2、編譯指令:指明表面著色器使用表面的函數(shù)和光照函數(shù),并設(shè)置可選參數(shù)
#pragma surface surfaceFunction lightModel [optionalparams]
表面函數(shù)(SurfaceFunction):包含屬性友反射率、光滑度、透明度等,使用輸入結(jié)構(gòu)體Input 來(lái)設(shè)置各種屬性。
光照函數(shù):使用表面函數(shù)來(lái)模擬光照效果,Unity內(nèi)置基于物理的光照模型Standard 和
StandardSpecular,非物理光照模型函數(shù)Lambert 和 BlinnPhong。
其他可選參數(shù):
自定義修改函數(shù):
vertex:VertexFunction:頂點(diǎn)修改,實(shí)現(xiàn)一些頂點(diǎn)動(dòng)畫(huà)等
finalcolor:ColorFunction:最終顏色修改、實(shí)現(xiàn)霧效
finalgbuffer:ColorFunction:延遲渲染修改,實(shí)現(xiàn)邊緣檢測(cè)等
finalprepass:ColorFunction:Prepass base修改
陰影:
addshadow:添加一個(gè)陰影投射Pass,一般FallBack 通用的光照模式可以找到,對(duì)于頂點(diǎn)動(dòng)畫(huà)、
透明度需要特殊處理。
fullforwardshadows:在前向渲染路徑支持所有光源類(lèi)型的陰影,默認(rèn)是只有平行光,添加這個(gè)參數(shù)可以讓點(diǎn)光火聚光燈友陰影渲染。
noshadows:取消所有陰影。
透明度混合測(cè)試:
alpha、alpha:auto:透明測(cè)試
alpha:blend 透明混合
alphatest:VariableName:VariableName 用來(lái)剔除不滿(mǎn)足的片元
光照:
noambient:不應(yīng)用任何環(huán)境光照或者光照探針(light probe)
novertexlights:不應(yīng)用逐頂點(diǎn)光照
noforwardadd:去掉所有前向渲染的額外pass,即支持逐像素平行光,其他光源用逐頂點(diǎn)或者陰影計(jì)算。
控制代碼生成:
exclude_path:deferred,exclude_path:forward,exclude_path:prepass 不需要為特定渲染路徑生成代碼。
3、兩個(gè)結(jié)構(gòu)體
Input 結(jié)構(gòu)體
包含主紋理和法線紋理的采樣坐標(biāo)uv_MainTex 和 uv_BumpMap

SurfaceOutput:存儲(chǔ)表面屬性結(jié)構(gòu)體,作為光照函數(shù)輸入來(lái)計(jì)算光照
// Lighting.cginc中定義
struct SurfaceOutput
{
? ? fixed3 Albedo;? // diffuse color
? ? fixed3 Normal;? // tangent space normal, if written
? ? fixed3 Emission;
? ? half Specular;? // specular power in 0..1 range,高光反射指數(shù)部分。
? ? fixed Gloss;? ? // specular intensity,高光反射強(qiáng)度系數(shù)。
? ? fixed Alpha;? ? // alpha for transparencies
};
// UnityPBSLighing.cginc中定義
struct SurfaceOutputStandard
{
? ? fixed3 Albedo;? ? ? // base (diffuse or specular) color
? ? fixed3 Normal;? ? ? // tangent space normal, if written
? ? half3 Emission;
? ? half Metallic;? ? ? // 0=non-metal, 1=metal
? ? half Smoothness;? ? // 0=rough, 1=smooth
? ? half Occlusion;? ? // occlusion (default 1)
? ? fixed Alpha;? ? ? ? // alpha for transparencies
};
struct SurfaceOutputStandardSpecular
{
? ? fixed3 Albedo;? ? ? // diffuse color
? ? fixed3 Specular;? ? // specular color
? ? fixed3 Normal;? ? ? // tangent space normal, if written
? ? half3 Emission;
? ? half Smoothness;? ? // 0=rough, 1=smooth
? ? half Occlusion;? ? // occlusion (default 1)
? ? fixed Alpha;? ? ? ? // alpha for transparencies
};
4、表面著色器的渲染流水線

5、表面著色器的優(yōu)缺點(diǎn):
優(yōu)點(diǎn):表面著色器是在頂點(diǎn)-幾何-片元著色器上層的抽象封裝,不需要寫(xiě)復(fù)雜的代碼,直接定義好相關(guān)的參數(shù)和編譯指令就可以實(shí)現(xiàn)需要的效果。
缺點(diǎn):表面著色器必須按照它的規(guī)則去寫(xiě)代碼,自由度不高,而且內(nèi)部生成的頂點(diǎn)-幾何-片元著色器也不是最優(yōu)的,往往會(huì)產(chǎn)生性能上面的問(wèn)題。