Shader Question

Can those objects be in full 3d motion, or are they on separate distance “layers” from camera?
With separate layers you probably could do material that uses scene depth and some multipply with add combination for pixel color.
In situation when you cannot use distance from camera to determine their shapes (pixels) i have no idea how to get more than 2 to correctly overlap.
Thinking of it even layered distance works for 2 only. This is not trivial thing. You see if you overlap something, there is no information about pixels below it.

I am not sure but there should be pixel XOR (NOT OR) operation (is what your boxes do).
And there is topic with scene outline shader, it is kind of similar toy your problem, if i find that post i add link here.

Here is that link, maybe this will help: