在 Xna/MonoGame 中程序生成 Texture2D [英] Procedurally generating a Texture2D in Xna/MonoGame

查看:87
本文介绍了在 Xna/MonoGame 中程序生成 Texture2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How could I procedurally generate a Texture2D using code? (ex: I want alternating pixels to be black and white on a 32x32 image)

推荐答案

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

You can create a new texutre using the 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 黑色纹理的示例

Example for 32x32 a black texture

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

这篇关于在 Xna/MonoGame 中程序生成 Texture2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