The distance shader uses sample rays to estimate the distance from the shading point to the nearest surface. This distance is mapped to a color linear gradient from near_color to far_color. You can use trace sets to include or exclude objects from the distance measurement.
A scene file that demonstrates the distance shader can be found here.
Scene files that demonstrate distance.out_direction can be found here.
distance.out_direction -> float_to_rgb -> flat.color