使用 ArrayMesh

This tutorial will present the basics of using an ArrayMesh.

为此, 我们将使用函数 add_surface_from_arrays() , 它最多需要四个参数. 前两个参数是必须的, 后两个参数是可选的.

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 Pool**Array (e.g. PoolVector3Array, PoolIntArray, etc.) for each type of information that will be used to build the surface.

The possible elements of arrays are listed below, together with the position they must have within arrays. See also Mesh.ArrayType.

索引

Mesh.ArrayType Enum

Array type

0

ARRAY_VERTEX

PoolVector3Array or PoolVector2Array

1

ARRAY_NORMAL

PoolVector3Array

2

ARRAY_TANGENT

PoolRealArray of groups of 4 floats. First 3 floats determine the tangent, and the last the binormal direction as -1 or 1.

3

ARRAY_COLOR

PoolColorArray

4

ARRAY_TEX_UV

PoolVector2Array or PoolVector3Array

5

ARRAY_TEX_UV2

PoolVector2Array or PoolVector3Array

6

ARRAY_BONES

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

7

ARRAY_WEIGHTS

PoolRealArray of groups of 4 floats. Each float lists the amount of weight an determined bone on ARRAY_BONES has on a given vertex.

8

ARRAY_INDEX

PoolIntArray

The array of vertices (at index 0) is always required. The index array is optional and will only be used if included. We won’t use it in this tutorial.

All the other arrays carry information about the vertices. They are also 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 two parameters in add_surface_from_arrays() are typically left empty.

ArrayMesh

In the editor, create a MeshInstance 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.

接下来, 在MeshInstance中添加一个脚本.

_ready() 下创建一个新的数组.

GDScript

  1. var surface_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.

GDScript

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

接下来, 为您将使用的每种数据类型创建数组.

GDScript

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

一旦你用几何体填充了你的数据数组, 就可以通过将每个数组添加到 surface_array , 然后提交到网格中来创建网格.

GDScript

  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. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array) # No blendshapes or compression used.

备注

在这个例子中,使用了 Mesh.PRIMITIVE_TRIANGLES,但你也可以使用网格所提供的任何图元类型。

Put together, the full code looks like:

GDScript

  1. extends MeshInstance
  2. func _ready():
  3. var surface_array= []
  4. surface_array.resize(Mesh.ARRAY_MAX)
  5. # PoolVector**Arrays 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. 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. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array) # No blendshapes or compression used.

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

生成几何体

这是生成球体的示例代码。尽管代码是用 GDScript 编写的,但是 Godot 并没有指定用特定的方式来实现它。这种实现方式与 ArrayMesh 无关,仅仅是一种通用的生成球体的方式。如果您觉得这比较难以理解,或者想更全面地了解程序式几何体,可以在网上寻找相关的教程进行学习。

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. # Insert setting up the PoolVector**Arrays here.
  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. # Insert committing to the ArrayMesh here.

保存

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.

GDScript

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