Skip to content

unitycoder/GridFogOfWar

 
 

Repository files navigation

FogOfWar for Unity

en ko

A grid-based Fog of War package for Unity, implemented with the Shadowcasting algorithm.

  • Unity: 2022.3 or later
  • Namespace: EunoLab.FogOfWar
image
녹화_2026_04_23_22_48_43_532

1. Installation

Unity Package Manager → → Install package from git URL → enter https://github.com/eunho5751/FogOfWar.git#1.0.1 → Install

image
image

2. Required Scene Setup

(1) Add the MainFogOfWar tag

Add a MainFogOfWar tag in Unity's Tags & Layers settings. A FogOfWar instance with this tag can be accessed at runtime via FogOfWar.Main.
※ The AutoRegisterToMain option on the FogOfWarUnit component accesses the global FogOfWar instance through FogOfWar.Main, so the tag is required whenever that option is enabled.

(2) Place the FogOfWar GameObject

  1. Create an empty GameObject and set its Tag to MainFogOfWar.
  2. Add the FogOfWar component to it.
  3. The GameObject's position becomes the center of the grid. Place it to match your map. (Enable the gizmos to visualize the grid area.)
  4. Assign the layer of your vision-blocking colliders (walls, etc.) to Obstacle Scan Mask.

(3) Prepare unit prefabs

For objects that should provide vision or be hidden by the fog (players, enemies, structures, etc.):

  • Add a FogOfWarUnit component (attach it to the root or topmost GameObject).
  • If you need show/hide behavior, add a FogOfWarRendererSwitcher or FogOfWarGraphicSwitcher component.

(4) (Optional) Pre-scan the grid data

If the Grid Data Asset field on the FogOfWar component is not assigned, obstacles are scanned automatically during initialization. To skip the runtime obstacle scan:

  1. Click Save Grid in the FogOfWar component's context menu → a .bytes file is generated.
  2. Assign the generated .bytes file to the FogOfWar's Grid Data Asset field.

3. Tuning FogOfWar Quality

The quality of the fog of war depends heavily on the FogOfWar component's settings, so here are a few essential options worth understanding.

(1) Grid Settings

image

Grid Dimensions : The number of tiles. Higher values increase the resolution of the fog texture and allow the grid to cover a larger area at the same tile size.
Grid Unit Scale : The world-space size of each tile. Larger values make each tile bigger, letting the grid cover a larger area at the same fog texture resolution.

As shown above, both Grid Dimensions and Grid Unit Scale can be used to control the area covered by the grid in the world, but adjusting only one of them can cause problems.
(❌) Covering a large area by only increasing Grid Dimensions raises the fog texture resolution, which increases CPU/GPU processing cost.
(❌) Covering a large area by only increasing Grid Unit Scale means many fog texture pixels have to be rendered per single tile, which lowers the visual quality of the fog.
(✅) Adjust both settings together to find the right balance of grid size and tile size for your project. (Use the gizmos to help.)

(2) Fog Settings

image

Fog Lerp Speed : Fog interpolation speed. Higher values make the fog texture react to changes more quickly.
Fog Blur Iterations/Radius/Sigma : Settings used for the Gaussian blur, applied to soften the vision boundaries.

image

4. Main Scripts

FogOfWar (required)

The main controller of the fog of war system. Handles grid generation, visibility updates, and fog texture rendering.

Main API

  • Activate(int? teamMask = null) / Deactivate() — turn the fog of war system ON / OFF
  • ScanGrid() — rescan obstacles in the current scene and refresh the grid
  • AddUnit(FogOfWarUnit) / RemoveUnit(FogOfWarUnit) / ContainsUnit(FogOfWarUnit)
  • IsVisible(Vector3 worldPos, int? teamMask = null) — whether a given world position is currently visible
  • static FogOfWar.Main — returns the instance tagged with MainFogOfWar
  • IsActivated — current activation state
  • VisibilityUpdateRate — visibility refresh frequency
  • TeamMask — the team layer mask whose vision is rendered

FogOfWarUnit

Attach to objects that should provide vision or be hidden by the fog.

Main API / Events

  • event Action<bool> VisibilityChanged — raised when the unit's visibility changes
  • bool IsVisible — current visibility
  • bool IsTeammate(int teamMask) — whether the unit belongs to the given mask

FogOfWarVisibilityHandlerBase (abstract class)

An abstract class that subscribes to a FogOfWarUnit's visibility-change event.

Overridable methods

  • OnAwake(), OnEnabled(), OnDisabled()
  • OnVisibilityChanged(bool isVisible) — called when visibility changes

Example: inherit from this class to add custom behavior such as muting audio or switching AI states when a unit is hidden by fog. (The FogOfWarRendererSwitcher and FogOfWarGraphicSwitcher components are implemented by inheriting from this class.)


FogOfWarRendererSwitcher

Turns the Renderer component on the same GameObject ON/OFF based on visibility.

FogOfWarGraphicSwitcher

Turns a UI Graphic component (Image, Text, etc.) on the same GameObject ON/OFF based on visibility.


TeamMaskAttribute

When applied to an int field, lets you edit teams 0–31 in the inspector as a checkbox mask.

[SerializeField, TeamMask] private int _allowedTeams;

5. Team Layers

  • Teams are identified by layer numbers from 0 to 31 (separate from Unity Layers).
  • FogOfWar.TeamMask is a bitmask — multiple teams' vision can be displayed at the same time.
  • FogOfWarUnit.TeamLayer is a single layer number — that team's vision clears the surrounding fog.

6. Sample

  • Import via Unity Package Manager → Samples → Import.
  • The demo is playable out of the box without additional setup. However, if you assign an obstacle layer to the Obstacles objects in the demo scene and include that layer in the FogOfWar component's Obstacle Scan Mask, you can see vision being blocked by the obstacles.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C# 91.2%
  • ShaderLab 8.8%