immediatemesh.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. .. _doc_immediatemesh:
  2. Using ImmediateMesh
  3. ===================
  4. The :ref:`ImmediateMesh <class_ImmediateMesh>` is a convenient tool to create
  5. dynamic geometry using an OpenGL 1.x-style API. Which makes it both approachable
  6. to use and efficient for meshes which need to be updated every frame.
  7. Generating complex geometry (several thousand vertices) with this tool is inefficient, even if it's
  8. done only once. Instead, it is designed to generate simple geometry that changes every frame.
  9. First, you need to create a :ref:`MeshInstance3D <class_meshinstance3d>` and add
  10. an :ref:`ImmediateMesh <class_ImmediateMesh>` to it in the Inspector.
  11. Next, add a script to the MeshInstance3D. The code for the ImmediateMesh should
  12. go in the ``_process()`` function if you want it to update each frame, or in the
  13. ``_ready()`` function if you want to create the mesh once and not update it. If
  14. you only generate a surface once, the ImmediateMesh is just as efficient as any
  15. other kind of mesh as the generated mesh is cached and reused.
  16. To begin generating geometry you must call ``surface_begin()``.
  17. ``surface_begin()`` takes a ``PrimitiveType`` as an argument. ``PrimitiveType``
  18. instructs the GPU how to arrange the primitive based on the vertices given
  19. whether it is triangles, lines, points, etc. A complete list can be found under
  20. the :ref:`Mesh <class_mesh>` class reference page.
  21. Once you have called ``surface_begin()`` you are ready to start adding vertices.
  22. You add vertices one at a time. First you add vertex specific attributes such as
  23. normals or UVs using ``surface_set_****()`` (e.g. ``surface_set_normal()``).
  24. Then you call ``surface_add_vertex()`` to add a vertex with those attributes.
  25. For example:
  26. .. tabs::
  27. .. code-tab:: gdscript GDScript
  28. # Add a vertex with normal and uv.
  29. surface_set_normal(Vector3(0, 1, 0))
  30. surface_set_uv(Vector2(1, 1))
  31. surface_add_vertex(Vector3(0, 0, 1))
  32. Only attributes added before the call to ``surface_add_vertex()`` will be
  33. included in that vertex. If you add an attribute twice before calling
  34. ``surface_add_vertex()``, only the second call will be used.
  35. Finally, once you have added all your vertices call ``surface_end()`` to signal
  36. that you have finished generating the surface. You can call ``surface_begin()``
  37. and ``surface_end()`` multiple times to generate multiple surfaces for the mesh.
  38. The example code below draws a single triangle in the ``_ready()`` function.
  39. .. tabs::
  40. .. code-tab:: gdscript GDScript
  41. extends MeshInstance3D
  42. func _ready():
  43. # Begin draw.
  44. mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
  45. # Prepare attributes for add_vertex.
  46. mesh.surface_set_normal(Vector3(0, 0, 1))
  47. mesh.surface_set_uv(Vector2(0, 0))
  48. # Call last for each vertex, adds the above attributes.
  49. mesh.surface_add_vertex(Vector3(-1, -1, 0))
  50. mesh.surface_set_normal(Vector3(0, 0, 1))
  51. mesh.surface_set_uv(Vector2(0, 1))
  52. mesh.surface_add_vertex(Vector3(-1, 1, 0))
  53. mesh.surface_set_normal(Vector3(0, 0, 1))
  54. mesh.surface_set_uv(Vector2(1, 1))
  55. mesh.surface_add_vertex(Vector3(1, 1, 0))
  56. # End drawing.
  57. mesh.surface_end()
  58. The ImmediateMesh can also be used across frames. Each time you call
  59. ``surface_begin()`` and ``surface_end()``, you are adding a new surface to the
  60. ImmediateMesh. If you want to recreate the mesh from scratch each frame, call
  61. ``clear_surfaces()`` before calling ``surface_begin()``.
  62. .. tabs::
  63. .. code-tab:: gdscript GDScript
  64. extends MeshInstance3D
  65. func _process(delta):
  66. # Clean up before drawing.
  67. mesh.clear_surfaces()
  68. # Begin draw.
  69. mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
  70. # Draw mesh.
  71. # End drawing.
  72. mesh.surface_end()
  73. The above code will dynamically create and draw a single surface each frame.