我有我從內容管道加載的Texture2D
。這是工作正常,但只要我嘗試在一個完全不同的Texture2D
使用SetData
都在我的遊戲中的紋理去完全是黑色的:Texture2D變黑
這是我HUDMeter類,我想要的類
Texture2D colorGrad = Content.Load<Texture2D>(GradientAsset);
Color[,] pixels = new Color[colorGrad.Width, colorGrad.Height];
Color[] pixels1D = new Color[colorGrad.Width * colorGrad.Height];
pixels = GetRedChannel(colorGrad);
pixels1D = Color2DToColor1D(pixels, colorGrad.Width);
System.Diagnostics.Debug.WriteLine(pixels[32,32]);
Gradient = colorGrad;
Gradient.SetData<Color>(pixels1D);
這些是使用Riemers教程
是正當紅protected Color[,] GetRedChannel(Texture2D texture)
{
Color[,] pixels = TextureTo2DArray(texture);
Color[,] output = new Color[texture.Width, texture.Height];
for (int x = 0; x < texture.Width; x++)
{
for (int y = 0; y < texture.Height; y++)
{
output[x,y] = new Color(pixels[x,y].G, 0, 0);
}
}
return output;
}
protected Color[,] TextureTo2DArray(Texture2D texture)
{
Color[] colors1D = new Color[texture.Width * texture.Height];
texture.GetData(colors1D);
Color[,] colors2D = new Color[texture.Width, texture.Height];
for (int x = 0; x < texture.Width; x++)
for (int y = 0; y < texture.Height; y++)
colors2D[x, y] = colors1D[x + y * texture.Width];
return colors2D;
}
private Color[] Color2DToColor1D (Color[,] colors, int width)
{
Color[] output = new Color[colors.Length];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < colors.Length/width; y++)
{
output[x + y * width] = colors[x % width, y % (colors.Length/width)];
}
}
return output;
}
這裏是繪製精靈的代碼,這工作得很好,是我怎麼總是吸引精靈:
batch.Draw(meter.Gradient, new Vector2(X, Y), Color.White);
更新:
其實我已經發現,精靈,唐」 t使用相同的文件不是黑色的。 Texture2D.SetData <>()實際上是否改變了文件本身?那有什麼用?
更新:
我只是試圖利用阿爾法以及RGB和它的工作。我認爲其中一種轉換方法存在問題。
代碼在哪裏? – ChrisF
哦,對,可能有點困難。它分佈在3個類,但我可以發佈我使用SetData的方法(可能是錯誤的方式) – annonymously
這將值得發佈*其中*這也被稱爲。 – ChrisF