ディレクトリからテクスチャをロードする際に問題が発生しました。たぶん最初にコードを書く:
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によってロードされたテクスチャなどの動作に到達します。残念ながら、この方法で取得されたテクスチャは、ゲームウィンドウを最小化すると消えます。私はこの問題について何かを読みましたが、レンダリングターゲットが結局nullに設定されているため、RenderTarget2Dに障害があることを多くのことが示しています。テクスチャを永続的に保持するにはどうすればよいですか?
編集-修正されたコード
OK、4番目のオプションを使用しましたが、完全に機能します。修正されたコードは次のとおりです。
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;
}
助けてくれてありがとう!