将字节数组转换为texture2D XNA(Converting byte array into texture2D XNA)

编程入门 行业动态 更新时间:2024-10-27 02:21:15
将字节数组转换为texture2D XNA(Converting byte array into texture2D XNA)

我有一个表示图像的字节数组。

每个字节表示特定像素的R,G或B的强度值(0-255)。 因此对于640x480图像,字节数组的大小为640 * 480 * 3(每个像素有3个字节代表它)。

字节按像素的顺序排列。 例如:

image[0] = the R value of x=0 y=0 image[1] = the G value of x=0 y=0 image[2] = the B value of x=0 y=0 image[3] = the R value of x=1 y=0

等等。

我想知道在XNA中将其绘制到屏幕的最有效方法是什么?

我的想法是做以下事情。

创建一个新的Texture2d对象 在draw()方法期间,循环遍历字节数组并在Texture2D对象上设置值 将此填充的对象绘制到屏幕上。

这是最快的方法吗? 如果更有效,我还可以以不同的顺序/格式存储字节数组。 在draw()方法中进行循环是否有任何缺点? 在update()期间这样做会更好吗?

编辑:

我试图在Texture2D上使用setData()以便每次我的字节数组更新时创建一个新的纹理(通常是一帧一次)。 fps现在低于15,而在60之前。代码如下:

public static Texture2D getImageFrame(GraphicsDevice gd) { if (cameraTex == null) { cameraTex = new Texture2D(gd, 640, 480, false, SurfaceFormat.Color); } cameraTex.SetData(imageFrame); return cameraTex; }

这称为每个draw()循环。

当然必须有一个更有效的方法来做到这一点?

I have a byte array representing an image.

Each byte represents an intensity value (0-255) of either R,G or B of a particular pixel. So for a 640x480 image the byte array is of size 640*480*3 (each pixel has 3 bytes representing it).

The bytes are in order of pixels. For instance:

image[0] = the R value of x=0 y=0 image[1] = the G value of x=0 y=0 image[2] = the B value of x=0 y=0 image[3] = the R value of x=1 y=0

and so on.

I am wondering what the most efficient way to draw this to screen in XNA is?

My thoughts are to do the following.

Create a new Texture2d object During the draw() method, loop through the byte array and set the values on the Texture2D object Draw this filled in object to the screen.

Is this the fastest way to do this? I can also store the byte array in a different order/format if it is more efficient. Is there any downside to doing the looping during the draw() method? Would it be better to do it during update()?

EDIT:

I have attempted to use setData() on a Texture2D inorder to create a new texture every time my byte array updates (usually once a frame). The fps is now below 15 whereas before it was at 60. The code is as follows:

public static Texture2D getImageFrame(GraphicsDevice gd) { if (cameraTex == null) { cameraTex = new Texture2D(gd, 640, 480, false, SurfaceFormat.Color); } cameraTex.SetData(imageFrame); return cameraTex; }

Which is called every draw() cycle.

Surely there has to be a more efficient way of doing this?

最满意答案

根据字节数组更新的方式(可能值得注意),您可以使用使用索引和计数参数的SetData重载更新纹理的较小片段。

显然,您需要添加一些跟踪像素区域最后更改的方法才能使其工作(再次取决于程序的结构)。 在任何情况下,我最近在另一个纹理上绘制血液飞溅纹理也有类似的性能影响,并设法使用此方法最小化命中。

public void SetData(T [] data,int startIndex,int elementCount)

public void SetData(int level,Nullable rect,T [] data,int startIndex,int elementCount)

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.texture2d.setdata.aspx

Depending on the way in which the byte array is updated (may be worth noting), you can probably update a smaller segment of a texture using the SetData overloads that use index and count parameters.

Obviously you'll need to add some way of tracking what regions of pixels have last changed for this to work (again depends on the structure of your program). In any case, I had a similar performance hit with drawing a blood splatter texture on another texture recently, and managed to minimize the hit using this method.

public void SetData (T[] data, int startIndex, int elementCount)

public void SetData (int level, Nullable rect, T[] data, int startIndex, int elementCount)

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.texture2d.setdata.aspx

更多推荐

本文发布于:2023-07-15 05:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   字节   texture2D   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!