更改 RenderTarget 会导致紫屏吗?
我试图在运行时更改 RenderTargets,因此我可以在运行时绘制一些元素,操作它们,然后最终将纹理绘制到屏幕上。问题是,如果我在运行时更改 RenderTarget,屏幕会变成紫色。这是我在 Draw 中得到的代码:
RenderTarget2D tempTarget = new RenderTarget2D(GraphicsDevice, 128, 128, 1,
GraphicsDevice.DisplayMode.Format, GraphicsDevice.PresentationParameters.MultiSampleType,
GraphicsDevice.PresentationParameters.MultiSampleQuality, RenderTargetUsage.PreserveContents);
GraphicsDevice.SetRenderTarget(0, tempTarget);
GraphicsDevice.Clear(ClearOptions.Target, Color.SpringGreen, 0, 0);
GraphicsDevice.SetRenderTarget(0, null);
如果我在运行时创建 RenderTarget(并且我确实需要在运行时创建内存中纹理并使用 SpriteBatch 在它们上绘制),那么如何创建 RenderTarget 似乎并不重要结果是完全紫色的屏幕。我可以做什么来解决这个问题?
I'm attempting to change RenderTargets at runtime, so I can draw some elements at runtime, manipulate them and then finally draw the texture to the screen. Problem is, the screen turns purple if I change the RenderTarget at runtime. Here's the code I've got in Draw:
RenderTarget2D tempTarget = new RenderTarget2D(GraphicsDevice, 128, 128, 1,
GraphicsDevice.DisplayMode.Format, GraphicsDevice.PresentationParameters.MultiSampleType,
GraphicsDevice.PresentationParameters.MultiSampleQuality, RenderTargetUsage.PreserveContents);
GraphicsDevice.SetRenderTarget(0, tempTarget);
GraphicsDevice.Clear(ClearOptions.Target, Color.SpringGreen, 0, 0);
GraphicsDevice.SetRenderTarget(0, null);
It doesn't seem to matter how I create the RenderTarget, if I do it at runtime (and I do need to create in-memory textures at runtime and draw on them with SpriteBatch) it results in an entirely purple screen. What can I do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来最好的选择是在 Draw 之外的其他地方创建 RenderTarget,在 Update 期间绘制到它,保存生成的纹理(并根据需要进行操作),然后在 Draw 期间绘制该纹理。
It looks like the best option is to create the RenderTarget somewhere other than Draw, draw to it during Update, save the resulting texture (and manipulate as necessary) then draw that texture during Draw.
我知道这已经晚了,但解决方案是在清除屏幕并开始绘制其他项目之前写入 RenderTarget。
这应该会阻止您按照其他人的建议在 Update 方法中进行渲染,这在很多方面都是违反直觉的。
I know this is late, but the solution is to write to the RenderTarget BEFORE you clear the screen and beginning drawing your other items.
This should prevent you from rendering in the Update method as suggested by others, which is counter-intuitive in many aspects.
当调用
spritebatch.End()
时,对象将写入后备缓冲区,或者在您的情况下写入tempTarget
。要制作纹理,然后使用 render2d
When
spritebatch.End()
is called objects are written to the backbuffer or in your case totempTarget
. To make the texture,then use the render2d