Fog of War (URP only)

Enemy Masses includes a Fog of War system designed specifically for high-performance scenarios with thousands of units. Unlike complex raycasting or volumetric fog solutions that can kill performance, this system uses a lightweight 2D texture projection method.

How It Works

  1. Grid System: The world is divided into a 2D grid (texture).

  2. Texture Projection: A simple plane with a transparent texture is rendered over the game world.

  3. Visibility Stamping: Each frame (or interval), the system "stamps" visible circles onto the texture around player units.

  4. Performance First:

    • Uses NativeArray and efficient byte manipulation.

    • Supports Round-Robin Sampling (updates only a portion of agents per frame).

    • Avoids expensive physics raycasts.


Setup Guide

1. Add the Component

  1. Create an empty GameObject in your scene and name it FogOfWar.

  2. Add the FogOfWarLite component.

  3. Grid Center: Assign a Transform that represents the center of your map (usually 0, 0, 0).

2. Configure the Grid

  • Grid X / Y: The resolution of the fog texture (e.g., 512x512). Higher = sharper fog edges but more memory.

  • Unit Scale: How many world units one pixel represents.

    • Example: If your map is 1000x1000 units, and Grid is 512x512, set Unit Scale to 2 (approx).

  • Plane Height: Height of the fog plane. Should be slightly above the ground but below units' heads if possible, or just above the ground to darken it.

3. Visibility Settings

  • Max Reveal Radius: The maximum distance a unit can see.

  • Keep Revealed: If checked, areas once seen remain visible (greyed out) but not fully black again (Classic RTS style).

  • Enable Refog: If checked, areas can slowly fade back to darkness or semi-darkness if units leave.

  • Refog Opacity: How dark the "visited but currently unseen" areas are (0 = clear, 1 = black).

4. Performance Tuning

  • Refresh Interval: How often the fog updates (e.g., 0.1s). Increasing this improves performance but makes fog updates "choppy".

  • Max Agents Per Tick: Limits how many units are processed per update loop.

  • Round Robin Sampling: Highly Recommended. Instead of checking all 5,000 units every frame, it checks a slice of them. This keeps the frame rate smooth even with massive armies.


Integration

With RTS Controller

The FogOfWarLite automatically finds the EnemyMassesRTSController to determine which factions are "Player Controlled". Only player units will clear the fog.

With Minimap

The RTSMinimapController can link to this system.

  • Assign this component to the Fog Of War Provider slot in the Minimap Controller.

  • The minimap will automatically hide enemy icons/units that are in the fog.


Customizing the Look

The system generates a PrimitiveType.Plane at runtime.

  • Fog Material: You can assign a custom material. It must use a shader that supports transparency and accepts a _MainTex.

  • Default: If left null, it creates a standard Unlit/Transparent black material.

Shader Note

For best results, use a shader that supports Soft Particles or Z-Test if you want the fog to intersect nicely with terrain, though the default flat plane is usually sufficient for top-down RTS views.

Last updated