xak的专栏

Sometimes when I look at the world, I can see a line behind it.

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属性设置成从外部设置(不暴露)。


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cubesky/article/details/76573279
个人分类: Unity开发 游戏开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

CommandBuffer.Blit BuiltinRenderTextureType.CameraTarget为空的问题

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