Whilst creating my 3D dungeon crawler I came into a few issues. These were all nothing to do with Monogame itself. It is a fantastic framework. The issues were with how to order my drawing calls and render to multiple RenderTargets.
The above image shows my incredibly basic 3d dungeon crawler. I wanted to have a map (top-left) and I didn’t want to have to create extra graphics. So I decided to use another render target using another camera.
To do this I needed another RenderTarget2D (essentially a framebuffer) and draw the view from an camera overhead. There were a few things, though, that needed to be considerered:
- When you switch render targets, monogame will clear it without clear having to be called. What this means, is that all drawing done previously is wiped.
- Setting the rendertarget to null means that the default backbuffer will be set.
- Any rasterizer states or stencil states will be preserved
Another issue I had was with this was drawing the GUI using SpriteBatch. What wasn’t plainly clear is that the SpriteBatch will change the DepthStencilState of the current GraphicsDevice. What this mean was, I would draw in 3D and then notice that my geometry wasn’t being clipped or draw in the correct order (depth buffering). Objects that should have been behind were being drawn in front. To get around this, simply reset the DepthStencilState before drawing 3D geometry.
The image above demonstrates how to avoid the issues discussed. The final thing to watch out for is Alphablending. Make sure to draw objects with alpha channels last if possible else, those areas will be drawn over.
I decided for ease of development that my enemies would be sprites. They are drawn no problems with no alphablending issues as they are drawn last. The door behind though was drawn before the next room and so shows black between the bars instead of a clear view to the next room.