URP renderfeature depth buffer为空的问题 depthattachment

花了我两天的时间才解决这个问题,记录下问题经过

起因

项目需要改渲染管线,在后处理之前插入一个后处理,然后特效再单独处理

过程

这里特效需要做深度测试,然后我用URP PostprocessPass抄来的代码发现depthbuffer丢失,如图:
在这里插入图片描述
一片黑啊,绘制代码如下

var colorLoadAction = RenderBufferLoadAction.DontCare;
if (m_Destination == RenderTargetHandle.CameraTarget && !cameraData.isDefaultViewport)
    colorLoadAction = RenderBufferLoadAction.Load;

// Note: We rendering to "camera target" we need to get the cameraData.targetTexture as this will get the targetTexture of the camera stack.
// Overlay cameras need to output to the target described in the base camera while doing camera stack.
RenderTargetIdentifier cameraTarget = new RenderTargetIdentifier(cameraData.targetTexture);
cameraTarget = (m_Destination == RenderTargetHandle.CameraTarget) ? cameraTarget : m_Destination.Identifier();

cmd.SetRenderTarget(cameraTarget, 
    colorLoadAction, RenderBufferStoreAction.Store,
    RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare);
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);


cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_Materials.uber);

所以根本就没绘制深度貌似,然后我也不知道怎么去绘制,搞了好久,后来发现用Blit就好了

Blit(cmd, GetSource(), GetDestination(),  m_Materials.uber);
Blit(cmd, GetDestination(), GetSource());

在这里插入图片描述

分析

打开ScriptableRendererPass,查看Blit的调用

public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material = null, int passIndex = 0)
{
    ScriptableRenderer.SetRenderTarget(cmd, destination, BuiltinRenderTextureType.CameraTarget, clearFlag, clearColor);
    cmd.Blit(source, destination, material, passIndex);
}

发现有个这个东西

BuiltinRenderTextureType.CameraTarget

原来是没传这个,不过cmd.setRenderTarget和 ScriptableRenderer.SetRenderTarget这个去设置肯定不一样,所以还是用后者吧

参考

顺便可以参考下这个https://github.com/Thousandyearsofwar/CommandBufferDemo/tree/main/Assets/Scripts/RenderFeatures/SetRenderTarget

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值