Surrounding a Terrain with Fog

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?

You could look at one of the volumetric fog assets on the asset store. I’ve not tried them, but this one allows you to specify a “void” area, so you could apply the fog to your entire terrain but create a spherical or box area in the center where there is no fog, which would be your playable area:

1 Like

Which asset is this? This link doesn’t specify a particular asset.

It should, i had some issue with some links recently, but anyway that is the name “Volumetric Fog & Mist”

You should have a chat with @frosted .

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. :slight_smile: Just wasn’t sure if it supported exactly what you’re looking for.

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.

1 Like

Why not dynamically adjust fog based on how far you are away from the center of the world?

1 Like

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.

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:

https://www.youtube.com/watch?v=W9QffKhu9PY

2 Likes

That’s pretty cool. I think that’d work.

1 Like

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.

     //Creates a spherical wall of fog at the set radius from the center point
    public static void DynamicFogWall(GameObject centerPoint, GameObject mainCamera, float fogRadius, float blendDistance)
    {
        float endPoint = 0;

        Vector3 relativePosition = centerPoint.transform.position - mainCamera.transform.position;
        float forward = Vector3.Dot(mainCamera.transform.forward, relativePosition.normalized);
        float distance = Vector3.Distance(centerPoint.transform.position, mainCamera.transform.position);

        if (forward < 0)
        {      
            endPoint = fogRadius - distance; //Fog distance when facing away from center
        }
        else
        {
            endPoint = fogRadius + (fogRadius - (fogRadius - distance)); //Fog distance when facing toward center
        }

        RenderSettings.fogMode = FogMode.Linear;
        RenderSettings.fogEndDistance = endPoint;
        RenderSettings.fogStartDistance = endPoint - blendDistance;  
    }