Custom Depth in Unreal Engine

Unreal Engine 4 introduced a new depth buffer along with its PBR rendering system. It’s called “Custom Depth” and can be used for effects like the selection outline that is built-in in the Editor, rendering of occluded meshes, or custom culling of transparency. I will discuss some of the basics of using this buffer in the material editor and explain some of the features I used it for in my own projects.

When using Custom Depth it’s important to read through the Issues & Limitations at the bottom of the page to prevent hours of headache and unnecessary debugging.

ue4_coloredoutlines01_small

Custom Depth 101

For those who have never used this feature before, you can enable Custom Depth for both Static and Skeletal meshes under the Rendering category named “Render Custom Depth” in the Details Panel.

ue4_customdepth_properties.

To view the render output for this particular buffer go to Buffer Visualization in the viewport options of the Editor Viewport. Note that objects close to the camera are near-black and may be difficult to see!

ue4_customdepth_visualisationdropdown
Custom Depth Visualizer in the Unreal Editor

Rendering Object Outlines

To render outlines around specific meshes (like a fallen ally, usable loot container, etc.) we need a buffer to get their silhouettes. Custom Depth is perfect for this.

With the buffer filled with our object depth info, we can perform a simple depth comparison by sampling the neighboring pixels in our post-process. If a neighbor has depth info but the pixel itself does not – we color it to our outline color.

We can use another technique for drawing a translucent overlay for all occluded geometry by comparing custom depth against SceneDepth. If CustomDepth is larger than SceneDepth we blend in a white tint with SceneColor.

Combining both techniques results in the post effect below:

ue4_customdepth_outlines_03

I skipped over most of the implementation details – if you have any questions feel free to ask them in the comment section below! I’ve made this effect available for download, feel free to modify and use this in your game. (Check the new paragraph for a much more detailed and advanced version of this effect!)

NEW: Multi-Color Outline Effect

A few engine versions ago Stencil Index buffer was added alongside Custom Depth. This enables us to create multi-colored outlines among many other cool new tricks! Click here to read about this new effect and how to use it.

[gumroad id=”ue4outlinecolored” type=”embed”]

Culling Inner Triangles

When using transparency on models like characters that have inner triangles (eyes, mouth, arm, or bit of armor sticking through) you will see a noticeable highlight in your material that attracts attention and simply doesn’t look right. We can solve this issue using Custom Depth.

By rendering our character into the Custom Depth buffer we can cull any pixels that are behind the outer shell of the mesh. A slight depth offset should be added when comparing the depth of Scene and Custom to prevent too many pixels to be culled including the outer shell. The material setup for this is quite simple:

materialnodes_depthculling

Material Nodes – Copy this URL content and you can directly paste the above nodes into your own material. (Project Source is available on GitHub)

I made a comparison of depth culling enabled and disabled in the setup below. The character on the right displays all occluded pixels in red. I rendered each character TWICE to work around a limitation where transparent materials do not render into custom depth at all (this is normal behavior for SceneDepth, but undesirable when explicitly enabling Custom Depth on a mesh with transparency applied) You can download the project source to see exactly how this was done. Note: As of 4.20+ you can render translucent materials for Custom Depth by enabling the option in the material: “Allow Custom Depth Writes”. This removes the need for a second mesh component which is a huge workflow improvement.

For our game Switch, we used this culling technique on a stealth effect giving it a more consistent ‘shell’ and highlight on a mesh that has a lot of overlapping pixels.

It’s important to know that if multiple materials use this technique that only the object closest to the camera is culled correctly. In this image, you can see the errors can would occur if you have multiple meshes overlapping. This should be taken into consideration when considering this technique – it may never be an issue if you use this effect sparingly or in isolated scenarios.

showingoverlappingissue

This issue can possibly be solved by adding a max depth delta when culling. I have had no need to dive into this further but may come back to this in the future when it starts to cause practical issues.

Other Uses

As previously mentioned, for our game Switch we used Custom Depth for a number of features, one of them included an x-ray material that exposes players behind a wall – I added a short video of an early proof of concept below.

Issues & Limitations

  • For translucent materials, you need to enable “Allow Custom Depth Writes” in your material.
  • Because the buffer is filled with all objects using Custom Depth it will not work when having multiple overlapped meshes (one behind the other) depending on how you’re using the depth value – a transparent mesh might be entirely culled if you use custom depth to cull inner triangles. A possible workaround is to add a maximum depth difference when looking for inner triangles to reduce the errors.

References

