I have some grass models made of alpha cards (pictured), and I’m looking for a way to move individual vertices up or down depending on the inclination of the terrain, rather than rotating the whole mesh based on the terrain normal.
Is there a way to achieve this through shaders? I’ve been told I could use a heightmap but I’m not sure how I’d get the values of the terrain into a heightmap.
This model is extracted from Red Dead Redemption 2, and through Renderdoc I see they don’t use heightmaps to modify the vertices. What they use exactly is a mystery.
I think the idea would be that if your terrain is generated by a heightmap, you’d feed the same heightmap into the grass shader.
If your terrain is pre-baked geometry or something, you’d presumably need to make a heightmap that corresponded with it. In theory you could do that offline with a bunch of vertical raycasts…
A vertex shader cannot do anything in this regard if it doesn’t have height/normal information about the terrain, which needs to be supplied via a texture.
As @hexgrid already hinted, most terrains start as heightmaps, so it’s reasonable to assume that you always have the heightmap to begin with.
If you for some reason don’t have it, it can be rendered at startup into a sub viewport by drawing the top view of the whole terrain mesh using a shader that encodes elevation. Or you can render on the fly for active terrain chunk(s) if the terrain is streamed in chunks.