Course contentsShow
← Unity Game Development
Lesson 735 of 82811. Performance and Data PersistenceFree lesson

Optimizing Texture Atlasing for Shared Materials

Learners will combine multiple textures into a single atlas to allow different objects to share materials and increase batching efficiency.

In this lesson, you will learn how to combine multiple textures into a single atlas to enable shared materials and drastically reduce draw calls.

The Problem: Draw Call Fragmentation

In Unity, every time the GPU renders an object with a unique material, it triggers a "draw call." If you have ten different crates, each using a unique texture, the GPU must switch states ten times. Even with dynamic or static batching, Unity cannot combine objects into a single batch if they reference different textures, as the GPU cannot swap textures mid-draw.

The Solution: Texture Atlasing

Texture Atlasing solves this by merging your individual texture files into one large "master" texture. By doing this, all your previously separate objects can now point to one single Material. When the render pipeline sees that all objects share the same Material and texture, it can collapse those objects into a single draw call, significantly lowering the CPU overhead.

Implementation Logic

To implement this, you re-map the UV coordinates of your 3D models. Instead of the mesh's UVs covering the entire 0-to-1 range of a standalone texture, they now map to a specific "island" within the atlas.

Here is how you handle the material assignment in code to ensure the batcher recognizes the shared state:

// Ensure all objects share the same material instance
Material sharedAtlasMaterial = myRenderer.sharedMaterial;

foreach (Renderer r in objectRenderers)
{
    // By assigning the same Material object, we allow 
    // the GPU to draw all these objects in one batch.
    r.sharedMaterial = sharedAtlasMaterial;
}

Important Considerations

  • Padding: When packing textures, include a few pixels of "padding" or "gutter" space between icons or textures. This prevents "bleeding," where the GPU samples colors from an adjacent texture at the edges of your model due to mipmap filtering.
  • Resolution Limits: Don't create an atlas so large that it exceeds your target hardware's maximum texture size (e.g., 4096px or 8192px), as this will force Unity to downscale your assets, resulting in visual quality loss.

Key Takeaway: By consolidating textures into an atlas, you transform multiple unique material instances into a single shared resource, allowing Unity’s batching systems to combine your geometry and drastically reduce CPU draw call overhead.