深入URP之Shader篇12: 深度值专题(3)

2,662 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第12天,点击查看活动详情

深度图及其应用

前两篇介绍了深度相关的一些知识,和URP Shader中提供的函数。而我们处理的深度一般会来自于深度图,本篇介绍URP Shader中深度图的使用。

深度图的生成

URP中有两种方式可以生成深度图。

  • 在情况允许的情况下,URP会在渲染完不透明物体之后使用一个Copy Depth Pass将场景的深度copy到深度图_CameaDepthTexture中。
  • 如果情况不允许直接copy,则会增加一个额外的PreDepthPass,这个不是就一个pass,而是一组pass,对于所有shader中包含DepthOnlyPass的物体都使用DepthOnlyPass绘制一遍深度到_CameraDepthTexture中。 看起来,PreDetphPass麻烦很多,虽然每次绘制只会更新depth texture,但是毕竟增加了很多draw call。那么什么情况不允许直接Copy Depth呢?这个之前分析Depth Only Pass时已经说过了。

Shader中定义深度图

深度图的定义在Packages\com.unity.render-pipelines.universal\ShaderLibrary\DeclareDepthTexture.hlsl中:

TEXTURE2D_X_FLOAT(_CameraDepthTexture);
SAMPLER(sampler_CameraDepthTexture);

float SampleSceneDepth(float2 uv)
{
    return SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(uv)).r;
}

深度图的采样

使用上面的SampleSceneDepth函数,内部使用了一个宏SAMPLE_TEXTURE2D_X,这个宏封装了VR的使用,对于正常渲染就是SAMPLE_TEXTURE2D,传入texture, sampler和uv坐标,得到该uv处的texel。由于现在的Unity版本已经全平台支持深度纹理格式了,所以深度图里面存的就是深度,不需要再decode了。(之前有些平台不支持深度纹理,会将深度值encode到普通纹理的颜色通道中,因此采样后需要decode)

深度图的应用

软粒子

普通的粒子面片和背景的交叉没有过渡,会显得很硬。比如早期CS游戏中的烟雾弹,烟雾扩散时可以看到半透明的片插入地面和墙面。而软粒子效果是指根据粒子的深度和背景上已有像素的深度计算出一个alpha值,用来做alpha混合,这样粒子效果就比较柔和了。 看一下URP中实现软粒子的shader代码:

// Soft particles - returns alpha value for fading particles based on the depth to the background pixel
float SoftParticles(float near, float far, float4 projection)
{
    float fade = 1;
    if (near > 0.0 || far > 0.0)
    {
        float sceneZ = LinearEyeDepth(SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(projection.xy / projection.w)).r, _ZBufferParams);
        float thisZ = LinearEyeDepth(projection.z / projection.w, _ZBufferParams);
        fade = saturate(far * ((sceneZ - near) - thisZ));
    }
    return fade;
}
  • 大部分都是我们看过的,先使用SAMPLE_TEXTURE2D_X采样_CameraDepthTexture获取到场景图中当前位置的深度,采样使用的uv坐标是projection.xy / projection.w,因为projection是clip space坐标,除w后变成NDC坐标,Unity内部处理为0~1之间,这就是屏幕空间坐标了。然后使用LinearEyeDepth转成view space的线性深度值。
  • 然后计算当前这个粒子的片段的线性深度值,使用projection.z / projection.w得到NDC的深度,然后转成view space的线性深度。
  • 这之后使用sceneZthisZ和near,far计算出alpha值。那么这是什么意思?near和far又是什么?找一下调用的地方看下:
 #if defined(_SOFTPARTICLES_ON)
     ALBEDO_MUL *= SoftParticles(SOFT_PARTICLE_NEAR_FADE, SOFT_PARTICLE_INV_FADE_DISTANCE, projectedPosition);
 #endif

找一下这两个宏的定义:

#define SOFT_PARTICLE_NEAR_FADE _SoftParticleFadeParams.x
#define SOFT_PARTICLE_INV_FADE_DISTANCE _SoftParticleFadeParams.y

再找一个_SoftParticleFadeParams赋值的地方:

            // Soft particles
            var useSoftParticles = false;
            if (material.HasProperty("_SoftParticlesEnabled"))
            {
                useSoftParticles = (material.GetFloat("_SoftParticlesEnabled") > 0.0f && isTransparent);
                if (useSoftParticles)
                {
                    var softParticlesNearFadeDistance = material.GetFloat("_SoftParticlesNearFadeDistance");
                    var softParticlesFarFadeDistance = material.GetFloat("_SoftParticlesFarFadeDistance");
                    // clamp values
                    if (softParticlesNearFadeDistance < 0.0f)
                    {
                        softParticlesNearFadeDistance = 0.0f;
                        material.SetFloat("_SoftParticlesNearFadeDistance", 0.0f);
                    }

                    if (softParticlesFarFadeDistance < 0.0f)
                    {
                        softParticlesFarFadeDistance = 0.0f;
                        material.SetFloat("_SoftParticlesFarFadeDistance", 0.0f);
                    }
                    // set keywords
                    material.SetVector("_SoftParticleFadeParams",
                        new Vector4(softParticlesNearFadeDistance,
                            1.0f / (softParticlesFarFadeDistance - softParticlesNearFadeDistance), 0.0f, 0.0f));
                }
                else
                {
                    material.SetVector("_SoftParticleFadeParams", new Vector4(0.0f, 0.0f, 0.0f, 0.0f));
                }
                CoreUtils.SetKeyword(material, "_SOFTPARTICLES_ON", useSoftParticles);
            }

这个代码位于Packages\com.unity.render-pipelines.universal\Editor\ShaderGUI\ParticleGUI.cs中,和我们一开始研究unlit shader时属性处理的BaseShaderGUI一样,都是在编辑器里面给材质设置属性和关键字用的。从这儿的逻辑看,如果开启了软粒子,SoftParticles的参数near就是softParticlesNearFadeDistance,即软粒子开始fade的深度值;参数far就是1.0f / (softParticlesFarFadeDistance - softParticlesNearFadeDistance) 即软粒子fade范围深度的倒数。在编辑器UI中,指定的是fade的范围,实际的near distance和far distance,然后转换成函数需要使用的参数。不得不说,这儿的far参数很有迷惑性,不看源码真不知道是啥。好了现在可以分析这个fade是怎么计算的了,转成基于nearDistance和farDistance的表达式:

fade = saturate( (sceneZ - thisZ - nearDis ) / (farDis - nearDis) );

首先我们使用的都是view space的线性深度,sceneZ - thisZ说明我们需要粒子在场景背景的前面(如果这个差是负数就会被saturate clamp为0,也就不会显示出来了),此时这个差值表示粒子在场景背景前面多远。当这个差值大于nearDis的时候就需要过渡了,过渡的范围就是nearDis到farDis之间,得到一个0~1之间的fade值。最终fade值再乘到粒子的alpha上进行混合。

本篇总结

本篇中分析了深度图的生成,定义和采样,结合软粒子效果看了一下深度图和线性深度的应用。