How to occlude an object from post processing effect using stencil

In our game, we can bring objects up to the camera to inspect them. I am applying certain settings in my post process volume to get a depth of field effect, but currently this depth of field effect applies to everything on screen. I wish to not include the inspected object in the depth of field effect so that none of it gets blurred. I assume I need to use the stencil buffer to occlude the object from the effect, though I don’t know how to implement this.

So far, I’m grabbing the post process volume in the level and changing the settings.

void AInspectionActor::EnableInspectionCameraDOF(bool isEnabled)
{
	if (_postProcessVolume != nullptr)
	{
		if (isEnabled)
		{
			// enable DOF
			_postProcessVolume->Settings.bOverride_DepthOfFieldFstop = true;
			_postProcessVolume->Settings.DepthOfFieldFstop = 4.0f;

			_postProcessVolume->Settings.bOverride_DepthOfFieldFocalDistance = true;
			_postProcessVolume->Settings.DepthOfFieldFocalDistance = 20.0f;
		}
		else
		{
			// disable DOF
			_postProcessVolume->Settings.DepthOfFieldFstop = 4.0f;
			_postProcessVolume->Settings.bOverride_DepthOfFieldFstop = false;

			_postProcessVolume->Settings.DepthOfFieldFocalDistance = 0.0f;
			_postProcessVolume->Settings.bOverride_DepthOfFieldFocalDistance = false;
		}
	}
}

What I have so far works great for applying a depth of field blur to the screen. I assume the next step is to create a post process material that gets added to the list in our PostProcessVolume?

This is where I’m lost. I’ve tried looking up other posts but I’m not really understanding what’s going on in them. Plus, they’re creating their effects in the blueprint and feeding that into their nodes, which decide to use that effect or not. In my case, I’m making my effects change in the code seen above and I don’t know how to access using those specific settings or not. If someone could explain how to do this in code all the better. If not, I’ll take how to do the remainder in blueprints. For reference, we’re using version 5.02 so some things might be a little different than they were in UE4.

You can do it in a few ways. With Post Processing you can exclude / include things with a mask like a CustomDepth mask.

https://www.youtube.com/watch?v=ZeYlal1nYrQ

You could also not use post processing but instead spawn a widget containing a canvas + fullscreen “blur” widget and render the inspected item in front of it. Could be done by showing a rendertarget of the model.

https://youtu.be/dKRsW-JtroM?t=269

Should be the same for UE5, you also won’t have to access BP from c++ if you separate the design process from the programming (do widget design, rendertarget setup, material setup in BP)

1 Like

First off, thank you for replying.

I might be wrong, but I’m not really looking to use depth as a determining factor for applying or not applying the effect. Part of my problem with all the tutorials I’ve found is they’re all showing how to use something based on its depth compared to other objects in the scene. I believe I want to use the stencil buffer for occlusion instead of the depth buffer. On the plus side, the tutorial you shared did do a better job of explaining using the depth buffer than other similar tutorials. I’ve bookmarked it in case I ever need to use it for something else later.

I hadn’t thought of your second idea. I think it could work if I can’t otherwise get this working, though I’m going to try a more “official” route first before resorting to a widget overlay.

The following are two different versions of a post process material that I created and placed in the array of materials in my post process volume. I feel like one of these should be close to what I need, but neither version works and I don’t know how to fix them. For reference, in my code, when I turn on the depth of field settings for the post process volume, I also set SetRenderCustomDepth to true and SetCustomDepthStencilValue to 50 for the object. DepthOfFieldFunction is a built in node, not an external function I created. I don’t know if it’s actually using the settings I set in the original post.

I believe we both meant the same, The first video shows how to set up the stencil which is not related to camera distance (depth) but a stencil ID. After setting the ID comparison logic on the material you have to also mark an actor you want to inspect with this exact ID.

What the DepthOfFieldFunction does or doesn’t process is not relevant for the end result because you implement a switch (Lerp) right after it which shows A or B where the stencil mask makes the final decision of what to render (A / B).
The nodes on the second image should work exactly the same and the logic seems fine to me.

So with those things set up there is not much left that could go wrong. Maybe the post processing volume has bounds set and you are outside of it? or the post processing effect strength is not set to max? Or it conflicts with another depth of field effect? I had a bit of trouble with the stencil ID myself some time ago and I can’t remember why.

  • Edit there is a free plugin on the marketplace (Horror Engine) which does exactly what you want