Hi!
Currently, I’m drawing all my 3D scene directly in the back buffer. So something like this:
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.CornflowerBlue);
effect.Alpha = 1f;
effect.Projection = camera.Projection;
effect.View = camera.View;
effect.World = camera.World;
foreach (var pass in effect.CurrentTechnique.Passes)
{
for (var i = 0; i < _buffers.Length; i++)
{
effect.Texture = _buffers[i].Texture2D;
pass.Apply();
GraphicsDevice.SetVertexBuffer(_buffers[i].VertexBuffer);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, _buffers[i].VertexCount / 3);
}
}
This results in a scene like this:
Now I’m trying create a UI, and for that I want to first draw to a different render target. The end goal is something like this:
var worldTarget = world.Draw();
var uiTarget = ui.Draw();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(worldTarget,...);
spriteBatch.Draw(uiTarget,...);
spriteBatch.End();
The problem is, before even starting to draw to different targets, calling SpriteBatch.Draw() messes up the existing scene. So if after the code described in 1 I try to call SpriteBatch.Draw()…
...see first code block...
spriteBatch.Begin();
spriteBatch.End();
This results in this:
I have tried drawing text inside the spriteBatch.Draw call, I’ve tried messing with different Begin parameters, I also tried implementing the whole different renderTarget first but the result is always the same, calling spriteBatch.Draw
messes up the scene depth.
This user seems to have had a similar problem (Textures being drawn as if immediate on multiple spritebatch calls.) but he was already using different render targets, which is not my case. Also his solution (creating the secondary render target with the DepthFormat.Depth24Stencil8 option) did not work in my case.
I’m assuming spriteBatch.Draw() is causing some change of the GraphicsDevice state, but I cant figure out what it is or how to fix it.
Any help or hint is appreciated, thanks!