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. Dismiss Notice

Getting Fog color and density

Discussion in 'Shaders' started by Kragh, Feb 5, 2010.

  1. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    I have modified terrain shaders targeted at SM 3.0, which disabled the inbuild easy-to-use fog functionality.
    So I need to obtain the values for fog color and density from Unity, so I can multiply them on the color output accordingly, but can't seem to figure out how to get the values. I'm still very new to the whole shader area, and the answer may be very simple...

    To pass them by script is not an option, as I really haven't got any material (or MeshRenderer component) to pass it to, because it's a terrain shader.

    ?
     
  2. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    Nobody seems to know anything about it, it seems.

    The other day I came up with this method in my fragment program, after some research. This gives me precisely the same result as when using the inbuild fog. And yeah, I know this should probably go into the vertex program instead. Will change it when I have it right.

    Code (csharp):
    1.  
    2.  
    3. uniform float4 _AddFog;
    4.  
    5. float4 manualFogEXP2Fragment (v2f i) : COLOR {
    6.  
    7.      half4 whateverColorCalculated = blahblah;
    8.  
    9.      float whatEverDistanceFromView = someOtherBlahBlah;
    10.  
    11.      float fogDensity = 0.0015;
    12.  
    13.      float eFactor = 2.7183;
    14.  
    15.      float fog = 1/(pow(eFactor,(pow(fogDensity  * whatEverDistanceFromView,2)))); 
    16.    
    17. half4 c = lerp(whateverColorCalculated ,_AddFog,1-fog);
    18.  
    19.      // Another method than lerp(): half4 c = (1-fog) * _AddFog + fog * whateverColorCalculated ;  
    20.    
    21.      return c;
    22.  
    23. }
    24.  
    25.  
    As you can see I still have the density of the fog as a hardcoded variable. I simply can't find a way of obtaining it automatically. At least I have the color from the uniform float4 value; _AddFog.

    Still no real solution to the density? Am I being stupid?
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    These should work:
    Code (csharp):
    1.  
    2. float4 _FogColor;
    3. float _FogDensity;
    4.  
     
  4. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    That's so strange. Because they do. And they were obviously some of the first guesses of variable names I tried. Have no idea what I did wrong elsewhere, when I first tried out these. I just know they didn't work then.
    Stupid.

    Thx Aras
     
  5. Bravo_cr

    Bravo_cr

    Joined:
    Jul 19, 2012
    Posts:
    148
    Hi,
    I was searching for exactly this (despite being a very old thread), but I cannot make it work. Using "float4 _FogColor;" always returns black, despite being configured properly in the RenderSettings. For additional info I'm trying to access this on the vertex shader. Any clues?
     
  6. gametr4x

    gametr4x

    Joined:
    Apr 22, 2009
    Posts:
    84
    Just in case anybody finds this, it's "unity_FogColor"
     
    AdrienVR, Furgl, CoughE and 2 others like this.
  7. Pawl

    Pawl

    Joined:
    Jun 23, 2013
    Posts:
    113
    Thanks gametr4x, declaring "float4 unity_FogColor" worked for me!
     
  8. mauriciofelippe

    mauriciofelippe

    Joined:
    Aug 9, 2017
    Posts:
    28
    angelonit likes this.
  9. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    Thanks @mauriciofelippe , this worked for me
    Code (CSharp):
    1. changed this->  UNITY_APPLY_FOG_COLOR(IN.fogCoord, result, fixed4(0, 0, 0, 0));
    2.  
    3. for this ->  UNITY_APPLY_FOG_COLOR(IN.fogCoord, result, unity_FogColor);