Home

Rain on Glasses Shader

Project:
Farsight
Year:
2026
Type:
Screen-space shader, gameplay system
Tech:
Unity URP, HLSL, C#
The same shot with the lens pass off on the left and on on the right
Left: lens effect off. Right: on.

The goal

In Farsight you're wearing glasses, and it rains a lot. I wanted the rain to actually collect on your glasses when you're outside, so it gets harder to see and the umbrella has a job to do.

Since the drops are right in front of your eyes, they should be blurry and warp what's behind them. And they should react to you: building up when you're out in the open, drying once you head inside, and staying off while your umbrella's up.

How it works

It's a full-screen effect on the player camera: two small blurred copies of the frame, then one pass that draws the drops. No drop textures, no particles. The drops are fixed to the screen, because your glasses move with your head. It runs before post-processing, so bloom and grading hit the drops the same way they hit everything else. There are three parts to it: where the drops go, what each drop looks like, and how wet your glasses are.

1. Placing the drops

The screen gets split into a grid, and each cell turns its position into a few random numbers. Those pick how big the cell's drop is, where it sits, and how early it shows up as the glasses get wet. To keep it cheap, each pixel only checks its own cell. That means a drop has to stay inside its cell, or it would get cut off at the edge.

Screen divided into cells, each shaded by its random valueSoft circles in a grid, at most one per cell, with some cells empty
A random value per cell, then at most one drop per cell. The lightest cells are still empty. Their drop shows up last. These show the layout only.

A drop can't be bigger than its cell, and a single grid looks regular. So three grids are stacked on top of each other for big, medium and small drops, each with its own cell size and offset. The edge blur is the same width no matter how big the drop is, so the small ones are basically all blur. They end up as faint smudges, which is pretty much what tiny droplets look like on real glasses.

Large, medium and small drop layers on a game frame, then combined
Large, medium and small layers on a frame from the game, then all three together.

2. Drawing a drop

Each drop works like a tiny lens. Inside it, I sample the image from the other side of the drop's centre, so each drop shows a small flipped view of what's behind it. The edge is darkened a little, like the rim of a real drop.

A close crop of a game frame before and after one drop's refraction
One drop on a game frame, enlarged and shown without the blur. The roof corner comes out flipped.

Blurring every drop separately would be expensive, so instead I make two blurry copies of the whole frame first, at a quarter and a sixteenth of the width and height. The drops sample from those, so they're always soft, and the blur costs the same however many drops there are.

Quarter-resolution copy of the frameSixteenth-resolution copy of the frame
Game frame before the lens passSame frame after the lens pass
The two blurred copies, then a game frame before and after the pass.

3. How wet the glasses are

Everything runs off one wetness value between 0 and 1. Each drop has its own threshold, so as it goes up, drops appear and grow one at a time, and as it goes down they shrink away again.

Drops appearing on a game frame as wetness rises, then drying away
Wetness rising from 0 to 1, then drying off again.
// cover: the wetness value. h: this drop's random threshold.
// r2: two random numbers. Multiplying them makes small drops more common.
// * 0.95: every drop has appeared by the time wetness reaches 1.
// * 5.0: each drop grows in over 0.2 of wetness instead of snapping on.
float size = lerp(0.14, 0.4, r2.x * r2.y)
           * saturate((cover - h * 0.95) * 5.0);

A small C# script sets that value based on what you're doing. It treats you as outside when a ray cast straight up from the camera hits nothing. Out there, your glasses get fully wet in about 25 seconds, and they dry in about 8 once you're indoors. Looking straight up gets them wet twice as fast, and looking straight down a quarter as fast. An open umbrella keeps them dry until you look up past 35 degrees. The umbrella tilts with your view, so from there its cover fades out, and past 60 it doesn't help at all.

Rain beads on the lenses, the umbrella opens and the lenses dry
Drops build up, the umbrella opens, and the glasses clear.

The diagrams and step-by-step frames were rendered with a Python port of the shader, run on frames from the game. The split image at the top and the umbrella clip were captured in Unity.