如何创建图像并保存以供以后在XNA中作为纹理绘制? [英] How do I create an image and save it for later to draw as texture in XNA?

查看:139
本文介绍了如何创建图像并保存以供以后在XNA中作为纹理绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,我需要绘制一个由正方形大小制成的圆圈(圆形由正方形组成)。我可以在每帧中以锯齿状圆形的形式绘制单色方形纹理,但它消耗了大量资源。我想做的就是在内存中的某个地方画一次,然后保存以画出每一帧。

In my game I need to draw a circle made of squares the sizes of a game tile (circle is made of squares). I could just draw monochrome square textures in form of a jagged circle each frame, but it consumes a significant amount of resources. What I'd like to do is to draw it somewhere in the memory just once and save to draw each frame after that.

我可以自己画一下这个圆圈并使用它作为一个准备好的纹理,但我的圆圈并不总是一样的。它在整个游戏中有不同的大小(并且它不是一半的时间圈,但我已经得到了说明在哪里绘制的算法),所以它必须以编程方式绘制。

I could simply draw said circle myself and use it as a ready texture, but my circle is not always the same. It has different size throughout the game (and it's not really a circle half of the time, but I've got the algo that says where to draw), so it has to be drawn programmatically.

推荐答案

首先将圆圈渲染为自定义 RenderTarget2D 。您可以像这样设置自定义渲染目标:

First you render the circle to a custom RenderTarget2D. You can set a custom render target like this:

GraphicsDevice.SetRenderTarget(renderTarget);

将圆圈渲染到渲染目标后,将其转换为 Texture2D 像这样:

After rendering your circle to the render target cast it to a Texture2D like this:

texture = (Texture2D)renderTarget;

阅读更多: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Render_to_texture.php

这篇关于如何创建图像并保存以供以后在XNA中作为纹理绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