在Xna/MonoGame中程序生成Texture2D

本文关键字:程序生成 Texture2D MonoGame Xna | 更新日期: 2023-09-27 18:11:40

如何使用代码生成Texture2D ?(例如:我希望在一个32 × 32的图像上交替像素为黑色和白色)

在Xna/MonoGame中程序生成Texture2D

你可以使用GraphicsDevice创建一个新的纹理。

    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);