optimizing_3d_performance.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. :article_outdated: True
  2. .. meta::
  3. :keywords: optimization
  4. .. _doc_optimizing_3d_performance:
  5. Optimizing 3D performance
  6. =========================
  7. Culling
  8. =======
  9. Godot will automatically perform view frustum culling in order to prevent
  10. rendering objects that are outside the viewport. This works well for games that
  11. take place in a small area, however things can quickly become problematic in
  12. larger levels.
  13. Occlusion culling
  14. ~~~~~~~~~~~~~~~~~
  15. Walking around a town for example, you may only be able to see a few buildings
  16. in the street you are in, as well as the sky and a few birds flying overhead. As
  17. far as a naive renderer is concerned however, you can still see the entire town.
  18. It won't just render the buildings in front of you, it will render the street
  19. behind that, with the people on that street, the buildings behind that. You
  20. quickly end up in situations where you are attempting to render 10× or 100× more
  21. than what is visible.
  22. Things aren't quite as bad as they seem, because the Z-buffer usually allows the
  23. GPU to only fully shade the objects that are at the front. This is called *depth
  24. prepass* and is enabled by default in Godot when using the GLES3 renderer.
  25. However, unneeded objects are still reducing performance.
  26. One way we can potentially reduce the amount to be rendered is to take advantage
  27. of occlusion. As of Godot 3.2.2, there is no built in support for occlusion in
  28. Godot. However, with careful design you can still get many of the advantages.
  29. For instance, in our city street scenario, you may be able to work out in advance
  30. that you can only see two other streets, ``B`` and ``C``, from street ``A``.
  31. Streets ``D`` to ``Z`` are hidden. In order to take advantage of occlusion, all
  32. you have to do is work out when your viewer is in street ``A`` (perhaps using
  33. Godot Areas), then you can hide the other streets.
  34. This is a manual version of what is known as a "potentially visible set". It is
  35. a very powerful technique for speeding up rendering. You can also use it to
  36. restrict physics or AI to the local area, and speed these up as well as
  37. rendering.
  38. .. note::
  39. In some cases, you may have to adapt your level design to add more occlusion
  40. opportunities. For example, you may have to add more walls to prevent the player
  41. from seeing too far away, which would decrease performance due to the lost
  42. opportunities for occlusion culling.
  43. Other occlusion techniques
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. There are other occlusion techniques such as portals, automatic PVS, and
  46. raster-based occlusion culling. Some of these may be available through add-ons
  47. and may be available in core Godot in the future.
  48. Transparent objects
  49. ~~~~~~~~~~~~~~~~~~~
  50. Godot sorts objects by :ref:`Material <class_Material>` and :ref:`Shader
  51. <class_Shader>` to improve performance. This, however, can not be done with
  52. transparent objects. Transparent objects are rendered from back to front to make
  53. blending with what is behind work. As a result,
  54. **try to use as few transparent objects as possible**. If an object has a
  55. small section with transparency, try to make that section a separate surface
  56. with its own material.
  57. For more information, see the :ref:`GPU optimizations <doc_gpu_optimization>`
  58. doc.
  59. Level of detail (LOD)
  60. =====================
  61. In some situations, particularly at a distance, it can be a good idea to
  62. **replace complex geometry with simpler versions**. The end user will probably
  63. not be able to see much difference. Consider looking at a large number of trees
  64. in the far distance. There are several strategies for replacing models at
  65. varying distance. You could use lower poly models, or use transparency to
  66. simulate more complex geometry.
  67. Billboards and imposters
  68. ~~~~~~~~~~~~~~~~~~~~~~~~
  69. The simplest version of using transparency to deal with LOD is billboards. For
  70. example, you can use a single transparent quad to represent a tree at distance.
  71. This can be very cheap to render, unless of course, there are many trees in
  72. front of each other. In which case transparency may start eating into fill rate
  73. (for more information on fill rate, see :ref:`doc_gpu_optimization`).
  74. An alternative is to render not just one tree, but a number of trees together as
  75. a group. This can be especially effective if you can see an area but cannot
  76. physically approach it in a game.
  77. You can make imposters by pre-rendering views of an object at different angles.
  78. Or you can even go one step further, and periodically re-render a view of an
  79. object onto a texture to be used as an imposter. At a distance, you need to move
  80. the viewer a considerable distance for the angle of view to change
  81. significantly. This can be complex to get working, but may be worth it depending
  82. on the type of project you are making.
  83. Use instancing (MultiMesh)
  84. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  85. If several identical objects have to be drawn in the same place or nearby, try
  86. using :ref:`MultiMesh <class_MultiMesh>` instead. MultiMesh allows the drawing
  87. of many thousands of objects at very little performance cost, making it ideal
  88. for flocks, grass, particles, and anything else where you have thousands of
  89. identical objects.
  90. Also see the :ref:`Using MultiMesh <doc_using_multimesh>` doc.
  91. Bake lighting
  92. =============
  93. Lighting objects is one of the most costly rendering operations. Realtime
  94. lighting, shadows (especially multiple lights), and GI are especially expensive.
  95. They may simply be too much for lower power mobile devices to handle.
  96. **Consider using baked lighting**, especially for mobile. This can look fantastic,
  97. but has the downside that it will not be dynamic. Sometimes, this is a trade-off
  98. worth making.
  99. In general, if several lights need to affect a scene, it's best to use
  100. :ref:`doc_using_lightmap_gi`. Baking can also improve the scene quality by adding
  101. indirect light bounces.
  102. Animation and skinning
  103. ======================
  104. Animation and vertex animation such as skinning and morphing can be very
  105. expensive on some platforms. You may need to lower the polycount considerably
  106. for animated models or limit the number of them on screen at any one time.
  107. Large worlds
  108. ============
  109. If you are making large worlds, there are different considerations than what you
  110. may be familiar with from smaller games.
  111. Large worlds may need to be built in tiles that can be loaded on demand as you
  112. move around the world. This can prevent memory use from getting out of hand, and
  113. also limit the processing needed to the local area.
  114. There may also be rendering and physics glitches due to floating point error in
  115. large worlds. You may be able to use techniques such as orienting the world
  116. around the player (rather than the other way around), or shifting the origin
  117. periodically to keep things centred around ``Vector3(0, 0, 0)``.