How achieve HeightReference effect with primitives

If is possible, how I can achieve effect like in that demo:

but using primitives (instead of entities).

I want my primitives (polygons for example) be clamped to ground (but also extruded).

When I make my primitives as groundprimitives they lose their height.

The equivalent of a clamped polygon entity is a GroundPrimitive polygon. So you should be able to construct your geometry (with extrude etc) and just create it as a GroundPrimitive and it should be clamped. See:

You may find some of the “development” Sandcastle examples helpful, like this GroundPrimitive one: