作用:
决定片元着色器要返回的是主纹理还是自发光颜色,以及如何编码输出结果。
源码和解析:
half4 UnityMetaFragment (UnityMetaInput IN)
{
half4 res = 0;
#if !defined(EDITOR_VISUALIZATION)
// x分量代表的是:烘焙表面漫反射对其他物体的影响,也就是让物体表面漫反射的颜色,参与gi。
if (unity_MetaFragmentControl.x)
{
res = half4(IN.Albedo,1);
// d3d9 shader compiler doesn't like NaNs and infinity.
// 限制成0~1
unity_OneOverOutputBoost = saturate(unity_OneOverOutputBoost);
// 对应Lighting面板里面的lightmapping setting - Albedo Boost,数值越大,漫反射越亮,越容易反射出附近表面的颜色
// 经过幂运算后限制到0~unity_MaxOutputValue
// 目的是用于调整漫反射的亮度
res.rgb = clamp(pow(res.rgb, unity_OneOverOutputBoost), 0, unity_MaxOutputValue);
}
// 其中y分量代表的是:让物体的自发光,参与gi。
if (unity_MetaFragmentControl.y)
{
half3 emission;
// 当前计算是不是在线性空间
if (unity_UseLinearSpace)
emission = IN.Emission;
else
emission = GammaToLinearSpace(IN.Emission); //转换到线性空间
res = half4(emission, 1.0);
}
// 下面是关于编辑器可视化变体的代码,暂时忽略
#else
if ( unity_VisualizationMode == EDITORVIZ_PBR_VALIDATION_ALBEDO)
{
res = UnityMeta_pbrAlbedo(IN);
}
else if (unity_VisualizationMode == EDITORVIZ_PBR_VALIDATION_METALSPECULAR)
{
res = UnityMeta_pbrMetalspec(IN);
}
else if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
{
res = tex2D(unity_EditorViz_Texture, IN.VizUV);
if (unity_EditorViz_Decode_HDR.x > 0)
res = half4(DecodeHDR(res, unity_EditorViz_Decode_HDR), 1);
if (unity_EditorViz_ConvertToLinearSpace)
res.rgb = LinearToGammaSpace(res.rgb);
res *= unity_EditorViz_ColorMul;
res += unity_EditorViz_ColorAdd;
}
else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
{
float result = dot(unity_EditorViz_ChannelSelect, tex2D(unity_EditorViz_Texture, IN.VizUV).rgba);
if (result == 0)
discard;
float atten = 1;
if (unity_EditorViz_LightType == 0)
{
// directional: no attenuation
}
else if (unity_EditorViz_LightType == 1)
{
// point
atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
}
else if (unity_EditorViz_LightType == 2)
{
// spot
atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
float cookie = tex2D(unity_EditorViz_LightTextureB, IN.LightCoord.xy / IN.LightCoord.w + 0.5).w;
atten *= (IN.LightCoord.z > 0) * cookie;
}
clip(atten - 0.001f);
res = float4(unity_EditorViz_Color.xyz * result, unity_EditorViz_Color.w);
}
#endif // EDITOR_VISUALIZATION
return res;
}
如果你勾选了,Global Illumination,则烘焙是自发光,否则是物体表面主纹理反射的颜色。对应代码里的unity_MetaFragmentControl两个分量。