From BreakEnderNerd Alert
Nerd Alert: How Games Make a Million Blades of Grass Sway in the Wind
By the BreakEnder newsroom ·
That field rippling as you ride through it is not physics. It is a tiny bit of math run on every blade, every frame, by your graphics card.
Welcome to Nerd Alert, our series about the tricks hiding behind the pretty pictures. We start with the one everybody notices and almost nobody thinks about: grass that moves.
Load into almost any open-world game, stand still in a meadow and watch. Waves roll across the field. Individual blades flick. Bushes nod. It looks like a weather simulation. It is not. Simulating real air flowing around millions of leaves would melt any console on earth. What you are seeing is a clever fake, and once you know how it works you will never unsee it.
First, what a vertex shader is
Every 3D object is a mesh: a skin of triangles whose corners are called vertices. Before the screen is drawn, the graphics card (the GPU) runs a small program on every single vertex. That program is called a vertex shader. Its main job is to say where the vertex ends up on screen, but nothing stops it from nudging the vertex a little first.
That is the whole secret. Grass "wind" is a vertex shader that pushes each vertex sideways by an amount that changes over time. The mesh on disk never moves. The GPU just bends it on the way to your eyes, sixty or more times a second, for free-ish.
The simplest sway: a sine wave
The cheapest version uses a sine wave, the smooth up-and-down curve from school maths. Feed it the clock and it swings back and forth forever. A first try looks like this:
offset = sin(time * speed) * strength;
position.x += offset;
Run that and the whole field rocks in lockstep like a synchronised swimming team. Not great. Two small fixes make it believable.
First, add the blade's world position into the wave, so different places are at different points of the swing:
offset = sin(time * speed + worldPos.x * 0.3 + worldPos.z * 0.2) * strength;
Now a ripple travels across the field. That traveling ripple is what your brain reads as a gust.
Second, pin the roots. Grass does not slide along the ground; it bends from the base. So the offset is scaled by height up the blade:
offset *= heightAlongBlade * heightAlongBlade;
Squaring the height makes the tip move a lot and the root not at all, which looks like a curve instead of a stiff stick tilting over. Artists often store that "how much may this vertex move" value in the mesh's vertex colors, so they can paint stiff stems and floppy leaf tips by hand.
Layers of wind
Real foliage moves at several speeds at once, and good games copy that. A classic breakdown, described in the Crysis vegetation chapter of NVIDIA's free GPU Gems 3 book (GPU Gems 3, chapter 16), splits motion into:
- Main bending: the whole plant or tree leans with the wind, slowly.
- Detail bending: leaves and branch edges flutter quickly and a bit randomly.
- Edge flutter: the thin outer rim of a leaf wobbles even faster.
Each layer is its own small wave with its own speed and size, and they are just added together. Three cheap waves stacked up look surprisingly organic.
Noise instead of pure waves
Sine waves repeat, and sharp-eyed players can spot the pattern. So many games replace or mix them with noise: a texture or function full of smooth random-looking values, such as the famous Perlin noise. The shader scrolls a noise texture across the world in the wind direction and reads it at each blade's position. Where the noise is bright, the gust is strong. Because the texture slides along, gusts visibly sweep across the landscape, which is exactly what tall grass does on a real hill.
wind = sampleNoise(worldPos.xz * 0.01 - windDir * time * gustSpeed);
position.xz += windDir * wind * strength * heightAlongBlade;
One wind to rule them all
The cherry on top is consistency. If the grass leans east while the flags flap west, the illusion breaks. Sucker Punch talked about exactly this for Ghost of Tsushima: one shared wind direction drives cloth, particles and grass, using many simple, fast simulations rather than one accurate expensive one, and the grass is generated per blade on the GPU with its own look and animation. See their GDC talks Procedural Grass in Ghost of Tsushima and Blowing from the West: Simulating Wind in Ghost of Tsushima. That game even uses the wind as a navigation guide, so the grass doubles as a UI element.
In practice a game keeps a few global values (wind direction, wind strength, gust speed) and hands them to every shader that cares. Weather systems just turn those knobs.
Why not real physics?
Cost. A physics engine moves objects by computing forces and collisions on the main processor. Doing that for every blade would be millions of tiny objects. The vertex shader trick costs a handful of maths operations per vertex on hardware that already processes millions of vertices per frame. It has no memory of the past and needs none, because a wave function only needs the current time.
The downside is that pure shader wind cannot react to you. The grass does not know a horse just ran through it. That needs an extra trick, which is the subject of the next Nerd Alert: interactive foliage bending.
Spot it yourself
Next time you play, look for these tells:
- Pause the game. In many titles the grass freezes too, because the clock fed to the shader stops.
- Stand on a hill and watch whether gusts roll across the field in one direction. That is scrolling noise.
- Look closely at bushes. Big slow lean plus small fast leaf flicker means layered wind.
- Look at far-away grass. It often stops swaying, because distant plants are swapped for simpler versions (more on that in our LOD article).
Glossary
- Vertex: a corner of a triangle in a 3D mesh.
- Vertex shader: a tiny GPU program run on every vertex, able to move it.
- Noise: smooth pseudo-random values used to make things look natural.
- Vertex color: extra data stored per vertex, often used as a mask rather than an actual color.
That is the trick behind every swaying meadow from indie games to the biggest blockbusters: a sine wave, some noise, and a GPU that does not get tired.
Want the full math behind this? Real-Time Rendering, Fourth Edition by Akenine-Möller, Haines and Hoffman is the reference most graphics programmers keep on their desk.
As an Amazon Associate, BreakEnder earns from qualifying purchases.