0

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?

  • Create new `Texture2D`s of size X and use a `RenderTarget` to draw the correct part of the "master" texture to the new textures. You should make sure that X is a power of 2 for maximum compatibility and memory efficiency. – Bradley Uffner Mar 12 '16 at 20:34
  • 1
    Why do you want to do this? Are you just trying to render a part of the texture or do you really need to have 2 different textures in memory? – craftworkgames Mar 13 '16 at 04:51
  • See [XNA Why is Texture.GetData one dimensional?](http://gamedev.stackexchange.com/questions/46775/xna-why-is-texture-getdata-one-dimensional) – libertylocked Mar 14 '16 at 17:13

1 Answers1

0

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!

bambucha
  • 56
  • 5