Optimizing sprite meshes for Unity

2015-09-21 Andreas Löw

TexturePacker 4 has two important new features for Unity users:

Rectangular sprites vs polygon meshes

Sprites for characters and game objects have to be well aligned to be easy to use in your game. The simplest way to achieve this is by adding some transparent pixels.

During the rendering phase every pixel of a sprite has to be processed. No matter if it's transparent or not. Transparency has a price that you pay with:

less pixels less GPU usage higher framerate

The amount of rendered transparency can be reduced by creating a tight fitting polygon around the solid pixels of a sprite. But: The vertices must be transformed by the CPU — introducing new costs.

less vertices less CPU usage higher framerate

The optimization has 2 goals: Keep the pixel count and the vertex count as low as possible.

Unity creates these meshes for you when using sprites — you might not even have noticed it. You can turn on the mesh display mode on the scene's drop down menu. Just change it from Shaded to Shaded Wireframe. Unfortunately there's no way of controlling the process.

See the example below — we are comparing Unity's meshes vs TexturePacker's optimized meshes:

Sprite polygon quality in Unity and TexturePacker
Unity 5.1.1.f1 vs TexturePacker 4.0.0

As you see in the example above TexturePacker creates way less triangles while keeping a good fit of the texture.

If you are already using our free TexturePacker Importer from the asset store there's not much you have to do — just set the Trim mode to Polygon.

The slider Tracer Tolerance gives you control over the mesh creation process.

Lower values create a tighter fitting mesh with less transparency but with more vertices. Higher values on the other hand reduce the number of vertices at the cost of adding more transparency.

The best ratio for vertices vs additional pixels depends on the hardware you are targeting. Calculating the vertices might be cheaper on a fast CPU than drawing the additional pixels. Whereas on a system with a fast GPU it might be better to reduce the number of vertices.

Sprite polygon mesh quality: pixels vs triangle count
Tracer tolerance setting: Triangle count vs overdraw

Polygon packing

TexturePacker 4 comes with an additional new feature: Polygon Packing. Simply set TexturePacker's Algorithm to Polygon.

Polygon packing is computational expensive and thus takes a bit longer than packing with MaxRects. The gain depends of course on your sprites. The more irregular and varying in size your sprite are the better the packing will be.

To use the polygon packed sprite in Unity you simply have to do nothing! TexturePacker Importer detects the polygon outlines in the tpsheet file and applies them to your sprites.

Packed sprite using polygon mesh sprites
Tight packed polygon sprites

Already available for you

TexturePacker 4 is currently available as beta version — a new TexturePacker Importer is available from Unity asset store.

You can download it from here:

Polygon mesh packing with TexturePacker

Did you like the tutorial? Please share!