Gridwright Start for $1

Godot Pixel Perfect Outline Shader for Pixel Art Sprites

A pixel perfect outline shader for Godot 4 sprites, tested in 4.7.2: the code, the Nearest filter, padding at the frame edge and stray pixels in sheets.

Sprite Outline Adder at work
Sprite Outline Adder at work: Outlines round sprites and whole sheets.

Your character looks fine on the plain grey background you drew it on. Then you drop it on the grass, next to a tree and three slimes, and it sinks into the scene. It needs a one-pixel outline, so you find an outline shader, paste it onto the Sprite2D, and get three new problems: the outline is soft and smeared, it stops at the top of the head and under the feet, and some animation frames show odd dark dots at their edges.

All three come from how the shader reads the texture. We tested the shader below in Godot 4.7.2 on the 16 x 16 adventurer from Top-Down Characters, drawn at 4x, and compared every screen pixel with an outline we computed outside Godot. Here are the shader, the three things that broke it, and the numbers. If you would rather have the outline in the PNG itself, the Sprite Outline Adder does that for a sprite or a whole sheet.

The outline shader

Save this as outline.gdshader. On your Sprite2D or AnimatedSprite2D, open CanvasItem > Material, pick New ShaderMaterial and load the file into its Shader slot.

Pixel Monsters
Pixel Monsters: 16x16 pixel art RPG monsters: slimes, bats, ghosts, skeletons; move, hurt and death animations. One of the 150 packs in Gridwright.
shader_type canvas_item;

uniform vec4 outline_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);
uniform int width : hint_range(1, 8) = 1;
uniform bool square_corners = false;

void fragment() {
	vec4 c = texture(TEXTURE, UV);
	if (c.a == 0.0) {
		float a = 0.0;
		for (int x = -width; x <= width; x++) {
			for (int y = -width; y <= width; y++) {
				if (!square_corners && abs(x) + abs(y) > width) {
					continue;
				}
				a = max(a, texture(TEXTURE, UV + vec2(float(x), float(y)) * TEXTURE_PIXEL_SIZE).a);
			}
		}
		if (a > 0.0) {
			c = outline_color;
		}
	}
	COLOR = c;
}

For every see-through pixel it looks at the pixels within width of it, and if any of them belongs to the sprite, it paints that pixel in the outline colour. TEXTURE_PIXEL_SIZE is one texture pixel, so the outline is measured in sprite pixels at any scale: at 4x every outline pixel came out as an exact 4 x 4 block.

square_corners picks the shape. Off, a pixel only counts its four straight neighbours, and the outline has no corners, so the steps of the sprite's edge stay visible. On, the diagonals count too and the corners close. On the adventurer that is 50 outline pixels without corners and 67 with them at width 1, and 104 and 139 at width 2.

With the three settings below, Godot's result matched our outline exactly: 0 of 5,184 screen pixels differed.

Set the texture filter to Nearest

In a new project the outline comes out soft and grey at the edges, and the sprite goes blurry with it. Godot 4.7.2 draws 2D textures with the Linear filter by default, and a Sprite2D's own filter starts on Inherit, so it gets Linear too. Linear blends every sample with its neighbours, so the shader reads blended alpha values.

Top-Down Characters
Top-Down Characters: 16x16 top-down pixel art RPG characters: 4 directions, idle and walk animations, emotes. One of the 150 packs in Gridwright.

In our test the same shader with Linear drew 460 different colours where there should have been 16, and 188 of the 324 pixels came out wrong.

Fix it once for the whole project in Project Settings > Rendering > Textures > Canvas Textures > Default Texture Filter: Nearest, or per node in CanvasItem > Texture > Filter: Nearest. Your pixel art needs Nearest anyway; Godot pixel perfect settings covers the rest of the window setup.

Give the sprite room at its frame edge

A canvas_item shader can only colour pixels inside the sprite's own rectangle. The outline goes outside the sprite, so wherever the art touches the edge of its image, there is nowhere to draw it.

