繁体   English   中英

将Texture2D拆分为二维数组

[英]Splitting a Texture2D into a two dimensional array

我想将单个Texture2D拆分为大小为x Texture2D,然后将其放入2D数组中。 原始Texture2D的大小将始终是x的倍数。 最简单的方法是什么?

您可以使用Rectangles来做到这一点。用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的Height更改为texture.Height即可。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM