【问题标题】:How can I shrink my large texture so that it stays centered?如何缩小我的大纹理以使其保持居中?
【发布时间】:2015-04-23 16:00:21
【问题描述】:

当我缩小我的大纹理时,它不会缩小图像。它只是从右下角开始切掉图像并沿对角线向上。

这就是我绘制精灵的方式:

spriteBatch.Draw(Tiles[0].TileTexture, new Vector2(x * TileWidth, y * TileHeight), Color.White);

这是我正在测试的基本图像。

这是 16,16 时的样子

还有 64,64

【问题讨论】:

  • TileWidth 和 TileHeight 和真实图片的宽高一样吗?
  • @Silveor 不。也许我做错了。但是当我之前这样做时,我有一个 16x16 的图像。我希望玩家能够放大瓷砖。但由于 16,16 太小,它会使图像看起来非常像素化和糟糕。所以我认为最好使用更高分辨率的图像并将其缩小,这样当他们放大时,它看起来会很好。这不是办法吗?

标签: c# xna texture2d


【解决方案1】:

如果您使用 16x16 的图像,则单元格大小应为 16x16。但是如果你想在单元格 16x16 中绘制 64x64 图像,你应该将图像缩小到 16x16,但在这种情况下,64x64 和 16x16 源图像之间没有区别。或者将单元格设为 64x64 像素。

关于缩放,您只能使用整数比例,例如 2x、3x 等。不允许使用非整数比例以避免图像失真。

SpriteBatch.Draw 有overloads。 要根据需要缩放图像,您可以使用重载 1、2 或 3,并将目标矩形作为第二个参数:

SpriteBatch.Draw(
    tileTexture,
    new Rectange(x * TileWidth, y * TileHeight, TileWidth, TileHeight),
    Color.White);

源图像将被分层为矩形。

或者您可以使用最后两个重载中的一个来缩放图像

SpriteBatch.Draw(
    tileTexture,
    new Vector2(x * TileWidth * scaleX, y * TileHeight * scaleY),
    null,
    Color.White,
    0,
    Vector2.Zero,
    new Vector2(scaleX, scaleY),
    SpriteEffects.None,
    0);

另外,如果您只需要用一个相同的图块填充背景,您可以使用任何 wrap SemplerState 参数调用 spritebatch 开始方法(对我来说,PointWrap 更好)并像这样绘制图块一次: SpriteBatch.Draw( 瓷砖纹理, 新矩形(0、0、GraphicsDevice.Viewport.Width、GraphicsDevice.Viewport.Height)、 颜色.白色);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2020-08-17
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多