Partial Post-Process Effect Application in UE4
After a long long time (yay summer?) I'm back with a post on what I found to be a rather useful technique. Imagine you have a post-processing effect that you apply to your viewport. But what if you want to apply it only to a certain portion of the screen? What if you want to use say edge detection only on a certain object?
Unsurprisingly, this is very doable in Unreal Engine using Custom Depth buffer that allows you to select only some objects and write there scene depth into it, while ignoring other objects.
Side note, this blog was inspired by one by Tom Looman and I most definitely suggest you go through it too, as it contains more information on issues and limitations of Custom Depth buffer.
Idea
Idea is actually very simple. Since Custom Depth buffers allows you to track only the objects that interest you, you can use it to decide whether to apply the effect on it or not. If the value in Custom Depth buffer is smaller than that in the of the Scene Depth buffer, you should apply the post-processing effect. Otherwise keep the image the same.
Setup
This approach also doesn't require writing any more code. First off, we have to decide which object will get to have post-processing applied. You can do this by clicking on the desired object and checking Render CustomDepth Pass as seen bellow.
If you have trouble finding it just write "custom" into search field.
This will add the object to Custom Depth buffer. You can visualize how your Custom Depth buffer currently looks via Buffer Visualization option.
In my case I added two objects into the buffer and here's what it looks like. The Depth buffer is shown on left, while Custom Depth buffer on right.
So far so good. Now let's change our post-processing effect so it takes Custom Depth buffer into consideration. In our case, I'll be using Sobel edge detection effect. Current material setup looks like this.
So first off we need to add Custom Depth and Scene Depth nodes. Both of these are SceneTexture nodes, only thing you need to do is to change type like this.
Now only plug these two values into If node. Since If node doesn't allow to work with vectors, we need to somehow get a single float out of the color. This can be easily done by using Mask node and taking R channel (since depth maps are written as greyscale images, you can actually take B or G channel if you want to).
Once this is done we plug masked Custom Depth as value B and masked Scene Depth as value A. If Scene Depth value is higher or equal to Custom Depth value you want to apply the effect (this means that nothing is blocking the object in Custom Depth buffer). If the value of Scene Depth is smaller, leave the original scene color (this means something is standing in front of the object where you want to apply the effect).
The setup looks like this.
Note that PostProcessInput0 color pin returns float4 so we use Mask node in order to only get RGB values.
Final result can for example look something like this.
Application
Partial application of post-processing effect can have many uses (so go wild :) ) ranging from outline detection, object highlighting, and so on. Before I wrap up this post I'd like to show a simple use with Sobel edge detection and First Person camera that can (sorta) mimic say a use of a special lens. The effect we want to achieve looks like this.
In this case we will have a plane in front of player's camera that will be marked for Custom Depth buffer. Everything that is "occluded" by the plane will have the effect applied.
All you need to do is add the plane to your hierarchy in your character blueprint (I parented it with Mesh so that it will move with camera, I also assume that you already have the PostProcess component applied to your character and that it has correct material set up). Make sure to place plane in eye-height of the player.
Next mark plane for Custom Depth pass as shown in Setup section of the post. Yay should work!
Not quiet unfortunately. Since our plane is still a solid object it will "block" everything beyond it so the effect will only be applied to the plane. As mentioned in Tom Looman's post, making plane transparent won't quite work. We need a way to not physically render the plane and yet to keep it in our Custom Depth buffer. Fortunately, this is pretty easy too! All you need to do is uncheck a checkbox.
Not quiet unfortunately. Since our plane is still a solid object it will "block" everything beyond it so the effect will only be applied to the plane. As mentioned in Tom Looman's post, making plane transparent won't quite work. We need a way to not physically render the plane and yet to keep it in our Custom Depth buffer. Fortunately, this is pretty easy too! All you need to do is uncheck a checkbox.
Select the plane and in Render section deselect "Render in Main Pass" as shown below.
And that's it! You should now have your own partially applied post-processing effect.
Results
To sum up the tutorial, here are once again results of the effect.
Notes
- Custom Depth buffer can't be applied to everything, but it is a very useful technique. So to save up some headache be sure to read some of its limitation and issue
Resources
Tom Looman - more information on Custom Depth buffer and effects it can produce
Official documentation - contains information on post-processing materials, custom depth, material blending and more
Sobel edge detection - tutorial for Sobel edge detector used in this post
Hi! I have just found your site. Great, quality content, lots of stuffs to learn! Thank you and keep up the good work!
ReplyDeleteI'm glad you found it useful :) I will definitely try.
ReplyDeleteHello, I just found your blog and youtube channel and plan on beginning three of your tutorials really soon, and this one in particular interests me because I want to use multiple different shader effects depending on what's being rendered. That said, I do have a question because this doesn't seem to be outlined in this post at all.
ReplyDeleteLet's say I want to use 4 different post process materials, I don't necessarily want to do that, that number is just for the sake of this question. The way this tutorial is set up seems to suggest that only 1 or 2 post process materials can be used with this method. So if I wanted to say apply the hatch shading only to characters, the oil painting material specifically to naturalistic environments like forests and rocky areas, a separate watercolor material to certain ethereal effects, and a general cell shading material for the purposes of coloring, shading, and lighting in certain areas. How would I go about this? All I'm asking for is a bit of elaboration.
Thank you and have a good day.
Hey, glad you found the tutorials useful. This post wasn't meant to show this, but rather just show a binary situation where you either apply the post processing material or you don't.
DeleteI'd say there are several ways to do what you are trying to achieve. You can use multiple post-processing materials and blend them together, which I tried to describe in this post: https://ferkizue.blogspot.com/2018/11/adding-occlusion-to-multicolor-soft_25.html
Or, if you'd prefer to have all the PP effect in one material (which makes it more effecient probably, as you don't need to run multiple PP processes). Then you only need to identify the objects for which you want to use PP material A, PP material B etc. The identification can be done using Custom Stencil Buffer, so say PP material A will have stencil value 1, PP material B will have stencil value 2, etc.
I talked about using of Custom Stencil buffer to identify the objects in the scene here: https://ferkizue.blogspot.com/2018/10/multicolor-soft-outline-in-unreal-engine.html
or you can check the original blog by Tom Looman here: https://www.tomlooman.com/multi-color-outline-post-process-in-unreal-engine-4/
I'm not entirely certain if this makes sense, but give it a shot. If you can't make it work, let me know and I'll try to write it down as a tutorial after New Year.
Cheers.
Thank you for such a quick reply, I always appreciate urgency in responses. I will definitely attempt to as soon as I actually have the materials to work with as not all of those materials I just listed have tutorials I can follow to build them and I'm still pretty much a novice with shader effects. Currently, I'm waiting for a response to the forum post on that exact topic so I can begin building those materials. As soon as I have them to work with I will definitely try the method you suggested and see how it works.
DeleteThank you and have a good day.
This is amazing. This is amazing. This is amazing.
ReplyDeleteThank you in general for this, but you've also given me a newfound appreciation for the power of PPVs.
Do you know of a way to selectively apply built-in post processing effects? This is working great for one effect I wanted to apply, but I'd like to also do basics like chromatic aberration, bloom, and exposure compensation. I'm sure I could recreate all of these effects in my post process material, but I'm not advanced enough to know how (and I'm assuming it would be more time consuming).
Hello there, unfortunately no clue, I haven't really looked into the built-in effects although if you are able to apply custom post processing after these are done, then I believe you can get them into the shaders and play with it
Delete