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 four parameters. The first two are required, while the second two are optional.

The first is the PrimitiveType, this is an OpenGL concept that instructs the GPU how to arrange the primitive based on the vertices given whether it is triangles, lines, points, etc. A complete list can be found under the Mesh class reference page.

The second 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 Pool**Array (e.g. PoolVector3Array, PoolIntArray, etc.) for each type of information.

  • ARRAY_VERTEX = 0 | PoolVector3Array or PoolVector2Array

  • ARRAY_NORMAL = 1 | PoolVector3Array

  • ARRAY_TANGENT = 2 | PoolRealArray of groups of 4 floats. first 3 floats determine the tangent, and the last the binormal direction as -1 or 1.

  • ARRAY_COLOR = 3 | PoolColorArray

  • ARRAY_TEX_UV = 4 | PoolVector2Array or PoolVector3Array

  • ARRAY_TEX_UV2 = 5 | PoolVector2Array or PoolVector3Array

  • ARRAY_BONES = 6 | PoolRealArray of groups of 4 floats or PoolIntArray of groups of 4 ints

  • ARRAY_WEIGHTS = 7 | PoolRealArray of groups of 4 floats

  • ARRAY_INDEX = 8 | PoolIntArray

The Array of vertices is always required. All the others are optional and will only be used if included.

Each array needs to have the same number of elements as the vertex array except for the index array. For arrays like tangents, an element is a group of 4 floats. So the array size will be four times the size of the vertex array size, but they will have the same number of elements

The index array is unique.

The third parameter is an array of blendshapes for the Mesh to use. While this tutorial does not cover using blendshapes, it is possible to specify them when creating a surface from arrays.

The last parameter is the compress flags which specifies which arrays to store with half as many bits. The values can be found in the classref for VisualServer under ArrayFormat.

For normal usage you will find it is best to leave the last two parameters empty.

ArrayMesh

Add an ArrayMesh to a MeshInstance. Normally, adding an ArrayMesh in the editor is not useful, but in this case it allows as to access the ArrayMesh from code without creating one.

Next, add a script to the MeshInstance.

Under _ready(), create a new Array.

GDScript

  1. var arr = []

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.

GDScript

  1. var arr = []
  2. arr.resize(Mesh.ARRAY_MAX)

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

GDScript

  1. var verts = PoolVector3Array()
  2. var uvs = PoolVector2Array()
  3. var normals = PoolVector3Array()
  4. var indices = PoolIntArray()

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.

GDScript

  1. arr[Mesh.ARRAY_VERTEX] = verts
  2. arr[Mesh.ARRAY_TEX_UV] = uvs
  3. arr[Mesh.ARRAY_NORMAL] = normals
  4. arr[Mesh.ARRAY_INDEX] = indices
  5. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr) # No blendshapes or compression used.

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:

GDScript

  1. extends MeshInstance
  2. func _ready():
  3. var arr = []
  4. arr.resize(Mesh.ARRAY_MAX)
  5. # PoolVectorXXArrays for mesh construction.
  6. var verts = PoolVector3Array()
  7. var uvs = PoolVector2Array()
  8. var normals = PoolVector3Array()
  9. var indices = PoolIntArray()
  10. #######################################
  11. ## Insert code here to generate mesh ##
  12. #######################################
  13. # Assign arrays to mesh array.
  14. arr[Mesh.ARRAY_VERTEX] = verts
  15. arr[Mesh.ARRAY_TEX_UV] = uvs
  16. arr[Mesh.ARRAY_NORMAL] = normals
  17. arr[Mesh.ARRAY_INDEX] = indices
  18. # Create mesh surface from mesh array.
  19. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr) # No blendshapes or compression used.

The code that goes in the middle can be whatever you want. Below we will present some example code that could go in the middle.

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.

GDScript

  1. extends MeshInstance
  2. var rings = 50
  3. var radial_segments = 50
  4. var height = 1
  5. var radius = 1
  6. func _ready():
  7. # Set up the PoolVectorXArrays.
  8. # Vertex indices.
  9. var thisrow = 0
  10. var prevrow = 0
  11. var point = 0
  12. # Loop over rings.
  13. for i in range(rings + 1):
  14. var v = float(i) / rings
  15. var w = sin(PI * v)
  16. var y = cos(PI * v)
  17. # Loop over segments in ring.
  18. for j in range(radial_segments):
  19. var u = float(j) / radial_segments
  20. var x = sin(u * PI * 2.0)
  21. var z = cos(u * PI * 2.0)
  22. var vert = Vector3(x * radius * w, y, z * radius * w)
  23. verts.append(vert)
  24. normals.append(vert.normalized())
  25. uvs.append(Vector2(u, v))
  26. point += 1
  27. # Create triangles in ring using indices.
  28. if i > 0 and j > 0:
  29. indices.append(prevrow + j - 1)
  30. indices.append(prevrow + j)
  31. indices.append(thisrow + j - 1)
  32. indices.append(prevrow + j)
  33. indices.append(thisrow + j)
  34. indices.append(thisrow + j - 1)
  35. if i > 0:
  36. indices.append(prevrow + radial_segments - 1)
  37. indices.append(prevrow)
  38. indices.append(thisrow + radial_segments - 1)
  39. indices.append(prevrow)
  40. indices.append(prevrow + radial_segments)
  41. indices.append(thisrow + radial_segments - 1)
  42. prevrow = thisrow
  43. thisrow = point
  44. # Commit to the ArrayMesh.

Combined with the code above, this code will generate a sphere.

When it comes to generating geometry with the ArrayMesh you need to understand what goes in each array and then you can follow tutorials for any language/engine and convert it into Godot.

Saving

Finally, Godot provides a single method to save ArrayMeshes using the ResourceSaver class. This is useful when you want to generate a mesh and then use it later without having to re-generate.

GDScript

  1. # Saves mesh to a .tres file with compression enabled.
  2. ResourceSaver.save("res://sphere.tres", mesh, 32)