large_world_coordinates.rst 16 KB

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