- Home /
How would I do fog of war?
Hi, I've reached an impasse in my game development. I know this has been asked many times before, but I just can't seem to figure out how to create fog of war for an RTS game. I've seen many solutions, and one in particular struck me as being doable. That one is modifying the visibility of portions of a mesh depending on a units position relative to it. An idea I had is modifying the visibility of the terrain, and then just using another script (one that I can easily write) to make visible other gameobjects if the unit is in range. Both seem like good ideas, but I've got no idea on how to implement them. That's where you come in... Thanks in advance!
Answer by Peter G · Apr 08, 2010 at 10:35 PM
The best idea I have seen is to place plane between your terrain and your camera then raycast from your player upwards into the plane. After that you find the point your raycast hit and lighten that area's vertices.
Here's a forum link with several scripts and examples.Unity Forum/ Fog of War
My only recommendation is to keep the vertex count as low as possible. Fog of war scripts usually require iterating through arrays of mesh colors (or pixels) nearly every frame.
Another idea is to use a texture and change the alpha of pixels instead of vertices. It would work very similarly to the mesh transparency idea.
That leaves me at a loss because I still don't know how to change specific vertices of a mesh. Could you explain that?
The scripting guide does a good job explaining the mesh scripting interface so read that first to understand the concepts behind procedurally modifying the mesh. Then the link does a good job explaining how to modify the color. Basically you iterate through the mesh and change the alpha of the vertices to either 1 or 0 and that makes it transparent or opaque.
Actually, I guess my real question is 'how do I find which vertices are closest to me?' I know I have to raycast up, but how do I find which vertices are hit when I raycast?
There are two ways I can think of. Find the triangle index of the mesh and find those vertices (it is as simple as the index * 3 + 0, 1, and 2 for each vertex respectively. That way involves less iterating, but only gives you a small clear area unless your triangles are very large. The other option is to gather all the vertices in the mesh and every time you update your raycast, iterate through the mesh vertices (if you don't know how to do that see the reference) and find the distance between the hit point and the vertex. You will have to use InverseTransformPoint to get the world space.
cont. Then if the distance is under a certain radius, you turn it clear.
Answer by risk01101 · May 23, 2014 at 10:31 AM
Here is my system:
1) Edit->Render Settings - Enable FOG - Fog Mode Exp2 - Fog Densiti: 0.001 - Ambient Light: RGB(28,28,28) 2) Remove all light in your map 3) Place Point Light inside your character:
Will several lights in screen will add problem like 200 spot lights ?
Answer by trileletri · Nov 02, 2011 at 03:12 PM
I once made a fog of war. It was really simple once it was made. However i can't locate the files to send you some code, but i remember the algorithm:
lay a plane above the ground
make black generic texture, let's say 512x512
locate certain pixels on texture (the ones which will represent a point from terrain, location of a real time strategy vehicle unit, for example)
using given pixel coordinates, draw a white circle around it on texture (radius is sight range)
for better looks, use blur shader on texture, to smooth "teeth" on texture
apply texture to plane
repeat each 10 frames, or tweak this until you're happy
Your answer
Follow this Question
Related Questions
RTS fog of war texture remapping shader,Fog of war plane texture remapping shader HDRP 0 Answers
Lighting Light Increase 1 Answer
Cheap way in making Fog of War 1 Answer
Multi-Unit Fog Of War for Mobile 2 Answers
RTS Style Camera Scrolling 8 Answers