CommandBuffer.Blit BuiltinRenderTextureType.CameraTarget为空的问题

Forward模式下,当使用CommandBuffer来实现后处理的时候,发现cb.Blit(BuiltinRenderTextureType.CameraTarget,...)无效,在FrameDebug中看到传入的_MainTex为Whilte,(在Deferred渲染模式下有效)

原因:

在Forward模式下,默认相机是渲染到BackBuffer,而在渲染的时候,无法直接读取BackBuffer(Deffered模式下有GBuffer,所以没有问题),解决办法,手动Grab一次(调用cb.Blit指定入材质即可,有额外性能消耗),或者相机开启forceIntoRenderTexture。(注:必须5.6以后才生效,如果开启了HDR或者使用了OnRenderImage则不需要开启(5.6以上))

类似问题:

So it seems that when OnRenderImage is being used, or when HDR rendering is on, or when anything that needs the camera to render to an intermediate buffer is enabled, the camera automatically renders to an intermediate buffer instead of the backbuffer (screen) directly. This is nice.

However when Unity cannot predict that the camera is going to need an intermediate buffer, and your command buffer does a Blit(), things go wrong: since Unity cannot know in advance what you're doing in the CB, it grabs the image from the back buffer again and the Blit() (which does not know where the input is coming from) flips it.

The issue can be fixed by enabling the camera's "forceIntoRenderTexture". This forces Unity to quit rendering to the back buffer directly and always render the camera to an intermediate buffer (regardless of HDR state, whether or not there are image effects present, etc.)


参考

https://forum.unity3d.com/threads/commandbuffer-rendering-scene-flipped-upside-down-in-forward-rendering.415922/

https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.SetGlobalTexture.html


注:使用CommandBuffer.Blit的时候,_MainTex的传入需要手动调用CommandBuffer.SetGlobalTexture,并且Shader中的_MainTex属性设置成从外部设置(不暴露)。


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值