125 Responses

  1. Comment by Torquemod:

    “You can fix the [sorting] issue with meshes not rendering in the correct order by changing the Translucency Sort Priority on the mesh. This of course doesn’t work if the meshes are moving (Characters) but for meshes in your environment, it works great. You can use both negative and positive values (I set the walls of my level to -1 to make sure they’re drawn last). Think of it as the ZOrder setting for Widgets. The higher numbers will always be drawn on top of lower numbers.”

  2. Hi there!
    Thanks for the great tutorial! I was wondering how you would make this work on mobile, since custom depth isnt supported. Maybe something with custom stencil?
    Any help would be appreciated 🙂

    Kind regards

  3. Hello Tom and thanks for the tutorial!

    A long time somebody asked:

    “Well done Tom, very helpful, learnt a lot. We are currently working on AR projects in which we wish to cull the objects behind a opaque object(a dummy). for example, we put a sphere in the front and a box behind which is partly covered by the sphere. since the sphere is opaque, normally, you will see the covered part of box, what we wish to achieve is to cull the pixels of covered part of box and leave it transparent, so that we can see the background. You‘ve managed to cull the inner pixels in the demo, is it possible to achieve this goal?”

    Basically, having an opaque object masked by a transparent one to show what’s behind the opaque object (the videocapture). This is actually transforming the overlapping issue shown here:
    https://i2.wp.com/www.tomlooman.com/wp-content/uploads/2014/09/showingoverlappingissue.jpg?ssl=1
    into a feature. But how?

  4. In Unreal Engine 4.21.2, I can only get the “Allow Custom Depth Writes” to work when the Opacity of the Translucent material is set to 0.34 or higher. Anything lower will not render my post process effects, even if the effect is as simple as getting the SceneTexture:CustomDepth to render on the emissive color. I’m not sure if this is the intended functionality, or the correct functionality and I would need to do something else to render post processing materials on top of translucent materials in my scene. Would you know?

    • Hey there,

      I’m experiencing the same bug and have been talking with Unreal staff about the problem. It appears this is an issue with our graphics cards specifically, as they can’t reproduce the issue. You wouldn’t happen to be running on a laptop, or a computer that uses a laptop graphics card, like I am? I’m running on an iMac using a GeForce GT 750M.

      • In case this problem still bothers you: in your material settings, there should be a parameter for opacity cap or opacity culling which is set to 0.33 by default I believe, so that’s probably why it caps at .34 for you.

        • Thank you Foodi.

          I was going crazy because I swore I got this working before but couldn’t get it right in 4.22.
          This solved the issue completely.

  5. Hi Tom,

    This is incredibly useful! Thanks for all the information!

    I was wondering if you could clarify one thing. Is Custom Depth, essentially returning the same (or very similar) data as Scene Depth… However, Custom Depth gets sort of “clamped” to the visible depth? Hence… Making the differemce useful?

    I am a little new to UE and sometimes struggling to understand the outputs of certain nodes and the visualize buffer shows colour that ranges from 0-1 which doesn’t quite make sense if that was the output of the node in the shader.

    On another note, the fix is definitelly working in 4.20. I got it working with one mesh only.

    Thanks again!
    Fred

    • Hi Fred, both depth outputs should run on the same values so they are easy to compare. In the material example I do add a tiny delta to one of the depths so you get less jitter errors when they are rendered further from the camera (as distant objects have less floating point precision basically)

    • At this time I believe the translucent materials DO have a checkbox to allow for custom depth writes but when I last checked this in 4.19 or so it was bugged and haven’t tested it since. That would be the way to get this to work for ragdolls as then the single mesh can both be translucent and write into the custom depth buffer.

  6. Hi Tom, great tutorial, thank you for this. I have a mobile game project using 4.14 when custom depth was first enabled for use on mobile devices. I managed to setup everything and it works beautifully in editor and in mobile preview. But once I port it to an iOS device, it’s not working. I wanted to just post here and ask just in case, but is there anything you can think of that might be causing it to not work on device?

  7. Hi Tom, thanks for an excellent tutorial. This all works great in my project but I have ran into a weird issue that I just can’t figure out.

    Whenever I have a delay of any kind (including timelines and level sequences) to trigger an animation on my translucent ghost character that uses custom depth, the material gets random holes. It looks like the culling gets messed up. However, when I trigger the same animation on the same character interactively or immediately on beginplay, this issue does not occur.

    Any suggestions on what this could be would be much appreciated.

    • So I got it working again. I have my custom depth mesh as a child to the main one and that apparently let to a sync problem whenever I use delays. Weird. Feel free to delete these questions/comments and thanks again for the awesome tutorial.

  8. Hello Tom,

    I know that custom depth can give you an outline or that sort of “x-ray effect” from the Arkham games…but is there a way to just be able to render the entire character on a separate layer and preserving its visuals – instead of making it an outline or filling it in with 1 color…

    For instance if you want to render projectiles in a 2.5D side-scroller so that it doesn’t clip through characters? Or if you want to have 2 characters one on top of the other – but without clipping – and it looks like one is in the fore-ground and one is in the background? like Donkey Kong Country Returns?

    – Arty

    • Deferred rendering (used by UE) doesn’t allow for a lot of per-mesh custom rendering options unfortunately. You could look into Render Targets or Depth Bias as possible options to push geometry forward and to render objects into a separate buffer to then draw it on top. (note that a Render Target is pretty expensive when it comes to Deferred Rendering)

        • They do support forward rendering, but I can’t recommend it as it’s still limited last I seen it. I do believe they are using that renderer now for Fortnite on certain platforms which will mature the code a little bit more.

  9. I have an interesting question:
    Imagine that you are doing a co-op game, but you want that outline only to be shown to the owner, but never to others, like an interactive object, if server is close the outline appears, but noone else see the outline, unless they are also close. And the opposite as well, client is close, it sees, server is far away, it doesn´t sees. Only visible for the owner…

    • You can simply run this logic only on the Local player controller and ignore the logic checks for others. Since they all run Tick() you can quite easily just set the “render custom depth” for an object for just the character that is close AND local and set it to false for all others.

      – Tom

  10. Thank you so much for your tutorials and C++ course!
    I’m working on a map of the solar system, where every planet has CustomDepth activated and should be outlined. Is it possible to hide the outlines on parts of a planet occluded by another planet?

    • Tricky, the thing is that it may be hard to tell which stencil mask is in front of the other. As custom depth itself only returns 1 value and doesn’t know which stencil value it belonged too. Meaning you may not have all the information to occlude them. You’ll have to play with the stencil values and maybe even change them on the fly based on distance to camera so you can assign higher stencil indices to objects with planet pivots farther away and use that to use occlusion with stencil masks instead of relying on custom depth.

      • Hmm, changing stencils on the fly… I didn’t know I can do it. Nice idea! I’ll give it a try. Thanks a lot!

    • Good question, you could try to make a post-process that renders it that way when sequencer runs? By this I mean to use the depth mask as the alpha in the post process material instead of whatever regular alpha value is used (I do recall that alpha can be rendered into RenderTargets, but I am not sure if Sequencer specifically let’s you render with Alpha…

      Good luck!

      Tom

  11. Hi Tom, great stuff. One question though – I’ve been playing around with custom depth but still can’t find the way to render one object/actor on top of others without any overlays, grids, transparecies, just normal, non ocluded fully lit object. Would this require additional pass for this occluded/wannabe in front object? Am I missing something obvious here? Best regards, Milosz.

  12. Hello,

    First of all nice work and nice blog. I have a special question using the CustomDepth to create a portal effect. But i’m blocking and i don’t want to use this ScenecaptureComponent2D. I was thinking about use the customdepth to render and hide it … But no real solution …
    If you have any idea ? 🙂
    Sorry if i made some mistakes.

    Best reagards,
    Marien

  13. Hi Tom

    I was wondering if you’d be able to quickly help me please – I want to outline parts of an object you can interact with, where I have a skeletal mesh for the object and each part as a static mesh that has the main render pass disabled; however, the default multi-colour outline you have provided (which is awesome, thanks so much for making it available) only outlines the back edges, meaning most of the interactable parts edges are not outlined making it almost non-existent.
    I noticed in the video for your Switch project that it displays outlines for all edges (both front and back) on your boxes so I’m hoping it is something you already have setup that wouldn’t be difficult or any effort to help me with.

    Thanks in advance.

    • For the Switch rendering I used a custom effect that isn’t using outlining. It sounds like you may need to use Fresnel or some texture overlays to make a nice outline.

      Or perhaps adjust the outline shader to check the different stencil indices to outline each stencil index individually. Haven’t looked into my outline effect (from the other blog) for quite some time, but I believe overlapping stencils do loose the lines like you mentioned (hence why you’d need to make your own, that treats each index completely individually and does not use Custom Depth for any of its checks, but only the indices)

      Hope that makes sense.

      – Tom

      • Hi Tom

        Thank you very much for getting back to me.

        I’ve just spent some time trying out Fresnel – it seems to basically give a similar result to the outline effect by default but it looks like I might be able to setup a normal map to achieve similar results so thanks for that suggestion.

        I’ll take a look at modifying the outline shader in the way you mentioned as that would allow for easier customisation and activation of effects but with no knowledge of shaders I don’t know if I’ll be able to get that working, unfortunately.
        I will try to read up on stencils but there don’t see to be any UE4 docs on it. If you know of any good resources I could read up on and/or If you are able to spare the time to detail a little more about how I would determine which individual stencil indices to use, that would help a lot.

        Thanks again.

        Tim

  14. Hi, Tom

    Thanks for your tutorial, which helped me much. But I still want to ask you some questions if you don’t mind~

    I am trying to develop sth by UnrealAR plugin, I want to achieve the occlusion between real objects and virtual objects. To do so, I need to get the meshes of real objects (use 3D reconstruction or sth else, not the point), render them into depth buffer, which should not be shaded in fact. Then render the virtual objects, check the depth buffer and determine which parts of them are occluded, discard these parts.

    For StaticMesh of real objects, I enabled “Render CustomDepth Pass” and disabled “Render in Main Pass”. In material of virtual objects, I checked the “SceneTexture:CustomDepth” and compared it with “Pixel Depth” to determine occlusion. But this method just worked for translucent material, I could not set the material of virtual objects to “opaque” due to the error “SceneTexture expressions cannot be used in opaque materials.”

    Is it possible to achieve such effect? Or can there be other ways to help me?

    Thanks for your help 🙂

    • Interesting problem. You might not have enough access for this (you’d want to not render in main pass, but still have that object render into the depth buffer for example)

      Perhaps it’s useful to try the Forward rendering of Unreal? It could help with some more control over per occlussion (although I have not played with that yet)

        • Hi Garden Lee,
          I’ve just came upon the same problem like you. Seems, that you have already solved it – would you be so kind and share some more information on how you’ve achieved that effect?
          That would be great!

          Thanks,
          Sergio

  15. Would there be a way to get the outline black? I’ve been playing around, but it doesn’t seem to be able to go fully black.

    • You can make them black. You can lerp between the original color (postprocessing0) and black with a Lerp node where Alpha is determined by the value whether to draw the outline or draw the scene (see any of my downloads on outline FX for examples)

      You may need to double-check that your material is in the correct render order (eg. before or after tonemapping) which you can try out in the material’s details panel.

  16. Hey Tom!
    Great asset! Question:
    Is there a way to turn it on/off dynamically in-game?
    So, I’m using a ray cast. When it hits a physics object, it would be outline. Then, when the ray isn’t hitting the obj, the outline would be turned off.

    As it stands now, I am able to turn the outline on, but not off.
    (I’m using “Set Render Custom Depth”)

    Any recommendations?

  17. Hi Tom

    Great tutorial. I really liked it.

    Is it normal that objects set to use custom depth render show through all other objects even if they are being occluded. I dont want the effect being shown if the actor is behind a wall or another object.
    Thanks

    • That depends on how the effect is set up. You can check for occlusion by comparing the Custom Depth with the Scene Depth (Some of that can already be seen in the existing material effect nodes) and only show the outline if the Custom Depth is CLOSER to the camera than the Scene Depth value.

      Hope that helps.

      – Tom

  18. Hey Tom, I have a question.

    How can I fade out the outline when it starts getting close to the middle of the object?

    Also how can i make it so that the outline hides behind objects? Like if the outline was just there to tell a player that there is an item that they can pick up, but you only want them to see it if it is not obstructed by a wall or something.

  19. Hey,

    I am trying to do the example of the sphere going through the object and having the different colour drawn over it. I have no idea how you managed to do this though, would you mind explaining how? 🙂

    I can get the outline material to work, but I haven’t managed to get any further than that.

    Thanks for your help

    • Ignore the last message, I thought I had to recreate the material and disconnected your stuff. It works now I connected your things in again 🙂

      The game looks awesome, keep up the great work

  20. Hi Tom,

    Is it possible to control which objects it cannot be seen through? For example I want the player to not be seen through when behind an enemy actor.

  21. Hi, again.

    I have download and test it your Rendering Object Outlines tutorial and I need to make a change on the material. The outline is fine. It’s what I need, but I don’t want to draw the occluded geometry. How can I modify it to don’t draw the translucent overlay when the object is occluded?

  22. Thanks. I’m not sure if I’m making it more difficult that it is. I want to do this: I have a set of vertices. These vertices are coordinates for points that are inside a box. I want to draw a line from one point to another one to draw a polygon. Someone has suggested me to use a procedural mesh. I use it and I have a problem: I only need the perimeter or, in other words, I only want to show its edge. This is why I want a transparent material. Do you have any idea about how to get what I need?

  23. Hi.

    Thanks for sharing your knowledge with us.

    I’m very new in Unreal Engine development and I want to draw a transparent procedural mesh with an edge (I want to draw its perimeter). I have tried with your Rendering Object Outlines and it doesn’t work.

    How can I use your Rendering Object Outlines to draw a transparent Procedural Mesh?

    • Transparent meshes can’t draw into the Custom Depth buffer, as a workaround you will need to create a copy of that proc mesh with a simple opaque material on it (And set “Render Main Pass” to false, and “Render Custom Depth” to true) this copy is purely to fill the Custom Depth and will then not be seen in your main scene.

      – Tom

  24. hey tom, Im trying to figure out a way to make certain objects not occlude, so if he s behind a certain wall he doesnt get the the effect, or in my case if his feet fall below the the ground or in tall grass. I cant figure out how you d tag objects or as what to not affect the custom depth, or alternativly how to affect the mask of the character, ie if the silloute faded out towards the characters feet.

    Any thoughts?

      • Hi Tom, I checked out the other blog as well. I got the multi colored outlines working but still can’t seem to figure out how to make a material/mesh block/filter only specific stencils.

        I tried making another post process material to identify and filter out specific stencils as well as making a surface and decal material.

        It seems like it should be obvious or intuitive since it is simply shown being down in the multi color outline tutorial but I must be missing something.

        I think you referred to it as a decal xray? Basically what I want to do is the reverse. Create a material that will block all of a certain stencil index but nothing else.

        Thanks!

        • Hi Justin,

          My multi-color outline FX does a few checks on specific stencils (like you mentioned), you could compare stencil and If-node or Lerp-node between original colors of the post process and whatever mask-ing color you want to apply if the stencil index matches (or reserve the inputs to get the opposite effect)

          Are you making this effect specifically for a Decal material or something else entirely?

          I wonder what you might be missing here to be honest. Did you verify using the Show flags that the stencils are drawn OK via View Mode in the viewport and check “Custom Stencil” (listed under Buffer Visualization). You may need to post some shots of what material you’re trying to build.

          – Tom

  25. I need to disable depth sorting for an actor with masked or opaque material (so it draws over the scene) and UE4 doesn’t seem to provide such option (only translucent materials have such option). Is it possible to achieve with custom depth pass and if so, would it be performance taxing on mobile ?

    How would I setup material / scene to achieve such effect ?

    Thanks

  26. Hey Tom, thank you so much for this, I’m completely new to all this post process texture stuff and this was really helpful. I got your texture working fine except when I try and apply it to a TextRenderActor, it seems to only show through other objects when looking at a specific angle, if I look straight on it wont show through. Just wondering if I need to do anything extra for a flat piece of geometry like the TextRenderActor.

    Thank you in advance!

    • That is a little surprising. I wouldn’t think it would. Do you have a screenshot to show your issue? I’ve never tried this on a text actor before.

  27. Well done Tom, very helpful, learnt a lot. We are currently working on AR projects in which we wish to cull the objects behind a opaque object(a dummy). for example, we put a sphere in the front and a box behind which is partly covered by the sphere. since the sphere is opaque, normally, you will see the covered part of box, what we wish to achieve is to cull the pixels of covered part of box and leave it transparent, so that we can see the background. You ‘ve managed to cull the inner pixels in the demo, is it possible to achieve this goal?

  28. Hi, I recently used your material and setup, how efficient !
    However I’m facing some issues when packaging for html use. It says “LogMaterial:Warning: Failed to compile Material /Game/StarterContent/Materials/Highlight/PP_Highlight.PP_Highlight for platform GLSL_ES2_WEBGL, Default Material will be used in game.”
    Any idea ?
    Thanks a lot !

    • Sounds like certain features/nodes are not available on that platform. Perhaps the Custom Depth feature is missing from your target device (HTML5) you are packaging for.

      • Actually I export to html using a scalable 3D or 2D for desktop/computer setting and open the file in localhost through Chrome. Perhaps is there the origin of the problem. I’ve tried with “maximum quality” setting too, but it fails compiling the material too.

  29. Thank you for this great tutorial. The part about “Custom-Depth for culling inner triangles.” is exactly what I was looking for. Unfortunately it doesn’t work.

    – I’m running Unreal 4.10 and created a new Project.
    – I imported a 3Ds Max Teapot and used the network you linked above and just pasted it
    – Project Settings are set to “Custom Depth-Stencil Pass: Enabled”
    – Teapot-Mesh-Settings are set to “[X] CustomDepth Stencil” & the mesh is static

    Here’s my Material-Setup: http://i.imgur.com/sv6yg9x.png

    It’s exactly what you did but I send a grey value in addition into the baseColor/opaque slot. But as you see, the teapot doesn’t look like it should. Instead a lot of the tris are visible “within” the shape of the teapot 🙁

    Do you have any idea what I’m doing wrong?

    • Hi Simon,

      There is an unfortunate hack you need to do for this to work.

      You’ll need a second equal mesh with a simple opaque material applied. For this mesh uncheck “Render Main Pass” under the render properties near the “Render Custom Depth” option. The renderer can’t draw translucent meshes into custom depth unfortunately, I hope this can be fixed in the future.

      Hope that helps,

      Tom

  30. Thanks you very much for the tutorial. Is it possible to render occluded object with its original material instead of outline/coloured mask? It would be really helpful in my project.

      • Sorry if I didn’t explained it correctly – I’ve made a picture, maybe it’s the best way to explain graphical things 🙂 This is what I would like to achieve: http://oi68.tinypic.com/33kf236.jpg
        I’ve decreased Alpha to 0 on this screenshot. But the texture of the occluded object still doesn’t show through this green wall.

        • Update, to give a better idea of what I mean: I’ve discovered that this is achieveable by applying a translucent material with “Disable Depth Test” and it does exactly what I need, but unfortunately my objects have opaque material.

          • That’s not possible with a post process effect. And from what I can see you can’t easily add a depth offset to materials, closest I got was with the translucent material on Alpha 1 – but of course lighting won’t look nearly as good.

            – Tom

  31. Thank you for showing this. At about 20 seconds into the video above (https://youtu.be/TKqL-gsfTWc?t=20), there’s a figure with a really cool effect. I didn’t find any information about it in this page, do you mind explaining how you achived that effect. And by the way, I saw some videos of Switch, and it looks incredible.

    • Hi Rafael,

      Thanks! The effect in the video was a mockup effect for something that could end up as a cloak for Switch. It’s basically a cloud texture of dots with a heavy BumpOffset applied with some Fresnel to make it look as odd as it does. It looks great on spherical objects.

      – Tom

  32. Big thanks for this! Really great tutorial.

    I would like to know if it’s possible to somehow also make this effect somehow glow?
    Again big thanks for this!

    • Try multiplying the outline color output with a value of 2 or more. Anything above 1.0 in materials will output as bloom/emissive.

      – Tom

  33. Thank you for this — very useful!
    I’m wondering if you know of a way to enable the Custom Depth rendering for a single instance of an Instanced Static Mesh component (rather than for all instances). Or maybe this is counter to the idea of an ISM?
    For context, I’d like to “focus” (like you have set up in your “UsableActor” tutorial) on a single instance in an ISM component.

  34. Hey there Tom,

    First of all thanks for sharing your work with us. Very helpful to many!
    I have been playing around with this, as well as with other versions I have found on the internet, but I haven’t been able to get it like you have it in the gif, where only the part of the mesh that is hidden behind another mesh is being rendered as a silhouette. The way it behaves for me is that it always has the outlining/silhouette, even if the mesh is not occluded. Could you help me out with this? Would make a lot of students very happy.

    • Hi Jos,

      Sounds like the material is missing a small depth delta to filter out himself from the occlusion check. Meaning that if the depth difference between CustomDepth and SceneDepth is very small, it should be ignored because we are probably occluding ourselves.

      If the download link in this post doesn’t give you the expected results, then have a look at the tutorial source:

      https://github.com/tomlooman/ue4-tutorials

      Cheers,

      Tom

  35. Hey Tom, thanks so far. It really looks like that what I was searching for… but sorry for the noob question. You lost me in the Material editor step. What are the basic settings for the material? Could you maybe zoom out? I have no clue what I need to set in “Details” and what slots do you mean? I have the default “Opacity” slot… do you mean that? It’s not active by default. Furthermore, how can I create these params? Thanks in advance, Martin

    • Hi Martin,

      To enable the “Opacity” pin you need to select that giant node with all the inputs like Base Color, Emissive etc. and set your Blend Mode to Translucent.

      – Tom

      • Yeah, I’ve got this giant node. (And I found these params by the way) I switched the Material to “Translucent” and plugged it into the “Opacity” Slot… Which is on the right side. But where does this “cable” from the left side coming from?

        • Whatever your base opacity was before applying that logic. Could be simply 1.0 or any other special opacity nodes you have set up to determine opacity, for example a texture sample.

  36. Thanks for your time and dedication Tom. I just started with Unreal Engine and I am still learning and trying to understand what others do and later replicate them and touch things to see how they interact.

    In this case I can’t even replicate the entire effect. I followed your steps but I am having some problems with the final step:

    I used a “post process volume” in scenario and set “Render Custom depth” in my player mesh and capsule to True.

    My problem is that you said “fill buffer and with it we can perform a simple depth comparison by sampling the neighborung pixels in our post process” but dunno where must do those things. I am totally sure that I am missing something, but can’t imagine what (I guess must be something very basic).

    Could you explain that last part for very novice users? I really will appreciate it.

    • Hello again. I managed to solve it. And Like I thought, was a simple problem. I thought that the “Post Process Volume musted be for every object, but it is just for entire map. I just musted scale up it.

      Thanks for your tutorial and blueprints! Now Ill try modificy it a bit for find a way to do what I want. Very, very appreciated!

  37. Hey Tom, great job! Unfortunately my main character is pretty small and I tried everything to increase bounds size to make outlines visible. When I change my physics or character bounds it decreases shadow size. Do You know how can I fix outlines without changing mesh bounds scale (shadow size)?

  38. A bit late considering you made this post last year, but I was wondering if custom depth can also be used on a per object basis. With this I mean is whether you can have the effects apply to the object (for instance, when you have sub objects and you always want this object to render over it) without having it affect the environment (using the last example; it would render over all it’s subobjects, but would not have any effect on any objects not part of it).

    • This buffer works on your entire scene, so you don’t have the option to simply filter out by subobject. But check out “CustomTagBuffers” at the bottom of the article to make that happen, note that it requires a custom engine build.

  39. Hi Tom!

    Can you make tutorial How to prevent weapons from penetrating the wall?
    How to implement it with custom depth or with custom C++ trick?

    • Hi!

      That’s a good feature! I will await the forward rendering support of Unreal Engine 4 however. That will allow us to do more rendering tricks on specific objects like the weapon so we can let it ignore depth and even render it on a different FOV to make it look better in first person. I don’t know what their timetable is on forward rendering, but it’s being worked on.

      – Tom

  40. Hey Tom, great article!
    I had a question, though. I’m not sure if my question relates to this topic but I guess that’s why I need to ask!
    So I watched the UE4 tutorial for “Location Based Opacity” in which a sphere actor was used to drive opacity on one material via a material parameter collection. Well I needed a way for multiple actors to drive that value. In other words, I am trying to figure out a way to have several actors drive opacity on one material. For example, in an RTS, when your friendly units get close to an enemy, the enemy is revealed. Each friendly unit drives the “Location” parameter for the opacity sphere. Well the problem is I can only get one actor’s location at a time.
    So after trying to figure it out I read about custom depth being able to do something similar. When a certain actor gets close to a hidden object, that object starts to be rendered based on proximity.
    I guess my questions are as follows: Is this possible? If so, how would I implement it? If not, is there any way for my first method to work?

    Thank you!

    • Hi Joseph,

      Custom Depth could be used here potentially. It generates another depth buffer to test your own pixels against. It would only work for translucent objects (not masked, since the depth data is only available AFTER masked materials have already been rendered) but it can be done. What you can do is draw the spheres into the buffer and when you are checking for opacity on your material you can compare custom depth vs. scene depth to see if they collide within a threshold. The example download should give you some helpers on how to compare these values. Also be sure to use the debug visualizer in the viewport to verify your custom depth result if what you expect before going into the material coding itself.

      – Tom

    • Hi Raikoh,

      SceneCapture2D isn’t part of the tutorial on this page. I think question is best asked on the forums or answerhub of Unrealengine.com

      – Tom

  41. Awesome tutorial, thanx Tom,

    I thought about using a dynamic material for the blendable and then use a blueprint to switch the colour of the material before activating the render custom depth to mesh. this should let you swith to different colours and other perimeters between targets etc but I’m having trouble setting the blendable material as a instanced dynamic material at begin play. when getting the post processing volume in the level blueprint I cant seam to find the function to set the blendable material.

    any ideas?

    • Hi Dean,

      Never set this through the level blueprint, but it may not be a Function, the Blendables is an array variable, so you may just need to get that array variable and call “Add” on that to attach your dynamic material.

      Hope that helps.

      Tom

  42. Hi Tom, Thank you for your reply and your interest.

    I tried on the forums, no luck.

    I am about to give up with this problem. It seems impossible to determine if a point is under the effect of a light in the material shader, mainly due to the fact that UE4 uses deferred lighting, hence it applies lighting once all the materials have been applied. Custom lighting, which was appliable in UDKand could be another approach, is also not longer present.

    I was even thinking in the possibility of using a layered material which, in one layer is fully white under any exposure to light and black in the shadow by deactivating GI. If I could get such a material, I could blend it multiplying by the texture map of the object and applying the result to the emmisive input.

    As I told you before, getting this “Fuly white diffuse reflection under any amount of light independently of the incidence angle” was possible to be done in UDK using the custom lighting input node of the material. Now I don’t have any idea of how to implement it.

    Thank you again.

    Luis

    • UDK definitely had a lot more lighting freedom, especially with multiple lighting channels which I’m missing my own self. I don’t expect a lot of forward rendering features if I read through comments on that subject (on their forums) except for lit transparency. Good luck with the project! I hope you find something, if I come up with something I’ll be sure to let you know.

      – Tom

  43. Hi Tom.

    Thank you for your response. Your approach with the RenderTarget seems too complex for me 🙁 . The solution would be easy if there were a way to determine in the material graph if a pixel is hit by a light or not, something that is needed in any case for the render engine to obtain the lighting values to that pixel. Do you know if it is possible to obtain this ?

    Thank you anyway for your valuable help. Your tutorials are great!

    • Hi Luis,

      The technique I explained is pretty much the same thing as lighting/shadow-mapping. I believe people asked on the forums whether it was possible to retrieve this information for stealth games (I believe the answer was no at the time) Since this is digging pretty deep into the rendering system, I don’t know if this is available without engine modification. I’d try my luck on the rendering forums: https://forums.unrealengine.com/forumdisplay.php?33-Rendering (or AnswerHub) to see if this is technically possible.

      – Tom

  44. Hi Tom

    Thank you for your tutorial. It is awsome what you can do with custom depth. I wonder if it could be appliable to the problem that I have now.

    I am trying to make a visibility check application for environmental impact. I am pursuing a way to indentify the areas that are visible from a given point (P) by tinting the materials of the objects present in the scene based on their visibility from that point. I would like that P could be moved to analize the scene interactively

    A possible solution could be to place a light in P and consider the visibility of areas of the scene based on its shadow, but I can’t find a way to check in the material editor whether the point in the surface of an object receives the effect of a given light or ir falls into its shadow.

    Resuming, what I need is to tint the objects (or parts of them, such in a building façade or the terrain itself, that can be partially visible and partially invisible) with different colours depending on their visibility from point P. Hence I need to set up materials for the objects that take that tint based on their visibility on a per pixel shader basis.

    Custom depth could be a solution if I could extract it from a camera located in P instead of the player’s camera. I do not know if it is possible.

    What do you think? I would appreciate very much your expert oppinion.

    Thank you very much in advance.

    Luis

    • Hi Luis,

      Interesting problem 🙂 What comes to mind is similar to cubemapping, in that you render from P in 6 directions like a cubemap and store this data in a RenderTarget(Cube) and would contain SceneDepth from point P. Now in your material you could sample this rendertarget data and reconstruct the depth value into a 3D position (not trivial, but all the info is available like P location, viewmatrix and the depth value of the pixel- this is similar to how shadows are constructed) with this info you can see if the depth matches the pixel you are currently shading (in your player camera) In this case you wouldn’t be using CustomDepth at all.

      Now this is just an initial thought, I don’t know if UE4 has all the support built in to do this exactly. I hope it gives you a starting point to build on.

      Good luck!

      – Tom

  45. Hi Tom,

    I have been playing with custom depth also for a good while but I stumbled always on the same issue, when enabling custom depth for the skel meshes of the template projects (the side scroller for example) it just does not work. Is there extra setup apart from enabling the components flag?

    Cheers,
    Moss

    • It should be enabled by default in a project, perhaps it is disabled for the demo project. Have a look at:

      Edit -> Project Settings -> Engine -> Rendering -> Custom Depth

      Have you tried static meshes and used the Buffer Visualization as shown in the article to see if you get any visual white when moving away from the object?

    • I haven’t figured out how to do this yet. We only have one buffer available and to my knowledge it’s impossible to clear this mid-frame. Definitely something I would like to have working though! I think having multiple custom depth buffers won’t happen any time soon (unless there is a good number of people interesting in this feature)

Leave a comment on this post!