Skip to content
Stylize · TechniqueBeginner

Sprite outline

Draw a border where a sprite's pixels meet the transparent space around it.

UVTexture2D / sprite

Cards

One live example to tweak, learn and copy.

How it works

A 2D game marks the selected unit, the item you can pick up or the enemy under the cursor with a colored border around the sprite. Drawing that border by hand would mean a second copy of every frame. A shader draws it from the sprite itself.

The idea fits in one sentence: a pixel belongs to the outline if it is transparent and one of its neighbours isn’t. The shader reads the sprite’s alpha at the pixel and at the texels next to it, and paints the line color where the pixel is empty but a neighbour is solid. The engine’s normal alpha blending then lays the result over the scene like any sprite.

Two details decide whether it works in a real project. The line grows outward, into space the sprite doesn’t use, so the image needs empty pixels around its art or the line is cut off at the quad’s edge. And in a sprite sheet (opens in a new tab) the neighbours of a frame’s border pixels belong to the next frame, so frames need a gap between them too.

Further reading