【问题标题】:How to use a scaling matrix如何使用缩放矩阵
【发布时间】:2014-03-08 19:57:15
【问题描述】:

我有一个用这段代码创建的矩阵:

    screenscalex = (float)_graphics.GraphicsDevice.Viewport.Width / 1920f;
    screenscaley = (float)_graphics.GraphicsDevice.Viewport.Height / 1920f;
    ScalingFactor = new Vector3(screenscalex, screenscaley, 1);
    Global.SpriteScale = Matrix.CreateScale(ScalingFactor);

但我不知道如何使用矩阵来缩小我的精灵这是我目前用来缩小它们的代码:

    batch.End();
    batch.Begin(SpriteSortMode.Immediate,null, null, null, null, null, Global.SpriteScale);
    //This is where the background gets drawn
    backgroundsprite = new Sprite(background, Vector2.Zero);
    backgroundsprite.Draw(batch);
    //ive tried this too below
    //batch.Draw(background, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);

【问题讨论】:

    标签: c# xna monogame


    【解决方案1】:

    在 Microsoft 为缩放精灵提供的示例中,它们使用 SpriteBatch.Draw 方法的重载,该方法接受 Single 类型的缩放参数。

    protected override void Draw(GameTime gameTime)
    {
      ...
      // Initialize the batch with the scaling matrix
      spriteBatch.Begin();
      // Draw a sprite at each corner
      for (int i = 0; i < spritepos.Length; i++)
      {
          spriteBatch.Draw(square, spritepos[i], null, Color.White,
              rotation, origin, scale, SpriteEffects.None, depth);
      }
      spriteBatch.End();
      base.Draw(gameTime);
    }
    

    Scaling Sprites Based On Screen Size (MSDN)

    SpriteBatch.Draw Method (MSDN)

    【讨论】:

    • 在这种情况下,规模会是什么
    • 他们的示例使用与您非常相似的方法来确定比例值,我会将其添加到我的答案中。
    • 那么 scale(在您的代码中)是否等同于我的代码中的 ScalingFactor?
    • 实际上,我相信您应该能够提供基于宽度的screenscale 值作为您的比例。抱歉,我无法提供更好的示例,我无法访问任何旧 XNA 源代码。
    【解决方案2】:

    这个问题似乎出现了很多。不久前,我在博客上写了一个快速教程,并附有解释。

    http://www.craftworkgames.com/blog/monogame-code-snippets/monogame-resolution-independence/

    最重要的代码是缩放矩阵,就像您在问题中已经创建的一样。

    var scaleX = (float)GraphicsDevice.Viewport.Width / (float)VirtualScreenWidth;
    var scaleY = (float)GraphicsDevice.Viewport.Height / (float)VirtualScreenHeight;
    
    _screenScale = new Vector3(scaleX, scaleY, 1.0f);     
    
    var scaleMatrix = Matrix.CreateScale(_screenScale);
    
    _spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, scaleMatrix);
    // your drawing code here
    _spriteBatch.End();
    

    然而,这还不是全部,所以我建议阅读完整的教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2013-05-09
      • 1970-01-01
      相关资源
      最近更新 更多