Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Fog Problem, Depth Buffer

Discussion in 'Shaders' started by spraycanmansam, Mar 29, 2014.

  1. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    [SOLVED] See 2nd post


    Hi all,

    I've ran into an annoying problem with our two fog effects for our game. Admittedly, I've known about the issue for a while, but put it in the 'fix-it-later' basket. We have two fog post effects: a volumetric fog for below the water surface, and an atmospheric fog.

    The problem is this: For the water fog, I need to fog based on the depth information of the terrain. For the atmospheric fog, I need to fog based on the ocean surface depth information so it doesn't fog the terrain underwater. Here's a picture illustrating the problem ---

    $depthprob.jpg

    As you can see the atmospheric fog extends down into the terrain due to the ocean not writing into the depth buffer. At the moment, I can have one or the other working as intended.

    I had thought I could render another depth texture with (or without) the ocean mesh, but any attempts to render with Unity's replacement depth shader resulted in all objects being written into the buffer as if they were opaque. This was obviously a problem when you looked at trees or foliage and the water fog is missing a big chunk of fog. A picture to illustrate ---

    $dprob2.jpg



    I would really appreciate any ideas or suggestions on how to get both working correctly.


    Kind Regards,
    Sam
     
    Last edited: Mar 29, 2014
  2. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    Ok, after all that, I actually managed to fix it.. :rolleyes: Normally I'd scrap the thread and mark it solved, but I'd rather explain what the problem was.. it certainly took me long enough to twig.

    I had thought that my original solution (rendering another depth texture for one of the fogs) would not work as I couldn't get the replacement shader to render transparent objects properly. Turns out it was a mixup on my part with the replacement shader settings. I had mis-understood the "replacementTag" parameter for the SetReplacementShader() function. I had mixed the tag up with the value.
    For example: "RenderType"="Opaque" ... "RenderType" is the tag and "Opaque" is the value, according to the docs. So the problem was solved using
    Code (csharp):
    1. camera.SetReplacementShader(shader, "RenderType");

    Silly mixup, gotta love em :rolleyes: :p
    I found the answer over in this thread, one of Aubergines posts about 2/3rds down the thread ---
    http://forum.unity3d.com/threads/130705-Simple-Multi-Pass-Rendering
     
  3. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    So how the result is?? can you post screenshot?
     
  4. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
  5. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    Sure :)
    Don't mind the lighting, it's more just to illustrate the two effects working together how they're meant to --- $FogTest.jpg
     
  6. ProFlares

    ProFlares

    Joined:
    Nov 1, 2013
    Posts:
    154
    Looks pretty :)

    Thanks for the info.
     
  7. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    That's pretty cool. the only thing that missing from WaterPro4 is the fog compatibility...
    Thanks for the info :)
     
  8. waka324

    waka324

    Joined:
    Sep 19, 2013
    Posts:
    3
    I'm solving this in an interesting way... I AM writing to the depth buffer in the ocean shader, and placing it one lower in the render queue. The only caveat is that transparent objects won't render correctly on the opposite side of the water. In my application, this works as you don't need to really go under water.
     
  9. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Nice water, is it Unity's built-in one or a 3rd-party or homemade shader?
     
  10. sgoodrow

    sgoodrow

    Joined:
    Sep 28, 2012
    Posts:
    150
    Were you able to get this to work with refraction from beneath the water as well? When I enable zwriting, the depth comparison for edge blending and foam no longer have the backdrop to compare against, so the depth of the water is zero and it looks transparent...
     
    Last edited: Aug 19, 2014