> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/ordering-layers-and-popups/mixing-gum-and-3d.md).

# Mixing Gum and 3D

## Introduction

Gum draw calls can be performed at any point in your game's Draw method. You can have Gum draw above or below 3D objects by moving the `GumUI.Draw()` call.

## XNA-Likes (MonoGame, FNA, KNI) and Draw

The `GumUI.Draw()` call internally uses a `SpriteBatch` to draw all UI. SpriteBatch can modify render states in a way that can break 3D rendering, such as by drawing Model instances, or the various G`raphicsDevice` draw calls.

You can verify that these state changes are breaking your 3D rendering by temporarily commenting-out `GumUI.Draw()`. If this is in fact the problem, you can restore the GraphicsDevice settings after the draw.

An older article by Shawn Hargreaves states:

> So exactly which states does SpriteBatch change? Here's the complete list:
>
> ```
>     GraphicsDevice.BlendState = BlendState.AlphaBlend;
>     GraphicsDevice.DepthStencilState = DepthStencilState.None;
>     GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
>     GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
> ```
>
> SpriteBatch also modifies the vertex buffer, index buffer, and applies its own effect onto the GraphicsDevice.
>
> Before you draw anything in 3D you will probably want to reset these states:
>
> ```
>     GraphicsDevice.BlendState = BlendState.Opaque;
>     GraphicsDevice.DepthStencilState = DepthStencilState.Default;
> ```
>
> Depending on your 3D content, you may also want to set:
>
> ```
>     GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
> ```

For more information see this article: <https://shawnhargreaves.com/blog/spritebatch-and-renderstates-in-xna-game-studio-4-0.html>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/gum/code/ordering-layers-and-popups/mixing-gum-and-3d.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
