Custom Depth in Unreal Engine 4

Unreal Engine 4 introduced a new depth buffer along with their PBR rendering system. It’s called “Custom Depth” and can be used for effects like the selection outline that is built-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 the techniques described for your own effect that utilize Custom Depth it’s important to read through the Issues & Limitations at the bottom of the page to prevent you hours of headache and unnecessary debugging.

ue4_builtin_selectionoutline

Custom Depth 101

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

ue4_customdepth_properties.

To view this particular buffer go to Buffer Visualization in the viewport-options.

ue4_customdepth_visualisationdropdown

Note that objects close to the camera are near-black and may be difficult to see!

ue4_customdepthbuffer

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 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.

Outline Post Process [Download]

Post Process material for outlines with translucent veil on occluded pixels.

After copying the .uasset files into the Content folder of your project you must add a Post Process Volume into your scene and enable the Unbound property, also don’t forget to add the post effect material under Blendables.

ue4_postvolume_properties

Culling Inner Triangles

When using transparency on models like character that have inner triangles (eyes, mouth, arm or bit of armor sticking through) you will see a noticable highlight in your material that attracts attention and simply doesnt 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 outter shell of the mesh. A slight depth offset should be added when comparing 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’s 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.

ue4_screenshotsaturday_hologram_small

For Switch we use 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 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 consistent 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

For our game Switch we are using Custom Depth for a number of features, one of them includes an x-ray material that exposes players behind a wall – I added a short video of an early proof of concept below.

Multi-Color Outline Effect

A few engine versions ago Stencil Index buffer was added alongside Custom Depth. This enables us to create multi-colored outline effects among many other cool effects! Click here to read about this new effect, included with a download link for the material.

ue4_coloredoutlines01_small

Issues & Limitations

  • Custom Depth does not work on translucent materials. In that case you need a second copy of the mesh using a simple opaque material with Custom Depth enabled and Render Main pass disabled. View on AnswerHub
  • 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

91 Comments

  1. Really cool info. Thanks 😀

    Is it possible to have two objects with outline of different colors at the same time?

    • 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)

  2. 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?

  3. 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

  4. 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

  5. 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

  6. 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

    • 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

  7. 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

  8. 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

  9. 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.

  10. 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)?

  11. 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!

  12. 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.

  13. 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

  14. 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.

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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.

  20. 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?

  21. 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.

  22. 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

    • I’m afraid I can’t be of much help for a mobile solution. Custom Depth isn’t available on Mobile. Perhaps offsetting the pixels can help you, but a quick test reveals that Pixel Depth Offset only works for pushing pixels further away unfortunately.

      – Tom

    • Did another test for a possible solution / direction on this effect.

      Above are the nodes, this should give you something to play with!

      – Tom

  23. 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

  24. 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

  25. 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?

  26. 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?

  27. 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.

  28. 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

  29. 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.

  30. 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

  31. 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?

  32. 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.

  33. 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)

  34. 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

Comments are closed.