>> Welcome back to
another Unreal Tips and Tricks. In this episode, we'll
continue to delve into post-process
material masking tricks. In a previous Tips
and Tricks, we looked at the custom
stencil buffer and how we could mask out
different objects giving them each their own
rendering style based on which stencil buffer
ID they were using. This time, we'll
be looking at how to use the Custom Depth
and Scene Depth render targets to mask out objects. As before in part 1, we need
to go to our project settings and then search for depth. From here, we need to go
down to Engine Rendering, Post-processing, and make
sure that Custom Depth Stencil Pass is enabled. If you recall from part 1,
we talked about the GBuffer on the video card and how
Unreal's rendering system uses render targets of the
various aspects of our scene render. Those render targets are
then sent to the GBuffer and put together to
create the final render. And remember, if we
go to our View mode, we can then go down to
Buffer Visualizations. And we can preview all of
those different render targets. In this case, we're
going to be making use of two render targets. And the first one is going to
be the Custom Depth Buffer. Now, when you turn
this on, everything is going to look
black in the scene. And that's because we haven't
enabled any objects to make use of the Custom Depth Buffer. But if we go back to our
Buffer Visualizations and then choose
Scene Depth, we can see that the entire scene has
a gray scale applied to it. Objects that are
closest to the camera will be rendered darkest,
while objects further away will get brighter and brighter. In this case, we're going
to be using the Custom Depth Buffer applied to this mannequin
so that we can mask him out from the rest of the scene. So in order to do that, we
need to select the mannequin. And then on the Details
panel, we'll search for depth. Here under Rendering,
we just need to enable the Render Custom
Depth Pass option here. Now, if we go back to
Buffer Visualizations and the select
Custom Depth, we can see that the rest of
the scene is now black while the character is making
use of the Custom Depth buffer. And so he's rendered in
a slightly gray tone. We can go back to Lit mode now. So the next step is to create
our post-process material. For this, we'll select
our post-process volume. And then we'll
search for material to bring us to our
post-process materials. If you recall from before,
post-process material is a material that can be
applied to the entire screen in what we call screen space. And these materials can
apply all sorts of effects. And in this case, we're
using them to create masks. To add our material,
we need to add it using the Add Elements button
on the array of materials here. This gives us a new element. And we'll use the dropdown
to choose Asset Reference. From here, we're ready
to plug our material in. But, of course, we're going
to need to create one. We'll call this material
PPM and then Custom Depth. As before, we need to change
the domain of our material from surface to
post-process allowing us to use this material in
our post-process volume. In our material,
we're going to need to get access to the
different render targets. In this case,
we're going to need three of those render targets. And that means we're going to
need three scene texture nodes. In the first part,
we talked about how scene textures give us access
to the different render targets in Unreal. The first one we're
going to need access to is our raw post-process input. So on the Details
panel, we're going to select Post-process
Input zero. And this gives us a
render of the Viewport without any post-process
effects applied to it. Now, I need to copy
and paste this twice. And we're going to
need to get access to the Custom Depth and
Scene Depth render targets. So for the first one
here, we'll go ahead and select Scene Depth, which
you'll find right at the top. And then for the
second one, we'll need Custom Depth, which is
found around the middle right above the Post-Process Input. We got to reorganize these so
my Custom Depth is on top here. And we're also going
to need a color value. We're going to
mask the character out using a color so
the character will be rendered as that color. So for that, we're going
to use a Vector 3. And we'll set the default
color in this case to a value of white. If you remember from part 1, we
used a lerp to do our masking. So I'm just holding the L
key down to create that lerp. And we're going to use the
full color of our scene here. We're going to plug that
into the A part of our lerp. And it will plug our color
here into the B input. And we'll be using Custom Depth
and Scene Depth as our mask. So what we want to do is we
want to subtract the Scene Depth from our Custom Depth. So we'll pull off of the
color of our Custom Depth and do a Subtract. And then we'll pull off of
the color on our Scene Depth and plug that into the
second input of our Subtract. From here, we want
to make sure that we clamp our values between the
proper ranges of 1 and 0 for an alpha mask. And then we can plug that into
our alpha channel on our lerp. And then we'll take our lerp and
plug that into our result node. You can see we
have an error here. And it's just letting us know
that we're mixing float threes and float fours. So we're using RGB alpha
as well as just RGB. To fix that, we're going
to add some masks here. So I'm going to pull
off of the color output of my Post-Process,
search for mask. And we're going to specify the
red, green, and blue channels on that only and plug
that into our A input. And then we'll do the same
thing off of our Clamp. We'll pull off of that,
search for a mask. And in this case, we only
need the red channel. And then we can plug that
into the alpha channel. One last thing so that we
can easily modify this. Let's convert our color
here to a parameter. And we'll call it Masked Color. Now, we simply need to
right-click our material and convert it into
a material instance. We'll call this
PPMI_CustomDepth. And now we can plug that into
our post-process material array. Now, you can see
right away that we do have some masking going on. But what we have is the opposite
effect of what we expected. So the entire scene has
been masked out in white while the character is
being fully rendered. And this gives us sort
of a Matrix-y room effect. And if we open up our
material instance, we can change the
color of the mask. So let's go ahead and make
it a red color instead. Now the other thing you're
going to probably notice is that the character has a
bit of a jittering effect. This jittering effect is caused
by temporal anti-aliasing. And there are a couple of
ways to deal with this. And the way we'll be doing it
is using our material settings. So we'll need to load up our
Custom Depth material again. On the result node,
if we scroll down to the Post-process
Rollout, you'll find a blendable location. Currently, the blendable
or post-processing material is being rendered
after tone mapping. This is the default setting. But if we changed this
to before tone mapping, you can now see that
the jittering is gone. Now, let's say we
want to reverse this. So the character is
being masked in red while the rest of the
scene is fully rendered. Back in our material, we simply
need to add an inversion node. So after the
Subtract here, we're going to pull off of that
and search for 1-. 1- will allow us to flip
the blacks and the whites in our mask. And we'll plug that
into our clamp. And now we can see
the character is fully masked in red while the rest
of the scene is full color. Now, obviously, this
isn't the only way that we could mask
the character out. We could apply all sorts
of other effects to him. We could make him
black and white. Or we could have the color
more translucent, perhaps, have it pulse as if to say the
character is being selected. We could even have an
outline applied this way. There's all sorts of
effects you can achieve using masking like this. All right, so that covers
masking using the Custom Depth and Scene Depth buffers. So we'll see you, then, on
another Unreal Tips and Tricks.