Creating a 3D mask in URP

I want to make a material for an object where any object with the material reveals a hidden layer.

I have looked up solutions online, however these seem to involve a selection of objects with this ability or the other objects are required to have their own shader. I want this to be shader agnostic. Basically, I want all objects of a certain layer to be visible only when viewed through the “prism” of another object. What would be the best way to achieve this?

Look up how the stencil buffer works (on YouTube look for stencil portals)