---
title: "Add height fog to a 3D game in C#"
url: https://recursiveai.org/onegame/cookbook/add-height-fog-to-3d-scene/
description: "Use OneGame height fog to fill low ground with mist. Set the base height, density and falloff in C#, then render two objects at different elevations."
version: "2.5.3"
---

Set `fog.Mode = FogMode.Height` so mist thickens toward the ground instead of depending on distance alone.

```shell
dotnet add package RecursiveAI.OneGame.Host
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneGame.Render3D;

var scene = new Scene3D();
var fog = scene.Fog;
fog.Enabled = true;
fog.Mode = FogMode.Height;
fog.Density = 0.25f;
fog.HeightBase = 0;
fog.HeightFalloff = 1;
System.Console.WriteLine(fog.Mode);
```

Configures the scene to pool fog near world Y = 0. Prints `Height`.

Unzip [the example](https://recursiveai.org/onegame/cookbook/add-height-fog-to-3d-scene/add-height-fog-to-3d-scene.zip), then `dotnet run`.

## Full program

The program builds two identical squares at elevations 0 and 2. The camera looks straight ahead from Y = 1, so the squares’ centers are equally far from the camera. It renders offscreen and needs no model or texture files.

`RecursiveAI.OneGame.Host` brings in the `RecursiveAI.OneGpu` API through its GPU backend dependencies, so you don’t need a separate install. The zip’s project enables implicit usings for `System` and `System.IO`.

In a running game, put the fog settings in `Render3DStage.UpdateScene` and apply them to the `scene.Fog` passed to that method.

On Linux and Windows, the full program needs a working Vulkan driver. On Linux with no Vulkan driver available, OneGame 2.5.3 stopped before rendering with `GpuUnsupportedException: vkCreateInstance failed: ErrorIncompatibleDriver`.

```csharp
using System.Numerics;
using OneGame.Render3D;
using RecursiveAI.OneGpu;

using var device = GpuBackends.CreateDevice();
using var renderer = new Renderer3D(device);
var scene = new Scene3D { Samples = 1 };
scene.Camera = Camera3D.LookAt(new(0, 1, 8), new(0, 1, 0), Vector3.UnitY);
scene.Environment.Background = BackgroundMode.Color;
scene.Environment.BackgroundColor = new(0.04f, 0.06f, 0.09f);
scene.Post.Bloom = false;
scene.Post.ToneMapper = ToneMapper.None;

var fog = scene.Fog;
fog.Enabled = true;
fog.Mode = FogMode.Height;
fog.Color = new(0.4f, 0.6f, 0.9f);
fog.Density = 0.25f;
fog.HeightBase = 0;
fog.HeightFalloff = 1;

var material = renderer.CreateMaterial("ochre", doubleSided: true,
    baseColor: new Vector4(0.6f, 0.2f, 0.03f, 1));
material.Style = MaterialStyle3D.Flat;
using var square = renderer.CreateMesh(material,
    [new(new(-0.5f, -0.5f, 0), Vector3.UnitZ, Vector2.Zero),
     new(new(0.5f, -0.5f, 0), Vector3.UnitZ, Vector2.UnitX),
     new(new(0.5f, 0.5f, 0), Vector3.UnitZ, Vector2.One),
     new(new(-0.5f, 0.5f, 0), Vector3.UnitZ, Vector2.UnitY)], [0, 1, 2, 0, 2, 3]);
scene.Add(square.Model, Matrix4x4.CreateTranslation(0, 0, 0));
scene.Add(square.Model, Matrix4x4.CreateTranslation(0, 2, 0));

using var target = new Offscreen3D(renderer, 160, 160);
var pixels = target.Capture(scene);
byte BlueAt(int y) => pixels[(y * target.Width + target.Width / 2) * 4 + 2];
Console.WriteLine($"Fog mode: {fog.Mode}");
Console.WriteLine($"Lower square is bluer: {BlueAt(97) > BlueAt(62)}");

using var output = File.Create("height-fog.ppm");
output.Write("P6\n160 160\n255\n"u8);
for (int i = 0; i < pixels.Length; i += 4) output.Write(pixels, i, 3);
Console.WriteLine("Wrote height-fog.ppm (160 x 160)");
```

```text
Fog mode: Height
Lower square is bluer: True
Wrote height-fog.ppm (160 x 160)
```

Open `height-fog.ppm` to see the lower square take on more of the blue fog color. If neither square changes when you raise the density, check that `Enabled` is true and the material isn’t marked `FogExempt`.

## Variations

Insert this before `target.Capture(scene)` to remove the height gradient but keep exponential distance fog:

```csharp
fog.HeightFalloff = 0;
```

To keep a mesh unfogged, set the flag on its material before drawing. In the example both squares share the same material, so this exempts both:

```csharp
material.FogExempt = true;
```

## Related recipes

- [Render planar water reflections in C#](https://recursiveai.org/onegame/cookbook/planar-reflection-water/)
- [Lower 3D render resolution without blurring game UI](https://recursiveai.org/onegame/cookbook/lower-3d-render-scale-keep-ui-sharp/)

## The short answer

Use `FogMode.Height` on the scene’s `Fog` settings. `HeightBase` is the world Y coordinate where the fog has the density you specify in `Density`. Above that height, density falls off exponentially. Increase `HeightFalloff` to thin the fog more quickly as you go up. Start by setting the base at your valley floor, then set the camera where the player will actually see the effect.

## How do you make fog hug the ground in a 3D game?

Set the base to a world height, not a height measured from the camera. The base is a reference point, not a cutoff: the fog becomes denser below it and thinner above it. At the example’s falloff of `1`, the local density one world unit above the base is about 37% of the base density. That is the density of the air at that point, not the fraction of an object’s color replaced by fog.

The renderer adds up density along the line between the camera and each surface. A camera high on a ridge can look along clear air above the fog or down through it into a valley. A distant object up high can still look hazy if much of the sight line crosses the dense part of the fog. Tune with two views: from the floor, where fog should obscure distant objects, and looking across the top of the layer, where peaks should remain visible.

## What’s the difference between height fog and distance fog?

In linear fog, `Start` and `End` control where the distance fade begins and ends. With exponential distance fog, `Density` controls how quickly it builds up over distance. Height fog adds altitude to the calculation; `Start` and `End` aren’t the controls to tune in this mode. Setting `HeightFalloff` to zero makes height fog equivalent to exponential distance fog, so the two squares in the example lose their altitude-dependent difference.

`MaxDensity` caps the amount of blending in every mode. If foreground and background blend to nearly the same color, use a lower cap to leave some object color visible. The built-in background color or environment backdrop is not fogged, so pick a fog color near the backdrop’s horizon color if you want distant terrain to meet it. A sky represented by a mesh is different: mark its material `FogExempt` to leave its color alone.
