multiple_resolutions.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. .. _doc_multiple_resolutions:
  2. Multiple resolutions
  3. ====================
  4. The problem of multiple resolutions
  5. -----------------------------------
  6. Developers often have trouble understanding how to best support multiple
  7. resolutions in their games. For desktop and console games, this is more or less
  8. straightforward, as most screen aspect ratios are 16:9 and resolutions
  9. are standard (720p, 1080p, 1440p, 4K, …).
  10. For mobile games, at first, it was easy. For many years, the iPhone and iPad
  11. used the same resolution. When *Retina* was implemented, they just doubled
  12. the pixel density; most developers had to supply assets in default and double
  13. resolutions.
  14. Nowadays, this is no longer the case, as there are plenty of different screen
  15. sizes, densities, and aspect ratios. Non-conventional sizes are also becoming
  16. increasingly popular, such as ultrawide displays.
  17. For 3D games, there is not much of a need to support multiple resolutions (from
  18. the aesthetic point of view). The 3D geometry will just fill the screen based on
  19. the field of view, disregarding the aspect ratio. The main reason one may want
  20. to support this, in this case, is for *performance* reasons (running in lower
  21. resolution to increase frames per second).
  22. For 2D and game UIs, this is a different matter, as art needs to be created
  23. using specific pixel sizes in software such as Photoshop, GIMP or Krita.
  24. Since layouts, aspect ratios, resolutions, and pixel densities can change so
  25. much, it is no longer possible to design UIs for every specific screen.
  26. Another method must be used.
  27. One size fits all
  28. -----------------
  29. The most common approach is to use a single *base* resolution and
  30. then fit it to everything else. This resolution is how most players are expected
  31. to play the game (given their hardware). For mobile, Google has useful `stats
  32. <https://developer.android.com/about/dashboards>`_ online, and for desktop,
  33. Steam `also does <https://store.steampowered.com/hwsurvey/>`_.
  34. As an example, Steam shows that the most common *primary display resolution* is
  35. 1920×1080, so a sensible approach is to develop a game for this resolution, then
  36. handle scaling for different sizes and aspect ratios.
  37. Godot provides a several useful tools to do this easily.
  38. Base size
  39. ---------
  40. A base size for the window can be specified in the Project Settings under
  41. **Display → Window**.
  42. .. image:: img/screenres.png
  43. However, what it does is not completely obvious; the engine will *not*
  44. attempt to switch the monitor to this resolution. Rather, think of this
  45. setting as the "design size", i.e. the size of the area that you work
  46. with in the editor. This setting corresponds directly to the size of the
  47. blue rectangle in the 2D editor.
  48. There is often a need to support devices with screen and window sizes
  49. that are different from this base size. Godot offers many ways to
  50. control how the viewport will be resized and stretched to different
  51. screen sizes.
  52. Resizing
  53. --------
  54. There are several types of devices, with several types of screens, which
  55. in turn have different pixel density and resolutions. Handling all of
  56. them can be a lot of work, so Godot tries to make the developer's life a
  57. little easier. The :ref:`Viewport <class_Viewport>`
  58. node has several functions to handle resizing, and the root node of the
  59. scene tree is always a viewport (scenes loaded are instanced as a child
  60. of it, and it can always be accessed by calling
  61. ``get_tree().get_root()`` or ``get_node("/root")``).
  62. In any case, while changing the root Viewport params is probably the
  63. most flexible way to deal with the problem, it can be a lot of work,
  64. code and guessing, so Godot provides a simple set of parameters in the
  65. project settings to handle multiple resolutions.
  66. Stretch settings
  67. ----------------
  68. Stretch settings are located in the project settings, it's just a bunch
  69. of configuration variables that provide several options:
  70. .. image:: img/stretchsettings.png
  71. Stretch Mode
  72. ^^^^^^^^^^^^
  73. The **Stretch Mode** setting defines how the base size is stretched to fit
  74. the resolution of the window or screen.
  75. .. image:: img/stretch.png
  76. The animations below use a "base size" of just 16×9 pixels to
  77. demonstrate the effect of different stretch modes. A single sprite, also
  78. 16×9 pixels in size, covers the entire viewport, and a diagonal
  79. :ref:`Line2D <class_Line2D>` is added on top of it:
  80. .. image:: img/stretch_demo_scene.png
  81. .. Animated GIFs are generated from:
  82. .. https://github.com/ttencate/godot_scaling_mode
  83. - **Stretch Mode = Disabled** (default): No stretching happens. One
  84. unit in the scene corresponds to one pixel on the screen. In this
  85. mode, the **Stretch Aspect** setting has no effect.
  86. This is a good option if you want full control over every screen
  87. pixel, and is probably the best option for 3D games.
  88. .. image:: img/stretch_disabled_expand.gif
  89. - **Stretch Mode = 2D**: In this mode, the size specified in
  90. display/width and display/height in the project settings is
  91. stretched to cover the whole screen (taking the **Stretch Aspect**
  92. setting into account). This means that everything is rendered
  93. directly at the target resolution. 3D is largely unaffected,
  94. while in 2D, there is no longer a 1:1 correspondence between sprite
  95. pixels and screen pixels, which may result in scaling artifacts.
  96. This is a good option if your 2D artwork has a sufficiently high
  97. resolution and does not require pixel-perfect rendering. Consider
  98. enabling texture filtering and mipmapping on your 2D textures and
  99. fonts.
  100. .. image:: img/stretch_2d_expand.gif
  101. - **Stretch Mode = Viewport**: Viewport scaling means that the size of
  102. the root :ref:`Viewport <class_Viewport>` is set precisely to the
  103. base size specified in the Project Settings' **Display** section.
  104. The scene is rendered to this viewport first. Finally, this viewport
  105. is scaled to fit the screen (taking the **Stretch Aspect** setting into
  106. account).
  107. This mode is useful when working with pixel-precise games, or for the
  108. sake of rendering to a lower resolution to improve performance.
  109. .. image:: img/stretch_viewport_expand.gif
  110. Stretch Aspect
  111. ^^^^^^^^^^^^^^
  112. The second setting is the stretch aspect. Note that this only takes effect if
  113. **Stretch Mode** is set to something other than **Disabled**.
  114. In the animations below, you will notice gray and black areas. The black
  115. areas are added by the engine and cannot be drawn into. The gray areas
  116. are part of your scene, and can be drawn to. The gray areas correspond
  117. to the region outside the blue frame you see in the 2D editor.
  118. - **Stretch Aspect = Ignore**: Ignore the aspect ratio when stretching
  119. the screen. This means that the original resolution will be stretched
  120. to exactly fill the screen, even if it's wider or narrower. This may
  121. result in nonuniform stretching: things looking wider or taller than
  122. designed.
  123. .. image:: img/stretch_viewport_ignore.gif
  124. - **Stretch Aspect = Keep**: Keep aspect ratio when stretching the
  125. screen. This means that the viewport retains its original size
  126. regardless of the screen resolution, and black bars will be added to
  127. the top/bottom of the screen ("letterboxing") or the sides
  128. ("pillarboxing").
  129. This is a good option if you know the aspect ratio of your target
  130. devices in advance, or if you don't want to handle different aspect
  131. ratios.
  132. .. image:: img/stretch_viewport_keep.gif
  133. - **Stretch Aspect = Keep Width**: Keep aspect ratio when stretching the
  134. screen. If the screen is wider than the base size, black bars are
  135. added at the left and right (pillarboxing). But if the screen is
  136. taller than the base resolution, the viewport will be grown in the
  137. vertical direction (and more content will be visible to the bottom).
  138. You can also think of this as "Expand Vertically".
  139. This is usually the best option for creating GUIs or HUDs that scale,
  140. so some controls can be anchored to the bottom
  141. (:ref:`doc_size_and_anchors`).
  142. .. image:: img/stretch_viewport_keep_width.gif
  143. - **Stretch Aspect = Keep Height**: Keep aspect ratio when stretching
  144. the screen. If the screen is taller than the base size, black
  145. bars are added at the top and bottom (letterboxing). But if the
  146. screen is wider than the base resolution, the viewport will be grown
  147. in the horizontal direction (and more content will be visible to the
  148. right). You can also think of this as "Expand Horizontally".
  149. This is usually the best option for 2D games that scroll horizontally
  150. (like runners or platformers).
  151. .. image:: img/stretch_viewport_keep_height.gif
  152. - **Stretch Aspect = Expand**: Keep aspect ratio when stretching the
  153. screen, but keep neither the base width nor height. Depending on the
  154. screen aspect ratio, the viewport will either be larger in the
  155. horizontal direction (if the screen is wider than the base size) or
  156. in the vertical direction (if the screen is taller than the original
  157. size).
  158. .. image:: img/stretch_viewport_expand.gif
  159. Stretch Shrink
  160. ^^^^^^^^^^^^^^
  161. The **Shrink** setting allows you to add an extra scaling factor on top of
  162. what the **Stretch** options above already provide. The default value of 1
  163. means that no scaling occurs.
  164. If, for example, you set **Shrink** to 4 and leave **Stretch Mode** on
  165. **Disabled**, each unit in your scene will correspond to 4×4 pixels on the
  166. screen.
  167. If **Stretch Mode** is set to something other than **Disabled**, the size of
  168. the root viewport is scaled down by the **Shrink** factor, and pixels
  169. in the output are scaled up by the same amount. This is rarely useful for
  170. 2D games, but can be used to increase performance in 3D games
  171. by rendering them at a lower resolution.
  172. From scripts
  173. ^^^^^^^^^^^^
  174. To configure stretching at runtime from a script, use the
  175. ``get_tree().set_screen_stretch()`` function (see
  176. :ref:`SceneTree.set_screen_stretch() <class_SceneTree_method_set_screen_stretch>`).
  177. Reducing aliasing on downsampling
  178. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  179. If the game has a very high base resolution (e.g. 3840×2160), aliasing might
  180. appear when downsampling to something considerably lower like 1280×720.
  181. Aliasing can be made less visible by shrinking all images by a factor of 2
  182. upon loading. This can be done by calling the method below before
  183. the game data is loaded:
  184. ::
  185. VisualServer.texture_set_shrink_all_x2_on_set_data(true)
  186. Handling aspect ratios
  187. ^^^^^^^^^^^^^^^^^^^^^^
  188. Once scaling for different resolutions is accounted for, make sure that
  189. your *user interface* also scales for different aspect ratios. This can be
  190. done using :ref:`anchors <doc_size_and_anchors>` and/or :ref:`containers
  191. <doc_gui_containers>`.
  192. Field of view scaling
  193. ^^^^^^^^^^^^^^^^^^^^^
  194. The 3D Camera node's **Keep Aspect** property defaults to the **Keep Height**
  195. scaling mode (also called *Hor+*). This is usually the best value for desktop
  196. games and mobile games in landscape mode, as widescreen displays will
  197. automatically use a wider field of view.
  198. However, if your 3D game is intended to be played in portrait mode, it may make
  199. more sense to use **Keep Width** instead (also called *Vert-*). This way,
  200. smartphones with an aspect ratio taller than 16:9 (e.g. 19:9) will use a
  201. *taller* field of view, which is more logical here.