XNA 2D - 如何设置深度缓冲区的深度。不使用SpriteBatch

时间:2017-10-15 17:00:51

标签: c# graphics xna depth-buffer

如何实际设置我想要绘制的内容的深度?我没有使用SpriteBatch。

My Sprite LoadContent如下所示:

public void LoadContent(ContentManager contentMananager)
    {
        myTexture = contentMananager.Load<Texture2D>("BarrelTrap");

        vertices[0] = new VertexPositionColorTexture()
        {
            TextureCoordinate = new Vector2(0, 0),
            Position = new Vector3(Position.X, Position.Y, 0),
            Color = Color.White
        };

        vertices[1] = new VertexPositionColorTexture()
        {
            TextureCoordinate = new Vector2(1, 0),
            Position = new Vector3(Position.X + myTexture.Width, Position.Y, 0),
            Color = Color.White
        };

        vertices[2] = new VertexPositionColorTexture()
        {
            TextureCoordinate = new Vector2(1, 1),
            Position = new Vector3(Position.X + myTexture.Width, Position.Y + myTexture.Height, 0),
            Color = Color.White
        };

        vertices[3] = new VertexPositionColorTexture()
        {
            TextureCoordinate = new Vector2(1, 1),
            Position = new Vector3(Position.X + myTexture.Width, Position.Y + myTexture.Height, 0),
            Color = Color.White
        };

        vertices[4] = new VertexPositionColorTexture()
        {
            TextureCoordinate = new Vector2(0, 1),
            Position = new Vector3(Position.X, Position.Y + myTexture.Height, 0),
            Color = Color.White
        };

        vertices[5] = new VertexPositionColorTexture()
        {
            TextureCoordinate = new Vector2(0, 0),
            Position = new Vector3(Position.X, Position.Y, 0),
            Color = Color.White
        };
    }

我的Sprite绘制代码是这样的:

public void Draw(GraphicsDevice graphics, BasicEffect effect)
    {
        effect.Texture = Texture;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {               
            pass.Apply();                
            graphics.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 2);
        }
    }

主游戏类中的绘制代码如下所示:

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(null);
        GraphicsDevice.Clear(Color.CornflowerBlue);
        Sprite.Draw(GraphicsDevice, BasicEffect);

        base.Draw(gameTime);
    }

将sprite绘制到backbuffer就好了。我知道如何创建和设置DepthStencilState,但是当我绘制它时,我无法找到如何实际设置精灵的深度。我希望能够在绘制时设置精灵的深度。 e.g。

Sprite.Depth = 0.5f;

然后确保该精灵的深度缓冲区中的每个像素都具有0.5f的值,但是我无法找到该怎么做。如果我使用SpriteBatch,一切正常。如果我为精灵中的每个顶点设置Z值,它就会消失。它似乎根本没有被绘制出来。我使用的BasicEffect和投影矩阵如下所示:

Matrix myProjection = Matrix.CreateOrthographicOffCenter(0, 1280, 720, 0, 0, 1);

myBasicEffect = new BasicEffect(GraphicsDevice)
{
    Projection = myProjection,
    TextureEnabled = true,
    VertexColorEnabled = true                
};

在这种情况下,我只是不明白深度缓冲区是如何写入的。我意识到我可以将所有精灵保存在某种列表中,按深度手动对它们进行排序,然后按顺序绘制它们,但我觉得应该有一个更简单的解决方案。 SpriteBatch.SortMode非常出色,但我无法复制效果。

我在所有地方都进行了搜索,但无法找到有关如何将值写入深度缓冲区的说明。

0 个答案:

没有答案