code
float startX = 100f;
float startY = 100f;
const float layerDepth1 = 0.2f;
vertices1 =
[
new VertexPositionColor(new Vector3(startX, startY, layerDepth1), Color.Green),
new VertexPositionColor(new Vector3(startX + 50, startY + 50, layerDepth1), Color.Green),
new VertexPositionColor(new Vector3(startX - 50, startY + 50, layerDepth1), Color.Green)
];
startX += 25f;
startY += 25f;
const float layerDepth2 = 0.1f;
vertices2 =
[
new VertexPositionColor(new Vector3(startX, startY, layerDepth2), Color.Red),
new VertexPositionColor(new Vector3(startX + 50, startY + 50, layerDepth2), Color.Red),
new VertexPositionColor(new Vector3(startX - 50, startY + 50, layerDepth2), Color.Red)
];
basicEffect = new BasicEffect(GraphicsDevice)
{
VertexColorEnabled = true,
LightingEnabled = false,
World = Matrix.Identity,
View = Matrix.Identity,
Projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, -1)
};
code
foreach (var pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices1, 0, 1);
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices2, 0, 1);
}
base.Draw(gameTime);
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace DrawOrderTest
{
public class Game1 : Game
{
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
private VertexPositionColor[] vertices1;
private VertexPositionColor[] vertices2;
private BasicEffect basicEffect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
float startX = 100f;
float startY = 100f;
const float layerDepth1 = -0.2f; // 使用负值
vertices1 =
[
new VertexPositionColor(new Vector3(startX, startY, layerDepth1), Color.Green),
new VertexPositionColor(new Vector3(startX + 50, startY + 50, layerDepth1), Color.Green),
new VertexPositionColor(new Vector3(startX - 50, startY + 50, layerDepth1), Color.Green)
];
startX += 25f;
startY += 25f;
const float layerDepth2 = -0.1f; // 使用负值
vertices2 =
[
new VertexPositionColor(new Vector3(startX, startY, layerDepth2), Color.Red),
new VertexPositionColor(new Vector3(startX + 50, startY + 50, layerDepth2), Color.Red),
new VertexPositionColor(new Vector3(startX - 50, startY + 50, layerDepth2), Color.Red)
];
basicEffect = new BasicEffect(GraphicsDevice)
{
VertexColorEnabled = true,
LightingEnabled = false,
World = Matrix.Identity,
View = Matrix.Identity,
Projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1) // zFarPlane 为 1
};
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
foreach (var pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices1, 0, 1);
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices2, 0, 1);
}
base.Draw(gameTime);
}
}
}