Outline shader with line getting thinner if further away

Hey guys,

I’m currently working on a post-process material that also includes an outline.This part of the shader is drawing the outline:

Unfortunately, I made this after a tutorial, and to be honest I don’t completely understand how it works. So that’s also part of the issue.

The thing with this shader the way it is, is that it’s drawing the outline always with the same size, no matter how far the objects are away. Which looks pretty ■■■■■■ for more distant objects, or also when things are in the background like the sky sphere here.

So I want it to get thinner and fade out with distance.

I first thought I could achieve this by copying this whole segment, changing the outline thickness to something very thin and then lerping both together with a distance fade, sphere mask, or the scene depth itself. But the issue with that is, that the outline is drawn on the outside of the object.

So let’s say I use the image below as an alpha to lerp the outlines together, the mask is actually portraying the distance of the object behind, instead of the distance of the object I want to define the outline thickness for.

which then leads to a result like this:

which is obviously not what I want.

Does anyone have an idea how to solve this?
If I can somehow move the outline to the inside of the object this would also be fine for me.

Can you share the tutorial?. And what do you think about using a camera vector to adjust the line width as per the distance?

Hey, thanks for the answer. I’m having trouble finding the tutorial again, but it was a written tutorial, and the guy who wrote it said it was integrated into unreal that way in earlier versions. Basically the outline part was exactly as in the screenshot just that he was using custom depth or custom stencil instead of scene depth.

Could you explain how to use camera vector or send a good tutorial? how does it work differently from sphere mask or using the scene depth?

In the alpha of your thickness control you can put a scene depth to control the thickness according to the distance from camera. Depending on what you are doing before or not, you have to mask the scene depth. Put the Scenedepth directly to post process emission to see it, then control the parameters for your specific scene.
In this case there is a log there to improve the separation between planes (if the distance is too big) and a invlerp to make it thinner in the right direction. The worldposition+texture makes the line not so straight.