多分辨率

The problem of multiple resolutions

Developers often have trouble understanding how to best support multiple resolutions in their games. For desktop and console games, this is more or less straightforward, as most screen aspect ratios are 16:9 and resolutions are standard (720p, 1080p, 1440p, 4K, …).

For mobile games, at first, it was easy. For many years, the iPhone and iPad used the same resolution. When Retina was implemented, they just doubled the pixel density; most developers had to supply assets in default and double resolutions.

Nowadays, this is no longer the case, as there are plenty of different screen sizes, densities, and aspect ratios. Non-conventional sizes are also becoming increasingly popular, such as ultrawide displays.

For 3D games, there is not much of a need to support multiple resolutions (from the aesthetic point of view). The 3D geometry will just fill the screen based on the field of view, disregarding the aspect ratio. The main reason one may want to support this, in this case, is for performance reasons (running in lower resolution to increase frames per second).

For 2D and game UIs, this is a different matter, as art needs to be created using specific pixel sizes in software such as Photoshop, GIMP or Krita.

Since layouts, aspect ratios, resolutions, and pixel densities can change so much, it is no longer possible to design UIs for every specific screen. Another method must be used.

万全之策

The most common approach is to use a single base resolution and then fit it to everything else. This resolution is how most players are expected to play the game (given their hardware). For mobile, Google has useful stats online, and for desktop, Steam also does.

As an example, Steam shows that the most common primary display resolution is 1920×1080, so a sensible approach is to develop a game for this resolution, then handle scaling for different sizes and aspect ratios.

Godot 还提供了一系列通用的容器。

基本大小

A base size for the window can be specified in the Project Settings under Display → Window.

../../_images/screenres.png

然而,它的作用并不完全明显; 引擎将 尝试将显示器切换到此分辨率。 相反,将此设置视为“设计大小”,即您在编辑器中使用的区域的大小。 此设置直接对应于2D编辑器中蓝色矩形的大小。

通常需要支持具有与该基本大小不同的屏幕和窗口大小的设备。 Godot提供了许多方法来控制视区的大小调整和拉伸到不同的屏幕大小。

注解

Godot遵循了现代多种分辨率的方法。引擎永远不会自行改变显示器的分辨率。虽然改变显示器的分辨率是最有效的方法,但这也是最不可靠的方法,因为如果游戏崩溃,它可能会让显示器卡在一个低分辨率上。这在macOS或Linux上很常见,因为它们对分辨率变化的处理不如Windows。

更改显示器的分辨率还会取消游戏开发者对过滤和纵横比拉伸的控制,这对于确保像素游戏的正确显示画面非常重要。

最重要的是,更改显示器的分辨率会使游戏的Alt-Tab键切换速度变慢,因为每次切换时显示器都必须更改分辨率。

调整大小

