Using the ArrayMesh

This tutorial will present the basics of using an ArrayMesh.

To do so, we will use the function add_surface_from_arrays(), which takes up to five parameters. The first two are required, while the last three are optional.

The first parameter is the PrimitiveType, an OpenGL concept that instructs the GPU how to arrange the primitive based on the vertices given, i.e. whether they represent triangles, lines, points, etc. See Mesh.PrimitiveType for the options available.

The second parameter, arrays, is the actual Array that stores the mesh information. The array is a normal Godot array that is constructed with empty brackets []. It stores a Packed**Array (e.g. PackedVector3Array, PackedInt32Array, etc.) for each type of information that will be used to build the surface.

Common elements of arrays are listed below, together with the position they must have within arrays. See Mesh.ArrayType for a full list.

Index

Mesh.ArrayType Enum

Array type

0

ARRAY_VERTEX

PackedVector3Array or PackedVector2Array

1

ARRAY_NORMAL

PackedVector3Array

2

ARRAY_TANGENT

PackedFloat32Array or PackedFloat64Array of groups of 4 floats. The first 3 floats determine the tangent, and the last float the binormal direction as -1 or 1.

3

ARRAY_COLOR

PackedColorArray

4

ARRAY_TEX_UV

PackedVector2Array or PackedVector3Array

5

ARRAY_TEX_UV2

PackedVector2Array or PackedVector3Array

10

ARRAY_BONES

PackedFloat32Array of groups of 4 floats or PackedInt32Array of groups of 4 ints. Each group lists indexes of 4 bones that affects a given vertex.

11

ARRAY_WEIGHTS

PackedFloat32Array or PackedFloat64Array of groups of 4 floats. Each float lists the amount of weight the corresponding bone in ARRAY_BONES has on a given vertex.

12

ARRAY_INDEX

PackedInt32Array

In most cases when creating a mesh, we define it by its vertex positions. So usually, the array of vertices (at index 0) is required, while the index array (at index 12) is optional and will only be used if included. It is also possible to create a mesh with only the index array and no vertex array, but that’s beyond the scope of this tutorial.

All the other arrays carry information about the vertices. They are optional and will only be used if included. Some of these arrays (e.g. ARRAY_COLOR) use one entry per vertex to provide extra information about vertices. They must have the same size as the vertex array. Other arrays (e.g. ARRAY_TANGENT) use four entries to describe a single vertex. These must be exactly four times larger than the vertex array.

For normal usage, the last three parameters in add_surface_from_arrays() are typically left empty.

Setting up the ArrayMesh

In the editor, create a MeshInstance3D and add an ArrayMesh to it in the Inspector. Normally, adding an ArrayMesh in the editor is not useful, but in this case it allows us to access the ArrayMesh from code without creating one.

Next, add a script to the MeshInstance3D.

Under _ready(), create a new Array.

GDScriptC#

  1. var surface_array = []
  1. var surfaceArray = new Godot.Collections.Array();

This will be the array that we keep our surface information in - it will hold all the arrays of data that the surface needs. Godot will expect it to be of size Mesh.ARRAY_MAX, so resize it accordingly.

GDScriptC#

  1. var surface_array = []
  2. surface_array.resize(Mesh.ARRAY_MAX)
  1. var surfaceArray = new Godot.Collections.Array();
  2. surfaceArray.Resize((int)Mesh.ArrayType.Max);

Next create the arrays for each data type you will use.

GDScriptC#

  1. var verts = PackedVector3Array()
  2. var uvs = PackedVector2Array()
  3. var normals = PackedVector3Array()
  4. var indices = PackedInt32Array()
  1. var verts = new List<Vector3>();
  2. var uvs = new List<Vector2>();
  3. var normals = new List<Vector3>();
  4. var indices = new List<int>();

Once you have filled your data arrays with your geometry you can create a mesh by adding each array to surface_array and then committing to the mesh.

GDScriptC#

  1. surface_array[Mesh.ARRAY_VERTEX] = verts
  2. surface_array[Mesh.ARRAY_TEX_UV] = uvs
  3. surface_array[Mesh.ARRAY_NORMAL] = normals
  4. surface_array[Mesh.ARRAY_INDEX] = indices
  5. # No blendshapes, lods, or compression used.
  6. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array)
  1. surfaceArray[(int)Mesh.ArrayType.Vertex] = verts.ToArray();
  2. surfaceArray[(int)Mesh.ArrayType.TexUV] = uvs.ToArray();
  3. surfaceArray[(int)Mesh.ArrayType.Normal] = normals.ToArray();
  4. surfaceArray[(int)Mesh.ArrayType.Index] = indices.ToArray();
  5. var arrMesh = Mesh as ArrayMesh;
  6. if (arrMesh != null)
  7. {
  8. // No blendshapes, lods, or compression used.
  9. arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfaceArray);
  10. }

Note

In this example, we used Mesh.PRIMITIVE_TRIANGLES, but you can use any primitive type available from mesh.

Put together, the full code looks like:

