Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How do I get (computationally cheap) floor fog? Or how to get ValveFog to work with curent unity?

Discussion in 'General Graphics' started by KaiGarbe, Jul 27, 2022.

  1. KaiGarbe

    KaiGarbe

    Joined:
    Apr 5, 2022
    Posts:
    2
    I want to have a height dependent fog covering the entirety of my scene. I remember using the ValveFog from the Lab Renderer Package, for exactly this purpose back in the day. This however doesn't work with current Unity versions. How could I get the ValveFog to run wirth current unity versions, or are there other ways to acieve my goal? I don't want to use Volumetric fog or particle systems, because I want to cover a large area (as far as the player can see) with the fog, and I fear that this would be too computationally expensive.
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
    Don't now ValveFog.

    First thing that comes to mind is to render a big flat quad in the x/z plane at the height where the fog ends and then calculate the fog density based on the distance between the plane and the z-buffer.

    That's basically how screen space fog works with a small modification.

    It's quite cheap. The main cost is blending. Main problem is that this only works for opaque geometry. Everything translucent must render fog in the pixel shader.
     
    KaiGarbe likes this.
  3. KaiGarbe

    KaiGarbe

    Joined:
    Apr 5, 2022
    Posts:
    2
    Thank you for your reply. The translucency problem, does it only occur, when looking at a translucent object through the fog, or when looking at the fog through a translucent material or both? I would very much like to have translucent geometry in my game. Could you explain in a bit more detail how I could implement your suggested solution with the plane? Where exactly do I "calculate fog"?(in which script/shader/component?)
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
    Translucent objects usually don't write to the depth buffer, so it's as if they are not there. If you render the fog after the alpha pass, all translucent objects will get the fog of what's behind it, even if the translucent object itself is not in the fog. If you render the fog before the alpha pass, all translucent objects won't have fog on them - unless you calculate the fog for those in the shader (that's how Unity does it).

    It would look like this. You can also see the translucency problem at the end of the video:


    You can look at the source of the screen space fog in the post-processing package to see how it's done:
    https://docs.unity3d.com/2018.3/Documentation/Manual/PostProcessing-Fog.html
     
    KaiGarbe likes this.
  5. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
    KaiGarbe likes this.
  6. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    560
    This is how Unity's DeferredFog.shader does it:

    Code (CSharp):
    1.         TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
    2.         TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
    3.  
    4.         #define SKYBOX_THREASHOLD_VALUE 0.9999
    5.  
    6.         float4 Frag(VaryingsDefault i) : SV_Target
    7.         {
    8.             half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoordStereo);
    9.  
    10.             float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo);
    11.             depth = Linear01Depth(depth);
    12.             float dist = ComputeFogDistance(depth);
    13.             half fog = 1.0 - ComputeFog(dist);
    14.  
    15.             return lerp(color, _FogColor, fog);
    16.         }
     
    KaiGarbe likes this.