merge_groups.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. .. _doc_merge_groups:
  2. MergeGroups
  3. ===========
  4. The ``MergeGroup`` node allows the user to merge similar meshes so that they can
  5. be grouped into a smaller number of nodes, and render using a single draw call
  6. (per :ref:`Material <class_Material>`). This can greatly increase performance when
  7. used with care.
  8. Explanation
  9. -----------
  10. Usually individual meshes in Godot are represented by their own
  11. :ref:`VisualInstance <class_VisualInstance>` in the
  12. :ref:`SceneTree <class_SceneTree>`, such as :ref:`MeshInstance <class_MeshInstance>`.
  13. These:
  14. - Move independently
  15. - Cull independently
  16. - Render individually
  17. In terms of performance however, each ``VisualInstance`` adds a cost, both in
  18. terms of management, and in terms of rendering. Each mesh (and each surface on a
  19. ``MeshInstance`` is rendered as an individual mesh) will require an expensive
  20. draw call to the graphics API, and often incur expensive rendering state changes.
  21. If instead we can identify groups of meshes that could be potentially be
  22. rendered together, and merge them ahead of time (either at design time, or
  23. during level load), this can potentially lighten the load on the CPU and GPU,
  24. and increase frame rates.
  25. The merging trade-off
  26. ^^^^^^^^^^^^^^^^^^^^^
  27. Lowering draw calls and reducing state changes tends to increase performance.
  28. However, merged meshes are culled as a unit, and therefore if any part of the
  29. merged mesh is visible in the view frustum, the cost of rendering all parts of
  30. the mesh are incurred. For this reason it is recommended that you evaluate
  31. performance both before and after merging, in order to find the best balance in
  32. each case.
  33. .. tip::
  34. Remember that the bottlenecks may be different on different hardware.
  35. Be sure to test on the lowest-end platforms you're targeting for your
  36. project, especially mobile devices and integrated graphics.
  37. Requirements
  38. ------------
  39. There are some requirements for any group of meshes to be merged.
  40. The most important requirement is that the source meshes must not move in
  41. relation to each other. They must be either be completely static (non-moving)
  42. or move together only as a single unit. This is because once several meshes are
  43. merged into one, the relative transforms of the source meshes cannot change,
  44. the transforms are baked into the new geometry.
  45. The simplest candidate for merging is static level geometry, such as walls,
  46. floors, tables etc. But groups of meshes that move "as one" are also good
  47. candidates. Examples might be a pirate ship, or spacecraft.
  48. Using MergeGroups
  49. -----------------
  50. After adding a ``MergeGroup`` node into your ``SceneTree``, move all the
  51. VisualInstances you want to be merged so that they are parented by the
  52. ``MergeGroup`` (either as children, or grandchildren). By default, if you now
  53. run the scene, the engine will automatically merge suitable meshes as the
  54. ``MergeGroup`` enters the ``SceneTree``.
  55. .. image:: img/merge_group_house.webp
  56. You can change the transform of the ``MergeGroup`` at runtime, and the whole
  57. group will move as a unit (``MergeGroup`` is derived from
  58. :ref:`Spatial <class_Spatial>`, so can be manipulated in all the regular ways,
  59. such as changing transform or visibility).
  60. Mesh similarity
  61. ---------------
  62. It is important to note that not all meshes (or more accurately, mesh surfaces,
  63. because merging takes place at the level of mesh surfaces) can be merged
  64. together. There are strict limitations for the mesh surfaces to match.
  65. All the following properties must match for merging to take place:
  66. - Material
  67. - Shadow casting settings
  68. - Visibility
  69. - Layer mask
  70. - Portal settings
  71. - Cull margin
  72. - Lightmap settings
  73. Usually this means only multiple instances of the same mesh will be merged (such
  74. as a group of walls), but similar variations of mesh (e.g. different sized
  75. tables) can sometimes be merged. Additionally, if you reuse materials (using
  76. e.g. a "wood" material in several different meshes), these can often be merged.
  77. Exceptions
  78. ----------
  79. When you place a hierarchy of nodes underneath a ``MergeGroup``, such as a
  80. pirate ship, there may be exceptions - meshes that you *do not* want merged.
  81. This may be because, for instance, you may want them to move independently of
  82. the ``MergeGroup``. An example might be a steering wheel, or sail.
  83. Godot allows you fine control over exactly which nodes should be considered for
  84. merging, via the ``merging_mode`` property which is present in the inspector
  85. for every ``Spatial``, inside the ``Misc`` tab.
  86. Merging modes:
  87. - ``Inherit`` - Inherit the setting from the parent. The default for the scene root node is ``On``.
  88. - ``On`` - Change the mode of the node (and any inheriting children) to allow merging.
  89. - ``Off`` - Change the mode of the node (and any inheriting children) to disallow merging.
  90. This means that if you, e.g. set the mode of a steering wheel to ``Off``, it
  91. will not be considered for merging, and neither will any children or
  92. grandchildren of the steering wheel (unless one of them explicitly reactivates
  93. merging with an ``On`` ``merge_mode``).
  94. Ways to use MergeGroups
  95. -----------------------
  96. There are three ways to use MergeGroups:
  97. AutoMerge
  98. ^^^^^^^^^
  99. The ``MergeGroup`` will attempt to merge any descendent merges as it enters the
  100. ``SceneTree``.
  101. This is the simplest method, and the best introduction to merging.
  102. Baking in the Editor
  103. ^^^^^^^^^^^^^^^^^^^^
  104. When the ``MergeGroup`` is selected in the editor, a new ``bake`` button should
  105. appear in the toolbar. This allows you to bake the entire merged scene out to a
  106. file (.tscn (text scene) or .scn (binary scene)) at *design time* rather than at
  107. *runtime*. This can be used creatively to build sensible composite objects which
  108. are later used to compose a game level (this general approach is often known as
  109. "kitbashing").
  110. Additionally, baking in advance offers a great way to preview what will happen
  111. when merging at runtime. It allows you to open the merged scene, and see which
  112. meshes were successfully merged, and which were problematic.
  113. .. figure:: img/bake_merge_group.webp
  114. :align: center
  115. Pros
  116. ~~~~
  117. - Allows you easily see the results of merging
  118. - Allows you to further build levels using these merged scenes
  119. - No time is taken by the merging process at runtime
  120. Cons
  121. ~~~~
  122. - Merged scenes typically require substantially more storage than the original meshes
  123. - The size of your exported game will likely increase
  124. - Larger merged scenes may take longer to load
  125. - Larger merged scenes may take up more RAM at runtime, especially on the GPU
  126. If you merge, e.g. 10 boxes, the merged scene will have to store 10 times as
  127. much geometry data, as the polygons are duplicated. More storage means your
  128. exported game will be larger, the data will take longer to load, and consume
  129. more RAM at runtime. For this reason, baking in advance tends to be more
  130. practical with low poly meshes and art styles.
  131. .. tip::
  132. Due to the increased storage requirements, it is recommended that, wherever possible,
  133. you bake scenes in binary format (``.scn``) rather than text (``.tscn``).
  134. This is because binary scenes are much more compact in terms of storage,
  135. while also being faster to load and save.
  136. Manually at runtime
  137. ^^^^^^^^^^^^^^^^^^^
  138. If the ``automerge`` property of a ``MergeGroup`` is disabled, then the node
  139. will do nothing at runtime until you call its ``merge_meshes()`` function.
  140. Triggering merging manually in this way has two major use cases:
  141. 1. Procedural levels. If you place objects (e.g. trees, boxes) at runtime using
  142. script, rather than placing them at design time, then you want a way to *delay*
  143. merging until after your placement is complete. Manually calling
  144. ``merge_meshes()`` allows this.
  145. 2. Merging parameters. These can be set via the ``MergeGroup::set_param()`` and
  146. ``MergeGroup::set_param_enabled()`` functions, prior to calling
  147. ``merge_meshes()``.
  148. Merging parameters
  149. ------------------
  150. Although the default operation of the ``MergeGroup`` works well in many
  151. circumstances, there are a number of parameters which can be altered prior to
  152. merging in order to access more advanced features. These can be set via the
  153. ``MergeGroup::set_param()`` functions, however the easiest way to visualize them
  154. is via the ``bake`` method, which displays a dialog allowing you to modify
  155. parameters. These are described in detail in the documentation for
  156. ``MergeGroup``.
  157. Merging by locality - grouping and splitting
  158. --------------------------------------------
  159. When merging large numbers of meshes across a large map, sometimes the merging
  160. goes too far, and results in a huge mesh that is too difficult to cull (as part
  161. is always in the view frustum).
  162. For example if you merge every tree in a forest, regardless of your viewpoint,
  163. the whole forest will be rendered. Although rendering each tree individually
  164. would be inefficient, rendering the whole forest in all cases is also
  165. inefficient, but in a different way.
  166. You may think instead to create several ``MergeGroups`` spread across the
  167. forest, and only merge the trees in the local area. This would create an ideal
  168. balance between reduced drawcalls, but still allowing broad scale culling to
  169. take effect. The downside is that this kind of thing could be extremely labour
  170. intensive in terms of scene design.
  171. For this reason, ``MergeGroup`` has built in functionality for helping deal with
  172. this problem automatically.
  173. .. note::
  174. Grouping and splitting is considered advanced, so is only available via the
  175. manual method (setting parameters and calling ``merge_meshes()`` explicitly),
  176. or via the bake method.
  177. Grouping
  178. ^^^^^^^^
  179. One of the simplest ways to get the advantages of merging without the
  180. disadvantages of loss of culling resolution is grouping. The ``group_size``
  181. parameter defaults to zero, which indicates that all suitable meshes be merged.
  182. But if you set ``group_size`` to e.g. 2, then only the closest pairs of meshes
  183. will be merged.
  184. If you have 10 trees, it will try to merge 5 pairs of trees, with each pair
  185. being as close together as possible (so that they can be culled efficiently).
  186. This same principle works even in a forest of 1,000 trees.
  187. Splitting
  188. ^^^^^^^^^
  189. The alternative approach to grouping is splitting. Splitting takes place as a
  190. post-step, *after* all the meshes have been merged.
  191. Following our forest example, if we assume our 1,000 tree forest has been merged
  192. into 1 huge mesh, splitting allows us to specify a 3D grid (with a horizontal
  193. number of splits and vertical number of splits) and the ``MergeGroup`` will
  194. attempt to split the large mesh into smaller ones more suitable for culling.
  195. For example, with 5 horizontal splits and 1 vertical split we should get 25
  196. final meshes (5 x 5 x 1) which should give us enough resolution for some decent
  197. culling for our forest.
  198. .. note::
  199. The reason for allowing separate horizontal and vertical splits is that
  200. many games are based on flat ground, where horizontal splits may be more
  201. important than vertical. Increasing vertical splits may be counter-productive.
  202. .. tip::
  203. Splitting can make a lot of sense if you are building a world with voxels.
  204. Other functionality
  205. ^^^^^^^^^^^^^^^^^^^
  206. Also via the ``MergeGroup`` parameters, ``CSG`` nodes (e.g. :ref:`CSGBox <class_CSGBox>`)
  207. and :ref:`GridMap <class_GridMap>` nodes can optionally be converted to a regular
  208. :ref:`MeshInstance <class_MeshInstance>`. This allows them to be merged like any other mesh.