W/S or up/down gas and brake · A/D or left/right arrows steer · Space drift · R flips the car back · drag to orbit · Esc exits
↻Rotate your device to landscape to drive.

Three.js & Rapier Physics (Part 3):
How to Add Rain and Weather Effects to a
GTA-Style Car Game

By Shane Brumback · · Three.js tutorial

Premium Three.js rain effect tutorial, Part 3 of my GTA-style driving series: drive the muscle car through a rainy night city with GPU rain streaks, wet roads that reflect the city lights, rippling puddles, tire spray, puddle splashes and wet tire tracks that dry out behind you. Live demo.

Three.js Rain Effect Shaders Particles Vehicle Physics
See it live

LIVE DEMO

The muscle car behind this page is the finished file running live. Launch the full-screen demo to drive it through the downtown streets, drift it with the handbrake and knock the barrels and cones around.

Free: sign in free with Google. W/S or up/down arrows give gas and brake, A/D or the left/right arrows steer, Space drifts, R flips the car back, drag or one finger orbits; on phones turn to landscape and use the twin-stick controls. Esc exits.

What Is a Three.js Rain Effect? (Quick Definition)

A Three.js rain effect has two halves: rain you see falling and surfaces that look wet. Here the falling rain is thousands of line streaks moved entirely in a vertex shader inside a box that follows the camera, and the wet look comes from patching the road materials with onBeforeCompile: darker color, low roughness, a city reflection map, puddles and ripples.

Introduction

Part 3 finishes the series. We take the night city and the drivable muscle car from Parts 1 and 2 and make it pour: rain streaks around the camera, wet asphalt and sidewalks that mirror the street lamps, puddles with rain ripples, spray that flies off the tires, big splashes when a wheel rolls through a puddle, and dark wet tire tracks that slowly dry out.

The downloadable code is the complete package: the car, the physics, the full night lighting and all of the weather in one file. Steps 1 to 19 are the night city from Part 2, and steps 20 to 25 add the rain, puddles, spray, knock-over street lamps, and wet-sheen palms on top.

What You'll Learn

  • How to draw thousands of falling rain streaks with one LineSegments object animated entirely on the GPU
  • How to build a reflection map of a night city with PMREMGenerator so wet surfaces mirror warm lamps and lit windows
  • How to patch MeshStandardMaterial with onBeforeCompile for wet roads, puddles and procedural ripples
  • How to run the same puddle pattern on the CPU so the tires know when they are in water
  • Tire spray and splashes from a particle pool, and wet tracks from an InstancedMesh that fade over time

Prerequisites

Facts

  • Libraries: three.js r160 and @dimforge/rapier3d-compat 0.14.0, both from a CDN. No build step.
  • The file is 25 steps: the 19 night city steps from Part 2 plus 6 weather, knock-over lamp, and wet-landscaping steps.
  • Rain: 9,000 streaks on desktop and 3,500 on phones in a 50 m box that follows the camera, falling at 24 m/s.
  • The wet reflection map is a small night city of 90 glowing panels baked once with PMREMGenerator.
  • Spray uses a pool of 1,200 droplets (500 on phones); wet tracks use up to 700 dabs (300 on phones) that dry out over 6 seconds.

Rain That Costs Almost Nothing

Every streak is two vertices that never change on the CPU. The vertex shader drops them with time, wraps them inside a 50 m box centered on the camera so the rain never runs out, tilts them with the wind and fades them near the lens and in the distance. Thousands of streaks are one draw call and zero JavaScript work per frame.

Puddles the Tires Can Feel

The puddles are a noise pattern in the road shader: where the noise is high the surface turns flat, dark and mirror-smooth, and drops ripple it. The same hash and noise run in JavaScript, so every physics step can ask whether a wheel is standing in a puddle and throw a big splash instead of a light mist.

GPU Rain Streaks & Puddle Roughness (Preview)

Part 3 keeps thousands of rain streaks at one draw call by wrapping particle positions around the camera in a Three.js vertex shader, while matching road puddles in both GLSL and JavaScript:

