Work in progress

The content of this page was not yet updated for Godot 4.3 and may be outdated. If you know how to improve this page or you can confirm that it’s up to date, feel free to open a pull request.

Optimization using MultiMeshes

For large amount of instances (in the thousands), that need to be constantly processed (and certain amount of control needs to be retained), using servers directly is the recommended optimization.

When the amount of objects reach the hundreds of thousands or millions, none of these approaches are efficient anymore. Still, depending on the requirements, there is one more optimization possible.

MultiMeshes

A MultiMesh is a single draw primitive that can draw up to millions of objects in one go. It’s extremely efficient because it uses the GPU hardware to do this.

The only drawback is that there is no screen or frustum culling possible for individual instances. This means, that millions of objects will be always or never drawn, depending on the visibility of the whole MultiMesh. It is possible to provide a custom visibility rect for them, but it will always be all-or-none visibility.

If the objects are simple enough (just a couple of vertices), this is generally not much of a problem as most modern GPUs are optimized for this use case. A workaround is to create several MultiMeshes for different areas of the world.

It is also possible to execute some logic inside the vertex shader (using the INSTANCE_ID or INSTANCE_CUSTOM built-in constants). For an example of animating thousands of objects in a MultiMesh, see the Animating thousands of fish tutorial. Information to the shader can be provided via textures (there are floating-point Image formats which are ideal for this).

Another alternative is to use a GDExtension and C++, which should be extremely efficient (it’s possible to set the entire state for all objects using linear memory via the RenderingServer.multimesh_set_buffer() function). This way, the array can be created with multiple threads, then set in one call, providing high cache efficiency.

Finally, it’s not required to have all MultiMesh instances visible. The amount of visible ones can be controlled with the MultiMesh.visible_instance_count property. The typical workflow is to allocate the maximum amount of instances that will be used, then change the amount visible depending on how many are currently needed.

Multimesh example

Here is an example of using a MultiMesh from code. Languages other than GDScript may be more efficient for millions of objects, but for a few thousands, GDScript should be fine.

GDScriptC#

  1. extends MultiMeshInstance3D
  2. func _ready():
  3. # Create the multimesh.
  4. multimesh = MultiMesh.new()
  5. # Set the format first.
  6. multimesh.transform_format = MultiMesh.TRANSFORM_3D
  7. # Then resize (otherwise, changing the format is not allowed).
  8. multimesh.instance_count = 10000
  9. # Maybe not all of them should be visible at first.
  10. multimesh.visible_instance_count = 1000
  11. # Set the transform of the instances.
  12. for i in multimesh.visible_instance_count:
  13. multimesh.set_instance_transform(i, Transform3D(Basis(), Vector3(i * 20, 0, 0)))
  1. using Godot;
  2. public partial class MyMultiMeshInstance3D : MultiMeshInstance3D
  3. {
  4. public override void _Ready()
  5. {
  6. // Create the multimesh.
  7. Multimesh = new MultiMesh();
  8. // Set the format first.
  9. Multimesh.TransformFormat = MultiMesh.TransformFormatEnum.Transform3D;
  10. // Then resize (otherwise, changing the format is not allowed)
  11. Multimesh.InstanceCount = 1000;
  12. // Maybe not all of them should be visible at first.
  13. Multimesh.VisibleInstanceCount = 1000;
  14. // Set the transform of the instances.
  15. for (int i = 0; i < Multimesh.VisibleInstanceCount; i++)
  16. {
  17. Multimesh.SetInstanceTransform(i, new Transform3D(Basis.Identity, new Vector3(i * 20, 0, 0)));
  18. }
  19. }
  20. }

User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next