Ultimate Fog of War is a solution for developers, who would like to make a game with Fog of War but want to avoid putting a lot of time in developing a custom solution. It is fast, easy to use and powerful, working in 2D, 3D and with sprites. The Fog calculation is threaded and runs on the CPU, while the display is part of a massive shader bundle, that comes with Ultimate Fog of War. Even the creation of custom shaders is fairly easy.
Getting started (RTS setup)
After importing Ultimate Fog of War into your project, you can get started by creating a Fog of War Manager. Click Toolbar>Fog of War> New Fog of War manager to open the Wizard. the Wizard helps you to create your level by defining the level e.g. level size, level height and visual appearance. After you are happy with the settings, click next to actually create the Fog of War Manager. In this step, you can also choose, if you want to create a new Unity terrain or nothing at all. This is only useful if you want to use another terrain system or another kind of Geometry. Choose the save location, hit save and you are done. Ultimate Fog of War is now ready to work.
The Resistence Map
The so called “Resistence Map” is a texture that stores information about your level. Each pixel represents a 1×1 square of your level. Each color channel stores the following information:
- Hard blockers (R)
- Height (G)
- Empty (B)
- Vision modifiers (A)
You can change the texture from code or using a tool like Photoshop. (Just make sure, that PNG will somtimes encode white or black there pixels are 100% transparent).
Hard blockers are simply patches that are defined as always invisible or not. This can be useful if you want to add buildings or some other custom functionality. Only 0 is completely free. 1-255 are ID that can be layered on top of eachother.
The height of your terrain does not define the absolute height in world units, but it defines the height between the lowest point and the highest point of your Terrain as defined by levelHeight on the Fog of War Manager. That means the higher your terrain is, the lower the precision will by: 256 steps / 15 height world Units = ~0.059 World Units between two steps. Since the value ranges from 0.0-1.0 with (1/256 step size), a revealer with an upvision of 0.5 means, that it can reveal patches that are higher than 0.5 + the height of the patch it is standing on. e.g:
A revealer is standing at the lowest point of the level with a height of 0.1 and an upvision of 0.5. Thus, it can reveal patches that are 0.6 or lower.
Vision modifiers work like tall grass or steam in Starcraft 2. A Revealer inside can see other Units outside, but it can’t be seen. It works by revealing all vision modifiers with the same ID. This has the advantage that if two sepparated patches of “Tall grass” is next to eachother and revealers of opposing factions are in them, they are unable to see eachother if both patches use a different ID. IDs are also in a range of 0-255. , where 0 means “empty”.
The Fog Map
After every tick, the Fog map is automatically forwarded to the shaders as a global variable. This texture is ready for use and can be implemented into custom shaders for display. The Fog of War manager takes care of which faction is currently revealed and changes the faction if necessary. You can also use this texture to create a min map or debug it in the Fog of War manager.
By default, Factions are limited to 4 and can be created on the Fog of War manager. In the factions tab, you can choose which faction is revealed, which faction(s) it can see and if it is supposed to be updated when not being revealed. This is handy if you want to have correct uncovered Fog of War. (Useful for spectators)
After having added factions to the Fog of War Manager, you can now add revealers with more than one faction to your game. (Useful for Multiplayer)
If you have a custom shader that is supposed to work with Ultimate Fog of War, you have to add a couple of lines to it. Please keep in mind that you have to do this for every pass (Important for big shaders that support defferred shading, multiple pixel lights etc.).
In order to correctly look up the Fog map correctly, we have to remap every vertex from a world position to a UV 0.0-1.0 range. Luckily, you dont have to worry about this, because Ultimate Fog of War does this for you. You only have to implement the function fixed4 FoWIntensity(float4 worldPosition) into your vertex programm. To be able to do so, include the FogOfWarMath.cginc into your shader. It is important to get the path correct or else the shader can’t find the file and it won’t compile.
In the next step, add the folowing lines:
#pragma multi_compile __ VerticalMode DDDMode
#pragma multi_compile __ FoWColor FoWAnimatedFog
This will implement other modes such as X-Y Fog of War and complete 3D Fog of War calculations. The second line implements animations of the Fog Map. Next, calculate the vertex world position and forward it to the fragment program:
o.worldPos = mul(unityObjectToWorld, v.vertex);
Finally, in your fragment programm, get the fog intensity and multiply it with your final output color to darken it:
return myColor * FoWIntensity(i.worldPos);
To get more info on converting shaders, check out the official documentation and the template shaders in the Ultimate Fog of War asset bundle. If you need support, you can contact me here: firstname.lastname@example.org