GDScriptC#

  1. extends MeshInstance3D
  2. func _ready():
  3. var surface_array = []
  4. surface_array.resize(Mesh.ARRAY_MAX)
  5. # PackedVector**Arrays for mesh construction.
  6. var verts = PackedVector3Array()
  7. var uvs = PackedVector2Array()
  8. var normals = PackedVector3Array()
  9. var indices = PackedInt32Array()
  10. #######################################
  11. ## Insert code here to generate mesh ##
  12. #######################################
  13. # Assign arrays to surface array.
  14. surface_array[Mesh.ARRAY_VERTEX] = verts
  15. surface_array[Mesh.ARRAY_TEX_UV] = uvs
  16. surface_array[Mesh.ARRAY_NORMAL] = normals
  17. surface_array[Mesh.ARRAY_INDEX] = indices
  18. # Create mesh surface from mesh array.
  19. # No blendshapes, lods, or compression used.
  20. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array)
  1. public partial class MyMeshInstance3D : MeshInstance3D
  2. {
  3. public override void _Ready()
  4. {
  5. var surfaceArray = new Godot.Collections.Array();
  6. surfaceArray.Resize((int)Mesh.ArrayType.Max);
  7. // C# arrays cannot be resized or expanded, so use Lists to create geometry.
  8. var verts = new List<Vector3>();
  9. var uvs = new List<Vector2>();
  10. var normals = new List<Vector3>();
  11. var indices = new List<int>();
  12. /***********************************
  13. * Insert code here to generate mesh.
  14. * *********************************/
  15. // Convert Lists to arrays and assign to surface array
  16. surfaceArray[(int)Mesh.ArrayType.Vertex] = verts.ToArray();
  17. surfaceArray[(int)Mesh.ArrayType.TexUV] = uvs.ToArray();
  18. surfaceArray[(int)Mesh.ArrayType.Normal] = normals.ToArray();
  19. surfaceArray[(int)Mesh.ArrayType.Index] = indices.ToArray();
  20. var arrMesh = Mesh as ArrayMesh;
  21. if (arrMesh != null)
  22. {
  23. // Create mesh surface from mesh array
  24. // No blendshapes, lods, or compression used.
  25. arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfaceArray);
  26. }
  27. }
  28. }

The code that goes in the middle can be whatever you want. Below we will present some example code for generating a sphere.

Generating geometry

Here is sample code for generating a sphere. Although the code is presented in GDScript, there is nothing Godot specific about the approach to generating it. This implementation has nothing in particular to do with ArrayMeshes and is just a generic approach to generating a sphere. If you are having trouble understanding it or want to learn more about procedural geometry in general, you can use any tutorial that you find online.

GDScriptC#

  1. extends MeshInstance3D
  2. var rings = 50
  3. var radial_segments = 50
  4. var radius = 1
  5. func _ready():
  6. # Insert setting up the PackedVector**Arrays here.
  7. # Vertex indices.
  8. var thisrow = 0
  9. var prevrow = 0
  10. var point = 0
  11. # Loop over rings.
  12. for i in range(rings + 1):
  13. var v = float(i) / rings
  14. var w = sin(PI * v)
  15. var y = cos(PI * v)
  16. # Loop over segments in ring.
  17. for j in range(radial_segments + 1):
  18. var u = float(j) / radial_segments
  19. var x = sin(u * PI * 2.0)
  20. var z = cos(u * PI * 2.0)
  21. var vert = Vector3(x * radius * w, y * radius, z * radius * w)
  22. verts.append(vert)
  23. normals.append(vert.normalized())
  24. uvs.append(Vector2(u, v))
  25. point += 1
  26. # Create triangles in ring using indices.
  27. if i > 0 and j > 0:
  28. indices.append(prevrow + j - 1)
  29. indices.append(prevrow + j)
  30. indices.append(thisrow + j - 1)
  31. indices.append(prevrow + j)
  32. indices.append(thisrow + j)
  33. indices.append(thisrow + j - 1)
  34. prevrow = thisrow
  35. thisrow = point
  36. # Insert committing to the ArrayMesh here.
  1. public partial class MyMeshInstance3D : MeshInstance3D
  2. {
  3. private int _rings = 50;
  4. private int _radialSegments = 50;
  5. private float _radius = 1;
  6. public override void _Ready()
  7. {
  8. // Insert setting up the surface array and lists here.
  9. // Vertex indices.
  10. var thisRow = 0;
  11. var prevRow = 0;
  12. var point = 0;
  13. // Loop over rings.
  14. for (var i = 0; i < _rings + 1; i++)
  15. {
  16. var v = ((float)i) / _rings;
  17. var w = Mathf.Sin(Mathf.Pi * v);
  18. var y = Mathf.Cos(Mathf.Pi * v);
  19. // Loop over segments in ring.
  20. for (var j = 0; j < _radialSegments + 1; j++)
  21. {
  22. var u = ((float)j) / _radialSegments;
  23. var x = Mathf.Sin(u * Mathf.Pi * 2);
  24. var z = Mathf.Cos(u * Mathf.Pi * 2);
  25. var vert = new Vector3(x * _radius * w, y * _radius, z * _radius * w);
  26. verts.Add(vert);
  27. normals.Add(vert.Normalized());
  28. uvs.Add(new Vector2(u, v));
  29. point += 1;
  30. // Create triangles in ring using indices.
  31. if (i > 0 && j > 0)
  32. {
  33. indices.Add(prevRow + j - 1);
  34. indices.Add(prevRow + j);
  35. indices.Add(thisRow + j - 1);
  36. indices.Add(prevRow + j);
  37. indices.Add(thisRow + j);
  38. indices.Add(thisRow + j - 1);
  39. }
  40. }
  41. prevRow = thisRow;
  42. thisRow = point;
  43. }
  44. // Insert committing to the ArrayMesh here.
  45. }
  46. }

Saving

Finally, we can use the ResourceSaver class to save the ArrayMesh. This is useful when you want to generate a mesh and then use it later without having to re-generate it.

GDScriptC#

  1. # Saves mesh to a .tres file with compression enabled.
  2. ResourceSaver.save(mesh, "res://sphere.tres", ResourceSaver.FLAG_COMPRESS)
  1. // Saves mesh to a .tres file with compression enabled.
  2. ResourceSaver.Save(Mesh, "res://sphere.tres", ResourceSaver.SaverFlags.Compress);

User-contributed notes

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

Previous Next