With some of the recent changes to Unreal Engine 4, rendering multi-color outlines is now possible! This is done through the use of Custom Stencil, a new buffer similar to Custom Depth – but allowing meshes to render as integer values. This provides us with a 1-255 range of indices that can be used to apply different outline colors to meshes and even combine multiple features such as the decal x-ray shown below by using a different stencil index.
The Original Custom Depth
The original outline material I made last year was based on Custom Depth, before Custom Stencil was available. This meant there was no way to determine the mesh type after it was drawn into the buffer as single channel depth value. More info on the effect and other uses for Custom Depth are available in my of my earlier posts. The original effect uses fewer instructions (97 vs. 144 of the new material) so if you don’t require multiple colors in your game you can consider sticking to the old effect.
The new effect is still using Custom Depth to determine the (optional) occlusion which adds the faint overlayed color adjusted by tweaking the FillAlpha parameter in the post process. This occlusion can be turned off in the material by unchecking FillOcclusion in the material instance.
Above: the Custom Depth visualizer.
Enabling Custom Stencil
Custom Stencil is disabled by default, to enable go to Window > Project Settings > Rendering > Post Process > Custom Depth-stencil Pass and set it to Enabled with Stencil.
Some of the meshes are not visible in the Custom Stencil visualizer in this example, their Stencil value is set to 0 (default), excluding them from this buffer.
To enable this visualizer go to your viewport, look for Lit > Buffer Visualizer > Custom Stencil.
You can enable Custom Depth and change the Stencil index through the editor menu of a mesh under the Rendering category.
If you’re using C++ you can define the stencil indices in your game header (ex. SurvivalGame.h) for your convenience and bookkeeping.
/* Stencil index mapping to PP_OutlineColored */ #define STENCIL_FRIENDLY_OUTLINE 252; #define STENCIL_NEUTRAL_OUTLINE 253; #define STENCIL_ENEMY_OUTLINE 254; #define STENCIL_ITEMHIGHLIGHT 255;
Enabling custom depth and setting the index in C++ is pretty straightforward.
GetMesh()->SetRenderCustomDepth(true); GetMesh()->CustomDepthStencilValue = STENCIL_ENEMY_OUTLINE; // or simply assign an int32 within 1-255 range.
Setting up the Post Process
To enable the outline you need to place a Post Process Volume. Make sure you set it to Unbound so it’s available regardless whether camera is inside the volume or not. With the post process volume selected, go to Settings > Blendables and add the PPI_OutlineColored as the first entry.
Outline post process for Unreal Engine 4.9 and above. Allows (4) different color outlines (up to 255 colors theoritically supported)