optimizing_3d_performance.rst 6.6 KB

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