You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/3d/procedural_geometry/meshdatatool.rst
+99-15Lines changed: 99 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,15 @@ Using the MeshDataTool
6
6
The :ref:`MeshDataTool <class_meshdatatool>` is not used to generate geometry. But it is helpful for dynamically altering geometry, for example
7
7
if you want to write a script to tessellate, simplify, or deform meshes.
8
8
9
-
The MeshDataTool is not as fast as altering arrays directly using ArrayMesh. However, it provides more information
9
+
The MeshDataTool is not as fast as altering arrays directly using :ref:`ArrayMesh<class_arraymesh>`. However, it provides more information
10
10
and tools to work with meshes than the ArrayMesh does. When the MeshDataTool
11
11
is used, it calculates mesh data that is not available in ArrayMeshes such as faces and edges, which are necessary
12
12
for certain mesh algorithms. If you do not need this extra information then it may be better to use an ArrayMesh.
13
13
14
14
.. note:: MeshDataTool can only be used on Meshes that use the PrimitiveType ``Mesh.PRIMITIVE_TRIANGLES``.
15
15
16
-
We initialize the MeshDataTool from an ArrayMesh by calling ``create_from_surface()``. If there is already data initialized in the MeshDataTool,
17
-
calling ``create_from_surface()`` will clear it for you. Alternatively, you can call ``clear()`` yourself before re-using the MeshDataTool.
16
+
We initialize the MeshDataTool from an ArrayMesh by calling :ref:`create_from_surface()<class_meshdatatool_method_create_from_surface>`. If there is already data initialized in the MeshDataTool,
17
+
calling ``create_from_surface()`` will clear it for you. Alternatively, you can call :ref:`clear()<class_meshdatatool_method_clear>` yourself before re-using the MeshDataTool.
18
18
19
19
In the examples below, assume an ArrayMesh called ``mesh`` has already been created. See :ref:`ArrayMesh tutorial <doc_arraymesh>` for an example of mesh generation.
20
20
@@ -24,6 +24,11 @@ In the examples below, assume an ArrayMesh called ``mesh`` has already been crea
24
24
var mdt = MeshDataTool.new()
25
25
mdt.create_from_surface(mesh, 0)
26
26
27
+
.. code-tab:: csharp C#
28
+
29
+
var mdt = new MeshDataTool();
30
+
mdt.CreateFromSurface(mesh, 0);
31
+
27
32
``create_from_surface()`` uses the vertex arrays from the ArrayMesh to calculate two additional arrays,
28
33
one for edges and one for faces, for a total of three arrays.
29
34
@@ -41,22 +46,38 @@ To access information from these arrays you use a function of the form ``get_***
41
46
.. tabs::
42
47
.. code-tab:: gdscript GDScript
43
48
44
-
mdt.get_vertex_count() # Returns number of vertices in vertex array.
45
-
mdt.get_vertex_faces(0) # Returns array of faces that contain vertex[0].
46
-
mdt.get_face_normal(1) # Calculates and returns face normal of the second face.
49
+
mdt.get_vertex_count() # Returns the number of vertices in the vertex array.
50
+
mdt.get_vertex_faces(0) # Returns an array of faces that contain vertex[0].
51
+
mdt.get_face_normal(1) # Calculates and returns the face normal of the second face.
47
52
mdt.get_edge_vertex(10, 1) # Returns the second vertex comprising the edge at index 10.
48
53
54
+
.. code-tab:: csharp C#
55
+
56
+
mdt.GetVertexCount(); // Returns the number of vertices in the vertex array.
57
+
mdt.GetVertexFaces(0); // Returns an array of faces that contain vertex[0].
58
+
mdt.GetFaceNormal(1); // Calculates and returns the face normal of the second face.
59
+
mdt.GetEdgeVertex(10, 1); // Returns the second vertex comprising the edge at index 10.
60
+
49
61
What you choose to do with these functions is up to you. A common use case is to iterate over all vertices
50
62
and transform them in some way:
51
63
52
64
.. tabs::
53
65
.. code-tab:: gdscript GDScript
54
66
55
-
for i in range(get_vertex_count):
67
+
for i in range(mdt.get_vertex_count()):
56
68
var vert = mdt.get_vertex(i)
57
-
vert *= 2.0 # Scales the vertex by doubling size.
69
+
vert *= 2.0 # Scales the vertex by doubling its size.
58
70
mdt.set_vertex(i, vert)
59
71
72
+
.. code-tab:: csharp C#
73
+
74
+
for (var i = 0; i < mdt.GetVertexCount(); i++)
75
+
{
76
+
Vector3 vert = mdt.GetVertex(i);
77
+
vert *= 2.0f; // Scales the vertex by doubling its size.
78
+
mdt.SetVertex(i, vert);
79
+
}
80
+
60
81
These modifications are not done in place on the ArrayMesh. If you are dynamically updating an existing ArrayMesh,
61
82
first delete the existing surface before adding a new one using :ref:`commit_to_surface() <class_meshdatatool_method_commit_to_surface>`:
62
83
@@ -66,6 +87,11 @@ first delete the existing surface before adding a new one using :ref:`commit_to_
66
87
mesh.clear_surfaces() # Deletes all of the mesh's surfaces.
67
88
mdt.commit_to_surface(mesh)
68
89
90
+
.. code-tab:: csharp C#
91
+
92
+
mesh.ClearSurfaces(); // Deletes all of the mesh's surfaces.
93
+
mdt.CommitToSurface(mesh);
94
+
69
95
Below is a complete example that turns a spherical mesh called ``mesh`` into a randomly deformed blob complete with updated normals and vertex colors.
70
96
See :ref:`ArrayMesh tutorial <doc_arraymesh>` for how to generate the base mesh.
71
97
@@ -84,34 +110,92 @@ See :ref:`ArrayMesh tutorial <doc_arraymesh>` for how to generate the base mesh.
0 commit comments