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
}

 

上一篇:C#:预处理器指令(转载16)


下一篇:'$' is not defined