For a diorama-like scene I want to make everything outside of a certain distance from the center transparent (or the color of the background). I have achieved this using the Distance Node and the SmoothStep node. However, when I rotate the camera behind an object, the object behind this object is still visible. See image.

Floor object still visible through the invisible box.

Current node setup (Vector3’s in bottom left are 1,0,1 and 0,0,0)
Before using transparency to do this, I achieved the result I’m looking for by setting the pixels outside of the radius to the color of the background. But once I implemented a dynamic/procedural gradient skybox, I couldn’t do that anymore because the background isn’t a single color anymore.
How can I hide the floor when looking through the transparent part of the box? Alternatively a way of getting the right background color would also be fine.
Added a picture of what I try to achieve.

Desired result. Copyrights to Lockpickle Games
On top of that, there is also some weird clipping going on when looking at the intersection of the box and the floor from some angles. I suspect this has something to do with the transparency rendering after the opaque parts.