Forcing SpriteKit Texture Atlas Generation to honour tile sizes

SpriteKit's texture atlas creation will try to efficiently pack in all images, finding out transparent areas in individual images that can be ignored while packing. This obviously will allow more images to be packed into a single texture.

In some cases, this is not desirable - while laying out a pixel perfect 16x16 tile based scene, I noticed glitches and gaps along tile boundaries. This stemmed to some of the 16x16 containing transparent regions which the texture atlas generation process took advantage of. While being unpacked behind the scenes and rendered onto the SKScene, gaps across tiles would surface.

 

I checked if forcing a 16x16 tiling on SpriteKit would resolve the issue - and it did. So, for creating texture atlases bound to a specific tile size, I opt to now modify images and write a black pixel with alpha of 0.004 (1/255) on all four corners of the image using ImageMagick's convert command line tool.

convert inputTile.png -fill 'rgba(0,0,0,0.004)' -draw 'point 0,0' -draw 'point 15,0' -draw 'point 0,15' -draw 'point 15,15' outputTile.png

I have a custom Run Script in Xcode that will run the above command on all my image files as a pre-processing step before texture atlas creation. This thankfully solved the weird gaps for good.