Hello,
I’m working on an RTS game and I am trying to implement Fog of war. In my game there will be lots of units and buildings.
I know that fog of war and easily become really heavy. Especially when there’s lot’s of units / building in the game. What is the cheapest way in making fog of war?
I don’t need anything necessarily fancy as well.
I’m pretty sure that I need to write shaders to achieve that, the problem is that I am not so good with shaders. As a matter of fact I’m quite terrible…
Can someone give some advice on this?
Thank you
There are definitely more solutions, and perhaps different requirements. For example, you may want a simple RTS-like FoW where a simple circular area is revealed around each unit, or you may want to have some line of sight obstruction , or even take the heightof the scene into consideration.
For me, the usual solution is: Generate a renderTexture that represents the whole level (well, only works for a finite-size levels, up to some size of course). Then you can render your unit’s visible area into this texture. You can do that simply by rendering a circle mesh, a quad with a circle texture, or some more advanced technique for line of sight and height - which may be suitable for a shader, but may also be done on CPU, ideally on a separate worker thread.
Make sure the process does not halt your game, by threading or at least coroutines (split rendering between frames)
After you have this world overlaying texture, it’s time to project it on the world. One obvious solution is to have every shader in the game to sample this texture in world space and darken itself accordingly, but that’s not very practical. Instead, I suggest you take this texture and project it on a rendered scene (as a camera image effect / postprocessing) using the camera’s depth texture.
An extra trick would be to render one channel (eg. red) as currently visible, and other channel (eg. green) as previously visited / seen areas.
The challenge I found when using the CPU method is the time it takes to upload the new texture content to the GPU (which blocks the main unity thread), so if you can, make everything work on the GPU entirely.
Now, you say you don’t have any experience with the shaders. Well perhaps it’s time to get some Here you’d be working with simple (not lit) single pass shaders only.