// Wrap rain streaks inside a 50 m box centered on the camera entirely on the GPU
vec3 rel = mod(position - uCamPos + vec3(0.0, -uTime * uFallSpeed, 0.0) + 25.0, 50.0) - 25.0;
vec3 worldPos = uCamPos + rel + uWindOffset * aStreakEnd;
gl_Position = projectionMatrix * viewMatrix * vec4(worldPos, 1.0);

// Patch Three.js MeshStandardMaterial roughness in onBeforeCompile so puddles mirror lamps
roadMat.onBeforeCompile = (shader) => {
  shader.uniforms.uRainTime = rainTimeUniform;
  shader.fragmentShader = shader.fragmentShader.replace(
    '#include <roughnessmap_fragment>',
    '#include <roughnessmap_fragment>
  roughnessFactor = mix(roughnessFactor, 0.06, puddleMask);'
  );
};

How It Works

  1. Imports, settings and renderer. Load three.js, OrbitControls, GLTFLoader, the meshopt decoder and Rapier, and set every driving number in one place.
  2. Night sky, moonlight and fog. Set the city at night with a black-blue sky, a few hundred stars, a weak blue moon for soft shadows, a low ambient fill and thick haze.
  3. Volumetric light beams and ground pools. Build fake volumetric light: soft additive cones that fade along the beam and at their edges, plus round glow pools on the ground, all instanced.
  4. Physics world, the roads and the downtown buildings. Start Rapier, load the four GLB models (roads, buildings, car and driver), turn the road meshes into one trimesh collider and move each building onto a city block.
  5. Scan the streets: ground fill and a drivable ground. Cast a ray down every 2 m to map the roads, flood-fill ground heights, and turn the whole ground grid into one trimesh collider with a solid box for each building, so the car can drive anywhere.
  6. Street props: barrels, cones, jersey barriers and rubble. Scatter knockable barrels and cones, fixed jersey barriers and decorative rubble along the sidewalk edges and across road exits.
  7. Street lamps and building floodlights. Stand a street lamp on the sidewalk edge about every 24 m with a volumetric beam and a ground pool, and wash street-facing walls with floodlights.
  8. Lit windows from the building materials. Light a random pattern of skyscraper windows using only the building materials: the shader finds glass by its low roughness and lights some cells.
  9. Real lights that follow the car. Move a small pool of real point lights between street lamps only while both lamps are off screen or deep in the haze, aimed at the street ahead, so you never see a light switch on.
  10. The car: model, four separate wheels, and the chassis body. Split the single four-wheel mesh into four wheel meshes, put each in a steering pivot, and give the car a dynamic body with a low ballast collider. A baked, posed driver sits inside with his hands on the wheel.
  11. Headlights, tail lights and lens flares. Give the headlights and brake lights glowing materials, add two spot lights with short volumetric beams, lens flares that draw in front of the car body, tail lights that brighten when you brake, and a soft contact shadow under the car.
  12. Raycast vehicle: suspension, tyre grip and steering. Create the Rapier vehicle controller and add four wheels with spring, damping, travel and friction settings.
  13. Driving input: keyboard and the mobile twin-stick. Map W/S and up/down to gas and brake, A/D or left/right arrows to steering, Space and R, and wire the site-wide twin-stick controls for phones.
  14. Engine, brakes, steering and the handbrake drift. Each physics step, turn the input into engine force, brakes, speed-sensitive steering and the handbrake drift settings for every wheel.
  15. HUD: speedometer and drift meter. Show km/h, mph, drive or reverse, and a drift readout based on how fast the car is moving sideways.
  16. Camera: attract-mode orbit behind the page, chase cam and drag orbit in the demo. Slowly orbit the parked car behind the page text, then in the demo follow the car and ease back behind it after you drag to orbit.
  17. Main loop, resize and Live Demo hooks. Step the physics at a fixed 60 Hz, copy the chassis, wheels and props into their meshes, move the moon, the real street lights, the car lights and the camera, and expose enter and exit for the Live Demo button.
  18. Rain: gloomy clouds and streaks that fall around the camera. Cover the stars with a slow, gloomy cloud deck, then draw thousands of rain streaks as one LineSegments object and move them only in the vertex shader, wrapped in a box that follows the camera, with a wetness value that fades the rain out or in.
  19. A night city for wet surfaces to reflect. Bake a small night city of warm lamps and cool lit windows around a glowing horizon into a reflection map with PMREMGenerator.
  20. Wet roads, puddles and ripples. Patch the road, sidewalk and ground materials with onBeforeCompile: darker and glossier when wet, flat mirror-like puddles from a noise mask, and ripple rings where drops land.
  21. Tire spray, puddle splashes and wet tire tracks. Check each wheel against the same puddle pattern on the CPU, throw spray and splashes from a particle pool, lay dark wet tracks that dry out over 6 seconds, and expose setOn to switch the rain.
  22. A driver, tire marks and dust. Seat a posed driver with his hands on the wheel, then lay fading rubber marks and kick up dust whenever the rear tires spin as you take off (forward or in reverse), you brake hard, the car slides sideways through a turn, or you pull the handbrake. The rain washes both out: wet roads get neither.
  23. Sound: engine, tire screech, crashes, the horn and the rain. Add real engine, tire screech, crash and horn recordings with the Web Audio API: the engine revs through the gears as you speed up and drops off when you let go of the gas, the tires squeal when they slip or you brake hard, a crash plays when the car hits something, and H holds the horn (M mutes). A rain loop fades in and out with the wet weather.
  24. Knock-over street lamps: bend, fall, flicker out and smoke. Give every street lamp a thin physics collider so a slow nudge bumps into it, and snap it when you hit it at speed: the base kinks over, the top swings down to the street, the light flickers and dies, smoke rolls off the bend, the car loses some speed, and the lamp stands back up later once it is far away and off screen.
  25. Palm trees and roadside landscaping. Line the sidewalks with instanced palm trees in raised stone planters (curved ringed trunks, coconuts and 10 arched feathered fronds), paired with clustered tropical shrubs, accent blooms, trimmed hedge strips and thin trunk colliders with damp, lower-roughness foliage and stone curbs that catch the rain.

