簡體   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