[TUTORIAL] Fog Of War

Wow, ive started ue4 in Compatibility Mode with win 7, and lags dissapeare! O_o
What the heck?
Btw, what version of ue4 are u using?

If you’ve enabled Dx11, you could try running the editor normally without the -D3D12-parameter set and see if there’s any difference. Dx12 is supposedly still very experimental and the GPU-drivers are probably not thoroughly optimized.

At the moment I use 4.10.1.

One more question.
How to make individual sight range for different units in one team?

Hi, This is my first time using C++ instead of blueprints so I am getting quite stuck :(. I am getting various errors when compiling the code such as “use of undefined type 'AFogOfWarManager” for example, I think it may be due to not changing the correct things in the code or not adding needed things. As I have simply copied and pasted the code it would be really helpful to know exactly what needs to be changed to make it compile smoothly.
Any help would be greatly appreciated.

Hi,

At the moment that capability would require a small rewrite. If you’re a coder, what I’d do is when registering the actor have another parameter for the sight radius. Then I’d store the actors with their assigned sight radius in a hashmap. This would require a little update in the FogOfWarWorker-thread to go from iteration over a list to map and use individual sight radii per registered actor. I’ll probably do that rewrite sometime along with a optimization to decrease the number of traces per actor.

Cheers,
.

I’m able to get it to work with multiple team based actors by selectively registering them with the FogOfWarManager. To get individual radius, inside of FogOfWarWorker I added the code below to the UpdateFowTexture() method which may not be the most efficient way but it works.



	for (auto Itr(Manager->FowActors.CreateIterator()); Itr; Itr++) {
		//Find actor position
		if(!*Itr) return;		
		FVector position = (*Itr)->GetActorLocation();

		// ----- START NEW CODE -----
		// Use sight radius from character attribute.
		ABaseCharacter* character = Cast<BaseCharacter>(*Itr);
		if (character)
		{
			sightTexels = (character->SightRadius / 100) * Manager->SamplesPerMeter; //Sight radius is in centimeters on character and the fow worker sightTexels expects meters.
		}
		// ----- END NEW CODE -----
...


You might need to include FogOfWarManager.h in the FogOfWarWorker.cpp since the set up in the first post expects it to be set up in your main project header I believe.

Oh, thanks for answer, but i already did individual sight myself :smiley:
Im a coder, but new to c++. The only one problem i still have, as someone in this topic, black square if fog of war isnt black (because i need map to be ‘discovered’) and if i use blur =S

As i see, this black strange thing only appearce in vertical blur, so need to look vertical blur code…

I didnt want to explore all code of bluring, cause im really new to c++, so i just added


		if (sum < 100){
				sum = 100;
			}

after vertical blur code, so he looks now this way


		//Vertical blur pass
		for (auto Itr(texelsToBlur.CreateIterator()); Itr; ++Itr) {
			int x = (Itr)->IntPoint().X;
			int y = (Itr)->IntPoint().Y;
			float sum = 0;
			for (int i = 0; i < Manager->blurKernelSize; i++) {
				int shiftedIndex = i - offset;
				if (y + shiftedIndex >= 0 && y + shiftedIndex <= signedSize - 1) {
					sum += (Manager->blurKernel* * Manager->HorizontalBlurData[x + (y + shiftedIndex) * signedSize]);
				}
			}
			if (sum < 100){
				sum = 100;
			}
			Manager->TextureData[x + y * signedSize] = FColor((uint8)sum, (uint8)sum, (uint8)sum, 255);
		}

and this vertical bug got fixed.
Hope it helps for anybody

  • THANK YOU for sharing this! In addition to FoW your tutorial helped me learn dynamic texture creation and multi-threading in a practical manner as well.

I used this FoW setup last year and had this thread bookmarked for replying but never got to it until now - I thought better late than never to drop by :slight_smile:

For people experiencing intermittent crashing - I found overriding EndPlay like this helped:


void AFogOfWarManager::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	FowActors.Empty();

	if (FowThread) {
		**FowThread->ShutDown();**
	}

}

I’m not using Fog Of War anymore (game no longer an RTS), but in any case - 's tutorial was a lifesaver when I really needed it, so thank you!

Hey man, that’s super nice of you to give thanks. As for multi-threading and dynamic texture creation in UE4, I learned both topics from tutorials so he should get at least 85% of the gratitude :slight_smile:

I have converted this to 4.10 and it is compiling, but I cannot add the SceneTexture:PostProcessInput0 node to the material. It just does not show up as an option. Any ideas what I’m missing?

Hi ,

I think you’d be able to add the node using these steps:

  1. Open you material.
  2. Select the material return node or don’t select any node at all. In the details panel under Material, change “Material Domain” from Surface to Post Process.
  3. Right click on the material black board and search for “SceneTexture”-node. Add the node.
  4. Select the node. In the details panel change “SceneTextureId” from SceneColor to PostProcessInput0.

Cheers,

Got it Thanks!! It was hiding in that pesky details panel. It is working correctly now thank you.

Hello! I got strange problem, FOW dont work in packaged game… Do u have any ideas?

@anonymous_user_964c5f2d thanks for this, this is very helpful and well explained.
A question, I am having some heavy fps oscillation, that cause i believe, a noticeable stuttering.
Are any of you having this kind of effect? It is expectable? My rig is pretty good. I’m using w10, so maybe could be related to that.

Thanks again!!

Also, FOW not working in network game, its always black =c

Guys, who used fog in network game?
I dont really understand, why it dont work…