0

我正在使用 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);
    }

它看起来如何... 它看起来如何

我希望它看起来如何... 我希望它看起来如何

为了获得这种外观,我需要将栅栏的绘制更改为...

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

0 回答 0