简体   繁体   English

从Texture2D XNA获取每个像素颜色的数组?

[英]Get an array of each pixels color from Texture2D XNA?

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. (请阅读下面的htmlcoderexe答案)此外,此代码还有另一个问题,请在此处阅读

Your code seems to draw each sprite at one pixel offset from the previous, but your other parameter suggests they are 40 pixel wide. 您的代码似乎将每个精灵绘制为与上一个精灵偏移一个像素,但是您的其他参数建议它们的宽度为40像素。 placeX and placeY will need to be multiplied by the stride of your tiles (40). placeX和placeY将需要乘以图块的步幅(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. 另外,在比较颜色的位中,可能会出现浮点颜色值(0.0f-1.0f)和字节颜色一起使用的问题。

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). 因此,它尝试构建0.0f-1.0f范围内的颜色,将其裁剪为1f(Color的float输入允许的最大值),最后得到白色(255,255,255),这不等于您的目标颜色(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. 您还需要使用placeX和placeY乘以40来创建绘图矩形,但不要写入变量-暂时仅使用placeX * 40,稍后再将其替换为常量。

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

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