Search Unity

Surrounding a Terrain with Fog

Discussion in 'General Graphics' started by jnbbender, Jul 25, 2017.

  1. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    487
    I see lot's of tutorials on how to create Fog but I can't figure out how to create a border of Fog. I don't want my character to "fall off" my terrain.

    I currently am frying him with radioactivity before he gets there and he nicely animates to his death. But I obviously don't want him to see the edge of the terrain and I need to give him a warning, red fog or something, but I can't figure out how to put a 20m diameter fog around my terrain.

    Any ideas?
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Kronnect likes this.
  3. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    487
    Which asset is this? This link doesn't specify a particular asset.
     
  4. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    It should, i had some issue with some links recently, but anyway that is the name "Volumetric Fog & Mist"
     
  5. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    You should have a chat with @frosted.
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Sorry, I forgot I was on the new asset store... try this one:
    https://www.assetstore.unity3d.com/en/#!/content/49858

    Now I would also recommend this one:
    https://www.assetstore.unity3d.com/en/#!/content/81802

    The author is very active and it's getting Gaia and CTS integration. I own that second one, I just haven't had a chance to try it yet. Maybe @DavidMiranda can chime in since it's his asset. :) Just wasn't sure if it supported exactly what you're looking for.
     
  7. DavidMiranda

    DavidMiranda

    Joined:
    Nov 30, 2012
    Posts:
    617
    Without modifications, FV can't do that right now (3.2). In future versions this could be done using distance fields. Atm, I have only additive cubes. But with subtractive spheres, this would be solved.
     
    Dustin-Horne likes this.
  8. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
    Why not dynamically adjust fog based on how far you are away from the center of the world?
     
    MichaelEGA likes this.
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    that would work for a round world but not a square one like a terrain. could do the same with x / z distances at the edges though.
     
  10. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,906
    Right, as @Dustin-Horne suggests Volumetric Fog & Mist provides a "Void" feature which can be spherical or squared. In some cases you may also want the void area to follow the player to reduce even more the viewable area.

    This video is quite old but shows the feature:
     
    Farelle and Dustin-Horne like this.
  11. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    487
    That's pretty cool. I think that'd work.
     
    Kronnect likes this.
  12. MichaelEGA

    MichaelEGA

    Joined:
    Oct 11, 2019
    Posts:
    40
    I been looking for an answer to this for a while. This was the best suggestion. Thank you.

    Here's how I did it:

    Edit: The code below only half works. Somehow I need to calculate the distance to the edge of the sphere taking into account not only the distance from the center compared to the radius for the sphere, but the rotation of the gameobject itself. I'm sure it's possible mathematically but not sure how.

    Edit Edit: I'm sure a maths solution exists for this, but I ended up just going for a practical solution. I created a sphere with inverted normals scaled it up to the size of the play area and had the player camera raycast to it. I then measured the distance between the player and the raycast hit.point and dynamically set the fog for that distance.

    Code (CSharp):
    1.      //Creates a spherical wall of fog at the set radius from the center point
    2.     public static void DynamicFogWall(GameObject centerPoint, GameObject mainCamera, float fogRadius, float blendDistance)
    3.     {
    4.         float endPoint = 0;
    5.  
    6.         Vector3 relativePosition = centerPoint.transform.position - mainCamera.transform.position;
    7.         float forward = Vector3.Dot(mainCamera.transform.forward, relativePosition.normalized);
    8.         float distance = Vector3.Distance(centerPoint.transform.position, mainCamera.transform.position);
    9.  
    10.         if (forward < 0)
    11.         {      
    12.             endPoint = fogRadius - distance; //Fog distance when facing away from center
    13.         }
    14.         else
    15.         {
    16.             endPoint = fogRadius + (fogRadius - (fogRadius - distance)); //Fog distance when facing toward center
    17.         }
    18.  
    19.         RenderSettings.fogMode = FogMode.Linear;
    20.         RenderSettings.fogEndDistance = endPoint;
    21.         RenderSettings.fogStartDistance = endPoint - blendDistance;  
    22.     }
    23.  
    24.  
     
    Last edited: Apr 15, 2024