我从目录加载纹理时遇到了一些问题。也许先编码:
private Texture2D LoadTextureStream(string filePath)
{
Texture2D file = null;
RenderTarget2D result = null;
try
{
using (System.IO.Stream titleStream = TitleContainer.OpenStream(filePath))
{
file = Texture2D.FromStream(GraphicsDevice, titleStream);
}
}
catch
{
throw new System.IO.FileLoadException("Cannot load '" + filePath + "' file!");
}
PresentationParameters pp = GraphicsDevice.PresentationParameters;
//Setup a render target to hold our final texture which will have premulitplied alpha values
result = new RenderTarget2D(GraphicsDevice, file.Width, file.Height, true, pp.BackBufferFormat, pp.DepthStencilFormat);
GraphicsDevice.SetRenderTarget(result);
GraphicsDevice.Clear(Color.Black);
//Multiply each color by the source alpha, and write in just the color values into the final texture
BlendState blendColor = new BlendState();
blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
blendColor.AlphaDestinationBlend = Blend.Zero;
blendColor.ColorDestinationBlend = Blend.Zero;
blendColor.AlphaSourceBlend = Blend.SourceAlpha;
blendColor.ColorSourceBlend = Blend.SourceAlpha;
SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice);
spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
//Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
BlendState blendAlpha = new BlendState();
blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
blendAlpha.AlphaDestinationBlend = Blend.Zero;
blendAlpha.ColorDestinationBlend = Blend.Zero;
blendAlpha.AlphaSourceBlend = Blend.One;
blendAlpha.ColorSourceBlend = Blend.One;
spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
//Release the GPU back to drawing to the screen
GraphicsDevice.SetRenderTarget(null);
return result as Texture2D;
}
首先,纹理是从流中加载的。然后,我更改了一些混合选项以达到行为,例如在 ContentPipeline 加载的纹理中。不幸的是,以这种方式获得的纹理会在游戏窗口最小化后消失。我读了一些关于这个问题的东西,很多事情表明 RenderTarget2D 是错误的,因为渲染目标毕竟设置为 null。我应该怎么做才能永久保留我的纹理?
编辑 - 固定代码
好的,我使用了第四个选项,它工作得很好。这是固定代码:
private Texture2D LoadTextureStream(string filePath)
{
Texture2D file = null;
Texture2D resultTexture;
RenderTarget2D result = null;
try
{
using (System.IO.Stream titleStream = TitleContainer.OpenStream(filePath))
{
file = Texture2D.FromStream(GraphicsDevice, titleStream);
}
}
catch
{
throw new System.IO.FileLoadException("Cannot load '" + filePath + "' file!");
}
PresentationParameters pp = GraphicsDevice.PresentationParameters;
//Setup a render target to hold our final texture which will have premulitplied alpha values
result = new RenderTarget2D(GraphicsDevice, file.Width, file.Height, true, pp.BackBufferFormat, pp.DepthStencilFormat);
GraphicsDevice.SetRenderTarget(result);
GraphicsDevice.Clear(Color.Black);
//Multiply each color by the source alpha, and write in just the color values into the final texture
BlendState blendColor = new BlendState();
blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
blendColor.AlphaDestinationBlend = Blend.Zero;
blendColor.ColorDestinationBlend = Blend.Zero;
blendColor.AlphaSourceBlend = Blend.SourceAlpha;
blendColor.ColorSourceBlend = Blend.SourceAlpha;
SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice);
spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
//Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
BlendState blendAlpha = new BlendState();
blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
blendAlpha.AlphaDestinationBlend = Blend.Zero;
blendAlpha.ColorDestinationBlend = Blend.Zero;
blendAlpha.AlphaSourceBlend = Blend.One;
blendAlpha.ColorSourceBlend = Blend.One;
spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
spriteBatch.Draw(file, file.Bounds, Color.White);
spriteBatch.End();
//Release the GPU back to drawing to the screen
GraphicsDevice.SetRenderTarget(null);
resultTexture = new Texture2D(GraphicsDevice, result.Width, result.Height);
Color[] data = new Color[result.Height * result.Width];
Color[] textureColor = new Color[result.Height * result.Width];
result.GetData<Color>(textureColor);
for (int i = 0; i < result.Height; i++)
{
for (int j = 0; j < result.Width; j++)
{
data[j + i * result.Width] = textureColor[j + i * result.Width];
}
}
resultTexture.SetData(data);
return resultTexture;
}
非常感谢您的帮助!