【问题标题】:How to draw characters to a Texture2D?如何将字符绘制到 Texture2D?
【发布时间】:2011-11-13 17:17:20
【问题描述】:

在 C# XNA 中,如何将单个字符绘制到 Texture2D 而不是 sprite 批处理上?我希望这样做是为了用字符 char\background 数据填充bool[,] 以分析其形状。

【问题讨论】:

  • 我看不出这与 DrawString() 有什么关系。
  • 我没有提到 DrawString(),反正我不知道如何使用它在 Texture2D 上绘图。
  • 它在标题中,在我编辑之前。

标签: c# xna rendering


【解决方案1】:

您可以使用渲染目标。基本思想是,不是将文本渲染到后台缓冲区,而是渲染到单独的缓冲区,然后它可以为您提供 Texture2D。

请看这里:http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertarget(v=xnagamestudio.31).aspx

提问者编辑:

经许可,我已添加到此答案中。在撰写本文时,MSDN 上的信息已经过时,看起来比需要的更复杂,因此我编写了自己的示例来说明如何做到这一点。

完成此操作的类可能必须从 IDisposable 继承并实现什么都不做的 void Dispose()。

PresentationParameters pp = graphicsDevice.PresentationParameters;
byte width = 20, height = 20; // for example

// pp.BackBufferWidth, pp.BackBufferHeight // for auto x and y sizes
RenderTarget2D render_target = new RenderTarget2D(graphicsDevice,
width, height, false, pp.BackBufferFormat, pp.DepthStencilFormat,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);

graphicsDevice.SetRenderTarget(render_target);
graphicsDevice.Clear(...); // possibly optional
spriteBatch.Begin();
// draw to the spriteBatch
spriteBatch.End();
graphicsDevice.SetRenderTarget(null); // Otherwise the SpriteBatch can't
// be used as a texture, this may also need to be done before using the
// SpriteBatch normally again to render to the screen.

// render_target can now be used as a Texture2D

此时这可能有用。 http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Texture_to_Colors.php

【讨论】:

  • 我卡在第 1 步,GfxComponent 来自哪里?
  • 看起来像 GfxComponent.CreateRenderTarget(GraphicsDevice, 1, SurfaceFormat.Single);在该部分的正下方定义。即,GfxComponent 是他们在该页面上定义的一个类。
  • 泰。我的 GrapicsDevice 的 SetRenderTarget 不喜欢(0,shadowRenderTarget 的实例)作为参数,但是当我取出 0 时就可以了。我继续。
  • 保护您“如果您签出第 3 步”。我创建和配置的 shadowRenderTarget 没有像 SpriteBatch 那样的 drawString,如何在其上绘制字符?还有一个错误,我的继承 IDisposable 的类没有实现 Dispose(),我可以使用空方法吗?在这种情况下返回类型是什么?
  • 自从我使用 spritebatch 已经有一段时间了,但你应该可以像往常一样使用它...begin()、draw、end(),它们有 drawScene() .是的,您可以只提供一个空的 Dispose() - 假设您没有任何需要处理的非托管资源。它应该返回 void。
猜你喜欢
  • 1970-01-01
  • 2011-10-09
  • 2018-07-26
  • 1970-01-01
  • 2013-12-07
  • 2016-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多