arraymesh.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. .. _doc_arraymesh:
  2. Using the ArrayMesh
  3. ===================
  4. This tutorial will present the basics of using an :ref:`ArrayMesh <class_arraymesh>`.
  5. To do so, we will use the function :ref:`add_surface_from_arrays() <class_ArrayMesh_method_add_surface_from_arrays>`,
  6. which takes up to five parameters. The first two are required, while the last three are optional.
  7. The first parameter is the ``PrimitiveType``, an OpenGL concept that instructs the GPU
  8. how to arrange the primitive based on the vertices given, i.e. whether they represent triangles,
  9. lines, points, etc. See :ref:`Mesh.PrimitiveType <enum_Mesh_PrimitiveType>` for the options available.
  10. The second parameter, ``arrays``, is the actual Array that stores the mesh information. The array is a normal Godot array that
  11. is constructed with empty brackets ``[]``. It stores a ``Packed**Array`` (e.g. PackedVector3Array,
  12. PackedInt32Array, etc.) for each type of information that will be used to build the surface.
  13. Common elements of ``arrays`` are listed below, together with the position they must have within ``arrays``.
  14. See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>` for a full list.`
  15. .. list-table::
  16. :class: wrap-normal
  17. :width: 100%
  18. :widths: auto
  19. :header-rows: 1
  20. * - Index
  21. - Mesh.ArrayType Enum
  22. - Array type
  23. * - 0
  24. - ``ARRAY_VERTEX``
  25. - :ref:`PackedVector3Array <class_PackedVector3Array>` or :ref:`PackedVector2Array <class_PackedVector2Array>`
  26. * - 1
  27. - ``ARRAY_NORMAL``
  28. - :ref:`PackedVector3Array <class_PackedVector3Array>`
  29. * - 2
  30. - ``ARRAY_TANGENT``
  31. - :ref:`PackedFloat32Array <class_PackedFloat32Array>` of groups of 4 floats. The first 3 floats determine the tangent, and the last float the binormal
  32. direction as -1 or 1.
  33. * - 3
  34. - ``ARRAY_COLOR``
  35. - :ref:`PackedColorArray <class_PackedColorArray>`
  36. * - 4
  37. - ``ARRAY_TEX_UV``
  38. - :ref:`PackedVector2Array <class_PackedVector2Array>` or :ref:`PackedVector3Array <class_PackedVector3Array>`
  39. * - 5
  40. - ``ARRAY_TEX_UV2``
  41. - :ref:`PackedVector2Array <class_PackedVector2Array>` or :ref:`PackedVector3Array <class_PackedVector3Array>`
  42. * - 10
  43. - ``ARRAY_BONES``
  44. - :ref:`PackedFloat32Array <class_PackedFloat32Array>` of groups of 4 floats or :ref:`PackedInt32Array <class_PackedInt32Array>` of groups of 4 ints. Each group lists indexes of 4 bones that affects a given vertex.
  45. * - 11
  46. - ``ARRAY_WEIGHTS``
  47. - :ref:`PackedFloat32Array <class_PackedFloat32Array>` of groups of 4 floats. Each float lists the amount of weight the corresponding bone in ``ARRAY_BONES`` has on a given vertex.
  48. * - 12
  49. - ``ARRAY_INDEX``
  50. - :ref:`PackedInt32Array <class_PackedInt32Array>`
  51. 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
  52. 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. In fact, we won't use the
  53. index array at all.
  54. 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``)
  55. 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
  56. four entries to describe a single vertex. These must be exactly four times larger than the vertex array.
  57. For normal usage, the last three parameters in :ref:`add_surface_from_arrays() <class_arraymesh_method_add_surface_from_arrays>` are typically left empty.
  58. Setting up the ArrayMesh
  59. ------------------------
  60. In the editor, create a :ref:`MeshInstance3D <class_meshinstance3d>` and add an :ref:`ArrayMesh <class_arraymesh>` to it in the Inspector.
  61. Normally, adding an ArrayMesh in the editor is not useful, but in this case it allows us to access the ArrayMesh
  62. from code without creating one.
  63. Next, add a script to the MeshInstance3D.
  64. Under ``_ready()``, create a new Array.
  65. .. tabs::
  66. .. code-tab:: gdscript GDScript
  67. var surface_array = []
  68. This will be the array that we keep our surface information in - it will hold
  69. all the arrays of data that the surface needs. Godot will expect it to be of
  70. size ``Mesh.ARRAY_MAX``, so resize it accordingly.
  71. .. tabs::
  72. .. code-tab:: gdscript GDScript
  73. var surface_array = []
  74. surface_array.resize(Mesh.ARRAY_MAX)
  75. Next create the arrays for each data type you will use.
  76. .. tabs::
  77. .. code-tab:: gdscript GDScript
  78. var verts = PackedVector3Array()
  79. var uvs = PackedVector2Array()
  80. var normals = PackedVector3Array()
  81. var indices = PackedInt32Array()
  82. Once you have filled your data arrays with your geometry you can create a mesh
  83. by adding each array to ``surface_array`` and then committing to the mesh.
  84. .. tabs::
  85. .. code-tab:: gdscript GDScript
  86. surface_array[Mesh.ARRAY_VERTEX] = verts
  87. surface_array[Mesh.ARRAY_TEX_UV] = uvs
  88. surface_array[Mesh.ARRAY_NORMAL] = normals
  89. surface_array[Mesh.ARRAY_INDEX] = indices
  90. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array) # No blendshapes, lods, or compression used.
  91. .. note:: In this example, we used ``Mesh.PRIMITIVE_TRIANGLES``, but you can use any primitive type
  92. available from mesh.
  93. Put together, the full code looks like:
  94. .. tabs::
  95. .. code-tab:: gdscript GDScript
  96. extends MeshInstance3D
  97. func _ready():
  98. var surface_array = []
  99. surface_array.resize(Mesh.ARRAY_MAX)
  100. # PackedVector**Arrays for mesh construction.
  101. var verts = PackedVector3Array()
  102. var uvs = PackedVector2Array()
  103. var normals = PackedVector3Array()
  104. var indices = PackedInt32Array()
  105. #######################################
  106. ## Insert code here to generate mesh ##
  107. #######################################
  108. # Assign arrays to surface array.
  109. surface_array[Mesh.ARRAY_VERTEX] = verts
  110. surface_array[Mesh.ARRAY_TEX_UV] = uvs
  111. surface_array[Mesh.ARRAY_NORMAL] = normals
  112. surface_array[Mesh.ARRAY_INDEX] = indices
  113. # Create mesh surface from mesh array.
  114. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array) # No blendshapes, lods, or compression used.
  115. The code that goes in the middle can be whatever you want. Below we will present some
  116. example code for generating a sphere.
  117. Generating geometry
  118. -------------------
  119. Here is sample code for generating a sphere. Although the code is presented in
  120. GDScript, there is nothing Godot specific about the approach to generating it.
  121. This implementation has nothing in particular to do with ArrayMeshes and is just a
  122. generic approach to generating a sphere. If you are having trouble understanding it
  123. or want to learn more about procedural geometry in general, you can use any tutorial
  124. that you find online.
  125. .. tabs::
  126. .. code-tab:: gdscript GDScript
  127. extends MeshInstance3D
  128. var rings = 50
  129. var radial_segments = 50
  130. var height = 1
  131. var radius = 1
  132. func _ready():
  133. # Insert setting up the PackedVector**Arrays here.
  134. # Vertex indices.
  135. var thisrow = 0
  136. var prevrow = 0
  137. var point = 0
  138. # Loop over rings.
  139. for i in range(rings + 1):
  140. var v = float(i) / rings
  141. var w = sin(PI * v)
  142. var y = cos(PI * v)
  143. # Loop over segments in ring.
  144. for j in range(radial_segments):
  145. var u = float(j) / radial_segments
  146. var x = sin(u * PI * 2.0)
  147. var z = cos(u * PI * 2.0)
  148. var vert = Vector3(x * radius * w, y, z * radius * w)
  149. verts.append(vert)
  150. normals.append(vert.normalized())
  151. uvs.append(Vector2(u, v))
  152. point += 1
  153. # Create triangles in ring using indices.
  154. if i > 0 and j > 0:
  155. indices.append(prevrow + j - 1)
  156. indices.append(prevrow + j)
  157. indices.append(thisrow + j - 1)
  158. indices.append(prevrow + j)
  159. indices.append(thisrow + j)
  160. indices.append(thisrow + j - 1)
  161. if i > 0:
  162. indices.append(prevrow + radial_segments - 1)
  163. indices.append(prevrow)
  164. indices.append(thisrow + radial_segments - 1)
  165. indices.append(prevrow)
  166. indices.append(prevrow + radial_segments)
  167. indices.append(thisrow + radial_segments - 1)
  168. prevrow = thisrow
  169. thisrow = point
  170. # Insert committing to the ArrayMesh here.
  171. Saving
  172. ------
  173. Finally, we can use the :ref:`ResourceSaver <class_resourcesaver>` class to save the ArrayMesh.
  174. This is useful when you want to generate a mesh and then use it later without having to re-generate it.
  175. .. tabs::
  176. .. code-tab:: gdscript GDScript
  177. # Saves mesh to a .tres file with compression enabled.
  178. ResourceSaver.save("res://sphere.tres", mesh, ResourceSaver.FLAG_COMPRESS)