I’m trying to use Texture2D objects and VertexPositionColor objects with DrawUserPrimitives at the same time in my draw calls, but my issue is that no matter in which order I call these, the Texture2D (blue square) is always on top. I can only really control the draw order between VertexPositionColor objects and Texture2D objects separately, but I can’t do it when I mix them. Why? The SpriteSortMode has also no effect. Here’s the sample code:
protected override void Draw(GameTime gameTime) { this.GraphicsDevice.Clear(Color.Transparent); GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; _basicEffect = new BasicEffect(this.GraphicsDevice); _basicEffect.World = Matrix.CreateTranslation(-this.GraphicsDevice.Viewport.Width / 2, -this.GraphicsDevice.Viewport.Height / 2, 0); _basicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, -1), new Vector3(0, 0, 0), new Vector3(0, -1, 0)); _basicEffect.Projection = Matrix.CreateOrthographic((float)GraphicsDevice.Viewport.Width, (float)GraphicsDevice.Viewport.Height, 1.0f, 100.0f); _basicEffect.VertexColorEnabled = true; GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
// setting up blue square
var blueSquare = new Texture2D(this.GraphicsDevice, 100, 100);
var blueColorData = new Color[100 * 100];
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
blueColorData[i * 100 + j] = Color.Blue;
}
}
blueSquare.SetData(blueColorData);
// setting up red square
VertexPositionColor[] redSquare = new VertexPositionColor[6];
redSquare[0].Position = new Vector3(0, 0, 0f);
redSquare[0].Color = Color.Red;
redSquare[1].Position = new Vector3(0, 100, 0f);
redSquare[1].Color = Color.Red;
redSquare[2].Position = new Vector3(100f, 100f, 0f);
redSquare[2].Color = Color.Red;
redSquare[3].Position = new Vector3(100, 100f, 0f);
redSquare[3].Color = Color.Red;
redSquare[4].Position = new Vector3(100, 0f, 0f);
redSquare[4].Color = Color.Red;
redSquare[5].Position = new Vector3(0f, 0f, 0f);
redSquare[5].Color = Color.Red;
bool firstBlueThenRed = true;
if (firstBlueThenRed)
{
var spriteBatch = new SpriteBatch(this.GraphicsDevice);
spriteBatch.Begin();
spriteBatch.Draw(blueSquare, new Vector2(50, 50), Color.White);
foreach (EffectPass pass in _basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
this.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, redSquare, 0, 2);
}
// we put the "End" here because it flushes everything to the screen
spriteBatch.End();
}
else
{
foreach (EffectPass pass in _basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
this.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, redSquare, 0, 2);
}
var spriteBatch = new SpriteBatch(this.GraphicsDevice);
spriteBatch.Begin();
spriteBatch.Draw(blueSquare, new Vector2(50, 50), Color.White);
spriteBatch.End();
}
base.Draw(gameTime);
}`
So how can I achieve that the DrawUserPrimitives draws the red square on top of the blue one which is a texture in a sprite batch?