複数のスプライトからランダムな島を作成する XNA ゲームを作成しています。それらを別のスレッドで作成し、RenderTarget2D を使用して単一のテクスチャにコンパイルします。
RenderTarget2D を作成するには、グラフィック デバイスが必要です。自動的に作成されたグラフィックス デバイスを使用すると、メイン ゲーム スレッドの描画呼び出しが競合することを除いて、ほとんどの場合問題なく動作します。グラフィックス デバイスで lock() を使用するとちらつきが発生し、それでもテクスチャが正しく作成されないことがあります。
独自のグラフィックス デバイスを作成すると、競合は発生しませんが、島が正しくレンダリングされず、純粋な白黒になります。なぜこれが起こるのか分かりません。基本的に、黒/白の代わりに同じ結果を得ることができる 2 番目のグラフィックス デバイスを作成する方法が必要です。誰でもアイデアはありますか?
TextureBuilder 専用の 2 つ目のグラフィックス デバイスを作成するために使用しているコードは次のとおりです。
var presParams = game.GraphicsDevice.PresentationParameters.Clone();
// Configure parameters for secondary graphics device
GraphicsDevice2 = new GraphicsDevice(game.GraphicsDevice.Adapter, GraphicsProfile.HiDef, presParams);
島を単一のテクスチャにレンダリングするために使用しているコードは次のとおりです。
public IslandTextureBuilder(List<obj_Island> islands, List<obj_IslandDecor> decorations, SeaGame game, Vector2 TL, Vector2 BR, int width, int height)
{
gDevice = game.Game.GraphicsDevice; //default graphics
//gDevice = game.GraphicsDevice2 //created graphics
render = new RenderTarget2D(gDevice, width, height, false, SurfaceFormat.Color, DepthFormat.None);
this.islands = islands;
this.decorations = decorations;
this.game = game;
this.width = width;
this.height = height;
this.TL = TL; //top left coordinate
this.BR = BR; //bottom right coordinate
}
public Texture2D getTexture()
{
lock (gDevice)
{
//Set render target. Clear the screen.
gDevice.SetRenderTarget(render);
gDevice.Clear(Color.Transparent);
//Point camera at the island
Camera cam = new Camera(gDevice.Viewport);
cam.Position = TL;
cam.Update();
//Draw all of the textures to render
SpriteBatch batch = new SpriteBatch(gDevice);
batch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.Transform);
{
foreach (obj_Island island in islands)
{
island.Draw(batch);
}
foreach (obj_IslandDecor decor in decorations)
{
decor.Draw(batch);
}
}
batch.End();
//Clear render target
gDevice.SetRenderTarget(null);
//Copy to texture2D for permanant storage
Texture2D texture = new Texture2D(gDevice, render.Width, render.Height);
Color[] color = new Color[render.Width * render.Height];
render.GetData<Color>(color);
texture.SetData<Color>(color);
Console.WriteLine("done");
return texture;
}
これは、透明な背景で何が起こるべきかです(デフォルトのデバイスを使用すると通常そうなります) http://i110.photobucket.com/albums/n81/taumonkey/GoodIsland.png
これは、デフォルトのデバイスが競合し、メイン スレッドが Clear() を呼び出すことができた場合に発生します (それもロックされているにもかかわらず)。
自分のグラフィック デバイス http://i110.photobucket.com/albums/n81/taumonkey/BadIsland.pngを使用すると、次のようになります。
提供されたヘルプに事前に感謝します!