Premium · Cloth Series Part 2 of 3
Three.js Cloth Simulation Part 2:
Wave a Flag in the Wind
By Shane Brumback · · Intermediate Three.js tutorial · Cloth Series Part 2 · about 30 minutes
Take the Part 1 cloth, tie its left edge to a flag pole and let the wind take it. Gusty wind, a ripple that runs to the tip and a shaded banner mesh turn dots and threads into a real waving flag.
PART 1 → PART 2
Start here first: Part 1, Hang a Verlet Cloth Sheet
New to cloth? Do Part 1 first. It builds the 32 by 20 particle grid, the threads and diagonal braces, and the Verlet solver as a sheet hanging from a bar, drawn as dots and threads. This page keeps that code and world and adds the new lesson on top.
Hang a Verlet Cloth Sheet (Part 1)SEE IT LIVE
The flag waving behind this page is the finished Part 2 running live, with a gust every few seconds. Launch the full-screen demo to orbit around it and send your own gusts with Space.
Free with Google sign-in. Mouse-drag or one finger orbits, scroll or pinch zooms. Space gust, R reset, Esc exits.
What Is a Three.js Flag Simulation? (Quick Definition)
A three.js flag simulation is a Verlet cloth pinned along one edge to a pole and pushed by a wind force every physics step. The particles also drive a textured BufferGeometry; recomputing its normals every frame lets the sun shade each fold, so the waving cloth reads as a real banner.
Introduction
Prerequisite: Part 1: Hang a Verlet Cloth Sheet. The sky, ground, rocks, particles, threads and solver all carry over, so this page focuses on what is new.
In Part 2 the cloth from Part 1 becomes a flag. The world is the same (sky, fog, lights, ground and rocks), and so are the particles, threads and solver. What changes: the bar becomes a flag pole and the pins move from the top row to the left column, a wind force with gusts and a travelling ripple pushes every particle, and the dots and threads are replaced by a shaded mesh with a painted banner texture.
It is the kind of prop I like for team shooters such as Super Soldier Battle, my Three.js browser FPS. Part 3 adds the last piece: tearing it apart.
What's New Compared to Part 1
- A flag pole instead of the bar: the left column of particles is pinned to the pole, not the top row.
- Wind and gravity: a wind that breathes between gentle and strong, Space gusts, and a ripple that travels from the pole to the tip.
- A triangle wind force: parts of the cloth that turn into the wind catch more of it, which makes the flutter.
- A shaded mesh: two triangles per square, UVs and a painted banner texture on a
MeshStandardMaterial, withcomputeVertexNormals()every frame so the sun lights each fold. - Forces in an array: the Verlet step now reads a per-particle acceleration array instead of a fixed gravity value.
What You'll Learn
- How to pin a cloth to a flag pole by pinning one column of particles
- How to build a wind force that changes over time, with gusts and a travelling wave
- How a per-triangle normal force makes the cloth flutter where it faces the wind
- How to drive a textured BufferGeometry straight from the simulation Float32Array
- How recomputing vertex normals every frame gives moving cloth real shading
- How to paint a banner texture on a canvas with CanvasTexture
Prerequisites
- Finish Part 1: Hang a Verlet Cloth Sheet first; this part reuses its particles, threads and solver
- Basic Three.js: scene, camera, renderer, a mesh and a material
- Helpful: the free Three.js Cloth Simulation article
Facts
- The flag is the same 640 particles (32 by 20) and 2,406 distance constraints as Part 1; the 20 particles on the pole are pinned.
- The wind strength breathes between about 5 and 17 (two sine waves) plus a gust that fades out; an automatic gust arrives every 5 to 9 seconds.
- The mesh is 1,178 triangles that read the simulation array directly; normals are recomputed every frame for shading.
- Physics runs at a fixed 60 steps per second with 14 constraint passes per step and 0.985 velocity damping.
- No physics engine is used. The example is one HTML file, about 320 lines of JavaScript, using three.js r160 from unpkg.
Frequently Asked Questions
How do you make a flag wave in three.js?
Pin one column of a Verlet cloth to a pole, then every physics step add gravity, a wind vector that changes over time, and a side-to-side wave that grows toward the free end. A force along each triangle's normal, scaled by how much it faces the wind, adds the flutter.
Why recompute vertex normals every frame?
Lighting uses the normals to decide how bright each part of the surface is. The cloth changes shape every frame, so computeVertexNormals() has to run every frame or the folds would be lit as if the flag were still flat.
Does the mesh copy the particle positions?
No. The BufferAttribute wraps the same Float32Array the physics writes to, so moving a particle moves its vertex. You only set needsUpdate so three.js uploads the new numbers.
What changed from Part 1?
The same particles, threads and solver; the bar becomes a pole and the pins move to the left column; wind and gusts are added; and the dots-and-threads view becomes a shaded, textured flag mesh.
Is the code free?
The live demo is free with a Google sign-in. The step-by-step code breakdown, the complete file and the Launch button are for members.
How It Works
- Imports, Scene, Camera and Renderer — In plain words: we load three.js and OrbitControls, make a scene and a camera, and put a full-page canvas on the screen.
- Lights, Ground and Rocks — In plain words: we add a sky light, a sun that casts shadows, a green ground disc and a few rocks so the flag has a place to stand.
- The Flag Pole — In plain words: a metal cylinder with a gold ball on top. The cloth will be tied to it.
- Cloth Particles: Positions, Previous Positions and Pins — In plain words: the flag is a 32 by 20 grid of dots. For each dot we remember where it is now and where it was one step ago, and the dots touching the pole are pinned so they never move.
- Distance Constraints (the Threads) — In plain words: every dot is tied to its right and lower neighbour by a thread of fixed length, plus two diagonal braces per square.
- The Flag Mesh and Its Banner Texture — In plain words: we paint a red, navy, white and gold banner on a canvas and wrap it on a mesh that shares the particle array. This replaces the Part 1 dots and threads.
- Wind and Gravity — In plain words: every step the wind changes a little. Each dot gets gravity, the wind, and a side-to-side wave that grows toward the free end; each triangle also gets pushed along its normal when it faces the wind.
- Verlet Integration and Constraint Solving — In plain words: move every free dot by its own momentum plus the forces, then pull each pair of dots back to the right distance 14 times.
- Gusts, Reset and Keys — In plain words: every 5 to 9 seconds a gust blows on its own. Space sends a gust now and R lays the flag out flat again.
- Fixed-Timestep Loop, Camera Framing and HUD — In plain words: physics always runs in 1/60 second steps, whatever the screen's frame rate; then we update the mesh, its normals and the readout, and draw. Behind the page text the camera slowly auto-orbits. In the live demo, OrbitControls takes over so you can orbit with a mouse drag or a finger.
That's the shape of the build. The detailed code breakdown for every step and the full copy-paste file are below. Members unlock them.
How To Use It
- Change the weather. Edit the numbers in
updateWind()for a calm day or a storm, and thegustvalue for stronger Space gusts. - Paint your own flag. Change the colours and shapes in
makeBannerTexture(); the UVs map the whole canvas onto the cloth. - Reuse the pattern. The same pinned cloth plus wind makes capes, sails, curtains and banners for a game.
Build a Tearable Wind Flag
Your flag waves. Part 3 gives every thread a breaking point so you can grab the flag, pull it and tear it open. Go to Part 3 →
READ THE FREE ARTICLE
The article explains Verlet integration, wind and tearing in plain words, compares writing cloth yourself with using a physics engine, links all three parts of this series, and is free to read.
Read the ArticlePlay It and Keep Learning
Play Super Soldier Battle free in your browser, my team-based Three.js shooter (read about it here). More physics: the Rapier rocket lander tutorial for rigid bodies, and walls that stop bullets with BVH raycasts.
Need Three.js physics or game work? I take freelance and contract work: contact me.
Play the Game