Tuesday, December 12, 2023

See through walls in Unreal Engine

See through walls in Unreal Engine

Introduction

This post is a brief tutorial on how to add an X-ray vision to your Unreal Engine project. So how do you get that super-power? If I have to put it in one sentence it would be "Use a Custom Depth Buffer during the Post Processing step".

Blue spheres turn pink when occluded, thanks to the power of PostPropcessing.

I recommend you check out this great video on the topic. Although things have changed a little bit since then, fundamentally, the technique stays unchanged. But in case you prefer a written word to a spoken one and want to gain additional insight into its workings continue reading and you will have it up and running in no time!


Tutorial

To implement the X-ray vision in your project just follow these simple steps:

  1. Create a new Post Process material by setting the corresponding Material domain in its Details panel:


  2. Fill in the following material blueprint:


  3. Place a PostProcessVolume into the scene. Add a Post Process Materials item to the volume and assign it with our new material. You can also check the Infinite Extent (Unbound) property unless you intend to render this material only if the camera is located inside the PostProcessVolume.
  4. Check the Render CustomDepth Pass property for every mesh you want to see through other actors.

And that is all! It was easy, wasn't it?

A window into the Material's inner workings

Now that we have our X-ray vision functioning, there is one more thing I wanted to share with you. It is a little bit of insight into how the material is actually building this post-process effect. 

We can quickly gain lots of intuition of its inner workings by separating the material on its main parts and connecting them independently onto the material output node to get a visualization of the material's processing. Let's try dissecting our material and studying it! 

Well, I actually already did exactly that and you can check all the intermediate material outputs on the following images. Instead of a thousand words of explanation, I propose readers take a minute and study these images. I'm sure that figuring out how the final image is constructed by combining the inputs on your own will help you gain a better understanding of the material.


Main X-ray material's parts: 1 - range restricting mask, 2 - unoccluded parts of Custom Depth meshes, 3 - occluded parts as the product of (1) and (2), 4 - blending the scene's render without Post-process effects with the X-ray color masked by regions of occlusion.

Conclusion

This is a very simple yet extremely useful technique. If you would like to make your X-ray even more powerful, I advise you to also learn about the Custom Stencil Buffers. With their help, for example, you can easily add configurable colors to different types of actors in your scene.