Creating a material which clips the owning mesh by an input mesh

I’m trying to create a material which takes an input mesh and clips parts of the material owning mesh which are outside it

I tried doing this with a primitive shape sphere (as “input mesh”) on a cube (“owning mesh”) as such:

hlsl code:

float dist = distance(WorldPosition, SphereCenter);
if (dist > SphereRadius)
{
   discard;
}

return 1.0;

and it works but is there a universal way to do this with any input mesh? not just with primitives like sphere or cube where I’d have to hardcode the equation