Spawning landscape layer foliage on virtual heightmap

Hello all!
Have recently created a virtual heightfield mesh for a project and setup foliage spawning based on layer sampling (see below)

However, due to the setup of the landscape layer sampling, it is causing the foliage to spawn on the landscape below and stick up through the virtual height map (see below)

Wondering if there is a different way to do this or some way to offset the foliage z value using the heightmap data.

You could use ray casts – start some distance above the landscape, and ray-cast down to the landscape to figure out where the ground is.

Wondering if there is a different way to do this or some way to offset the foliage z value using the heightmap data.

Yes, you just need to add a “Runtime Virtual Texture Sample” node in your foliage material graph and reference the RTV that contains the world height information (the same one you referenced in the VHM).

Then you subtract the Absolute World Position (this is the actual position of the foliage, which is defined by the landscape mesh) from the RTV World Height and you’ll have the offset you need to correct the differences between the the VHM and the landscape. Create a vector 3 and add that value to the Z (leave X and Y empty) and multiply it by the VertexNormalWS (considering the vertex normals of your cluster is set as a “dome” shape, where Z values goes from 0 to 1 from top to base).

You can plug this directly into the World Position Offset of your material attributes or add it to any other graph you already have in there (in case of wind animations or so).