meshdatatool.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. .. _doc_meshdatatool:
  2. Using the MeshDataTool
  3. ======================
  4. The :ref:`MeshDataTool <class_meshdatatool>` is not used to generate geometry. But it is helpful for dynamically altering geometry, for example
  5. if you want to write a script to tessellate, simplify, or deform meshes.
  6. The MeshDataTool is not as fast as altering arrays directly using ArrayMesh. However, it provides more information
  7. and tools to work with meshes than the ArrayMesh does. When the MeshDataTool
  8. is used, it calculates mesh data that is not available in ArrayMeshes such as faces and edges, which are necessary
  9. for certain mesh algorithms. If you do not need this extra information then it may be better to use an ArrayMesh.
  10. .. note:: MeshDataTool can only be used on Meshes that use the PrimitiveType ``Mesh.PRIMITIVE_TRIANGLES``.
  11. As an example, let's walk through the process of deforming the mesh generated in the :ref:`ArrayMesh tutorial <doc_arraymesh>`.
  12. Assume the mesh is stored in an ArrayMesh named ``mesh``. We then initialize the MeshDataTool from
  13. ``mesh`` by calling ``create_from_surface()``. If there is already data initialized in the MeshDataTool
  14. calling ``create_from_surface()`` will clear it for you. Alternatively, you can call ``clear()`` yourself
  15. before re-using the MeshDataTool
  16. .. tabs::
  17. .. code-tab:: gdscript GDScript
  18. var mdt = MeshDataTool.new()
  19. mdt.create_from_surface(mesh)
  20. ``create_from_surface()`` uses the vertex arrays from the ArrayMesh to calculate two additional arrays,
  21. one for edges and one for faces.
  22. An edge is a connection between any two vertices. Each edge in the edge array contains a reference to
  23. the two vertices it is composed of, and up to two faces that it is contained within.
  24. A face is a triangle made up of three vertices and three corresponding edges. Each face in the face array contains
  25. a reference to the three vertices and three edges it is composed of.
  26. The vertex array contains edges, faces, normals, color, tangent, uv, uv2, bones, and weight information connected
  27. with each vertex.
  28. To access information from these arrays you use a function of the form ``get_****()``:
  29. .. tabs::
  30. .. code-tab:: gdscript GDScript
  31. mdt.get_vertex_count() # Returns number of vertices in vertex array.
  32. mdt.get_vertex_faces(0) # Returns array of faces that contain vertex[0].
  33. mdt.get_face_normal(1) # Calculates and returns face normal.
  34. mdt.get_edge_vertex(10, 1) # Returns the second vertex comprising the edge at index 10.
  35. What you choose to do with these functions is up to you. A common use case is to iterate over all vertices
  36. and transform them in some way:
  37. .. tabs::
  38. .. code-tab:: gdscript GDScript
  39. for i in range(get_vertex_count):
  40. var vert = mdt.get_vertex(i)
  41. vert *= 2.0 # Scales the vertex by doubling size.
  42. mdt.set_vertex(i, vert)
  43. Finally, ``commit_to_surface()`` adds a new surface to the ArrayMesh. So if you are dynamically
  44. updating an existing ArrayMesh, first delete the existing surface before adding a new one.
  45. .. tabs::
  46. .. code-tab:: gdscript GDScript
  47. mesh.surface_remove(0) # Deletes the first surface of the mesh.
  48. mdt.commit_to_surface(mesh)
  49. Below is a complete example that creates a pulsing blob complete with new normals and vertex colors.
  50. .. tabs::
  51. .. code-tab:: gdscript GDScript
  52. extends MeshInstance
  53. var sn = OpenSimplexNoise.new()
  54. var mdt = MeshDataTool.new()
  55. func _ready():
  56. sn.period = 0.7
  57. mdt.create_from_surface(mesh, 0)
  58. for i in range(mdt.get_vertex_count()):
  59. var vertex = mdt.get_vertex(i).normalized()
  60. # Push out vertex by noise.
  61. vertex = vertex * (sn.get_noise_3dv(vertex) * 0.5 + 0.75)
  62. mdt.set_vertex(i, vertex)
  63. # Calculate vertex normals, face-by-face.
  64. for i in range(mdt.get_face_count()):
  65. # Get the index in the vertex array.
  66. var a = mdt.get_face_vertex(i, 0)
  67. var b = mdt.get_face_vertex(i, 1)
  68. var c = mdt.get_face_vertex(i, 2)
  69. # Get vertex position using vertex index.
  70. var ap = mdt.get_vertex(a)
  71. var bp = mdt.get_vertex(b)
  72. var cp = mdt.get_vertex(c)
  73. # Calculate face normal.
  74. var n = (bp - cp).cross(ap - bp).normalized()
  75. # Add face normal to current vertex normal.
  76. # This will not result in perfect normals, but it will be close.
  77. mdt.set_vertex_normal(a, n + mdt.get_vertex_normal(a))
  78. mdt.set_vertex_normal(b, n + mdt.get_vertex_normal(b))
  79. mdt.set_vertex_normal(c, n + mdt.get_vertex_normal(c))
  80. # Run through vertices one last time to normalize normals and
  81. # set color to normal.
  82. for i in range(mdt.get_vertex_count()):
  83. var v = mdt.get_vertex_normal(i).normalized()
  84. mdt.set_vertex_normal(i, v)
  85. mdt.set_vertex_color(i, Color(v.x, v.y, v.z))
  86. mesh.surface_remove(0)
  87. mdt.commit_to_surface(mesh)