I would like to split a single Texture2D into Texture2D's of size x
, and put those into a 2D array. The original Texture2D's size will always be a multiple of x
. What is the easiest way to do this?
You can do that with Rectangles.. Go trough the texture with 2 for statements (one for x coordinate, one for y coordinate) and make rectangles, then get color data for the rectangle and create a new texture with it.
texture = your source texture
newTexture = new piece of the texture to put into an array
Example:
for (int x = 0; x < texture.Width; x += texture.Width / nrPieces)
{
for (int y = 0; y < texture.Height; y += texture.Height / nrPieces)
{
Rectangle sourceRectangle = new Rectangle(x, y, texture.Width / _nrParticles, texture.Height / _nrParticles);
Texture2D newTexture = new Texture2D(GameServices.GetService<GraphicsDevice>(), sourceRectangle.Width, sourceRectangle.Height);
Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height];
texture.GetData(0, sourceRectangle, data, 0, data.Length);
newTexture.SetData(data);
// TODO put new texture into an array
}
}
So all you have to do is put the new texture into an array, however you want. If you ment to split only on the X axis, simply remove one for statement and change the Height of the sourceRectangle to texture.Height.
Hope this helps!