0
\$\begingroup\$

For optimization purposes and reducing batches count, I am trying to combine my floor tiles textures into different atlases. Sharing the same material with an atlas can decrease draw calls and hence increase the performance. Here is my code snippet that creating atlas but i am unable to understand how to get texture back from the atlas:

public class TexturePack : MonoBehaviour
{
    public GameObject[] allFloor;

    public GameObject[] newFloor;
    // Source textures.
    Texture2D[] atlasTextures;

    // Rectangles for individual atlas textures.
    Rect[] rects;
    public Texture2D atlas;
    void Start()
    {
        atlasTextures = new Texture2D[allFloor.Length];

        for (int i = 0; i < atlasTextures.Length; i++)
        {
            atlasTextures[i] = (Texture2D) allFloor[i].gameObject.GetComponent<MeshRenderer>().material.mainTexture;
        }
        // Pack the individual textures into the smallest possible space,
        // while leaving a two pixel gap between their edges.
        atlas = new Texture2D(512, 512);
        rects = atlas.PackTextures(atlasTextures, 2, 1024);

        //how to get texture from the atlas for specifc object
    }
}

Within unity will it work or i have to go to modelling software for uv mapping?

\$\endgroup\$
1
  • \$\begingroup\$ The method returns to you a collection of UV rectangles corresponding to each input texture. So I presume you then updated the UV coordinates for each vertex of your mesh to scale and shift them into the rectangle for the texture it was using previously? Where did you run into trouble with this? \$\endgroup\$
    – DMGregory
    Sep 15 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.