本文介绍了XNA 在运行时在 Texture2D 上绘制/绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好(如果你在早上)

Morning all (if its morning where you are)

我一直环顾四周,没有看到一个令人满意的方法,所以想我会四处打听...

I have been looking around and have not seen a satisfactory method for doing this so thought I would ask around...

理想世界我希望能够生成一个透明的 Texture2D 对象.将此绘制到屏幕上,我希望能够对其进行绘制",即当鼠标左键按下时,光标所在的任何像素都应设置为黑色.在此之后,我将需要能够使用此纹理.

Ideal world I would like to be able to generate a transparent Texture2D object. Drawing this to the screen I would like to be able to "paint" to it, i.e. when the left mouse button is down whatever pixel the cursor is over should be set to black. Following this I would then need to be able to use this texture.

使用纹理是简单的部分,我们可以简单地为绘画"对象创建一个新的 Texture2D 属性,并在 SpriteBatch.Draw 方法中使用它.两个棘手的部分是

Using the texture is the easy part, we can simply make a new Texture2D attribute for a "painting" object and use that in the SpriteBatch.Draw method. The two tricky parts are

  1. 生成指定大小的 texture2D 对象,在代码中填充透明.
  2. 即时编辑该纹理 2D(即能够更改像素颜色)

如果有人对此有任何经验,我们将不胜感激.

If anyone has any experience of these you input would be very much appreciated.

推荐答案

您可以使用 RenderTarget2D (MSDN),它本身就是一个 Texture2D(所以你可以在 SpriteBatch.Draw 中使用它).这允许您以与渲染到屏幕上相同的方式渲染到纹理上.您需要使用 GraphicsDevice.SetRenderTarget (MSDN) 进行设置.

You can either use a RenderTarget2D (MSDN), which is itself a Texture2D (so you can use it in SpriteBatch.Draw). This allows you to render onto a texture in the same way you render onto the screen. You need to use GraphicsDevice.SetRenderTarget (MSDN) to set this up.

或者您可以使用 Texture2D.SetData (MSDN) 直接操作像素.您可以直接构建一个透明的 Texture2D (MSDN).不要忘记Dispose您自己创建的任何纹理或其他资源!

Or you can use Texture2D.SetData (MSDN) to manipulate pixels directly. You can construct a transparent Texture2D directly (MSDN). Don't forget to Dispose of any textures or other resources you create yourself!

这篇关于XNA 在运行时在 Texture2D 上绘制/绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:25