我想使用for循環將圖像分成多個texture2ds。(XNA)從另一個Texture2D創建一個新的Texture2D
我想要做這樣的事情:
newTexture = Texture2D.CopyImage(biggerTexture, x, y, width, height);
是否有可能做到這一點?
我想使用for循環將圖像分成多個texture2ds。(XNA)從另一個Texture2D創建一個新的Texture2D
我想要做這樣的事情:
newTexture = Texture2D.CopyImage(biggerTexture, x, y, width, height);
是否有可能做到這一點?
我唯一想到的就是使用Texture2D.FromStream()
,但它會讀取一個完整的圖像文件,所以它不會真正起作用。
我爲我的一款遊戲做的是創建一個圍繞Texture2D
的包裝,它只使用接受源矩形和目標矩形的SpriteBatch.Draw()
重載繪製紋理的特定部分。
您是否想過使用GetData
和SetData
?
我只是寫用於測試目的的擴展方法:
public static class TextureExtension
{
/// <summary>
/// Creates a new texture from an area of the texture.
/// </summary>
/// <param name="graphics">The current GraphicsDevice</param>
/// <param name="rect">The dimension you want to have</param>
/// <returns>The partial Texture.</returns>
public static Texture2D CreateTexture(this Texture2D src, GraphicsDevice graphics, Rectangle rect)
{
Texture2D tex = new Texture2D(graphics, rect.Width, rect.Height);
int count = rect.Width * rect.Height;
Color[] data = new Color[count];
src.GetData(0, rect, data, 0, count);
tex.SetData(data);
return tex;
}
}
現在,您可以調用它像這樣:
newTexture = sourceTexture.CreateTexture(GraphicsDevice, new Rectangle(50, 50, 100, 100));
如果你只是想繪製紋理的一部分,你可以建議使用像domi1819那樣的SpriteBatch
過載。
我在我的遊戲中使用了sourceRectangle解決方案,所以現在我正在爲每個圖塊上的遊戲繪製整個圖集,但使用sourceRectangle進行裁剪。不過感謝這個解決方案,如果我需要它,我可以使用它。 – emil1000123
我在我的遊戲中使用了sourceRectangle解決方案,所以現在,我正在爲每個圖塊上的遊戲繪製整個圖集,但是使用sourceRectangle裁剪它。 – emil1000123