multiple_resolutions.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. :article_outdated: True
  2. .. _doc_multiple_resolutions:
  3. Multiple resolutions
  4. ====================
  5. The problem of multiple resolutions
  6. -----------------------------------
  7. Developers often have trouble understanding how to best support multiple
  8. resolutions in their games. For desktop and console games, this is more or less
  9. straightforward, as most screen aspect ratios are 16:9 and resolutions
  10. are standard (720p, 1080p, 1440p, 4K, …).
  11. For mobile games, at first, it was easy. For many years, the iPhone and iPad
  12. used the same resolution. When *Retina* was implemented, they just doubled
  13. the pixel density; most developers had to supply assets in default and double
  14. resolutions.
  15. Nowadays, this is no longer the case, as there are plenty of different screen
  16. sizes, densities, and aspect ratios. Non-conventional sizes are also becoming
  17. increasingly popular, such as ultrawide displays.
  18. For 3D games, there is not much of a need to support multiple resolutions (from
  19. the aesthetic point of view). The 3D geometry will just fill the screen based on
  20. the field of view, disregarding the aspect ratio. The main reason one may want
  21. to support this, in this case, is for *performance* reasons (running in lower
  22. resolution to increase frames per second).
  23. For 2D and game UIs, this is a different matter, as art needs to be created
  24. using specific pixel sizes in software such as Photoshop, GIMP or Krita.
  25. Since layouts, aspect ratios, resolutions, and pixel densities can change so
  26. much, it is no longer possible to design UIs for every specific screen.
  27. Another method must be used.
  28. One size fits all
  29. -----------------
  30. The most common approach is to use a single *base* resolution and
  31. then fit it to everything else. This resolution is how most players are expected
  32. to play the game (given their hardware). For mobile, Google has useful `stats
  33. <https://developer.android.com/about/dashboards>`_ online, and for desktop,
  34. Steam `also does <https://store.steampowered.com/hwsurvey/>`_.
  35. As an example, Steam shows that the most common *primary display resolution* is
  36. 1920×1080, so a sensible approach is to develop a game for this resolution, then
  37. handle scaling for different sizes and aspect ratios.
  38. Godot provides several useful tools to do this easily.
  39. .. seealso::
  40. You can see how Godot's support for multiple resolutions works in action using the
  41. `Multiple Resolutions and Aspect Ratios demo project <https://github.com/godotengine/godot-demo-projects/tree/master/gui/multiple_resolutions>`__.
  42. Base size
  43. ---------
  44. A base size for the window can be specified in the Project Settings under
  45. **Display → Window**.
  46. .. image:: img/screenres.png
  47. However, what it does is not completely obvious; the engine will *not*
  48. attempt to switch the monitor to this resolution. Rather, think of this
  49. setting as the "design size", i.e. the size of the area that you work
  50. with in the editor. This setting corresponds directly to the size of the
  51. blue rectangle in the 2D editor.
  52. There is often a need to support devices with screen and window sizes
  53. that are different from this base size. Godot offers many ways to
  54. control how the viewport will be resized and stretched to different
  55. screen sizes.
  56. To configure the stretch base size at runtime from a script, use the
  57. ``get_tree().root.content_scale_size`` property (see
  58. :ref:`Window.content_scale_size <class_Window_property_content_scale_size>`).
  59. Changing this value can indirectly change the size of 2D elements. However, to
  60. provide an user-accessible scaling option, using
  61. :ref:`doc_multiple_resolutions_stretch_scale` is recommended as it's easier to
  62. adjust.
  63. .. note::
  64. Godot follows a modern approach to multiple resolutions. The engine will
  65. never change the monitor's resolution on its own. While changing the
  66. monitor's resolution is the most efficient approach, it's also the least
  67. reliable approach as it can leave the monitor stuck on a low resolution if
  68. the game crashes. This is especially common on macOS or Linux which don't
  69. handle resolution changes as well as Windows.
  70. Changing the monitor's resolution also removes any control from the game
  71. developer over filtering and aspect ratio stretching, which can be important
  72. to ensure correct display for pixel art games.
  73. On top of that, changing the monitor's resolution makes alt-tabbing in and
  74. out of a game much slower since the monitor has to change resolutions every
  75. time this is done.
  76. Resizing
  77. --------
  78. There are several types of devices, with several types of screens, which
  79. in turn have different pixel density and resolutions. Handling all of
  80. them can be a lot of work, so Godot tries to make the developer's life a
  81. little easier. The :ref:`Viewport <class_Viewport>`
  82. node has several functions to handle resizing, and the root node of the
  83. scene tree is always a viewport (scenes loaded are instanced as a child
  84. of it, and it can always be accessed by calling
  85. ``get_tree().root`` or ``get_node("/root")``).
  86. In any case, while changing the root Viewport params is probably the
  87. most flexible way to deal with the problem, it can be a lot of work,
  88. code and guessing, so Godot provides a set of parameters in the
  89. project settings to handle multiple resolutions.
  90. Stretch settings
  91. ----------------
  92. Stretch settings are located in the project settings and provide several options:
  93. .. image:: img/stretchsettings.png
  94. Stretch Mode
  95. ^^^^^^^^^^^^
  96. The **Stretch Mode** setting defines how the base size is stretched to fit
  97. the resolution of the window or screen.
  98. .. image:: img/stretch.png
  99. The animations below use a "base size" of just 16×9 pixels to
  100. demonstrate the effect of different stretch modes. A single sprite, also
  101. 16×9 pixels in size, covers the entire viewport, and a diagonal
  102. :ref:`Line2D <class_Line2D>` is added on top of it:
  103. .. image:: img/stretch_demo_scene.png
  104. .. Animated GIFs are generated from:
  105. .. https://github.com/ttencate/godot_scaling_mode
  106. - **Stretch Mode = Disabled** (default): No stretching happens. One
  107. unit in the scene corresponds to one pixel on the screen. In this
  108. mode, the **Stretch Aspect** setting has no effect.
  109. .. image:: img/stretch_disabled_expand.gif
  110. - **Stretch Mode = Canvas Items**: In this mode, the base size specified in
  111. width and height in the project settings is
  112. stretched to cover the whole screen (taking the **Stretch Aspect**
  113. setting into account). This means that everything is rendered
  114. directly at the target resolution. 3D is unaffected,
  115. while in 2D, there is no longer a 1:1 correspondence between sprite
  116. pixels and screen pixels, which may result in scaling artifacts.
  117. .. image:: img/stretch_2d_expand.gif
  118. - **Stretch Mode = Viewport**: Viewport scaling means that the size of
  119. the root :ref:`Viewport <class_Viewport>` is set precisely to the
  120. base size specified in the Project Settings' **Display** section.
  121. The scene is rendered to this viewport first. Finally, this viewport
  122. is scaled to fit the screen (taking the **Stretch Aspect** setting into
  123. account).
  124. .. image:: img/stretch_viewport_expand.gif
  125. To configure the stretch mode at runtime from a script, use the
  126. ``get_tree().root.content_scale_mode`` property (see
  127. :ref:`Window.content_scale_mode <class_Window_property_content_scale_mode>`
  128. and the :ref:`ContentScaleMode <enum_Window_ContentScaleMode>` enum).
  129. Stretch Aspect
  130. ^^^^^^^^^^^^^^
  131. The second setting is the stretch aspect. Note that this only takes effect if
  132. **Stretch Mode** is set to something other than **Disabled**.
  133. In the animations below, you will notice gray and black areas. The black
  134. areas are added by the engine and cannot be drawn into. The gray areas
  135. are part of your scene, and can be drawn to. The gray areas correspond
  136. to the region outside the blue frame you see in the 2D editor.
  137. - **Stretch Aspect = Ignore**: Ignore the aspect ratio when stretching
  138. the screen. This means that the original resolution will be stretched
  139. to exactly fill the screen, even if it's wider or narrower. This may
  140. result in nonuniform stretching: things looking wider or taller than
  141. designed.
  142. .. image:: img/stretch_viewport_ignore.gif
  143. - **Stretch Aspect = Keep**: Keep aspect ratio when stretching the
  144. screen. This means that the viewport retains its original size
  145. regardless of the screen resolution, and black bars will be added to
  146. the top/bottom of the screen ("letterboxing") or the sides
  147. ("pillarboxing").
  148. This is a good option if you know the aspect ratio of your target
  149. devices in advance, or if you don't want to handle different aspect
  150. ratios.
  151. .. image:: img/stretch_viewport_keep.gif
  152. - **Stretch Aspect = Keep Width**: Keep aspect ratio when stretching the
  153. screen. If the screen is wider than the base size, black bars are
  154. added at the left and right (pillarboxing). But if the screen is
  155. taller than the base resolution, the viewport will be grown in the
  156. vertical direction (and more content will be visible to the bottom).
  157. You can also think of this as "Expand Vertically".
  158. This is usually the best option for creating GUIs or HUDs that scale,
  159. so some controls can be anchored to the bottom
  160. (:ref:`doc_size_and_anchors`).
  161. .. image:: img/stretch_viewport_keep_width.gif
  162. - **Stretch Aspect = Keep Height**: Keep aspect ratio when stretching
  163. the screen. If the screen is taller than the base size, black
  164. bars are added at the top and bottom (letterboxing). But if the
  165. screen is wider than the base resolution, the viewport will be grown
  166. in the horizontal direction (and more content will be visible to the
  167. right). You can also think of this as "Expand Horizontally".
  168. This is usually the best option for 2D games that scroll horizontally
  169. (like runners or platformers).
  170. .. image:: img/stretch_viewport_keep_height.gif
  171. - **Stretch Aspect = Expand**: Keep aspect ratio when stretching the
  172. screen, but keep neither the base width nor height. Depending on the
  173. screen aspect ratio, the viewport will either be larger in the
  174. horizontal direction (if the screen is wider than the base size) or
  175. in the vertical direction (if the screen is taller than the original
  176. size).
  177. .. image:: img/stretch_viewport_expand.gif
  178. .. tip::
  179. To support both portrait and landscape mode with a similar automatically
  180. determined scale factor, set your project's base resolution to be a *square*
  181. (1:1 aspect ratio) instead of a rectangle. For instance, if you wish to design
  182. for 1280×720 as the base resolution but wish to support both portrait and
  183. landscape mode, use 720×720 as the project's base window size in the
  184. Project Settings.
  185. To allow the user to choose their preferred screen orientation at run-time,
  186. remember to set **Display > Window > Handheld > Orientation** to ``sensor``.
  187. To configure the stretch aspect at runtime from a script, use the
  188. ``get_tree().root.content_scale_aspect`` property (see
  189. :ref:`Window.content_scale_aspect <class_Window_property_content_scale_aspect>`
  190. and the :ref:`ContentScaleAspect <enum_Window_ContentScaleAspect>` enum).
  191. .. _doc_multiple_resolutions_stretch_scale:
  192. Stretch Scale
  193. ^^^^^^^^^^^^^
  194. The **Scale** setting allows you to add an extra scaling factor on top of
  195. what the **Stretch** options above already provide. The default value of ``1.0``
  196. means that no additional scaling occurs.
  197. For example, if you set **Scale** to ``2.0`` and leave **Stretch Mode** on
  198. **Disabled**, each unit in your scene will correspond to 2×2 pixels on the
  199. screen. This is a good way to provide scaling options for non-game applications.
  200. If **Stretch Mode** is set to **canvas_items**, 2D elements will be scaled
  201. relative to the base window size, then multiplied by the **Scale** setting. This
  202. can be exposed to players to allow them to adjust the automatically determined
  203. scale to their liking, for better accessibility.
  204. If **Stretch Mode** is set to **viewport**, the viewport's resolution is divided
  205. by **Scale**. This makes pixels look larger and reduces rendering resolution
  206. (with a given window size), which can improve performance.
  207. To configure the stretch scale at runtime from a script, use the
  208. ``get_tree().root.content_scale_factor`` property (see
  209. :ref:`Window.content_scale_factor <class_Window_property_content_scale_factor>`).
  210. Common use case scenarios
  211. -------------------------
  212. The following settings are recommended to support multiple resolutions and aspect
  213. ratios well.
  214. Desktop game
  215. ^^^^^^^^^^^^
  216. **Non-pixel art:**
  217. - Set the base window width to ``1920`` and window height to ``1080``. If you have a
  218. display smaller than 1920×1080, set **Test Width** and **Test Height** to
  219. lower values to make the window smaller when the project starts.
  220. - Alternatively, if you're targeting high-end devices primarily, set the base
  221. window width to ``3840`` and window height to ``2160``.
  222. This allows you to provide higher resolution 2D assets, resulting in crisper
  223. visuals at the cost of higher memory usage and file sizes.
  224. Note that this will make non-mipmapped textures grainy on low resolution devices,
  225. so make sure to follow the instructions described in
  226. :ref:`doc_multiple_resolutions_reducing_aliasing_on_downsampling`.
  227. - Set the stretch mode to ``canvas_items``.
  228. - Set the stretch aspect to ``expand``. This allows for supporting multiple aspect ratios
  229. and makes better use of tall smartphone displays (such as 18:9 or 19:9 aspect ratios).
  230. - Configure Control nodes' anchors to snap to the correct corners using the **Layout** menu.
  231. **Pixel art:**
  232. - Set the base window size to the viewport size you intend to use. Most pixel art games
  233. use viewport sizes between 256×224 and 640×480. Higher viewport sizes will
  234. require using higher resolution artwork, unless you intend to show more of the
  235. game world at a given time.
  236. - Set the stretch mode to ``viewport``.
  237. - Set the stretch aspect to ``keep`` to enforce a single aspect ratio (with
  238. black bars). As an alternative, you can set the stretch aspect to ``expand`` to
  239. support multiple aspect ratios.
  240. - If using the ``expand`` stretch aspect, Configure Control nodes' anchors to
  241. snap to the correct corners using the **Layout** menu.
  242. .. note::
  243. The ``viewport`` stretch mode provides low-resolution rendering that is then
  244. stretched to the final window size. If you are OK with sprites being able to
  245. move or rotate in "sub-pixel" positions or wish to have a high resolution 3D
  246. viewport, you should use the ``canvas_items`` stretch mode instead of the ``viewport``
  247. stretch mode.
  248. Godot currently doesn't have a way to enforce integer scaling when using the
  249. ``canvas_items`` or ``viewport`` stretch mode, which means pixel art may look bad if the
  250. final window size is not a multiple of the base window size.
  251. To fix this, use an add-on such as the `Integer Resolution Handler <https://github.com/Yukitty/godot-addon-integer_resolution_handler>`__.
  252. Mobile game in landscape mode
  253. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  254. Godot is configured to use landscape mode by default. This means you don't need
  255. to change the display orientation project setting.
  256. - Set the base window width to ``1280`` and window height to ``720``.
  257. - Alternatively, if you're targeting high-end devices primarily, set the base
  258. window width to ``1920`` and window height to ``1080``.
  259. This allows you to provide higher resolution 2D assets, resulting in crisper
  260. visuals at the cost of higher memory usage and file sizes. Many devices have
  261. even higher resolution displays (1440p), but the difference with 1080p is
  262. barely visible given the small size of smartphone displays.
  263. Note that this will make non-mipmapped textures grainy on low resolution devices,
  264. so make sure to follow the instructions described in
  265. :ref:`doc_multiple_resolutions_reducing_aliasing_on_downsampling`.
  266. - Set the stretch mode to ``canvas_items``.
  267. - Set the stretch aspect to ``expand``. This allows for supporting multiple aspect ratios
  268. and makes better use of tall smartphone displays (such as 18:9 or 19:9 aspect ratios).
  269. - Configure Control nodes' anchors to snap to the correct corners using the **Layout** menu.
  270. .. tip::
  271. To better support tablets and foldable phones (which frequently feature
  272. displays with aspect ratios close to 4:3), consider using a base resolution
  273. that has a 4:3 aspect ratio while following the rest of the instructions
  274. here. For instance, you can set the base window width to ``1280`` and the
  275. base window height to ``960``.
  276. Mobile game in portrait mode
  277. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  278. - Set the base window width to ``720`` and window height to ``1280``.
  279. - Alternatively, if you're targeting high-end devices primarily, set the base
  280. window width to ``1080`` and window height to ``1920``.
  281. This allows you to provide higher resolution 2D assets, resulting in crisper
  282. visuals at the cost of higher memory usage and file sizes. Many devices have
  283. even higher resolution displays (1440p), but the difference with 1080p is
  284. barely visible given the small size of smartphone displays.
  285. Note that this will make non-mipmapped textures grainy on low resolution devices,
  286. so make sure to follow the instructions described in
  287. :ref:`doc_multiple_resolutions_reducing_aliasing_on_downsampling`.
  288. - Set **Display > Window > Handheld > Orientation** to ``portrait``.
  289. - Set the stretch mode to ``canvas_items``.
  290. - Set the stretch aspect to ``expand``. This allows for supporting multiple aspect ratios
  291. and makes better use of tall smartphone displays (such as 18:9 or 19:9 aspect ratios).
  292. - Configure Control nodes' anchors to snap to the correct corners using the **Layout** menu.
  293. .. tip::
  294. To better support tablets and foldable phones (which frequently feature
  295. displays with aspect ratios close to 4:3), consider using a base resolution
  296. that has a 3:4 aspect ratio while following the rest of the instructions
  297. here. For instance, you can set the base window width to ``960`` and the
  298. base window height to ``1280``.
  299. Non-game application
  300. ^^^^^^^^^^^^^^^^^^^^
  301. - Set the base window width and height to the smallest window size that you intend to target.
  302. This is not required, but this ensures that you design your UI with small window sizes in mind.
  303. - Keep the stretch mode to its default value, ``disabled``.
  304. - Keep the stretch aspect to its default value, ``ignore``
  305. (its value won't be used since the stretch mode is ``disabled``).
  306. - You can define a minimum window size by setting ``OS.min_window_size`` in a
  307. script's ``_ready()`` function. This prevents the user from resizing the application
  308. below a certain size, which could break the UI layout.
  309. .. note::
  310. Godot doesn't support manually overriding the 2D scale factor yet, so it is
  311. not possible to have hiDPI support in non-game applications. Due to this, it
  312. is recommended to leave **Allow Hidpi** disabled in non-game applications to
  313. allow for the OS to use its low-DPI fallback.
  314. hiDPI support
  315. -------------
  316. By default, Godot projects aren't considered DPI-aware by the operating system.
  317. This is done to improve performance on low-end systems, since the operating
  318. system's DPI fallback scaling will be faster than letting the application scale
  319. itself (even when using the ``viewport`` stretch mode).
  320. However, the OS-provided DPI fallback scaling doesn't play well with fullscreen
  321. mode. If you want crisp visuals on hiDPI displays or if project uses fullscreen,
  322. it's recommended to enable **Display > Window > Dpi > Allow Hidpi** in the
  323. Project Settings.
  324. **Allow Hidpi** is only effective on Windows and macOS. It's ignored on all
  325. other platforms.
  326. .. note::
  327. The Godot editor itself is always marked as DPI-aware. Running the project
  328. from the editor will only be DPI-aware if **Allow Hidpi** is enabled in the
  329. Project Settings.
  330. .. _doc_multiple_resolutions_reducing_aliasing_on_downsampling:
  331. Reducing aliasing on downsampling
  332. ---------------------------------
  333. If the game has a very high base resolution (e.g. 3840×2160), aliasing might
  334. appear when downsampling to something considerably lower like 1280×720.
  335. To resolve this, you can enable mipmaps on all your 2D textures. However, enabling mipmaps
  336. will increase memory usage which may be problematic on low-end mobile devices.
  337. Handling aspect ratios
  338. ----------------------
  339. Once scaling for different resolutions is accounted for, make sure that
  340. your *user interface* also scales for different aspect ratios. This can be
  341. done using :ref:`anchors <doc_size_and_anchors>` and/or :ref:`containers
  342. <doc_gui_containers>`.
  343. Field of view scaling
  344. ---------------------
  345. The 3D Camera node's **Keep Aspect** property defaults to the **Keep Height**
  346. scaling mode (also called *Hor+*). This is usually the best value for desktop
  347. games and mobile games in landscape mode, as widescreen displays will
  348. automatically use a wider field of view.
  349. However, if your 3D game is intended to be played in portrait mode, it may make
  350. more sense to use **Keep Width** instead (also called *Vert-*). This way,
  351. smartphones with an aspect ratio taller than 16:9 (e.g. 19:9) will use a
  352. *taller* field of view, which is more logical here.
  353. Scaling 2D and 3D elements differently using Viewports
  354. ------------------------------------------------------
  355. Using multiple Viewport nodes, you can have different scales for various
  356. elements. For instance, you can use this to render the 3D world at a low
  357. resolution while keeping 2D elements at the native resolution. This can improve
  358. performance significantly while keeping the HUD and other 2D elements crisp.
  359. This is done by using the root Viewport node only for 2D elements, then creating
  360. a Viewport node to display the 3D world and displaying it using a
  361. SubViewportContainer or TextureRect node. There will effectively be two viewports
  362. in the final project. One upside of using TextureRect over SubViewportContainer is
  363. that it allows enable linear filtering. This makes scaled 3D viewports look
  364. better in many cases.
  365. See the
  366. `3D viewport scaling demo <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/3d_scaling>`__
  367. for examples.