unity烘焙靜態光照貼圖裡的UnityMetaFragment方法
阿新 • • 發佈:2021-02-13
技術標籤:Unity Shader
作用:
決定片元著色器要返回的是主紋理還是自發光顏色,以及如何編碼輸出結果。
原始碼和解析:
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兩個分量。