large_world_coordinates.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. Large world coordinates
  2. =======================
  3. .. note::
  4. Large world coordinates are mainly useful in 3D projects; they are rarely
  5. required in 2D projects. Also, unlike 3D rendering, 2D rendering currently
  6. doesn't benefit from increased precision when large world coordinates are
  7. enabled.
  8. Why use large world coordinates?
  9. --------------------------------
  10. In Godot, physics simulation and rendering both rely on *floating-point* numbers.
  11. However, in computing, floating-point numbers have **limited precision and range**.
  12. This can be a problem for games with huge worlds, such as space or planetary-scale
  13. simulation games.
  14. Precision is the greatest when the value is close to ``0.0``. Precision becomes
  15. gradually lower as the value increases or decreases away from ``0.0``. This
  16. occurs every time the floating-point number's *exponent* increases, which
  17. happens when the floating-point number surpasses a power of 2 value (2, 4, 8,
  18. 16, …). Every time this occurs, the number's minimum step will *increase*,
  19. resulting in a loss of precision.
  20. In practice, this means that as the player moves away from the world origin
  21. (``Vector2(0, 0)`` in 2D games or ``Vector3(0, 0, 0)`` in 3D games), precision
  22. will decrease.
  23. This loss of precision can result in objects appearing to "vibrate" when far
  24. away from the world origin, as the model's position will snap to the
  25. nearest value that can be represented in a floating-point number. This can also
  26. result in physics glitches that only occur when the player is far from the world
  27. origin.
  28. The range determines the minimum and maximum values that can be stored in the
  29. number. If the player tries to move past this range, they will simply not be
  30. able to. However, in practice, floating-point precision almost always becomes
  31. a problem before the range does.
  32. The range and precision (minimum step between two exponent intervals) are
  33. determined by the floating-point number type. The *theoretical* range allows
  34. extremely high values to be stored in single-precision floats, but with very low
  35. precision. In practice, a floating-point type that cannot represent all integer
  36. values is not very useful. At extreme values, precision becomes so low that the
  37. number cannot even distinguish two separate *integer* values from each other.
  38. This is the range where individual integer values can be represented in a
  39. floating-point number:
  40. - **Single-precision float range (represent all integers):** Between -16,777,216 and 16,777,216
  41. - **Double-precision float range (represent all integers):** Between -9 quadrillon and 9 quadrillon
  42. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  43. | Range | Single step | Double step | Comment |
  44. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  45. | [1; 2] | ~0.0000001 | ~1e-15 | Precision becomes greater near 0.0 (this table is abbreviated). |
  46. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  47. | [2; 4] | ~0.0000002 | ~1e-15 | |
  48. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  49. | [4; 8] | ~0.0000005 | ~1e-15 | |
  50. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  51. | [8; 16] | ~0.000001 | ~1e-14 | |
  52. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  53. | [16; 32] | ~0.000002 | ~1e-14 | |
  54. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  55. | [32; 64] | ~0.000004 | ~1e-14 | |
  56. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  57. | [64; 128] | ~0.000008 | ~1e-13 | |
  58. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  59. | [128; 256] | ~0.000015 | ~1e-13 | |
  60. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  61. | [256; 512] | ~0.00003 | ~1e-13 | |
  62. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  63. | [512; 1024] | ~0.00006 | ~1e-12 | |
  64. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  65. | [1024; 2048] | ~0.0001 | ~1e-12 | |
  66. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  67. | [2048; 4096] | ~0.0002 | ~1e-12 | Maximum *recommended* single-precision range for a first-person 3D game |
  68. | | | | without rendering artifacts or physics glitches. |
  69. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  70. | [4096; 8192] | ~0.0005 | ~1e-12 | Maximum *recommended* single-precision range for a third-person 3D game |
  71. | | | | without rendering artifacts or physics glitches. |
  72. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  73. | [8192; 16384] | ~0.001 | ~1e-12 | |
  74. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  75. | [16384; 32768] | ~0.0019 | ~1e-11 | Maximum *recommended* single-precision range for a top-down 3D game |
  76. | | | | without rendering artifacts or physics glitches. |
  77. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  78. | [32768; 65536] | ~0.0039 | ~1e-11 | Maximum *recommended* single-precision range for any 3D game. Double |
  79. | | | | precision (large world coordinates) is usually required past this point. |
  80. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  81. | [65536; 131072] | ~0.0078 | ~1e-11 | |
  82. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  83. | [131072; 262144] | ~0.0156 | ~1e-10 | |
  84. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  85. | > 262144 | > ~0.0313 | ~1e-10 (0.0000000001) | Double-precision remains far more precise than single-precision |
  86. | | | | past this value. |
  87. +----------------------+-----------------------+-----------------------+-----------------------------------------------------------------------------+
  88. When using single-precision floats, it is possible to go past the suggested
  89. ranges, but more visible artifacting will occur and physics glitches will be
  90. more common (such as the player not walking straight in certain directions).
  91. .. seealso::
  92. See the `Demystifying Floating Point Precision <https://blog.demofox.org/2017/11/21/>`__
  93. article for more information.
  94. How large world coordinates work
  95. --------------------------------
  96. Large world coordinates (also known as **double-precision physics**) increase
  97. the precision level of all floating-point computations within the engine.
  98. By default, :ref:`class_float` is 64-bit in GDScript, but :ref:`class_Vector2`,
  99. :ref:`class_Vector3` and :ref:`class_Vector4` are 32-bit. This means that the
  100. precision of vector types is much more limited. To resolve this, we can increase
  101. the number of bits used to represent a floating-point number in a Vector type.
  102. This results in an *exponential* increase in precision, which means the final
  103. value is not just twice as precise, but potentially thousands of times more
  104. precise at high values. The maximum value that can be represented is also
  105. greatly increased by going from a single-precision float to a double-precision
  106. float.
  107. To avoid model snapping issues when far away from the world origin, Godot's 3D
  108. rendering engine will increase its precision for rendering operations when large
  109. world coordinates are enabled. The shaders do not use double-precision floats
  110. for performance reasons, but an `alternative solution <https://github.com/godotengine/godot/pull/66178>`__
  111. is used to emulate double precision for rendering using single-precision floats.
  112. .. note::
  113. Enabling large world coordinates comes with a performance and memory usage
  114. penalty, especially on 32-bit CPUs. Only enable large world coordinates if
  115. you actually need them.
  116. This feature is tailored towards mid-range/high-end desktop platforms. Large
  117. world coordinates may not perform well on low-end mobile devices, unless you
  118. take steps to reduce CPU usage with other means (such as decreasing the
  119. number of physics ticks per second).
  120. On low-end platforms, an *origin shifting* approach can be used instead to
  121. allow for large worlds without using double-precision physics and rendering.
  122. Origin shifting works with single-precision floats, but it introduces more
  123. complexity to game logic, especially in multiplayer games. Therefore, origin
  124. shifting is not detailed on this page.
  125. Who are large world coordinates for?
  126. ------------------------------------
  127. Large world coordinates are typically required for 3D space or planetary-scale
  128. simulation games. This extends to games that require supporting *very* fast
  129. movement speeds, but also very slow *and* precise movements at times.
  130. On the other hand, it's important to only use large world coordinates when
  131. actually required (for performance reasons). Large world coordinates are usually
  132. **not** required for:
  133. - 2D games, as precision issues are usually less noticeable.
  134. - Games with small-scale or medium-scale worlds.
  135. - Games with large worlds, but split into different levels with loading
  136. sequences in between. You can center each level portion around the world
  137. origin to avoid precision issues without a performance penalty.
  138. - Open world games with a *playable on-foot area* not exceeding 8192×8192 meters
  139. (centered around the world origin). As shown in the above table, the level of
  140. precision remains acceptable within that range, even for a first-person game.
  141. **If in doubt**, you probably don't need to use large world coordinates in your
  142. project. For reference, most modern AAA open world titles don't use a large
  143. world coordinates system and still rely on single-precision floats for both
  144. rendering and physics.
  145. Enabling large world coordinates
  146. --------------------------------
  147. This process requires recompiling the editor and all export template binaries
  148. you intend to use. If you only intend to export your project in release mode,
  149. you can skip the compilation of debug export templates. In any case, you'll need
  150. to compile an editor build so you can test your large precision world without
  151. having to export the project every time.
  152. See the :ref:`Compiling <toc-devel-compiling>` section for compiling
  153. instructions for each target platform. You will need to add the ``precision=double``
  154. SCons option when compiling the editor and export templates.
  155. The resulting binaries will be named with a ``.double`` suffix to distinguish
  156. them from single-precision binaries (which lack any precision suffix). You can
  157. then specify the binaries as custom export templates in your project's export
  158. presets in the Export dialog.
  159. Compatibility between single-precision and double-precision builds
  160. -------------------------------------------------------------------
  161. When saving a *binary* resource using the :ref:`class_ResourceSaver` singleton,
  162. a special flag is stored in the file if the resource was saved using a build
  163. that uses double-precision numbers. As a result, all binary resources will
  164. change on disk when you switch to a double-precision build and save over them.
  165. Both single-precision and double-precision builds support using the
  166. :ref:`class_ResourceLoader` singleton on resources that use this special flag.
  167. This means single-precision builds can load resources saved using
  168. double-precision builds and vice versa. Text-based resources don't store a
  169. double-precision flag, as they don't require such a flag for correct reading.
  170. Known incompatibilities
  171. ^^^^^^^^^^^^^^^^^^^^^^^
  172. - In a networked multiplayer game, the server and all clients should be using
  173. the same build type to ensure precision remains consistent across clients.
  174. Using different build types *may* work, but various issues can occur.
  175. - The GDExtension API changes in an incompatible way in double-precision builds.
  176. This means extensions **must** be rebuilt to work with double-precision
  177. builds. On the extension developer's end, the ``REAL_T_IS_DOUBLE`` define is
  178. enabled when building a GDExtension with ``precision=double``.
  179. ``real_t`` can be used as an alias for ``float`` in single-precision builds,
  180. and ``double`` in double-precision builds.
  181. Limitations
  182. -----------
  183. Since 3D rendering shaders don't actually use double-precision floats, there are
  184. some limitations when it comes to 3D rendering precision:
  185. - Shaders using the ``skip_vertex_transform`` or ``world_vertex_coords`` don't
  186. benefit from increased precision.
  187. - :ref:`Triplanar mapping <doc_standard_material_3d_triplanar_mapping>` doesn't
  188. benefit from increased precision. Materials using triplanr mapping will exhibit
  189. visible jittering when far away from the world origin.
  190. 2D rendering currently doesn't benefit from increased precision when large world
  191. coordinates are enabled. This can cause visible model snapping to occur when
  192. far away from the world origin (starting from a few million pixels at typical
  193. zoom levels). 2D physics calculations will still benefit from increased
  194. precision though.