简体   繁体   English

在Xna / MonoGame中程序生成Texture2D

[英]Procedurally generating a Texture2D in Xna/MonoGame

How could I procedurally generate a Texture2D using code? 我怎样才能使用代码在程序上生成Texture2D? (ex: I want alternating pixels to be black and white on a 32x32 image) (例如:我想在32x32图像上交替使用黑白像素)

You can create a new texutre using the GraphicsDevice. 您可以使用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;
    }

Example for 32x32 a black texture 32x32黑色纹理的示例

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

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

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