Make automatic dirt on ground for any meshes (with absolute world position?)

Hello,
I was wondering how can I manage to have some dirt at the base of my mesh, but if the mesh is elevated in the air, the dirt isn’t here anymore.

Basically, let’s say my ground level is entierly flat and at Z=0, what I want is to detect Z=0 so when I put a rock in the ground, the dirt affect it, same for the base of the trees… but the higher part of the tree will not be affected because its too high.

I was thinking about using a Ramp as a mask, and constraining that ramp to be between Z=0 and Z=+50 for exemple, so any polygon between that area will be affected, but if a mesh go higher than 50cm, the high part will not be affected.

I have no clue about how to proceed, I would apreciate any help.
So far I created a sort of Inverted snow shader (so the affected faces are from the bottom) then I have a ramp but I dont know how to make that ramp acting with world position between z=0 and z=a value