Most efficient way to combine textures?

I have a need in my current project to combine multiple textures into one. I’m not sure the most practical and memory efficient way to go about this. I’ve considered many options including using a List and then do something like:

Texture2D[,] textureGrid = new Texture2D[numberOfHorizontalTextures, numberofVerticalTextures];

Then go through each texture horizontally and use GetData(Color[] c) to get the pixels, add them to a destination texture using SetData, move down to the next vertical row and start again. This, unfortunattely, means that with each iteration along the horizontal, GetData is called on each texture over and over again. I’ve also considered using GetData in a Dictionary, like this:

Dictionary<Texture2D, Color[]> textureDictionary = new Dictionary<Texture2D, Color[]>

This, however, is essentially an array of arrays and textures, which would get memory heavy in almost no time. I can’t just iterate through a List, since each texture in the combined texture needs a specific X and Y position in order to make the desired image.

Am I overthinking this? Is there a quick and less memory intensive way of doing this that I’m just not aware of?

-Turtle

You can also use a RenderTarget to render images onto or next to each other (that’s actually what it is for).
http://rbwhitaker.wikidot.com/render-to-texture
Plus, RenderTargets don’t use GetData, which is important for you if you are planning to run your project on an iPhone (GetData causes UI freeze on iOS). If you have big textures or lots of merging, I’d do all these while your game is loading, not during runtime.

1 Like

Wow. It’s been so long since I went through those tutorials. RenderTarget is also a feature I’ve never ever had a reason to use, so I completely forgot about it. Thank you. That’s incredibly helpful.

1 Like