XNA菜鸟在这里,每天都在学习。我刚刚弄清楚如何使用 RenderTarget2D 将多个纹理合成为一个。然而,虽然我可以将 RenderTarget2D 用作 Texture2D 来满足大多数用途,但有一个关键的区别:当调整后缓冲区大小时,这些渲染的纹理会丢失(毫无疑问在其他情况下,例如图形设备内存不足)。
目前,我只是将完成的 RenderTarget2D 复制到新的非易失性 Texture2D 对象中。不过,我这样做的代码非常丑陋。有没有更优雅的方法来做到这一点?也许我只是累了,但我在谷歌或SO上找不到答案。
稍微简化一下:
public static Texture2D MergeTextures(int width, int height, IEnumerable<Tuple<Texture2D, Color>> textures)
{
RenderTarget2D buffer = new RenderTarget2D(_device, width, height);
_device.SetRenderTarget(buffer);
_device.Clear(Color.Transparent);
SpriteBatch spriteBatch = new SpriteBatch(_device);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
// Paint each texture over the one before, in the appropriate color
Rectangle rectangle = new Rectangle(0, 0, width, height);
foreach (Tuple<Texture2D, Color> texture in textures)
spriteBatch.Draw(texture.Item1, rectangle, texture.Item2);
spriteBatch.End();
_device.SetRenderTarget((RenderTarget2D)null);
// Write the merged texture to a Texture2D, so we don't lose it when resizing the back buffer
// This is POWERFUL ugly code, and probably terribly, terribly slow
Texture2D mergedTexture = new Texture2D(_device, width, height);
Color[] content = new Color[width * height];
buffer.GetData<Color>(content);
mergedTexture.SetData<Color>(content);
return mergedTexture;
}
我想我should检查 IsContentLost 并根据需要重新渲染,但这发生在我的主绘图循环的中间,当然你不能嵌套 SpriteBatches。我可以维护一个“渲染 TODO”列表,在主 SpriteBatch 结束后处理这些列表,然后它们可用于下一帧。这是首选策略吗?
该代码仅被调用几次,因此性能不是问题,但我想学习如何正确地做事。