33Using ImmediateMesh
44===================
55
6- Unlike the SurfaceTool or ArrayMesh, :ref: `ImmediateMesh <class_ImmediateMesh >` is an actual
7- node. Being a node makes it quick to add to a scene and get visual output. It uses an OpenGL 1.x-style
8- API like SurfaceTool, but it's actually designed to create meshes on the fly .
6+ The :ref: `ImmediateMesh <class_ImmediateMesh >` is a convenient tool to create
7+ dynamic geometry using an OpenGL 1.x-style API. Which makes it both approachable
8+ to use and efficient for meshes which need to be updated every frame .
99
10- Generating complex geometry (several thousand vertices) with this node is inefficient, even if it's
10+ Generating complex geometry (several thousand vertices) with this tool is inefficient, even if it's
1111done only once. Instead, it is designed to generate simple geometry that changes every frame.
1212
13- Before starting, you should clear the geometry by calling ``clear_surfaces() ``. This ensures that
14- you are not building upon the geometry from the previous frame. If you want to keep geometry between frames, do
15- not call ``clear_surfaces() ``.
13+ First, you need to create a :ref: `MeshInstance3D <class_meshinstance3d >` and add
14+ an :ref: `ImmediateMesh <class_ImmediateMesh >` to it in the Inspector.
1615
17- To begin generating geometry you must call ``surface_begin() ``. ``surface_begin() `` takes a ``PrimitiveType `` as an argument.
18- ``PrimitiveType `` is an OpenGL concept that instructs the GPU how to arrange the primitive based on the
19- vertices given whether it is triangles, lines, points, etc. A complete list can be found under
16+ Next, add a script to the MeshInstance3D. The code for the ImmediateMesh should
17+ go in the ``_process() `` function if you want it to update each frame, or in the
18+ ``_ready() `` function if you want to create the mesh once and not update it. If
19+ you only generate a surface once, the ImmediateMesh is just as efficient as any
20+ other kind of mesh as the generated mesh is cached and reused.
21+
22+ To begin generating geometry you must call ``surface_begin() ``.
23+ ``surface_begin() `` takes a ``PrimitiveType `` as an argument. ``PrimitiveType ``
24+ instructs the GPU how to arrange the primitive based on the vertices given
25+ whether it is triangles, lines, points, etc. A complete list can be found under
2026the :ref: `Mesh <class_mesh >` class reference page.
2127
22- Once you have called ``surface_begin() `` you are ready to start adding vertices. You add vertices one at a time.
23- First you add vertex specific attributes such as 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. For example:
28+ Once you have called ``surface_begin() `` you are ready to start adding vertices.
29+ You add vertices one at a time. First you add vertex specific attributes such as
30+ normals or UVs using ``surface_set_****() `` (e.g. ``surface_set_normal() ``).
31+ Then you call ``surface_add_vertex() `` to add a vertex with those attributes.
32+ For example:
2533
2634.. tabs ::
2735 .. code-tab :: gdscript GDScript
@@ -31,37 +39,63 @@ Then you call ``surface_add_vertex()`` to add a vertex with those attributes. Fo
3139 surface_set_uv(Vector2(1, 1))
3240 surface_add_vertex(Vector3(0, 0, 1))
3341
34- Only attributes added before the call to ``surface_add_vertex() `` will be included in that vertex.
42+ Only attributes added before the call to ``surface_add_vertex() `` will be
43+ included in that vertex. If you add an attribute twice before calling
44+ ``surface_add_vertex() ``, only the second call will be used.
3545
36- Finally, once you have added all your vertices call ``surface_end() `` to signal that you have finished generating the mesh.
46+ Finally, once you have added all your vertices call ``surface_end() `` to signal
47+ that you have finished generating the surface. You can call ``surface_begin() ``
48+ and ``surface_end() `` multiple times to generate multiple surfaces for the mesh.
3749
38- The example code below draws a single triangle.
50+ The example code below draws a single triangle in the `` _ready() `` function.
3951
4052.. tabs ::
4153 .. code-tab :: gdscript GDScript
4254
43- extends ImmediateMesh
44-
45- func _process(_delta):
46- # Clean up before drawing.
47- clear_surfaces()
55+ extends MeshInstance3D
4856
57+ func _ready():
4958 # Begin draw.
50- surface_begin(Mesh.PRIMITIVE_TRIANGLES)
51-
52- # Prepare attributes for surface_add_vertex .
53- surface_set_normal(Vector3(0, 0, 1))
54- surface_set_uv(Vector2(0, 0))
59+ mesh. surface_begin(Mesh.PRIMITIVE_TRIANGLES)
60+
61+ # Prepare attributes for add_vertex .
62+ mesh. surface_set_normal(Vector3(0, 0, 1))
63+ mesh. surface_set_uv(Vector2(0, 0))
5564 # Call last for each vertex, adds the above attributes.
56- surface_add_vertex(Vector3(-1, -1, 0))
65+ mesh. surface_add_vertex(Vector3(-1, -1, 0))
5766
58- surface_set_normal(Vector3(0, 0, 1))
59- surface_set_uv(Vector2(0, 1))
60- surface_add_vertex(Vector3(-1, 1, 0))
67+ mesh. surface_set_normal(Vector3(0, 0, 1))
68+ mesh. surface_set_uv(Vector2(0, 1))
69+ mesh. surface_add_vertex(Vector3(-1, 1, 0))
6170
62- surface_set_normal(Vector3(0, 0, 1))
63- surface_set_uv(Vector2(1, 1))
64- surface_add_vertex(Vector3(1, 1, 0))
71+ mesh. surface_set_normal(Vector3(0, 0, 1))
72+ mesh. surface_set_uv(Vector2(1, 1))
73+ mesh. surface_add_vertex(Vector3(1, 1, 0))
6574
6675 # End drawing.
67- surface_end()
76+ mesh.surface_end()
77+
78+ The ImmediateMesh can also be used across frames. Each time you call
79+ ``surface_begin() `` and ``surface_end() ``, you are adding a new surface to the
80+ ImmediateMesh. If you want to recreate the mesh from scratch each frame, call
81+ ``surface_clear() `` before calling ``surface_begin() ``.
82+
83+ .. tabs ::
84+ .. code-tab :: gdscript GDScript
85+
86+ extends MeshInstance3D
87+
88+ func _process(delta):
89+
90+ # Clean up before drawing.
91+ mesh.clear_surfaces()
92+
93+ # Begin draw.
94+ mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
95+
96+ # Draw mesh.
97+
98+ # End drawing.
99+ mesh.surface_end()
100+
101+ The above code will dynamically create and draw a single surface each frame.
0 commit comments