我遇到了一个问题,我现在似乎无法找到一个不同的解决方案,目前对我目前的想法更有效。
我正在与XNA进行一场小游戏,它将随机生成一条路径,但我不知道如何分离线条上方和下方的纹理,因为这些线条是随机生成的。我希望这张图片可以解释这个问题:
因此随机添加了这些行。我自己的想法是创建许多不同高度的矩形并将它们彼此相邻放置,但我认为这有点低效,它也迫使我的线条足够厚,使得矩形可以更宽一些只有1-2个像素,否则你可能会在线附近看到一些“emtpy”点。
如果有任何不清楚的地方,请随时提出,我会将其添加到问题中,但我认为我的问题应该足够明确。
答案 0 :(得分:2)
你可以替换像素你不需要透明像素:
//extract pixel data from texture
Texture2D topTexture = ...
Color[] topTextureData = new Color[topTexture.Width * topTexture.Height];
topTexture.GetData<Color>(topTextureData);
for(int x = 0; x < topTexture.Width; x++)
{
//depending on how you represent lines, set transparent all the pixels at and below line
//basically, for each x dimension, you find where the line is - you have to
//write the method for getting this y, as I don't know how you represent lines
int lineY = GetLineYAtThisX(x);
//all the pixels at (and below) the line are set transparent
for(int y = lineY; y < topTexture.Height; y++)
{
topTextureData[x + y * topTexture.Width] = Color.Transparent;
}
}
//save this data into another texture, so you don't ruin the original one.
Texture2D maskedTopTexture = new Texture2D(GraphicsDevice, topTexture.Width, topTexture.Height);
maskedTopTexture.SetData<Color>(topTextureData);
你甚至不需要为底部做这个,只需在它上面画一个顶部。
答案 1 :(得分:1)
解决此问题的一个方法是创建一个遮罩纹理,遮挡所选区域之外的纹理。
不会绘制黑色区域。对其他纹理做相反的事情,你会得到你想要的。
我不确定如何使用spritebatch,但是问题的一般解决方案可能使用了掩码。