From BreakEnderNerd Alert

Nerd Alert: Interactive Foliage, or Why Grass Parts When You Walk Through It

By the BreakEnder newsroom ยท

Wind sway is a timer. Grass that bends around your boots needs to know where you are. Here is how games tell millions of blades about one player.

Last time we saw that swaying grass is a vertex shader wobbling each blade with a wave. That trick has one big blind spot: the grass has no idea you exist. Yet in plenty of modern games, tall grass leans away as your character pushes through, flattens under a tank and slowly springs back. How?

The problem

A vertex shader runs on the GPU for each vertex, and it only knows what you hand it. Giving every blade a list of every character in the world would be absurdly slow. So games pick one of a few smarter routes, from cheap to fancy.

Route 1: pass in the player position

The easiest version handles just one thing, usually you. The game sends the player's position to the grass shader each frame. Each blade measures its distance to the player and, if close, gets pushed away:

toPlayer = bladePos - playerPos;
d = length(toPlayer);
push = saturate(1 - d / radius);
position.xz += normalize(toPlayer.xz) * push * bendAmount * heightAlongBlade;

"saturate" just clamps a number between 0 and 1. Blades right at your feet get the full push; blades at the edge of the radius get none. Multiplying by height along the blade keeps the roots planted, same as with wind.

This is cheap and looks good for a single hero. Many games stretch it to a small fixed array of positions, say the player, their horse and a few enemies. Past a handful, it gets slow because every blade checks every entry.

Route 2: the bend map

To support anything that moves, games flip the problem around. Instead of each blade asking "who is near me?", the world paints "something is here" into a texture, and every blade just reads its pixel.

That texture is often called an interaction map or bend map. It covers the area around the camera, seen from above, like a little top-down mini-map. Each frame:

  • Every character, vehicle or explosion draws a soft blob into the map where it is. The color can store the push direction (red for sideways X, green for sideways Z) and the strength.
  • The grass vertex shader converts its world position into a spot on the map, reads the value, and bends by that much in that direction.

Now a hundred soldiers cost about the same as one, because each blade still does exactly one texture lookup. The map scrolls with the camera, since you only need interaction near where people are looking.

Making it spring back

If you wipe the map clean each frame, grass snaps upright the instant you leave. Real grass recovers slowly. The fix is to fade the map instead of clearing it: each frame the old picture is multiplied by something like 0.95 before new blobs are drawn. The trail behind you fades over a second or two and the grass rises gradually. Raise the fade value and you get flattened crop circles that last; lower it and grass is springy.

Some games go further and run a tiny spring simulation per pixel, storing both bend and bend speed, so blades overshoot and wobble back. It costs a bit more but the "boing" sells it.

Route 3: per-blade simulation on the GPU

The top tier treats each blade as a small object with state. Modern GPUs can run general-purpose programs called compute shaders, so a game can store a direction and velocity for every blade and update them all in parallel each frame, adding wind, player pushes and a spring force back to upright. Sucker Punch described grass generated and animated per blade on the GPU for Ghost of Tsushima in their GDC talk (Procedural Grass in Ghost of Tsushima). This is costlier and usually saved for grass near the camera.

Combining with wind

Interaction and wind are just two offsets added together. A typical shader computes the wind sway, computes the push from the bend map, and blends them. A useful detail: when grass is strongly pushed, reduce the wind on it, because a blade squashed under a boot should not also wave happily in the breeze.

The stretchy blade bug

Here is a classic bug. If you push the tip of a blade sideways by a large amount, the blade stretches like chewing gum, because you moved the top without moving it down. Good shaders keep the blade's length roughly constant: as the tip moves sideways, it also drops. A quick approximation is to lower the vertex by an amount that grows with the push, or to rotate the blade around its root instead of sliding the top. Look at bent grass in your favourite game; if it gets taller when you walk through it, someone skipped this step.

Trees and bigger plants

Big trees rarely bend for a single person, but bushes and reeds often do. The same bend map works; artists just paint which parts of the mesh may move. Large trees usually react only to big events like explosions, which can be a strong temporary blob in the map or a simple "shove" value passed to nearby trees.

Spot it yourself

  • Walk in a circle in tall grass and look back. If you see a fading trail, it is a bend map with fade.
  • Count how many characters push grass. Just you? Probably route 1.
  • Watch grass far from the camera while a car drives through it. Often it does not react, because the bend map only covers the area near you.

Glossary

  • Interaction map or bend map: a top-down texture storing where and how hard things push foliage.
  • Compute shader: a GPU program for general calculations, not tied to drawing.
  • Saturate: clamp a value between 0 and 1.

Wind gives grass life. Interaction gives it manners. Next, we look at why sunlit leaves glow, and how foliage lighting fakes light passing through a leaf.

For a deeper dive, Real-Time Rendering, Fourth Edition covers this technique and its neighbors in detail, with references to the original papers.

As an Amazon Associate, BreakEnder earns from qualifying purchases.