|
|
|
|
Reply From: |
gmaps |
Since noone answered, I’ll answer myself as I have successfully implemented a FOW (with some issues though).
So first, step, we need a texture. I usually use a texture of map_size * 2.
Next, we need to paint the areas where the units are. I tried doing that in GDScript, i hade premade matrices and just pasted them to an image i then converted to texture. Even though its O(n) performance, It had a big impact on FPS. I was advised on Godot discord to do the brute force GPU implementation. I encoded the unit positions and ranges into texture and then looped over them in the shader. If it gets slow with unit number I can always implement some version of quadtree to speed it up. If a location is in range of a unit, I make the color 1 (Red channel), otherwise make it 0 (or in between if it’s on the edge - maybe this fade step is unnecessary as I also do a blurring step aftewrards).
But it’s also important to check the previous value as we also want to store information about the discovered area. So when I set Red channel to 1, i also set the Green channel to max(0.5 * 1.0 (value of red), previous_green). And in next iteration before processing the locations I retrieve the pixel screen texture and set the current red color to previous green.
I added a timer to 0.1s to compute the texture, which was enough for “smoothy” look.
So the areas that I currently see have orangy color (combination of red and green), the discovered ones are green and others are black.
Then I do two pass blur on the texture.
Second step:
I add a quad (check this article) to screen that represents the Fog of war, and give it the visibility texture to sample. I get the pixel location, and check if it’s level of visibility and I turn alpha on the quad to “visibility” level.
And that’s it.
Note: the transparent objects will have issues with this method and will have to be “fog shaded” separately, by passing visibility texture directly to their shader.
Note2: Downside of this approach is that if you move camera in nondiscovered area and look against the discovered, you may see black silhouettes of objects in nondiscovered area (e.g. trees). From gameplay perspective it’s not that bad, as we can hide and show units based on physics areas, so we can’t use this hack to see units/buildings. Fixing it may include passing the visibility shader to all objects and then moving all y coordinates to 0 in vertex shader. But it’s not worth the hassle, at least for me. If anyone has an idea how to solve this issue in an convenient manner, please do tell.
Feel free to comment if you have questions/issues with FOW.