Deferred Renderer + Multiple Cameras + Posts/SSAO = Bugged! (UFPS, OnRenderImage())

Hi,

it seems something strange is going on if u use the Deferred Renderer (Pro Version) and have multiple cameras setup with Post/Image effects.

As example UFPS uses the default FPS camera setup, which means having everything, except the weapon model in the main scene and overlay this with just a camera that renders the weapon model on top. This helps with z-fighting, since u can finetune the near-plane per camera. It also solves the problem of bigger weapon models clipping through near walls/objects.

The problem is that all image effects are broken for those setups and the Deferred Renderer. Don’t get confused by UFPS inspector camera values, those will be correctly overridden per script on play.

So the main camera correctly has camera.depth set to 0, while the weapon is set to 1, so the render order is ensured, also the weapon camera is set correctly to only clear-z. The main camera also correctly renders everything except weapons and the local player, while the weapon camera only renders the weapon.

So any image based effect that is attached to the main camera should correctly process only this camera/rendertarget inside OnRenderImage(), while the weapon camera should have no impact at all, if no image effect is attached to it.

What happens is, as if the “OnRenderImage()” function inside the SSAO script on the main camera, gets the final render result including all cameras, therefor the z-values are already cleared by the weapon camera and SSAO cant work.

The correct way would be to call and process OnRenderImage() per camera, not after all cameras are rendered. I suspect this is some kind of bug and not the general behavior of the Deferred Renderer, since each camera should always be processed in order and separately, since otherwise it would be impossible to have different image effects per camera.

so @Unity whats going on here?

thx Andy

no one can help on this?

Man I need some screenshots, showing the problem. :slight_smile:

Perhaps even a small sample project. I’ve used multiple cameras, deferred rendering and made it work. But I believe my main camera rendered last.

Did u attach image effects like the ambient obscurance/occlusion to the first rendered camera? The other way is to have 2 different setup image effects on both cameras, which should show that only the second version is applied.

I found the problem + fix and suspect its a wrong or missing resolve + copy in the deferred pipeline.

Here is the bug:
The first camera renders normally into the ScreenRT and than applies all image filters correctly in order, now the resolve+copy is missing, which means the second camera does not get the final image processed RT, but simply works with the normal screenRT, that has no image effects applied.

I suspect the normal screenRT is resolved into new separated RT for the “OnRenderImage(RenderTexture src, RenderTexture dest)” call to work and at the end the last RT is either not used as input for the next camera or is not copied over to the normal screenRT.

The fix is simple, at the end of the image effects a script copys the result back to the main screen RT, so the next camera correctly uses this as input.

I attached the script (CopyToScreenRT.cs), simply put it ordered lastly (after all image effect scripts) on the same camera that has image effects. This will make sure the processed RT is copied to the old screen RT the next camera gets as input.

bye Andy

PS: I also setup a example project (TestDeferredBug2.7z) showing the problem and fix in 2 scenes.

1348581–66265–$CopyToScreenRT.cs (1.06 KB)
1348581–66266–$TestDeferredBug2.7z (1.02 MB)

2 Likes

Your two scenes show the problem perfectly. In cases like this where you have a clearly wrong behavior with a simple repro you should just file the bug, and perhaps use the forum to raise awareness.

Anyways that is what I call a perfect repro project, I filed the bug report and passed it on to the developers!

Thanks

can you post a scene with multiple cameras? ssao works with script perfectly, but cameras - not.

The test scene i attached has 2 cameras, just check the main camera it has a secondary child camera, that mimics what normal FPS do.

maybe i dont understand you, but my goal - add another camera (so. main came will have 2 childs) and render at one camera weapon, at other - selected layer with glow, then main camera compose that. i achive that with simple cameras, but in UFPS - not.

Like i noted my script “CopyToScreenRT.cs” should be attached to all camera’s that also have image effects like SSAO attached, while my script needs to-be ordered last. There is nothing else required. If something does not work u have to check your camera “depth” values which basically act like layers, means the cameras are rendered one by one from lowest to highest depth value. Also check your clear flags, the second and all following cameras should only clear depth, not color/skybox.

Maybe explain in more detail why your 3 cameras in UFPS do what is not working atm?

PS: Also keep in mind that UFPS sets the layers and depth values via script, so all inspector values are overridden, for the main and weapon camera.

seems - that a problem, because i changed manually depth, culling and other, nothing help. will check it, thank you)

I’d the same problem as you (2 cameras with deferred lighting and image effects), and I’ve used your fix script, and all works perfectly! Thank you Andy2222! Problem solved!

Awesome! we’re using multiple cameras too… world, vehicle, viewmodel. this fix is priceless!

Just replying to thank you for fixing my DoF problem!! Previously the transparent parts were DoFed, so I split the rendering out to separate cameras… which killed the DoF effect!

You rule!! :smile:

Thanks for this script! It fixed all my problems with shaders like SSAO and the multiple camera setup in UFPS.

Works really well! however I found a bug when using this method and a shader that uses Grabpass. as soon as the shader goes on screen it’s like the depth buffer doesn’t get clear anymore and you only see second cam. I think it’s more a bug with unity though…

I’ve been avoiding multiple cameras because they never worked with image effects, but if this miracle script fixes everything I will marry you.

This bug Still exists in 4.6b10!

And this script still fixes it, so not all bad :slight_smile:

Hey!
Your script works very well, but I discovered a couple of similar bugs, clearly connected with deferred rendering and usage of multiple cameras.

Firstly, while using muliple cameras to render the environment with camera A and the gun with camera B, the gun does not receive shadows for some reason. This is also the case while using forward rendering.

Furthermore I discovered a gamebraking bug after updateing to Unity 4.3.4, which has not been fixed in Unity 4.5.
When I am using a second camera to render only some particular objects, the screen simple freezes when I am looking in certain directions. These directions seem to be random.

This makes my game unplayable and brought very much frustration with it.
It would be very nice if someone could help me with my problems or if anyone has to deal with similar issues.

Greets, Felix

I think I’ve experienced the same problem.

I found these 2 solutions:

  • Set the Clear Flags property of the first camera to Depth only
  • Enable anti-aliasing in the quality settings

Let me know if that fixes it

Did you ever get this script to work with UFPS? I am having no luck. My SSAO shuts off every time I press play and I’m thinking this is the problem.