Help with Outline material.

I am making a generic outline material, so it works on any mesh. Essentially I am trying to make a Sobel-esque outline, but I need help with what to do when the outlined object is behind another object.

Currently it does outline the mesh correctly, as it is using the CustomStencil buffer to allow for multiple colored outlines.
image

But I want it to outline where it intersects the floor, something like this.
image

Imagine that, but the same color all around, is what I am after.

Any guidance or links to tutorials with outlines like this would be greatly appreciated. Thanks.

You might try combining what you have with either a depth fade node, or mesh distance fields.

I also saw