Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Monogame上替换纹理2D上的RGB值?_C#_Monogame_Texture2d - Fatal编程技术网

C# 如何在Monogame上替换纹理2D上的RGB值?

C# 如何在Monogame上替换纹理2D上的RGB值?,c#,monogame,texture2d,C#,Monogame,Texture2d,我正在使用C#和Monogame 3.2。 我目前正在开发一个2D游戏,比如《星际迷航》,我需要让这些区块相互连接,如果没有,则需要一些奇特的边界。 我正在做的是,我有一个覆盖整个32*32图像的纹理,还有一个名为“trimImage”的自定义函数来修剪图像,使其具有精美的边框。 但是,我需要找到一种方法,在纹理2D中的特定像素处设置一个透明像素,这样我就可以创建该边界 看到紫色的污垢,我基本上希望它修剪图像到这样的东西,当其他块连接到它时,它将连接 有人知道如何或至少有更好的方法来实现这种“

我正在使用C#和Monogame 3.2。 我目前正在开发一个2D游戏,比如《星际迷航》,我需要让这些区块相互连接,如果没有,则需要一些奇特的边界。 我正在做的是,我有一个覆盖整个32*32图像的纹理,还有一个名为“trimImage”的自定义函数来修剪图像,使其具有精美的边框。 但是,我需要找到一种方法,在纹理2D中的特定像素处设置一个透明像素,这样我就可以创建该边界

看到紫色的污垢,我基本上希望它修剪图像到这样的东西,当其他块连接到它时,它将连接

有人知道如何或至少有更好的方法来实现这种“边界”效应吗?谢谢


注意:在我的trim类中,我真正拥有的只是一些注释,没有其他内容。

不确定您希望边框看起来像什么,但您可以使用Texture2D.GetData()函数执行如下颜色替换:

private Color TRANSPARENT = Color.Transparent;
private Color BAD_COLOR = Color.Purple;
private const int DEVIATION = 10;

public Texture2D trimImage(Texture2D texture)
{
    /// Get the data from the original texture and place it in an array
    Color[] colorData = new Color[texture.Width * texture.Height];
    texture.GetData<Color>(colorData);

    /// Loop through the array and change the RGB values you choose
    for (int x = 0; x < texture.Width; x++)
    {
        for (int y = 0; y < texture.Height; y++)
        {
            Color pixel = colorData[x * texture.Height + y];
            /// Check if the color is within the range
            if (MathHelper.Distance(pixel.R, BAD_COLOR.R) < DEVIATION &&
                MathHelper.Distance(pixel.G, BAD_COLOR.G) < DEVIATION &&
                MathHelper.Distance(pixel.B, BAD_COLOR.B) < DEVIATION &&
                pixel.A != 0f)
            {
                /// Make that color transparent
                pixel = TRANSPARENT;
            }
        }
    }

    /// Put the color array into the new texture and return it
    texture.SetData<Color>(colorData);
    return texture;
}
private Color TRANSPARENT=Color.TRANSPARENT;
私密颜色坏_Color=Color.Purple;
私有常数的内部偏差=10;
公共纹理2D修剪图像(纹理2D纹理)
{
///从原始纹理获取数据并将其放置在数组中
Color[]colorData=新颜色[texture.Width*texture.Height];
GetData(colorData);
///在阵列中循环并更改所选的RGB值
对于(int x=0;x

能够创建不同的边框只需更改循环参数并选择适当的像素即可。希望它有帮助

不确定这是否是您正在尝试的,但它可能会对您有所帮助: