从Texture2D XNA获取每个像素颜色的数组? [英] Get an array of each pixels color from Texture2D XNA?

查看:225
本文介绍了从Texture2D XNA获取每个像素颜色的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像包含一个级别的布局,我想通过从图像中读取每个像素的颜色,并绘制相应的块来加载游戏中的级别。我使用这个代码:

  public void readLevel(string path,GraphicsDevice graphics)
{
/ /获取颜色数组
Texture2D level = Content.Load< Texture2D>(path);
Color [] colors = new Color [level.Width * level.Height];
level.GetData(colors);

//读取每个像素和绘图级别
Vector3 brickRGB = new Vector3(128,128,128);

int placeX = 0;
int placeY = 0;

foreach(颜色像素)
{
SpriteBatch spriteBatch = new SpriteBatch(graphics);
spriteBatch.Begin();

if(pixel == new Color(brickRGB))
{
Texture2D brick = Content.Load< Texture2D>(blocks / brick);
spriteBatch.Draw(brick,new Rectangle(placeX,placeY,40,40),Color.White);
}


if(placeX == 22)
{
placeX = 0;
placeY ++;
}
else
spriteBatch.End();
}
}

但它只是显示一个空白屏幕。帮助将不胜感激。



编辑:问题解决! (阅读htmlcoderexe的答案下面)此外,此代码还有另一个问题,请阅读这里。 / p>

解决方案

您的代码似乎将每个精灵绘制在与前一个像素相差一个像素的位置,但是您的其他参数建议它们为40像素宽。 placeX和placeY将需要乘以你的瓦片的步幅(40)。



此外,在比较颜色的位,你可能有一个问题浮点颜色值(0.0f-1.0f)和字节颜色一起使用。

  new Color(brickRGB)

这转换为:

  new Color(new Vector3(128f,128f,128f))

来自0.0f-1.0f范围的颜色,将其剪切到1f(颜色的浮动输入允许的最大值),并且结束为白色(255,255,255),这不等于目标颜色(128,128,128)



为了解决这个问题,请尝试更改

  Vector3 brickRGB = new Vector3(128,128,128); 

  color brickRGB = new Color(128,128,128); 

和此部分

  if(pixel == new Color(brickRGB))

p>

  if(pixel == brickRGB)

您还需要使用placeX和placeY乘以40来创建绘图矩形,但不要写入变量 - 现在只需使用placeX * 40,稍后将其替换为常量。 p>

I have a image that contains a layout for a level, and I want to load the level in the game by reading each pixels color from the image, and drawing the corresponding block. I am using this code:

public void readLevel(string path, GraphicsDevice graphics)
    {
        //GET AN ARRAY OF COLORS
        Texture2D level = Content.Load<Texture2D>(path);
        Color[] colors = new Color[level.Width * level.Height];
        level.GetData(colors);

        //READ EACH PIXEL AND DRAW LEVEL
        Vector3 brickRGB = new Vector3(128, 128, 128);

        int placeX = 0;
        int placeY = 0;

        foreach (Color pixel in colors)
        {
            SpriteBatch spriteBatch = new SpriteBatch(graphics);
            spriteBatch.Begin();

            if (pixel == new Color(brickRGB))
            {
                Texture2D brick = Content.Load<Texture2D>("blocks/brick");
                spriteBatch.Draw(brick, new Rectangle(placeX, placeY, 40, 40), Color.White);
            }


            if(placeX == 22)
            {
                placeX = 0;
                placeY++;
            }
            else 
            spriteBatch.End();
        }
    }

But it just shows a blank screen. Help would be appreciated!

EDIT: PROBLEM FIXED! (Read htmlcoderexe's answer below) Also, there was another problem with this code, read here.

解决方案

Your code seems to draw each sprite at one pixel offset from the previous, but your other parameter suggests they are 40 pixel wide. placeX and placeY will need to be multiplied by the stride of your tiles (40).

Also, in the bit where you compare colours, you might be having a problem with floating point colour values (0.0f-1.0f) and byte colours being used together.

new Color(brickRGB)

This translates to:

new Color(new Vector3(128f,128f,128f))

So it tries constructing a colour from the 0.0f-1.0f range, clips it down to 1f (the allowed maximum for float input for Color), and you end up with a white colour (255,255,255), which is not equal to your target colour (128,128,128).

To get around this, try changing

 Vector3 brickRGB = new Vector3(128, 128, 128);

to

 Color brickRGB = new Color(128, 128, 128);

and this part

 if (pixel == new Color(brickRGB))

to just

if (pixel == brickRGB)

You will also need to create your drawing rectangle with placeX and placeY multiplied by 40, but do not write to the variable - just use placeX*40 for now and replace it with a constant later.

这篇关于从Texture2D XNA获取每个像素颜色的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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