unity烘焙静态光照贴图里的UnityMetaFragment方法

作用:

决定片元着色器要返回的是主纹理还是自发光颜色,以及如何编码输出结果。

源码和解析:

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两个分量。

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值