The adventurer is 16 x 16 and touches the top and bottom of its frame, like many walk cycles. Without padding the shader drew 36 of its 50 outline pixels: the 14 above the head and under the feet were missing, and the outline looked open at both ends.

The fix is a transparent border at least as wide as the outline, on every side. With 1 px of padding (18 x 18) all 50 were there. A wider outline needs wider padding: with only 1 px, a 2 px square outline drew 114 of its 139 pixels.

Add the padding in your editor, the same amount on each side (in Aseprite, Sprite > Canvas Size). A Sprite2D is centred by default, so it stays where it was.

Sprite sheets: stray pixels from the next frame

This one shows up as dark dots on some animation frames. When a Sprite2D draws one frame of a sheet, TEXTURE is still the whole sheet. At a frame's edge the shader's neighbour samples read the frame next to it, and where that frame's sprite touches the edge, it paints an outline pixel that belongs to neither.

The adventurer's sheet is 96 x 64: 24 frames of 16 x 16 with no gap between them. Outlined as packed, 14 of the 24 frames picked up stray outline pixels from their neighbours, 26 in all. The left-facing idle frame got one under its feet from the frame below, and it was the same pixel whether we cut the frame with Region, with Hframes and Vframes, or with an AtlasTexture, which is what SpriteFrames uses for frames added from a sheet. The one-pixel step was right in all three; only the frame edge was the problem.

The fix is the same padding, on every frame: re-pack the sheet so each frame has a transparent border as wide as the outline. Our 16 x 16 frames became 18 x 18 in a 108 x 72 sheet, and the stray pixels went from 26 to 0. Then set the new frame size in Hframes, the region or the SpriteFrames sheet dialog.

Shader or baked into the PNG

The shader suits outlines that change while the game runs: a colour per team, or an outline only while the mouse is over the sprite. For a hover highlight, keep the material in a variable and switch it:

@export var outline_material: ShaderMaterial

func set_highlight(on: bool) -> void:
	$Sprite2D.material = outline_material if on else null

An outline that is always on is simpler baked into the PNG: no material on every node, no padding to keep in mind, and the same file works in Unity, GameMaker or any other engine.

The fast way: the Sprite Outline Adder

The Sprite Outline Adder puts a hard-edged outline round every sprite, pixel for pixel, in your browser. Your art is never uploaded.

  1. Drop a sprite or a sheet, or several at once, or try it on ours: six monsters from Pixel Monsters.
  2. Pick the outline: outside the sprite or on its own edge, 1 to 8 px, with square, round or no corners, in one colour or taking on the sprite's own colours. Click your sprite to take one of its colours, outline the holes inside it too if you like, and add a second outline round the first for a sticker or pickup look. Before and after show at once, on a checker, dark, light or grass background.
  3. For a sheet, check the frame size it found. It outlines each frame on its own, so no outline reaches into the next frame, and Make room grows every frame by the outline's width, so a sprite on its frame's edge isn't cut. That is the padding and re-packing above, done for you.
  4. Download the ZIP: the outlined PNG, the outline on its own for a hover or selection highlight, and for a sheet a JSON of where every frame is now and a Godot 4 SpriteFrames (.tres) to drop on an AnimatedSprite2D.
Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes

FAQ

Why is my Godot outline shader blurry?

The texture filter is Linear, Godot's default. Set Default Texture Filter to Nearest in the Project Settings, or the node's Texture > Filter.

Why is the outline missing at the top of my sprite?

The sprite touches the edge of its image, and the shader can't draw outside it. Add transparent padding at least as wide as the outline.

Why do some animation frames get extra outline pixels?

The frames sit edge to edge in the sheet, so the shader reads into the next frame. Re-pack the sheet with a gap as wide as the outline round every frame.

Outline your next character

Open the Sprite Outline Adder, drop your sheet with Make room on, and keep the shader above for outlines that change in play. For 16 x 16 characters to try it on, Top-Down Characters has idle and walk sheets in four directions, and Pixel Monsters has the monsters from the tool's sample. The tool and both packs are part of Gridwright: 44 tools and 150 asset packs.

Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes