【问题标题】:How to Replace a RGB Value on a Texture2D on Monogame?如何在 Monogame 上替换 Texture2D 上的 RGB 值?
【发布时间】:2014-11-22 17:18:42
【问题描述】:
我正在使用 C# 和 Monogame 3.2。
我目前正在开发一款 2D 游戏,比如 Starbound,我需要让这些块相互连接,如果没有,则有一些花哨的边框。
我正在做的是我有一个覆盖整个 32*32 图像的纹理,以及一个名为“trimImage”的自定义函数来修剪图像,使其具有精美的边框。
但是,我需要找到一种方法在 Texture2D 中的特定像素处设置透明像素,这样我才能制作该边框。
http://i.imgur.com/dvh6sI6.png
看到紫色的泥土,我基本上希望它把图像修剪成这样,当其他块连接到它时它会连接。
有谁知道如何或至少有更好的方法来实现这种“边界”效果?谢谢。
注意:在我的修剪课程中,我确实只有一些 cmets,没有别的。
【问题讨论】:
标签:
c#
monogame
texture2d
【解决方案1】:
不确定您希望边框看起来像什么,但您可以使用 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;
}
能够制作不同的边框只需更改循环参数并选择适当的像素。希望对你有帮助