using_multimesh.rst 4.3 KB

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