简体   繁体   English

将字节数组转换为texture2D XNA

[英]Converting byte array into texture2D XNA

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. 每个字节表示特定像素的R,G或B的强度值(0-255)。 So for a 640x480 image the byte array is of size 640*480*3 (each pixel has 3 bytes representing it). 因此对于640x480图像,字节数组的大小为640 * 480 * 3(每个像素有3个字节代表它)。

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? 我想知道在XNA中将其绘制到屏幕的最有效方法是什么?

My thoughts are to do the following. 我的想法是做以下事情。

  1. Create a new Texture2d object 创建一个新的Texture2d对象
  2. During the draw() method, loop through the byte array and set the values on the Texture2D object 在draw()方法期间,循环遍历字节数组并在Texture2D对象上设置值
  3. 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? 在draw()方法中进行循环是否有任何缺点? Would it be better to do it during update()? 在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). 我试图在Texture2D上使用setData()以便每次我的字节数组更新时创建一个新的纹理(通常是一帧一次)。 The fps is now below 15 whereas before it was at 60. The code is as follows: 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;
    }

Which is called every draw() cycle. 这称为每个draw()循环。

Surely there has to be a more efficient way of doing this? 当然必须有一种更有效的方法来做到这一点?

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. 根据字节数组更新的方式(可能值得注意),您可以使用使用索引和计数参数的SetData重载来更新纹理的较小片段。

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(T [] data,int startIndex,int elementCount)

public void SetData (int level, Nullable rect, 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 http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.texture2d.setdata.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM