Class FogOfWarMap
The part of a scene a character has explored, as a grid of coverage values over the scene's map rectangle.
public sealed class FogOfWarMap
- Inheritance
-
FogOfWarMap
- Inherited Members
Remarks
Coverage, not a bit. Each cell holds how much fog is left over it — 255 for never visited, 0 for fully explored — rather than a single explored flag. A bit per cell is a quarter of the memory and produces a hard checkerboard edge wherever the player walked, because the smallest unit of reveal is then a whole cell. Storing coverage lets a reveal write a radial falloff, so the boundary between explored and unexplored is a soft ring at the edge of the player's sight rather than a staircase of squares, and it costs a byte per 16 square metres of world.
Reveal only ever lowers a value. Fog does not come back. That makes the grid monotonic, which is what allows the overlapping reveals of a player walking in circles to be applied in any order without the result depending on the order — and means a partial save can only ever lose progress, never invent it.
It is not authoritative. This lives on the player's machine and is never sent to the server. Anything that matters — Cartography experience, a reward for full exploration — must be decided by the server from where the character actually walked, which it already knows. See FogOfWarStore for what the signature on the file does and does not buy.
Constructors
FogOfWarMap(Rect, float)
Builds an entirely unexplored map over a world rectangle.
public FogOfWarMap(Rect worldRect, float cellSize)
Parameters
worldRectRectThe world rectangle to cover, on the XZ plane.
cellSizefloatSize of one cell in world metres.
Fields
Explored
Fog value meaning the cell is fully explored.
public const byte Explored = 0
Field Value
Unexplored
Fog value meaning the cell has never been seen.
public const byte Unexplored = 255
Field Value
Properties
CellSize
Size of one cell in world metres.
public float CellSize { get; }
Property Value
Cells
Fog coverage per cell, row-major with Z as the row.
public byte[] Cells { get; }
Property Value
- byte[]
CellsX
Number of cells across the X axis.
public int CellsX { get; }
Property Value
CellsZ
Number of cells across the Z axis.
public int CellsZ { get; }
Property Value
IsDirty
Whether anything has changed since the last time the flag was cleared.
public bool IsDirty { get; }
Property Value
WorldRect
The world rectangle the grid covers, on the XZ plane.
public Rect WorldRect { get; }
Property Value
- Rect
Methods
ClearDirty()
Clears the dirty flag after the map has been written to disk.
public void ClearDirty()
ExploredAt(Vector3)
How explored a world position is.
public float ExploredAt(Vector3 worldPosition)
Parameters
worldPositionVector3The position to test.
Returns
- float
Zero for fully fogged, one for fully explored.
Remarks
A position outside the grid reads as explored. Off-map is not a place the player can discover, and reporting it as fogged would hide every marker that sits just outside a scene's derived bounds — which, since those bounds are a boundary volume plus padding, includes things a level designer legitimately put at the edge.
ExploredFraction()
The fraction of the whole map that has been explored.
public float ExploredFraction()
Returns
- float
Zero to one.
Remarks
Walks every cell, so it is for a panel refreshing a progress readout rather than for a per-frame caller. At 4 metre cells a two-kilometre scene is a quarter of a million bytes, which is a fraction of a millisecond but not free.
FromCells(Rect, float, byte[])
Rebuilds a map from cells loaded off disk.
public static FogOfWarMap FromCells(Rect worldRect, float cellSize, byte[] cells)
Parameters
worldRectRectThe world rectangle the cells cover.
cellSizefloatSize of one cell in world metres.
cellsbyte[]The cell data. Must be exactly the grid's size.
Returns
- FogOfWarMap
The loaded map, or null when the data does not match the grid.
Remarks
Returns null rather than resizing. A cell array of the wrong length means the scene's bounds or cell size changed since the file was written, and stretching old data across new bounds would put the player's explored ground in the wrong place — worse than starting again, because it looks plausible.
GetTexture()
The fog as a texture, alpha carrying coverage, rebuilt only when the grid has changed.
public Texture2D GetTexture()
Returns
- Texture2D
The fog texture. Never null once the map exists.
Remarks
RGBA rather than a single-channel format because UI Toolkit multiplies the sampled texel by the vertex colour: leaving the colour channels at white lets the fog be tinted to whatever suits the theme, where a single-channel texture would force it to be black.
Bilinear, and that matters. At four metres a cell is a tenth of the minimap's width, so point sampling would show the grid itself; interpolation between cells, on top of the radial falloff the reveal already writes, is what makes the edge read as mist rather than as tiling.
IsDiscovered(Vector3)
Whether a world position counts as discovered for the purposes of hiding markers.
public bool IsDiscovered(Vector3 worldPosition)
Parameters
worldPositionVector3The position to test.
Returns
- bool
True when the position is more than half explored.
ReleaseTexture()
Destroys the texture.
public void ReleaseTexture()
Reveal(Vector3, float)
Marks the area around a world position as explored.
public bool Reveal(Vector3 worldPosition, float radius)
Parameters
worldPositionVector3Centre of the reveal, in world space.
radiusfloatRadius of the reveal in world metres.
Returns
- bool
True when at least one cell changed.
RevealAll()
Reveals the entire map.
public void RevealAll()
Remarks
For scenes whose definition turns fog off, and for the map baker's preview. Cheaper and clearer than special-casing "no fog" at every draw site.
TryGetCell(Vector3, out int, out int)
The grid cell containing a world position.
public bool TryGetCell(Vector3 worldPosition, out int x, out int z)
Parameters
Returns
- bool
True when the position is inside the grid.