XNA spritebatch和primative使用不同的轴具有相同的效果

时间:2012-10-31 03:03:55

标签: xna directx monogame

我正在使用monogame 3.0(XNA-C#)开发侧卷轴游戏。我正在使用TriangleStrip绘制地面和一个spritebatch来绘制栅栏。我希望栅栏位于地面上,但它似乎使用不同的轴(负面而不是正面)。为了使事情保持一致,我需要翻转围栏并使用负x和y。精灵和基元都使用相同的视图矩阵和项目。

当原语使用相同的矩阵时,为什么spritebatch需要负轴,而原语使用正轴?

protected override void Initialize()
{
    viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, new Vector3(0,1, 0));
    projectionMatrix = Matrix.CreateOrthographicOffCenter(0,GraphicsDevice.Viewport.Width,  
                        0, GraphicsDevice.Viewport.Height, 0f, 200f) *Matrix.CreateScale(new Vector3(.5f, .5f, 1f));

    GraphicsDevice.RasterizerState.CullMode = CullMode.None;


    _groundBasicEffect = new BasicEffect(GraphicsDevice);
    _groundBasicEffect.View = viewMatrix;
    _groundBasicEffect.Projection = projectionMatrix;
    _groundBasicEffect.VertexColorEnabled = true;
    _groundBasicEffect.World = world;

    _fenceBasicEffect = new BasicEffect(GraphicsDevice);
    _fenceBasicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, new Vector3(0,1,  0));
    _fenceBasicEffect.Projection = projectionMatrix;
    _fenceBasicEffect.TextureEnabled = true;
    _fenceBasicEffect.World = world;     

    base.Initialize();
}
protected override void LoadContent()
{
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        fenceTexture = this.Content.Load<Texture2D>(@"Fence1");
        vertexData = new VertexPositionColor[8];
        vertexData[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Black);
        vertexData[1] = new VertexPositionColor(new Vector3(0, 400, 0), Color.Black);
        vertexData[2] = new VertexPositionColor(new Vector3(200, 0, 0), Color.Black);
        vertexData[3] = new VertexPositionColor(new Vector3(200, 400, 0), Color.Black);
        vertexData[4] = new VertexPositionColor(new Vector3(300, 0, 0), Color.Black);
        vertexData[5] = new VertexPositionColor(new Vector3(300, 430, 0), Color.Black);
        vertexData[6] = new VertexPositionColor(new Vector3(1200, 0, 0), Color.Black);
        vertexData[7] = new VertexPositionColor(new Vector3(1200, 430, 0), Color.Black);
        vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertexData.Length, BufferUsage.None);
        vertexBuffer.SetData(vertexData);

    }
protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);
        GraphicsDevice.SetVertexBuffer(vertexBuffer);

        _groundBasicEffect.CurrentTechnique.Passes[0].Apply();
        GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertexData, 0, vertexData.Length -2);

        _spriteBatch.Begin(0, null, null, null, null, _fenceBasicEffect);
        _spriteBatch.Draw(fenceTexture, Vector2.Zero, new Rectangle(0, 0, 1130, 221), Color.White, 0, new Vector2(0, 200), 1, SpriteEffects.None, 0);
        _spriteBatch.End();
        base.Draw(gameTime);
    }

看起来如何...... How it looks

我期待它看起来...... How I expect it to look

为了得到这个看,我需要将围栏的Draw改为......

_spriteBatch.Draw(fenceTexture, Vector2.Zero, new Rectangle(0, 0, 1130, 221), Color.White, 0, new Vector2(0, -400), 1, SpriteEffects.FlipVertically, 0);

0 个答案:

没有答案