Sprites optimization for 2d games Unity 3d

In 2D games, optimizing the performance of opaque and transparent materials is crucial for smooth gameplay and good user experience.

Opaque materials are those that do not allow light to pass through them, like a wall or a tree trunk. These materials are easy to render because the renderer does not need to worry about what is behind them.

On the other hand, transparent materials, such as glass or smoke, are more complex to render because they allow light to pass through them and thus require additional calculations to be done by the renderer. This can cause performance issues, especially in games with a large number of transparent objects on screen.

Are you looking to optimize your game’s sprite performance? There are various ways to do this, but our focus today is on reducing overdraw during rendering. One simple technique is to change the sprite mesh type to “Tight” in Unity. This will automatically generate a mesh for your sprite, reducing the amount of overdraw. Alternatively, you can create a custom mesh by using the Sprite Editor’s “Custom Outline” tab. By doing so, you can achieve the correct mesh for your sprite and further optimize performance.

Img 1: Sprite mesh type Full Rect transparent

Img 2: Sprite mesh type Tight transparent

In Image 1, we can see a lot of overdraws which translates to a heavy workload for your GPU. This is caused by using the Full Rect mesh type for sprites, which is the worst thing you can do for performance. The Full Rect mesh type should only be used for small objects that don’t create significant overdraws.

In Image 2, we used the Tight mesh type for sprites, resulting in 30-40% less overdraw. This means less calculation for the GPU and more FPS in the game, resulting in a smoother gaming experience.

If the Tight mesh type creates a lot of triangles for small objects, it may be better to use Full Rect mesh type. However, this should only be used for extra small objects to reduce the number of triangles and prevent big overdraws. Keep in mind that Full Rect mesh type is not suitable for sprites as it can cause a lot of overdraw and strain on the GPU, resulting in lower FPS in the game.

Sprite Assist is the best solution if the game structure allows us to do this.

To optimize the rendering of sprites, one approach is to split the sprite into two parts within the mesh. The opaque part can be rendered using an opaque material, while the edges can be rendered with a transparent material. This helps reduce overdraw and improve performance in your game.

To accomplish this, we can use the Unity-Sprite Assist tool. By using the complex mesh method, we can generate a prefab that consists of two meshes and two materials, one for the opaque part and one for the transparent part after replace the existing sprite with a generated prefab (Hold alt drag and drop prefab to your game object in hierarchy).

Img 3: Sprite converted to two meshes with transparent and opaque

When using an opaque material, it’s necessary to use Z sorting to ensure correct results. But don’t worry, the results are worth it! As shown in Image 3, the difference is amazing. From my own experience, I’ve seen a significant boost in game FPS by optimizing meshes and using opaque materials in all possible areas. In fact, I’ve even optimized some games from 30 FPS to 60+ FPS using these techniques.

Finding the right balance between the number of triangles and overdraws is crucial. While using opaque materials can increase the triangle count, it can significantly improve the game’s performance if done correctly.

I hope you found this post to be informative and useful.

Nik Dorn Written by:

Comments are closed.