Skip to content

Pixel Snapping

earlygrey edited this page Aug 9, 2019 · 2 revisions

Sometimes when drawing lines, especially when individual pixels are noticeable such as when drawing to a lower resolution frame buffer that will be upscaled later, it's important that the centres of the pixels you want drawn are contained in the line quad since only those pixels will be drawn.

For example, if you're trying to draw a pixel-width horizontal line, the centres of pixels might lie exactly on the edges of your quad. Since positions are represented with floating point numbers, you might get one of the following results:

Shape Drawer has an option (default off) to snap the endpoints of lines to the nearest pixel centre points, then offset it a little so that the centre of the pixels corresponding to the start and endpoints are always contained in the line quad. So you get something like:

This works by keeping track of the size of a screen pixel in world coordinates (the coordinates you draw in), and using that to figure out how much to offset the start and end points. You can either set this manually using setPixelSize(), or use updatePixelSize() or update() which will calculate it automatically using the current projection and transformation matrices of the batch.

This is only used when calling line(), and does not work when using join types.

Clone this wiki locally