Efficiently update mesh data every frame

Godot Version

4.2.1

Question

My goal is to update a few vertices in a mesh every frame. In OpenGL you can easily do this with glBufferSubData or in Unity with GraphicsBuffer.SetData followed by a Graphics.DrawProceduralIndirect call. In Godot it seems like you have to create a new surface every frame, which presumably has a lot of overhead. Example Godot C# code for updating a mesh:

        var mesh = new ArrayMesh();
        var arrays = new Godot.Collections.Array();
        arrays.Resize((int)Mesh.ArrayType.Max);
        arrays[(int)Mesh.ArrayType.Vertex] = vertices;
        arrays[(int)Mesh.ArrayType.Index] = indices;
        mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
        Mesh = mesh;

My question is, is there a more efficient way of doing this? Possibly by creating a plugin? I only need to update 2 vertices per frame.

There are multiple ways of doing procedural geometry in Godot Procedural geometry — Godot Engine (stable) documentation in English Choose the one that’s better suited for your needs.