更改 RenderTarget 会导致紫屏吗?

发布于 2024-09-10 14:29:26 字数 743 浏览 9 评论 0原文

我试图在运行时更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-09-17 14:29:26

看起来最好的选择是在 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.

蓝咒 2024-09-17 14:29:26

我知道这已经晚了,但解决方案是在清除屏幕并开始绘制其他项目之前写入 RenderTarget。

protected override void Draw(GameTime gameTime)
{
     GraphicsDevice.SetRenderTarget(_renderTarget);

     //...
     //Perform Rendering to the specified target
     //...

     GraphicsDevice.SetRenderTarget(null);

     GraphicsDevice.Clear(Color.CornflowerBlue);

     //...
     //Code that draws to the users screen goes here
     //...
}

这应该会阻止您按照其他人的建议在 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.

protected override void Draw(GameTime gameTime)
{
     GraphicsDevice.SetRenderTarget(_renderTarget);

     //...
     //Perform Rendering to the specified target
     //...

     GraphicsDevice.SetRenderTarget(null);

     GraphicsDevice.Clear(Color.CornflowerBlue);

     //...
     //Code that draws to the users screen goes here
     //...
}

This should prevent you from rendering in the Update method as suggested by others, which is counter-intuitive in many aspects.

我还不会笑 2024-09-17 14:29:26

当调用 spritebatch.End() 时,对象将写入后备缓冲区,或者在您的情况下写入 tempTarget。要制作纹理,

  • 请更改目标
  • 调用开始
  • 调用所有绘制
  • 结束 spritebatch
  • 将目标设置回 null

然后使用 render2d

When spritebatch.End() is called objects are written to the backbuffer or in your case to tempTarget. To make the texture,

  • change the target
  • call begin
  • call all of the draws
  • end the spritebatch
  • set target back to null

then use the render2d

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文