Search Unity

Unity 5 breaks global fog when using two cameras with deferred rendering

Discussion in 'Shaders' started by Steve2375, Mar 28, 2015.

  1. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    We are using two cameras in deferred mode: one is rendering the far scenery (near clip plane > 100m) and the other is rendering near the player (near clip plane = 0.1, far clip plane = 100). We are using the global fog image effect on the far camera to hide the end of the world.

    It seems Unity did never support his setup, but with 4.x it was possible to get it running using the CopyToScreenRT.cs script (see: http://forum.unity3d.com/threads/de...-posts-ssao-bugged-ufps-onrenderimage.198632/). But after switching to Unity 5 this does not work any more.

    Is nobody using the global fog (or any fog) with two cameras in deferred mode?
    Is there a workaround to this problem or any other solution to be able to render the far scenery with a separate camera?
     
  2. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    What is your problem, exactly? I am using Global Fog with 2 cameras in deferred and it is working fine. Could you post a screenshot of your camera setup (hierarchy, settings) and screenshot of the rendering problem?
     
  3. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    Thank you very much for your help!
    Here is my setup (the main camera renders up to 100m, the far camera starting from 100 to 4000):

    I enabled the Unity 5 GlobalFog effect on the far camera but do not see anything (all that happens is that the Time Of Day clouds get removed):
    pic1.jpg

    If I enable the global fog on the main camera it renders up to the 100m and then stops:
    pic2.jpg

    What I would like to have is this (render the fog in the distance and also over the TOD skydome, I took this screenshot with the old Unity 4 GlobalFog effect using only one camera):
    pic3.jpg

    Is there anything wrong with my setup?
     
  4. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    Sorry the inspector content might be too small.

    This is the inspector for the first image
    pic1_i.jpg

    And here for the second.
    pic2_i.jpg
     
  5. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    The problem is that both Camera and FarCamera are using different depth buffers. Global Fog renders itself based on the depth map and Camera is rendering only part of the scene thus, generating partial depth map. You should combine depth map from Camera and FarCamera and use cam which is on top to render Global Fog. You may do this in several ways:
    * copy Camera's depth buffer into RenderTexture, do the same for FarCamera. Pass that into the modified Global Fog shader and add them together
    * render whole scene with 3rd camera which renders whole scene and has all image effects (not optimal way)
    * try setting FarCam clear flags to "none". Most likely it will generate some graphics bugs (color buffer not clearing each frame) but it's worth checking


    If none of these is an option you could explain why you need this setup and maybe I'll come with some alternative solution.
     
  6. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    Thank you very much for the quick reply! I think I will need some time to try your suggested approach.

    Btw: to let me understand why there is no fog at all on the FarCamera, it is the fact that the main camera has a different depth buffer than the far camera and thus clears all image effects of the FarCamera, right?

    And: do I need to access the depth buffer from C# (to pass it to the new fog shader)? This guy needs to do the same: http://forum.unity3d.com/threads/accessing-depth-buffer-via-c.314962/. Hopefully there is an answer to this...

    > If none of these is an option you could explain why you need this setup and maybe I'll come with some alternative solution.
    If I use only one camera (with near plane of 0.1 and far plane of 4000) you'll see very strong flickering at the border of the distant lake (I use a water plane for this).
     
  7. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    The reason FarCamera has no Global Fog is because Camera has Depth Buffer generated from it's own (Camera) geometry and not Camera+FarCamera geometry. Also, Camera has depth of 0 and FarCamera has depth -1 which means that FarCamera is always on top. No matter what you'll do it will overlap Camera's output. I've just noticed that. My advice: since FarCamera renders stuff in the back, set it's depth to 1 instead of -1.

    http://docs.unity3d.com/ScriptReference/Camera-depth.html
     
  8. Steve2375

    Steve2375

    Joined:
    Mar 28, 2015
    Posts:
    42
    Maybe I am wrong but the documentation says
    > Cameras with lower depth are rendered before cameras with higher depth.
    so the FarCamera with depth -1 will be rendered before the camera with depth 0. At least if I set the FarCamera to 1 I do not see any output from the main camera any more (of course I also adjusted the Clear Flags).

    Can you show your configuration and maybe also a screenshot in which two cameras render the global fog on the far camera?

    Btw: regarding this http://issuetracker.unity3d.com/iss...am-makes-globalfog-and-other-effects-not-work it seems that this is a real issue with the Unity Engine.

    I searched and found a lot of posts regarding this problem during the last days and what makes me sad is that none of the Unity developers tried to help on this :(
     
  9. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Has anyone found a workaround for this? I'm using a six camera deffered rendering setup in an Oculus game and have lost fog entirely after migrating from Unity 4. Its pretty critical for my game. Any ideas?
     
  10. Beloudest

    Beloudest

    Joined:
    Mar 13, 2015
    Posts:
    247
    I'd love to know too, I tried going back to a mix of deferred and forward cam's but lost too many nice visual effects in the process. I have 3 cam's, 1. Player/World Cam (depth 0,clear skybox), 2.Weapon Cam (depth 1, depth only,culled), 3.UI/Helmet/PostEffects Cam (depth 2,depth only,culled). With all 3 deferred I can get no Global Fog. If I make cam 2 & 3 forward it works.
     
  11. Kas_

    Kas_

    Joined:
    Dec 27, 2017
    Posts:
    71
    Anyone ever find a solution here? I am running into this same issue