using_multimesh.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Multimesh example
  37. -----------------
  38. Here is an example of using a MultiMesh from code. Languages other than GDScript may be more
  39. efficient for millions of objects, but for a few thousands, GDScript should be fine.
  40. .. tabs::
  41. .. code-tab:: gdscript GDScript
  42. extends MultiMeshInstance
  43. func _ready():
  44. # Create the multimesh.
  45. multimesh = MultiMesh.new()
  46. # Set the format first.
  47. multimesh.transform_format = MultiMesh.TRANSFORM_3D
  48. multimesh.color_format = MultiMesh.COLOR_NONE
  49. multimesh.custom_data_format = MultiMesh.CUSTOM_DATA_NONE
  50. # Then resize (otherwise, changing the format is not allowed).
  51. multimesh.instance_count = 10000
  52. # Maybe not all of them should be visible at first.
  53. multimesh.visible_instance_count = 1000
  54. # Set the transform of the instances.
  55. for i in multimesh.visible_instance_count:
  56. multimesh.set_instance_transform(i, Transform(Basis(), Vector3(i * 20, 0, 0)))
  57. .. code-tab:: csharp C#
  58. using Godot;
  59. using System;
  60. public class YourClassName : MultiMeshInstance
  61. {
  62. public override void _Ready()
  63. {
  64. // Create the multimesh.
  65. Multimesh = new MultiMesh();
  66. // Set the format first.
  67. Multimesh.TransformFormat = MultiMesh.TransformFormatEnum.Transform3d;
  68. Multimesh.ColorFormat = MultiMesh.ColorFormatEnum.None;
  69. Multimesh.CustomDataFormat = MultiMesh.CustomDataFormatEnum.None;
  70. // Then resize (otherwise, changing the format is not allowed)
  71. Multimesh.InstanceCount = 1000;
  72. // Maybe not all of them should be visible at first.
  73. Multimesh.VisibleInstanceCount = 1000;
  74. // Set the transform of the instances.
  75. for (int i = 0; i < Multimesh.VisibleInstanceCount; i++)
  76. {
  77. Multimesh.SetInstanceTransform(i, new Transform(Basis.Identity, new Vector3(i * 20, 0, 0)));
  78. }
  79. }
  80. }