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

Ground fog.

Discussion in 'Shaders' started by Unitard, Sep 19, 2008.

  1. Unitard

    Unitard

    Joined:
    Jan 27, 2008
    Posts:
    29
    Quick question... I want to have fog in my game that is not distance based (there should be some of that too) but based on height on the Y axis. Cliffs fading off into the great grey oblivion below and hiding the fact that they only go down 20 meters.


    I don't yet know how to go about this, but my intuition tells me I am going to need a custom shader. Also that that shader I want is probably ridiculously simple. Also that I am simpler still, when it comes to coding shaders. Anyone know how to do this/ already done it?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    "Volumetric fog" is the magic word, and yes, you'll need a custom shader (if it's possible).

    The challenge is that it's not simply a Y position you need to take into account, but a whole range of them (from the camera's origin to the pixel being rendered.) You could probably fake it with just one position, or by averaging or taking the minimum of the two.
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    The way it's usually done (in a custom shader):

    You have eye position. And you have the position of vertex (or pixel) being rendered. And you know the altitude of your fog. So you compute the length of the line that goes from eye to the vertex/pixel, that is below the fog altitude. That gives the distance that is "in fog". From that distance, you compute the fog density (using linear, exponential or whatever fog function you want).

    Doing this in a vertex shader would be faster, but for large polygons this can be inaccurate. Doing this in a pixel shader would be slower, but more accurate.
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Aras that sounds very cool, and you're essentially saying it could be done.

    I've only just started looking at volumetric fog, and it does add a lot of realism.

    Maybe something for a future unity demo?

    Cheers
    AaronC
     
  5. Omega

    Omega

    Joined:
    Jul 31, 2007
    Posts:
    125
    This thread might be dead, but what the heck.
    It sounds like the cliff doesn't move, so you could just add a trans -> gray gradient onto the texture in the editor of your choice. And what do you know, it fades into an endless gray... 20 meters. Add a bottom or gray skybox and you're good to go.
     
  6. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    We do this using the fixed function pipeline: encode the Y depth into the alpha channel of your lightmap.

    you can do something like

    Code (csharp):
    1.  
    2. SubShader {
    3.     Blend AppSrcAdd AppDstAdd
    4.     Fog { Color [_AddFog] }
    5.  
    6.     // Ambient pass
    7.     Pass {
    8.         Name "BASE"
    9.         Tags {"LightMode" = "PixelOrNone"}
    10.         Color [_PPLAmbient]
    11.         BindChannels {
    12.             Bind "Vertex", vertex
    13.             Bind "normal", normal
    14.             Bind "texcoord", texcoord0 // main uses 1st uv
    15.             Bind "texcoord1", texcoord1 // lightmap uses 2nd uv
    16.             Bind "texcoord1", texcoord2 // lightmap uses 2nd uv
    17.         }
    18.         SetTexture [_MainTex] {
    19.             constantColor [_Color]
    20.             combine texture * constant
    21.         }
    22.        
    23.         SetTexture [_LightMap] {
    24.             combine texture * previous DOUBLE
    25.         }
    26.        
    27.         SetTexture [_LightMap] {
    28.             constantColor [_GradientColor]
    29.             combine constant lerp (texture) previous
    30.         }
    31.        
    32.     }
    33. }
    34.  
    Where gradientcolor is your vertical fog color.
    we also still do regular fog ontop of this.

    It doesn't work for dynamic objects, but if you're looking for the effects of things going down really far, this works just fine.

    Bye, Lucas
     
  7. cwisbg

    cwisbg

    Joined:
    Jul 27, 2011
    Posts:
    88

    I really want to try this out in my scene, not sure how to implament it?
    Do i add it to a shader, is it java?
     
  8. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    More necroposting! Hooray. This thread has been inactive for 3 years.#

    Also, it's Unity 2 shader code. And Unity does NOT USE JAVA.

    It uses Unityscript, which in turn is JavaScript (note: NOT JAVA), just modified really for Unity.