XNA 在运行时在 Texture2D 上绘制/绘制
早上好(如果你所在的地方是早上)
我一直在环顾四周,没有找到令人满意的方法,所以我想我会四处询问......
理想的世界我希望能够生成一个透明的Texture2D对象。将其绘制到屏幕上我希望能够“绘制”它,即当鼠标左键按下时,光标所在的任何像素都应设置为黑色。接下来我需要能够使用这个纹理。
使用纹理是最简单的部分,我们可以简单地为“绘画”对象创建一个新的Texture2D属性,并在SpriteBatch.Draw方法中使用它。两个棘手的部分是
- 生成指定大小的texture2D对象,并在代码中填充透明度。
- 动态编辑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...
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.
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
- Generating a texture2D object of a specified size, filled with transparency in code.
- Editing that texture2D on the fly (i.e. being able to alter pixel colours)
If anyone has any experience of these you input would be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
RenderTarget2D
(MSDN ),它本身就是一个Texture2D
(因此您可以在SpriteBatch.Draw
中使用它)。这允许您以与渲染到屏幕上相同的方式渲染到纹理上。您需要使用GraphicsDevice.SetRenderTarget
(MSDN) 进行设置。或者您可以使用
Texture2D.SetData
(MSDN) 直接操作像素。您可以直接构建透明的Texture2D
(MSDN)。不要忘记处置
您自己创建的任何纹理或其他资源!You can either use a
RenderTarget2D
(MSDN), which is itself aTexture2D
(so you can use it inSpriteBatch.Draw
). This allows you to render onto a texture in the same way you render onto the screen. You need to useGraphicsDevice.SetRenderTarget
(MSDN) to set this up.Or you can use
Texture2D.SetData
(MSDN) to manipulate pixels directly. You can construct a transparentTexture2D
directly (MSDN). Don't forget toDispose
of any textures or other resources you create yourself!