微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UnitySampleBakedOcclusion -- 对阴影蒙版采样

作用:

阴影蒙版取样,阴影蒙版可以在Window - Rendering - Lighting - Mixed Lightinig - Lighting Mode - Shadowmask启用

源码:

// 对阴影蒙版取样
// 需要光照贴图的UV坐标和世界位置作为输入参数
fixed UnitySampleBakedOcclusion (float2 lightmapUV, float3 worldPos)
{
    // 判断是否启用了阴影遮罩
    #if defined (SHADOWS_SHADOWMASK)
        // 判断是否启用光照贴图
        #if defined(LIGHTMAP_ON)
            // 利用光照贴图的uv对阴影遮罩贴图进行采样
            fixed4 rawOcclusionMask = UNITY_SAMPLE_TEX2D(unity_ShadowMask, lightmapUV.xy);
        #else
            // 没有启用静态光照贴图,则利用光照探头进行采样
            fixed4 rawOcclusionMask = fixed4(1.0, 1.0, 1.0, 1.0);
            #if UNITY_LIGHT_PROBE_PROXY_VOLUME
                if (unity_ProbeVolumeParams.x == 1.0)
                    rawOcclusionMask = LPPV_SampleProbeOcclusion(worldPos);
                else
                    rawOcclusionMask = UNITY_SAMPLE_TEX2D(unity_ShadowMask, lightmapUV.xy);
            #else
                rawOcclusionMask = UNITY_SAMPLE_TEX2D(unity_ShadowMask, lightmapUV.xy);
            #endif
        #endif
        // 阴影遮罩采样后的值是一个的四通道的值,这里根据当前是第几个灯光,取得对应的通道
        // unity_OcclusionMaskSelector解析:https://blog.csdn.net/zengjunjie59/article/details/113860903
        return saturate(dot(rawOcclusionMask, unity_OcclusionMaskSelector));

    #else

        //In forward dynamic objects can only get baked occlusion from LPPV, light probe occlusion is done on the cpu by attenuating the light color.
        fixed atten = 1.0f;
        #if defined(UNITY_INSTANCING_ENABLED) && defined(UNITY_USE_SHCOEFFS_ARRAYS)
            // ...unless we are doing instancing, and the attenuation is packed into SHC array's .w component.
            atten = unity_SHC.w;
        #endif

        #if UNITY_LIGHT_PROBE_PROXY_VOLUME && !defined(LIGHTMAP_ON) && !UNITY_STANDARD_SIMPLE
            fixed4 rawOcclusionMask = atten.xxxx;
            if (unity_ProbeVolumeParams.x == 1.0)
                rawOcclusionMask = LPPV_SampleProbeOcclusion(worldPos);
            return saturate(dot(rawOcclusionMask, unity_OcclusionMaskSelector));
        #endif

        return atten;
    #endif
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