【发布时间】:2017-11-29 08:10:19
【问题描述】:
我想使用 for 循环将图像分成多个 texture2ds。
我想做这样的事情:
newTexture = Texture2D.CopyImage(biggerTexture, x, y, width, height);
可以这样做吗?
【问题讨论】:
标签: c# graphics xna monogame texture2d
我想使用 for 循环将图像分成多个 texture2ds。
我想做这样的事情:
newTexture = Texture2D.CopyImage(biggerTexture, x, y, width, height);
可以这样做吗?
【问题讨论】:
标签: c# graphics xna monogame texture2d
您是否考虑过使用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 重载。
【讨论】:
我唯一想到的是使用Texture2D.FromStream(),但它会读取整个图像文件,因此在您的情况下它不会真正起作用。
我为我的一个游戏所做的是围绕Texture2D 创建一个包装器,它只使用接受源矩形和目标矩形的SpriteBatch.Draw() 重载来绘制纹理的特定部分。
【讨论】: