- Home /
CommandBuffer's CameraEvent ordering not in sync with shader rendering queue?
I'm trying to use a camera script to get my camera to render to a RenderTexture via CommandBuffers. I also have a custom shader which I specifically set its Render Queue to "Queue" = "Overlay" because I don't want it to render geometry until the RenderTexture has been written because I don't want objects with my shader to be captured in the RenderTexture.
I added my CommandBuffer to the camera with the CameraEvent.BeforeImageEffects; CameraEvent because I want my CommandBuffer actions to be executed before the camera renders my custom shader.
Now this makes sense in theory because according to Unity's documentation my CommandBuffer should be executed before image effects are rendered and so the RenderTexture my CommandBuffer tells the camera to write to should NOT contain any geometry which uses my custom shader because since its Queue is set to "Overlay" it should be considered an image effect and rendered right after, which will give my custom shader access to the RenderTexture I just made which is necessary for my shader to work.
UNFORTUNATELY for some reason this doesn't work at all - the camera is executing my CommandBuffer after rendering my custom shader geometry and so its being captured in the RenderTexture and giving me an unwanted hall of mirrors effect on my shader.
Additionally, for some reason the Scene View camera ,when in the editor's Scene tab, doesn't execute my CommandBuffer at all, it seems to work if I set the CameraEvent to CameraEvent.AfterForwardAlpha but of course, this only works when the camera is using Forward rendering where I need it to work in any rendering path.
Is this a defect or am I missing something?
Answer by coldwarrior5 · Jun 01, 2020 at 10:24 AM
I had the same issue @kvekos. I have been playing with it for half an hour and I found the cause of the issue.
When it comes to logic, I agree that BeforeImageEffect should yield a texture with no effects applied. However, the way it is implemented you have to sort the command buffers manually in order to get it working which is confusing and is not mentioned in the documentation. Basically, the texture blit has to be the first command buffer in the list in order to execute before the effects get applied, again weird as you would expect it to work automatically given the Event is called BeforeImageEffects.
Here is an example that works for me:
_sceneCapture = new CommandBuffer
{
name = "Scene view"
};
RenderTargetIdentifier sceneViewID = new RenderTargetIdentifier(_sceneView);
_sceneCapture.Blit(BuiltinRenderTextureType.CurrentActive, sceneViewID);
var commandBuffers = _attachedCamera.GetCommandBuffers(CameraEvent.BeforeImageEffects);
if (!commandBuffers.Contains(_sceneCapture))
{
_attachedCamera.RemoveCommandBuffers(CameraEvent.BeforeImageEffects);
// First we add our effect
_attachedCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, _sceneCapture);
// And then we readd all of the others
foreach(CommandBuffer buffer in commandBuffers)
_attachedCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, buffer);
}