# Atmospheric Height Fog, Sun Shafts & Rim-Readable Lighting in Unity 6 URP

> Three-point outdoor/interior URP lighting recipe combining directional sun + warm/cool fill, exponential height/distance fog, ACES/Neutral post-processing, and anti-darkness floors so captures never read black or washed out.

- **Canonical page:** https://gamecrew.ai/techniques/unity/atmospheric-lighting-fog
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Unity 6
- **Category:** lighting
- **Tags:** `lighting`, `fog`, `atmosphere`, `rim`, `bloom`, `tonemapping`, `readability`, `urp`, `color-grading`
- **Verification:** Compile-verified pattern

## When to Use & Field Best Practices

Two classic lighting failures ruin otherwise good vertical slices:
1. **Pitch-black shadow sides (`brightness < 0.06`, `VERY DARK`)**: Relying on a single `DirectionalLight` with dark sky/ambient settings leaves every surface facing away from the sun at $(0,0,0)$ in scenes without realtime GI.
2. **Washed-out grey soup (`contrast < 0.05`, `LOW CONTRAST`)**: Setting `RenderSettings.fogDensity` too high ($> 0.025$) turns mid-ground silhouettes into flat grey.

Field-proven lighting architecture for readable, atmospheric slices:
- **Warm Key / Cool Trilight Ambient (`AmbientMode.Trilight`)**: Pair a warm directional key (`#FFE6C2`, intensity `1.3–1.8`, elevation `28°–48°`) with `AmbientMode.Trilight` (`ambientSkyColor` `#4B6B9E`, `ambientEquatorColor` `#5A5268`, `ambientGroundColor` `#2A2433`) so every upward, side, and downward normal reads distinct planes even in full shadow!
- **Rule of Depth Separation (Foreground Dark/Crisp → Midground Saturated → Background Hazy)**: Calibrate `FogMode.ExponentialSquared` density from camera-to-arena distance $D$: $\rho \approx \frac{0.45}{D}$ (e.g. `0.006` for a 75 m island, never `0.04`).
- **URP `Volume` Tonemapping + Restrained Bloom**: Use `TonemappingMode.Neutral` (preserves vibrant stylized hues) or `ACES` (cinematic highlight roll-off, pair with `+0.25` post-exposure so midtones don't darken), and `Bloom` (`threshold = 1.05`, `intensity = 0.28`) reserved strictly for emissive beacons, pickups, and hazards.

## Mathematical & Algorithmic Core

For `FogMode.ExponentialSquared` at view distance $d$, visibility factor $f = e^{-(\rho d)^2}$:
- To retain **65% contrast** at gameplay distance $D_{\text{play}}$ ($f = 0.65$):
  $$\rho = \frac{\sqrt{-\ln(0.65)}}{D_{\text{play}}} \approx \frac{0.656}{D_{\text{play}}}$$
  Example: if the player views an arena $80\text{ m}$ across, set `fogDensity = 0.0075f`.

## Engine Implementation

### Deterministic URP Lighting, Trilight Ambient & Post Volume Builder (`Assets/Game/Runtime/Rendering/AtmosphereRig.cs`)

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace Game.Rendering
{
    public static class AtmosphereRig
    {
        public struct Mood
        {
            public Color SunColor;
            public float SunIntensity;
            public Vector3 SunEuler;
            public Color SkyAmbient;
            public Color EquatorAmbient;
            public Color GroundAmbient;
            public Color FogColor;
            public float ArenaSpanMeters;
            public float PostExposure;
            public float BloomIntensity;

            public static Mood GoldenIsland(float arenaSpanMeters = 90f) => new Mood
            {
                SunColor = new Color(1.00f, 0.90f, 0.74f),
                SunIntensity = 1.55f,
                SunEuler = new Vector3(36f, -42f, 0f),
                SkyAmbient = new Color(0.32f, 0.46f, 0.68f),
                EquatorAmbient = new Color(0.40f, 0.36f, 0.46f),
                GroundAmbient = new Color(0.20f, 0.17f, 0.22f),
                FogColor = new Color(0.62f, 0.73f, 0.86f),
                ArenaSpanMeters = arenaSpanMeters,
                PostExposure = 0.20f,
                BloomIntensity = 0.26f
            };
        }

        public static void Apply(Mood mood, Transform parent = null)
        {
            RenderSettings.ambientMode = AmbientMode.Trilight;
            RenderSettings.ambientSkyColor = mood.SkyAmbient;
            RenderSettings.ambientEquatorColor = mood.EquatorAmbient;
            RenderSettings.ambientGroundColor = mood.GroundAmbient;

            RenderSettings.fog = true;
            RenderSettings.fogMode = FogMode.ExponentialSquared;
            RenderSettings.fogColor = mood.FogColor;
            RenderSettings.fogDensity = 0.65f / Mathf.Max(25f, mood.ArenaSpanMeters);

            var sunGo = GameObject.Find("Directional Light") ?? new GameObject("Directional Light");
            if (parent != null) sunGo.transform.SetParent(parent, false);
            sunGo.transform.rotation = Quaternion.Euler(mood.SunEuler);
            var sun = sunGo.GetComponent<Light>() ?? sunGo.AddComponent<Light>();
            sun.type = LightType.Directional;
            sun.color = mood.SunColor;
            sun.intensity = mood.SunIntensity;
            sun.shadows = LightShadows.Soft;

            var volGo = new GameObject("AtmosphereGlobalVolume");
            if (parent != null) volGo.transform.SetParent(parent, false);
            var vol = volGo.AddComponent<Volume>();
            vol.isGlobal = true;
            var profile = ScriptableObject.CreateInstance<VolumeProfile>();
            vol.sharedProfile = profile;

            var tonemap = profile.Add<Tonemapping>(true);
            tonemap.mode.Override(TonemappingMode.Neutral);

            var colorAdj = profile.Add<ColorAdjustments>(true);
            colorAdj.postExposure.Override(mood.PostExposure);
            colorAdj.contrast.Override(10f);
            colorAdj.saturation.Override(8f);

            var bloom = profile.Add<Bloom>(true);
            bloom.threshold.Override(1.05f);
            bloom.intensity.Override(mood.BloomIntensity);
            bloom.scatter.Override(0.62f);

            foreach (var cam in Object.FindObjectsByType<Camera>(FindObjectsSortMode.None))
            {
                var data = cam.GetUniversalAdditionalCameraData();
                if (data != null) data.renderPostProcessing = true;
            }
        }
    }
}
```

## Browser, Mobile & Software-Renderer Budgets

- **Realtime Light Count**: Keep realtime shadow-casting lights to **1 directional sun**; use non-shadow-casting point lights (`LightShadows.None`, range 6–14 m) for beacons, torches, and pickups so WebGL 2 and CPU `llvmpipe` stay well inside URP's per-object light budget.
- **Anti-Aliasing on Software Renderers**: Never force 4x/8x MSAA on `Camera` or `RenderTexture` when `SystemInfo.graphicsDeviceName` contains `llvmpipe`, `softpipe`, or `SwiftShader`.

## Verification Checklist

1. Confirm `cam.GetUniversalAdditionalCameraData().renderPostProcessing = true` on the main camera (otherwise `Volume` overrides have zero effect!).
2. Run `capture` and inspect every view's logged brightness ($\ge 0.10$ for day/dusk, $\ge 0.08$ for night) and contrast ($\ge 0.08$).

## Official references

- Naty Hoffman & Arceth Preetham (SIGGRAPH 2002 — Rendering Outdoor Light Scattering in Real Time)
- Valve Art Direction (Character Readability & Ambient Cubes)
- Inigo Quilez (Analytical Exponential Fog & Sun Mie Glow)
