Using ImmediateGeometry

    Generating complex geometry (several thousand vertices) with this node is inefficient, even if it’s done only once. Instead, it is designed to generate simple geometry that changes every frame.

    Before starting, you should clear the geometry by calling . This ensures that you are not building upon the geometry from the previous frame. If you want to keep geometry between frames, do not call clear().

    Once you have called begin() you are ready to start adding vertices. You add vertices one at a time. First you add vertex specific attributes such as normals or UVs using set_****() (e.g. set_normal()). Then you call add_vertex() to add a vertex with those attributes. For example:

    GDScript

    Finally, once you have added all your vertices call end() to signal that you have finished generating the mesh.

    The example code below draws a single triangle.

    1. func _process(_delta):
    2. # Clean up before drawing.
    3. clear()
    4. # Begin draw.
    5. # Prepare attributes for add_vertex.
    6. set_normal(Vector3(0, 0, 1))
    7. set_uv(Vector2(0, 0))
    8. add_vertex(Vector3(-1, -1, 0))
    9. set_uv(Vector2(0, 1))
    10. add_vertex(Vector3(-1, 1, 0))
    11. set_normal(Vector3(0, 0, 1))
    12. set_uv(Vector2(1, 1))
    13. add_vertex(Vector3(1, 1, 0))