The detailed code breakdown and the full copy-paste file are below. Members unlock them. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.

How To Use It

  • Change DROPS, RAIN_FALL and the wind vector in step 18 for a drizzle or a storm.
  • Move the 0.6 and 0.68 thresholds in puddleMask to make puddles rarer or bigger, and change the 0.62 in inPuddle to match.
  • Call window.__carDemo.weather.setOn(false) to let the city dry out, and setOn(true) to start the rain again.
  • Lower SPRAY and TRACKS if an older phone struggles.

Frequently Asked Questions

How do I make rain in Three.js?

Use one LineSegments object with two vertices per streak and move them in the vertex shader: subtract time times the fall speed, wrap the height with mod, and wrap x and z inside a box centered on the camera. Additive blending with depth writes off keeps it light.

How do I make wet roads and puddles in Three.js?

Patch the road's MeshStandardMaterial with onBeforeCompile: darken the color, lower the roughness, give it an environment map of city lights, and use a noise mask to make puddles flat and almost mirror-smooth, with small procedural ripple rings for raindrops.

How does the tire spray know when a wheel is in a puddle?

The puddle noise is written twice, once in GLSL for the road and once in JavaScript with the same hash. Each frame the code reads every wheel's contact point from the Rapier vehicle controller and checks the JavaScript version.

Is the full source free?

No. This is a premium Level 3 tutorial. The live demo is free: sign in free with Google to launch it. Members unlock the step-by-step code, the full file with the car, the night lighting and all the weather, and the Launch button.

Does the rain run on phones?

Yes. Phones get fewer rain streaks, spray droplets and tire tracks, and the demo uses the site-wide twin-stick controls in landscape.

Related Tutorials and Articles

READ THE FREE ARTICLE

Want the why before the how? Read Three.js Car Physics: Why GTA-Style Driving Feels So Good, then go drift in Drift Dash.

3D Model Credits