Using spritebatch causes vertices not to render correctly

I’m a bit confused by this. I’ve created a simple landscape using a simplex noise function and a triangle list of VertexPositionNormalTextures. I use a BasicEffect with default lighting, just to give me a basic shader.

Here is the body of my draw method:

foreach (var pass in effect.CurrentTechnique.Passes)
{
      pass.Apply();
      graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, verts.Length/3);
}

So far, everything looks good.

I want to add a crosshair, and assumed I would simply add a texture using a Spritebatch, so now my draw method is:


foreach (var pass in effect.CurrentTechnique.Passes)
{
      pass.Apply();

      graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, verts.Length/3);
}

spriteBatch.Begin();
spriteBatch.Draw(crosshair, new Vector2(graphics.PreferredBackBufferWidth/2 - crosshair.Width/2, 
    graphics.PreferredBackBufferHeight/2 - crosshair.Height/2), Color.White);
spriteBatch.End();

For some reason, this causes my vertices not to render properly - some of the faces of my blocks are missing:

I’m sure there’s a logical reason for this, and my understanding is that Spritebatch actually renders the texture in 3D, which I guess is interfering with my blocks somehow, but I can’t figure out why only some surfaces are disappearing - it seems to be affecting the normals which determine the direction for the shader to cast light on the blocks because if you rotate the view they display correctly from some sides.

Anyone know what I’m misunderstanding here?

Looks like your culling mode is not correct - SpriteBatch sets all sorts of things, so always make sure to set your desired values (BlendState and CullMode) before rendering your stuff

How do I set the cullmode? - I can see in the method signature for spriteBatch.Begin() I can set the blendstate, but not the cullmode.

device.RasterizerState = RasterizerState.CullCounterClockwise;

it’s worth a try at least - Counter/Clockwise depends on how your geometry is made … CullNone works also but its the slowest

oh, and you have to set the cullmode before YOUR renderings, because YOUR renderings look like the cullmode is wrong (just looks like it)

Unfortunately, that didn’t seem to work. I even tried RasterizerState.CullNone which didn’t work either. I agree it looks like a culling issue, but I can’t figure out what’s going on and googling it hasn’t thrown up many useful answers.

Dunno if this will help you, but I had a similar problem.
I’ve just had a look at my code where I made the following note:

spriteBatch.End();
//NOTE: VERY IMPORTANT to add this after spriteBatch.End…otherwise severe artifacting of models!!!
graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

I now add this to my main Draw routine when using SpriteBatch together with 3D stuff, and have had no such problems since.

1 Like

Fantastic! Thank you @3DRaddict - that solved it! Awesome - I have no idea how I would have found that without your suggestion. Probably saved me hours of searching.