Effective RTS Fog Of War system

Okay, I want to create an effective Fog of War system.

What I am currently doing is:

Creating solid black planes(tiles) over my map. When a character moves it explores tiles.

A while(true) coroutine does the following (pseudocode):

// set shadow
while(true) {
    foreach(exploredTile) setColor(0, 0, 0 ,0.5f)

    // remove shadow around NPCs
    foreach(npc)
        foreach(exploredTile where (distance(tile, npc) <= npc.ViewRange)) setColor(0, 0, 0, 0)

    yield return 0
}

Okay, I hope so far is everything clear. Feel free to ask for more information.

This works actually pretty good - at least when I have only a handful of NPCs. Obviously this becomes laggy when the amount of NPCs increases.

So what I want to discuss is, how can I effectively (un)shadow my tiles?

Just to emphasize what I’m doing here a very basic image of my concept… Each tile can be: Transparent (visible), Half-Transparent (shadow), Solid (unexplored)
Green = Map
Black = Tiles

1746508--110332--foo.png

Why not use shaders? Would seem a lot cheaper to me.

Well I dont have much experience with shaders.
How should I pass the information if a point is visible, shadow or darkened to the shader?

As far as I know I can’t pass any arrays (or similiar) to a shader. Just floats, vertices, matrices

Since you suggested to use shaders, I’ve tried to wrap my head around this. But I’m struggling with the logic a bit.


Kind of an update. I guess I need some per-pixel (or per-tile) uniform. But I’m not sure how to pass it to my shader without declaring a variable for each tile… :slight_smile:


Okay I’ve got the basics working so far. I just use a plane and use the vertex color alpha channel :slight_smile:

Look up Cut out shaders. You simply put a cut out texture on top of the player and use that in the shader for the fog.

Hm… This could be a good idea for the lightning around NPCs. I’ll try this later. But don’t forget the shadowed spots.

What I’m currently doing is (as I already descriped roughly) creating a plane with a custom mesh to have enough vertices (tiles). But this won’t work for huge maps (65k vertices limit). So the obvious step would be to either make the tiles bigger (have less vertices) or have many planes.

Since smaller tiles aren’t an opinion I’ll create many planes. Any concerns with this solution?


Well, this gets incredible laggy :confused:
One option would be to just have planes around my viewport. But this would be conflicting with my minimap. hmm…