在Xna / MonoGame中程序生成Texture2D

时间:2015-09-06 23:18:12

标签: c# xna xna-4.0 monogame

我如何使用代码在程序上生成Texture2D? (例如:我想在32x32图像上交替使用黑白像素)

1 个答案:

答案 0 :(得分:5)

您可以使用GraphicsDevice创建新的texutre。

    public static Texture2D CreateTexture(GraphicsDevice device, int width,int height, Func<int,Color> paint)
       {
        //initialize a texture
        Texture2D texture = new Texture2D(device, width, height);

        //the array holds the color for each pixel in the texture
        Color[] data = new Color[width * height];
        for(int pixel=0;pixel<data.Count();pixel++)
        {
            //the function applies the color according to the specified pixel
            data[pixel] = paint(pixel);
        }

        //set the color
        texture.SetData(data);

        return texture;
    }

32x32黑色纹理的示例

 CreateTexture(device,32,32,pixel => Color.Black);