C# 如何使用缩放矩阵

C# 如何使用缩放矩阵,c#,xna,monogame,C#,Xna,Monogame,我用以下代码创建了一个矩阵: screenscalex = (float)_graphics.GraphicsDevice.Viewport.Width / 1920f; screenscaley = (float)_graphics.GraphicsDevice.Viewport.Height / 1920f; ScalingFactor = new Vector3(screenscalex, screenscaley, 1); Global.SpriteSca

我用以下代码创建了一个矩阵:

    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);

在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);
}
protected override void Draw(游戏时间游戏时间)
{
...
//使用缩放矩阵初始化批处理
spriteBatch.Begin();
//在每个角落画一个精灵
for(int i=0;i


这个问题似乎被提了很多。不久前,我在我的博客上写了一篇快速教程,并给出了解释

代码中最重要的部分是缩放矩阵,很像您在问题中已经创建的矩阵

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();

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

在这种情况下,什么是缩放比例?他们的示例使用了一种与您非常类似的方法来确定缩放比例值,我将它添加到我的答案中。那么缩放比例(在您的代码中)是否等同于我的代码中的缩放比例?实际上,我相信您应该能够提供基于宽度的
屏幕比例
值作为您的比例。很抱歉,我无法提供更好的示例,我无法访问任何旧的XNA源代码。