将 Texture2D 拆分为二维数组

Splitting a Texture2D into a two dimensional array

我想将单个 Texture2D 拆分为大小为 x 的 Texture2D,并将它们放入二维数组中。原始 Texture2D 的大小将始终是 x 的倍数。最简单的方法是什么?

你可以用矩形来做到这一点.. 使用 2 个 for 语句(一个用于 x 坐标,一个用于 y 坐标)遍历纹理并制作矩形,然后获取矩形的颜色数据并使用它创建一个新纹理。

纹理 = 您的源纹理

newTexture = 要放入数组的新纹理块

示例:

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
        }
    }

因此,您所要做的就是将新纹理放入数组中,随心所欲。 如果您只想在 X 轴上拆分,只需删除一个 for 语句并将 sourceRectangle 的高度更改为 texture.Height.

希望对您有所帮助!