[TUTORIAL] Fog Of War

A little help

Hey guys, i have been following this thread for some time, trying to implement the fog of war in an already existing project running UE4 ver 4.11.2, my project already has dynamic textures working, so the RHI Core and the Materials are working, but i can´t manage to compile the project with the code from this tutorial, the errors displayed in VSC are:



Error	C2084	function 'AFogOfWarManager::AFogOfWarManager(const FObjectInitializer &)' **already has a body	**ThirdPersonDef	C:\Users\Milo\Documents\Unreal Projects\ThirdPersonDef\Source\ThirdPersonDef\FogOfWarManager.cpp	6
Error	C2664	'double modf(double,double *)': **cannot convert argument 2 from 'float *' to 'double ***'	ThirdPersonDef	C:\Users\Milo\Documents\Unreal Projects\ThirdPersonDef\Source\ThirdPersonDef\FogOfWarWorker.cpp 	73	

The errors are related to this part of the code

in FogOfWarManager.cpp line 6:


#include "ThirdPersonDef.h"
#include "FogOfWarManager.h"

AFogOfWarManager::AFogOfWarManager(const FObjectInitializer &FOI) : Super(FOI) {
	PrimaryActorTick.bCanEverTick = true;
	textureRegions = new FUpdateTextureRegion2D(0, 0, 0, 0, TextureSize, TextureSize);

....


And in FogOfWarWorker.cpp line 73




...

		FVector2D fractions = FVector2D(modf(position.X / 50.0f, &integerX), modf(position.Y / 50.0f, &integerY));
		FVector2D textureSpacePos = FVector2D(posX, posY);
		int size = (int)Manager->TextureSize;
...



I was wondering if any one had this errors while moving the project from 4.9 to 4.11.2 ?

Project files 4.12.4

Ok i got the project files working, and now i upgraded the project to 4.12.4.
Any one can download the project from here: FOW
I will try to keep the project up to date with the latest release of UE4.

Good evening lads. I’m trying to implement this to work in a 2.5 D platformer I’m creating. What logic would you approach this with if you only wanted this to work within buildings or certain areas basically in order to hide the contents of the rooms you’re not in, but still be able to maintain view distance outside of buildings or through house windows?

7thNoodle,

The FOW-effect is applied through a post-process-volume. In the tutorial it says you should apply to an unbounded PP-volume, but there’s nothing keeping you from using bounded volumes. The main obstacle to use this FOW-solution for a platformer is that it’s written to to visibility checks and render the effect in the Z-plane. Assuming your platformer is some kind of side-scroller you’d have to rewrite the solution to be axis-independent in order for it to work outside the Z-plane.

Cheers,

hehe love the language of the description :slight_smile: and looking nice btw.

Thanks! Making it a “creative writing assignment” somehow made it easier to finish. Love your tutorials btw, great work :slight_smile:

Hello,
You use node “Absolute World Position”. Is it work with transparent materials?

Rendolf,

Sorry but I don’t understand your question. The “Absolute World Position”-node just outputs the position of a pixel from the G-buffer and has nothing to do with transparency. You can read more about the node here: Coordinates Material Expressions in Unreal Engine | Unreal Engine 5.3 Documentation

Cheers,

Ok, imagine you have two objects. One of them is transparent and second is opaque. Transparent object is between camera and opaque object. Absolute World Position returns the position of pixel that belongs to opaque object (not transparent that is closer to camera) and it will cause issue. Do you understand what I mean?

Ahh, now I understand. I’ve never really thought about that particular use case, but I imagine it won’t work at all. If you’ve already rendered the transparent actor over the opaque one the pixels affected will remain affected and there is no way to undo it in the PP-mat. You could maybe get around it with some clever use of custom depth, or you’d have to disable rendering the transparent actor if it’s in the fog.

How change height of sight?
I want that my character see upper thing than default or sight in 100 point in up.

Maybe you can look in this part of the code

You might want to check the file FogOfWarWorker.cpp

There is a part of the code that checks with a line trace if there is a hit found, the command needs a starting and a ending point, in the code they are: position and currentWorldSpacePos


if (!Manager->GetWorld()->LineTraceTestByChannel(position, currentWorldSpacePos, ECC_WorldStatic, queryParams)) {

More on this command here

if you look some lines above this command, the code is inside a nested loop, which i assume is the responsible for iterating around the surrounding texels/positions of the character.
this nested loop iterates in X and Y but the Z component is not changed.


FVector currentWorldSpacePos = FVector(((x - (int)halfTextureSize)) * dividend, ((y - (int)halfTextureSize)) * dividend, position.Z);//<---see the position.Z, this part is not changed inside the loop

in fact the position.Z comes from a call outside the loop some lines above


FVector position = (*Itr)->GetActorLocation();

Playing with this Z value (adding some vector or scalar) to make the line traces above the Actor Location could give you the a change in height of sight.

I hope this can be of help, is not a solution but a starting point.

How fix error when rebuild project to 4.13?
I have error LNK2019.

funkish,
If you get Linker-errors, it’s probably because you didn’t add module dependencies to RHI and RenderCore. Did you complete these steps?:

  1. Open the file named MyProject.Build.cs
  2. Locate the line PublicDependencyModuleNames.AddRange(new string] { “Core”, “Engine”…
  3. Add “RHI” and “RenderCore” to the array.

I just upgraded to 4.14, and it worked without any editing, so it shouldn’t be an issue when upgrading.

Cheers,

New powers!

Hi, i have just updated the project to 4.14

Link to the zip file —> HERE

Also, i have been making some modifications to the code, adding some new features:

A new component called RegisterToFOWManager: this new component is added to every actor that needs to affect the dynamic texture

The component has the following features
A dropdown selection for choosing the FOW manager
3 Boolean variables

	Write Unfog : Check if the the actor able to reveal areas of the terrain
	Write FOW: Check if the actor able to affect the Fog of war
	Write Terra Incognita: Check if is the actor able to affect the terra incognita area

1 Boolean variable that reports if the actor is currently in the Terra Incognita

Also, the FOWManager can now load images or masks that represent the Fog of war, white for FOW, black for Terra Incognita, some info on how it is done here

when loading images, it is very important to set the texture to No MipMaps and Compression to DisplacementMap, otherwise the editor will crash. This will be addressed soon.

https://youtube.com/watch?v=vSoFOZO_J14

The Zip file containing the project is HERE, the project is also for the 4.14.0 version of UE4

As this thread is a tutorial in the next posts i will explain in depth the changes i’ve made to the code, where and why.

Have fun!

Hi, Milotron!
It’s a nice stuff. I played with it and noticed: unveiling areas is too rough. Any ides on how can it be smoothed?

Hi, flash7, unfortunately i did not implement the new features inside the part of the code that performs the blurring for reducing the roughness when unveiling, partially because i still don’t understand how the blurring is performed in the code :frowning:
But i am still working on it, so i think soon i will upload a project with the blurring working.

I happen to also did something similar, but probably less functionality and may be suitable for simple fow http://phungdinhthang.com/2016/12/12/unreal-engine-4-fog-of-war-tutorial/

@Milotron. This is some pretty cool stuff. I just had to reply that I like the first post with the mission reports as an old fan of the 40K and WH tabletop games. Looks pretty slick and you actually use threads (programming, not forum threads). A lot of “tutorials” don’t, so It is nice to see some more “advanced” features used.

The credit should go to , he is the one who wrote the tutorial, I just modded a small part inspired by his code :slight_smile: