# How to use custom depth selectively for certain objects?

**URL:** <https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232>\
**Category:** Rendering\
**Tags:** unreal-engine, Rendering, Blueprint, question\
**Created:** [April 4, 2023, 4:14am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232 "2023-04-04T04:14:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sulleyyyyyy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sulleyyyyyy](https://forums.unrealengine.com/u/Sulleyyyyyy)\
**Post date:** [April 4, 2023, 4:14am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/1 "2023-04-04T04:14:36Z")

</div>

Hi, I want to implement occlusion hightlight using materials. When my character is occluded by some objects it can still emit some color rather than being totally invisible. Here is my material blueprint:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/1/7/41766b9337fa97e4f4bf89ab0c8ceb102aba60c9.png)  
and here is the result:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/c/8/ac822bdbea6c9132aa127db038de053b85d096ff.jpeg)  
where I use an infinite post process volume and set my character’s `Render CustomDepth Pass` to true.

Now everyhing works fine. But the problem is: anything that blocks my character will make an effect on the custom depth, but I really want some controllability over the objects with which I can determine some will make my character become transparent while others won’t.

For example in the following two figures, the first cube occludes the character but it does not make any effect to the transparency but the second one, if I specify, will make an effect.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/9/4/694ec13b9e3bc93ccdfad296ec57c012078fb9b8.jpeg)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/e/d/1ede041e248a5bfc0162999382254e3a404faf37.jpeg)

How can I do this using either blueprint or code?

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [April 4, 2023, 4:59am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/2 "2023-04-04T04:59:30Z")

</div>

Try this guy: [[Multi-color Outline Post Process - Tom Looman](https://www.tomlooman.com/unreal-engine-outline-multi-color-post-process/)]

---

<div class="post-metadata">

**Author:** ![Sulleyyyyyy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sulleyyyyyy](https://forums.unrealengine.com/u/Sulleyyyyyy)\
**Post date:** [April 4, 2023, 11:41am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/3 "2023-04-04T11:41:38Z")

</div>

Hi, thanks for your reply. I tried custom depth stencil but it did not work in my case because i still cannot determine the occlusion relationship between the occluder and my character.

That being said, if i set the stencil value of the occluder to 1 and that of my character to 64, then the custom depth buffer will store a value of 65 at the pixel of occlusion but no more information about which one is at front. In this case, even if my character is in front of the occluder (closer to camera), the character will still become transparent.

---

<div class="post-metadata">

**Author:** ![Sulleyyyyyy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sulleyyyyyy](https://forums.unrealengine.com/u/Sulleyyyyyy)\
**Post date:** [April 4, 2023, 11:49am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/4 "2023-04-04T11:49:46Z")

</div>

> **[Overlapping Custom Depth Stencils](https://medium.com/unreal-engine-material-guides/overlapping-custom-depth-stencils-a084aa763f10)**
>
> In this post I’ll assume that you are already familiar with UE4 editor basics, material basics, scene depth and post process materials. If…

I referred to this tutorial but it does not allow me to determine which object is occluded.

---

<div class="post-metadata">

**Author:** ![jeffmeyer3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jeffmeyer3d/32/965059_2.png) [@jeffmeyer3d](https://forums.unrealengine.com/u/jeffmeyer3d)\
**Post date:** [April 4, 2023, 7:07pm UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/5 "2023-04-04T19:07:01Z")

</div>

Try adding custom tags to the objects that you don’t want to cause the effect, then do a check if the tag exists before executing the effect.

---

<div class="post-metadata">

**Author:** ![Sulleyyyyyy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sulleyyyyyy](https://forums.unrealengine.com/u/Sulleyyyyyy)\
**Post date:** [April 5, 2023, 6:55am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/6 "2023-04-05T06:55:27Z")

</div>

Hi jeff, could you please offer more hints? I cannot find any tags in the material editor.

---

<div class="post-metadata">

**Author:** ![Sulleyyyyyy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sulleyyyyyy](https://forums.unrealengine.com/u/Sulleyyyyyy)\
**Post date:** [April 6, 2023, 6:54am UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/7 "2023-04-06T06:54:21Z")

</div>

Perhaps I need to create custom shader based on C++? But I conjecture there is a workaround with pure material editor and HLSL.

---

<div class="post-metadata">

**Author:** ![Frenetic](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frenetic/32/1142542_2.png) [@Frenetic](https://forums.unrealengine.com/u/Frenetic)\
**Post date:** [April 6, 2023, 1:22pm UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/8 "2023-04-06T13:22:12Z")

</div>

Just loosely thinking here but if you run a line/sphere/whatever trace on/around your character, can you get the things found and update values in their respective material-instances?

Maybe update an MPC that materials can centrally-reference?

Writing your own shader might be a possibility. It seems that, mechanically, you’d want to use a bitmask AND depth and I don’t know how to get both of those via a material/shader.

The trace might help you sort depth, but no idea how performant it might be on top of everything else. If you are already doing some kind of trace (like for lockons) maybe piggy-back?

---

<div class="post-metadata">

**Author:** ![jeffmeyer3d](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jeffmeyer3d/32/965059_2.png) [@jeffmeyer3d](https://forums.unrealengine.com/u/jeffmeyer3d)\
**Post date:** [May 15, 2023, 7:40pm UTC](https://forums.unrealengine.com/t/how-to-use-custom-depth-selectively-for-certain-objects/842232/10 "2023-05-15T19:40:51Z")

</div>

Sure thing.

1.) Select your actor → Details Panel → Actor → Advanced → Tags (+)

2.) Add a Tags Array Element by clicking the (+) sign

3.) Type in a name for your Tag, such as “Occluder” (or whatever you want to call it)

4.) In Blueprint, add the node “Get All Actors of Class with Tag”

On the node, be sure to use the “Actor Class” drop down to select the class you want to match.  
Also, on the “Tag” input, be sure you type in the same exact tag name you used for the actor(s) you want to fetch.

From there you can, set the result as a variable, and fetch them using a For Each Loop on it to do whatever you need to do…such as setting Custom Depth on / off.
