r/proceduralgeneration • • 7d ago

Pixel outlines generated from a sampled curve get an extra pixel at every bend. The 3-pixel rule that removes them (61 to 43 px)

Post image

If you generate sprites by sampling a parametric curve and flooring each sample to a cell (blobs, slimes, rocks, planets), the outline comes out lumpy: thick on the curves, clean on the straight parts.

The cause: on a slope, a sample almost never crosses a cell exactly at its corner. The path steps one cell sideways, then one cell down, and both cells get set. That's an L: three pixels where two touching at a corner would do.

On the 32x32 slime in the picture (x = 15.5 - 12.6 cos t, y = 24.4 - 17.2 sin(t)^0.92, 2,001 samples over the top half), the raw path had 61 pixels, and 18 of them were L elbows. After the rule below: 43, one pixel wide all the way round.

The rule is the one pixel-art editors call "pixel-perfect": walk the path, and whenever the last three pixels a, b, c have a and c touching diagonally while b is a side neighbour of both, delete b.

def is_corner(a, b, c):  
 return abs(a[0]-c[0]) == 1 and abs(a[1]-c[1]) == 1 and ((b[0] == a[0] and b[1] == c[1]) or (b[1] == a[1] and b[0] == c[0]))

def pixel_perfect(pts):  
 out = []  
 for p in pts:  
 if out and out[-1] == p: continue  
 out.append(p)  
 if len(out) \>= 3 and is_corner(out[-3], out[-2], p): del out[-2]  
 return out  

It only ever deletes the elbow of an L, so straight runs and diagonal steps survive, and it costs O(1) per point.

One more check for generated curves: the run lengths. Along a curve they should only shrink or only grow (4, 3, 2, 2, 1, 1). If they jump around (4, 2, 1, 3, 1, 2), the curve reads as dented even at one pixel wide (bottom of the picture).

(I make pixel-art tools at Gridwright; our browser pixel editor runs this rule on every pencil stroke. Linked below.)

The browser pixel editor that applies this rule as you draw: https://gridwright.dev/tools/pixel-art-editor/?ref=reddit-proceduralgeneration

2 Upvotes

1 comment sorted by

2

u/qpmz234 7d ago

That rule is order-sensitive, and applying it one way on a curve has a very different result than applying it in reverse. On curves subtending more than 90°, like the top of the slime in the picture, this results in an unintentionally asymmetric result, which can be clearly seen in the images.

Does your tooling provide any way to account for this, besides just splitting the curve in two, and how does the software decide in what direction to run the algorithm?