市面上有着各种各样的设备, 拥有各种类型的屏幕, 依次有着不同的像素密度和分辨率。处理所有的类型工作量巨大, 所以Godot试图让开发者的生活变得更简单。 Viewport 节点提供了几个处理大小调整的函数, 而场景树的根节点始终是一个Viewport (场景将作为它的子节点被实例化, 并且始终可以通过调用``get_tree().get_root()`` 或``get_node("/root")`` 来访问它。

在任何情况下, 虽然更改``根视区``的参数可能是解决问题的最灵活方法, 但这样做的可能包含大量工作、大量代码和大量推测过程, 因此Godot在``项目设置``中提供了一组简单的参数来处理多分辨率问题。

拉伸设置

Stretch settings are located in the project settings and provide several options:

../../_images/stretchsettings.png

拉伸模式 (Stretch mode)

The Stretch Mode setting defines how the base size is stretched to fit the resolution of the window or screen.

../../_images/stretch.png

下面的动画使用仅16×9像素的“基本大小”来演示不同拉伸模式的效果。 单个精灵,大小也是16×9像素,覆盖整个视区,并在其上添加一个对角线 Line2D :

../../_images/stretch_demo_scene.png
  • Stretch Mode = Disabled (default): No stretching happens. One unit in the scene corresponds to one pixel on the screen. In this mode, the Stretch Aspect setting has no effect.

    如果您想要完全控制每个屏幕像素,这是一个不错的选择,并且可能是3D游戏的最佳选择。

    ../../_images/stretch_disabled_expand.gif
  • Stretch Mode = 2D: In this mode, the size specified in display/width and display/height in the project settings is stretched to cover the whole screen (taking the Stretch Aspect setting into account). This means that everything is rendered directly at the target resolution. 3D is largely unaffected, while in 2D, there is no longer a 1:1 correspondence between sprite pixels and screen pixels, which may result in scaling artifacts.

    如果您的2D图稿具有足够高的分辨率并且不需要像素完美渲染,那么这是一个不错的选择。 考虑在2D纹理和字体上启用纹理过滤和mipmapping。

    ../../_images/stretch_2d_expand.gif
  • Stretch Mode = Viewport: Viewport scaling means that the size of the root Viewport is set precisely to the base size specified in the Project Settings' Display section. The scene is rendered to this viewport first. Finally, this viewport is scaled to fit the screen (taking the Stretch Aspect setting into account).

    This mode is useful when working with pixel-precise games, or for the sake of rendering to a lower resolution to improve performance.

    ../../_images/stretch_viewport_expand.gif

拉伸比例(Stretch aspect)

The second setting is the stretch aspect. Note that this only takes effect if Stretch Mode is set to something other than Disabled.

在下面的动画中,您会注意到灰色和黑色区域。 黑色区域由引擎添加,无法绘制。 灰色区域是场景的一部分,可以绘制。 灰色区域对应于您在2D编辑器中看到的蓝色框架外的区域。

  • Stretch Aspect = Ignore: Ignore the aspect ratio when stretching the screen. This means that the original resolution will be stretched to exactly fill the screen, even if it's wider or narrower. This may result in nonuniform stretching: things looking wider or taller than designed.

    ../../_images/stretch_viewport_ignore.gif
  • Stretch Aspect = Keep: Keep aspect ratio when stretching the screen. This means that the viewport retains its original size regardless of the screen resolution, and black bars will be added to the top/bottom of the screen ("letterboxing") or the sides ("pillarboxing").

    如果您事先知道目标设备的宽高比,或者您不想处理不同的宽高比,这是一个不错的选择。

    ../../_images/stretch_viewport_keep.gif
  • Stretch Aspect = Keep Width: Keep aspect ratio when stretching the screen. If the screen is wider than the base size, black bars are added at the left and right (pillarboxing). But if the screen is taller than the base resolution, the viewport will be grown in the vertical direction (and more content will be visible to the bottom). You can also think of this as "Expand Vertically".

    这通常是创建可扩展的GUI或HUD的最佳选择,因此一些控件可以锚定到底部( 大小和锚定点)。

    ../../_images/stretch_viewport_keep_width.gif
  • Stretch Aspect = Keep Height: Keep aspect ratio when stretching the screen. If the screen is taller than the base size, black bars are added at the top and bottom (letterboxing). But if the screen is wider than the base resolution, the viewport will be grown in the horizontal direction (and more content will be visible to the right). You can also think of this as "Expand Horizontally".

    这通常是水平滚动的2D游戏的最佳选择(如跑步者或平台游戏者)。

    ../../_images/stretch_viewport_keep_height.gif
  • Stretch Aspect = Expand: Keep aspect ratio when stretching the screen, but keep neither the base width nor height. Depending on the screen aspect ratio, the viewport will either be larger in the horizontal direction (if the screen is wider than the base size) or in the vertical direction (if the screen is taller than the original size).

    ../../_images/stretch_viewport_expand.gif

拉伸收缩

The Shrink setting allows you to add an extra scaling factor on top of what the Stretch options above already provide. The default value of 1 means that no scaling occurs.

If, for example, you set Shrink to 4 and leave Stretch Mode on Disabled, each unit in your scene will correspond to 4×4 pixels on the screen.

If Stretch Mode is set to something other than Disabled, the size of the root viewport is scaled down by the Shrink factor, and pixels in the output are scaled up by the same amount. This is rarely useful for 2D games, but can be used to increase performance in 3D games by rendering them at a lower resolution.

来自脚本

To configure stretching at runtime from a script, use the get_tree().set_screen_stretch() method (see SceneTree.set_screen_stretch()).

减少缩减取样的混叠

如果游戏的基本分辨率很高(如3840×2160),当采样降到相当低的分辨率(如1280×720)时,可能会出现锯齿。可以通过在加载时将所有图像缩小2倍来减少锯齿的出现。这可以通过在加载游戏数据之前调用下面的方法来实现:

VisualServer.texture_set_shrink_all_x2_on_set_data(true)

或者,也可以在所有2D纹理上启用mipmap。然而,启用mipmap会增加内存的使用量,这个在低端移动设备上可能会出现问题。

Handling aspect ratios

Once scaling for different resolutions is accounted for, make sure that your user interface also scales for different aspect ratios. This can be done using anchors and/or containers.

视场角(Field of view)缩放

3D相机节点的 Keep Aspect 属性默认为 Keep Height 缩放模式(也称为 Hor+ )。在横屏模式下,这通常是桌面游戏和手机游戏的最佳选择,因为宽屏显示器会自动使用更宽的视野。

然而,如果您的3D游戏打算使用纵向模式,那么使用**Keep Width保持宽度**称为(Vert-)可能会更有意义。这样,宽高比大于16:9(例如19:9)的智能手机将使用更高的视野,这在这里更符合逻辑。

使用视图端口以不同的方式缩放 2D 和 3D 元素

使用多个视图窗口节点,可以对不同的元素使用不同的比例。例如,您可以使用此选项以低分辨率渲染3D世界,同时将2D元素保持在原生分辨率。这可以显著提高性能,同时保持HUD和其他2D元素的清晰度。

这是通过只对2D元素使用根Viewport节点,然后创建一个Viewport节点来显示3D世界并使用ViewportContainer或TextureRect节点来实现的。最终项目中实际上将有两个视图窗口。与ViewportContainer相比,使用TextureRect的一个好处是它允许启用线性过滤。这使得缩放的3D视图窗口在许多情况下看起来更好。

有关示例,请参见3D视口缩放演示 <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/3d_scaling>`__ 。