using_multimesh.rst 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. .. _doc_using_multimesh:
  2. Optimization using MultiMeshes
  3. ==============================
  4. For large amount of instances (in the thousands), that need to be constantly processed
  5. (and certain amount of control needs to be retained),
  6. :ref:`using servers directly <doc_using_servers>` is the recommended optimization.
  7. When the amount of objects reach the hundreds of thousands or millions,
  8. none of these approaches are efficient anymore. Still, depending on the requirements, there
  9. is one more optimization possible.
  10. MultiMeshes
  11. -----------
  12. A :ref:`MultiMesh<class_MultiMesh>` is a single draw primitive that can draw up to millions
  13. of objects in one go. It's extremely efficient because it uses the GPU hardware to do this
  14. (in OpenGL ES 2.0, it's less efficient because there is no hardware support for it, though).
  15. The only drawback is that there is no *screen* or *frustum* culling possible for individual instances.
  16. This means, that millions of objects will be *always* or *never* drawn, depending on the visibility
  17. of the whole MultiMesh. It is possible to provide a custom visibility rect for them, but it will always
  18. be *all-or-none* visibility.
  19. If the objects are simple enough (just a couple of vertices), this is generally not much of a problem
  20. as most modern GPUs are optimized for this use case. A workaround is to create several MultiMeshes
  21. for different areas of the world.
  22. It is also possible to execute some logic inside the vertex shader (using the ``INSTANCE_ID`` or
  23. ``INSTANCE_CUSTOM`` built-in constants). For an example of animating thousands of objects in a MultiMesh,
  24. see the :ref:`Animating thousands of fish <doc_animating_thousands_of_fish>` tutorial. Information
  25. to the shader can be provided via textures (there are floating point :ref:`Image<class_Image>` formats
  26. which are ideal for this).
  27. Another alternative is to use GDNative and C++, which should be extremely efficient (it's possible
  28. to set the entire state for all objects using linear memory via the
  29. :ref:`VisualServer.multimesh_set_as_bulk_array() <class_VisualServer_method_multimesh_set_as_bulk_array>`
  30. function). This way, the array can be created with multiple threads, then set in one call, providing
  31. high cache efficiency.
  32. Finally, it's not required to have all MultiMesh instances visible. The amount of visible ones can be
  33. controlled with the :ref:`MultiMesh.visible_instance_count <class_MultiMesh_property_visible_instance_count>`
  34. property. The typical workflow is to allocate the maximum amount of instances that will be used,
  35. then change the amount visible depending on how many are currently needed.
  36. .. note:: The :ref:`MultiMesh.visible_instance_count <class_MultiMesh_property_visible_instance_count>`
  37. property was first added in Godot 3.1.1-stable.
  38. Multimesh example
  39. -----------------
  40. Here is an example of using a MultiMesh from code (using GDScript). Other languages may be more
  41. efficient for millions of objects, but for a few thousands, GDScript should be fine.
  42. .. tabs::
  43. .. code-tab:: gdscript GDScript
  44. extends MultiMeshInstance
  45. func _ready():
  46. # Create the multimesh.
  47. multimesh = MultiMesh.new()
  48. # Set the format first.
  49. multimesh.transform_format = MultiMesh.TRANSFORM_3D
  50. multimesh.color_format = MultiMesh.COLOR_NONE
  51. multimesh.custom_data_format = MultiMesh.CUSTOM_DATA_NONE
  52. # Then resize (otherwise, changing the format is not allowed).
  53. multimesh.instance_count = 10000
  54. # Maybe not all of them should be visible at first.
  55. # Note: Property only available in Godot 3.1.1 or later.
  56. multimesh.visible_instance_count = 1000
  57. # Set the transform of the instances.
  58. for i in multimesh.visible_instance_count:
  59. multimesh.set_instance_transform(i, Transform(Basis(), Vector3(i * 20, 0, 0)))