在Xna/MonoGame中进行Texture2D的程序化生成

9
我该如何使用代码生成纹理(例如:我想在一个32x32的图像上交替显示黑白像素)?
1个回答

12

您可以使用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);

那么对于一个32x32的纹理,它将被保存在一个1024色长的数组中? - user4901669

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接