build-lens-raindrops
Build lens raindrops
Start with a moving scene containing one strong contrast edge, such as a lit sign against a dark wall. A convincing bead bends that edge inside its silhouette. To judge the motion, look at a stationary bead and a runner side by side.
Watch the lens comparison or inspect the taillight detail. Both sides use the same recorded game frame. The right side runs the two bundled shaders over it. The lower pair magnifies the same region in each render.
Wire the two passes
- Finish rendering the 3D scene, including the lights you want refracted, to a sampleable color target. Give it clamp-to-edge sampling. Keep the HUD in a later pass or DOM layer so speed, messages and buttons stay readable.
- Draw drop-field.frag on a fullscreen triangle into a separate, linearly filtered RGBA8 target at half the scene's width and height. The red and green channels store biased signed slopes. Blue carries droplet height. Treat this field as linear data, with blending, tone mapping, color conversion and mipmaps off.
- Draw refract.frag to a different target using the scene color and field. Feed both passes a bottom-left-origin
vUvin [0,1]. GiveuAspectthe scene's width divided by height. In the field pass, setuFieldSizeto the actual field target size in pixels. Resize targets when viewport or render scale changes. Apply the output color transform once in the host pipeline. - Feed
uIntensity(0–1) anduPhase(accumulated animation time) to the field. FeeduAmount(0–1) anduBendto the composite. Try 0.005 foruBendin viewport-height units. Both shaders use WebGL 2. In a node-material pipeline the same stages are a data render target followed by a displaced scene-color lookup.
The sample combines fine pinned beads with larger runners. Per-column timing breaks up parallel movement. Each moving bead has a round head and thinner tail extending upward. Taking differences of the scalar field produces a slope which bends the underlying image. Reversing the UV origin reverses the apparent fall; correct the coordinate convention at the pass boundary.
Drive it from weather and speed
The game's driving lens used a half-resolution field, a speed ramp between 30 and 240 km/h, and separate wetting and drying rates. Its intro glass had a separate treatment. Adapt those thresholds to the game's actual cruising speed. A slow market drive may need visible drizzle even at idle.