Skip to content

Commit cfadb9d

Browse files
authored
improve documentation of SurfaceTool (#6405)
* improve documentation of SurfaceTool * add suggestion from @clayjohn
1 parent 4fdeac0 commit cfadb9d

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

tutorials/3d/procedural_geometry/surfacetool.rst

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Attributes are added before each vertex is added:
1515
.. tabs::
1616
.. code-tab:: gdscript GDScript
1717

18+
var st = SurfaceTool.new()
19+
20+
st.begin(Mesh.PRIMITIVE_TRIANGLES)
21+
1822
st.set_normal() # Overwritten by normal below.
1923
st.set_normal() # Added to next vertex.
2024
st.set_color() # Added to next vertex.
@@ -29,16 +33,20 @@ Attributes are added before each vertex is added:
2933
st.AddVertex(); // Captures normal and color above.
3034
st.SetNormal(); // Normal never added to a vertex.
3135

32-
When finished generating your geometry with the :ref:`SurfaceTool <class_surfacetool>`
36+
When finished generating your geometry with the :ref:`SurfaceTool <class_surfacetool>`,
3337
call ``commit()`` to finish generating the mesh. If an :ref:`ArrayMesh <class_ArrayMesh>` is passed
34-
to ``commit()`` then it appends a new surface to the end of the ArrayMesh. While if nothing is passed
38+
to ``commit()``, then it appends a new surface to the end of the ArrayMesh. While if nothing is passed
3539
in, ``commit()`` returns an ArrayMesh.
3640

3741
.. tabs::
3842
.. code-tab:: gdscript GDScript
3943

44+
# Add surface to existing ArrayMesh.
4045
st.commit(mesh)
41-
# Or:
46+
47+
# -- Or Alternatively --
48+
49+
# Create new ArrayMesh.
4250
var mesh = st.commit()
4351

4452
.. code-tab:: csharp
@@ -47,7 +55,7 @@ in, ``commit()`` returns an ArrayMesh.
4755
// Or:
4856
var mesh = st.Commit();
4957

50-
Code creates a triangle with indices
58+
The code below creates a triangle without indices.
5159

5260
.. tabs::
5361
.. code-tab:: gdscript GDScript
@@ -97,14 +105,37 @@ Code creates a triangle with indices
97105
var mesh = st.Commit();
98106

99107
You can optionally add an index array, either by calling ``add_index()`` and adding
100-
vertices to the index array or by calling ``index()`` which shrinks the vertex array
101-
to remove duplicate vertices.
108+
vertices to the index array manually, or by calling ``index()`` once,
109+
which generates the index array automatically and
110+
shrinks the vertex array to remove duplicate vertices.
102111

103112
.. tabs::
104113
.. code-tab:: gdscript GDScript
114+
# Suppose we have a quad defined by 6 vertices as follows
115+
st.add_vertex(Vector3(-1, 1, 0))
116+
st.add_vertex(Vector3(1, 1, 0))
117+
st.add_vertex(Vector3(-1, -1, 0))
118+
119+
st.add_vertex(Vector3(1, 1, 0))
120+
st.add_vertex(Vector3(1, -1, 0))
121+
st.add_vertex(Vector3(-1, -1, 0))
122+
123+
# We can make the quad more efficient by using an index array and only utilizing 4 vertices
124+
125+
# Suppose we have a quad defined by 6 vertices as follows
126+
st.add_vertex(Vector3(-1, 1, 0))
127+
st.add_vertex(Vector3(1, 1, 0))
128+
st.add_vertex(Vector3(-1, -1, 0))
129+
130+
st.add_vertex(Vector3(1, 1, 0))
131+
st.add_vertex(Vector3(1, -1, 0))
132+
st.add_vertex(Vector3(-1, -1, 0))
133+
134+
# We can make the quad more efficient by using an index array and only utilizing 4 vertices
105135

106136
# Creates a quad from four corner vertices.
107-
# add_index does not need to be called before add_vertex.
137+
# add_index() can be called before or after add_vertex()
138+
# since it's not an attribute of a vertex itself.
108139
st.add_index(0)
109140
st.add_index(1)
110141
st.add_index(2)
@@ -113,7 +144,7 @@ to remove duplicate vertices.
113144
st.add_index(3)
114145
st.add_index(2)
115146

116-
# Alternatively:
147+
# Alternatively we can use ``st.index()`` which will create the quad for us and remove the duplicate vertices
117148
st.index()
118149

119150
.. code-tab:: csharp
@@ -161,12 +192,15 @@ normals set already.
161192
st.generate_normals()
162193
st.generate_tangents()
163194

195+
st.commit(mesh)
196+
164197
.. code-tab:: csharp
165198

166199
st.GenerateNormals();
167200
st.GenerateTangents();
168201

169-
By default, when generating normals, they will be calculated on a per-face basis. If you want
170-
smooth vertex normals, when adding vertices, call ``add_smooth_group()``. ``add_smooth_group()``
171-
needs to be called while building the geometry, e.g. before the call to ``add_vertex()``
172-
(if non-indexed) or ``add_index()`` (if indexed).
202+
By default, when generating normals, they will be calculated on a per-vertex basis (i.e. they will
203+
be "smooth normals"). If you want flat vertex normals (i.e. a single normal vector per face), when
204+
adding vertices, call ``add_smooth_group(i)`` where ``i`` is a unique number per vertex.
205+
``add_smooth_group()`` needs to be called while building the geometry, e.g. before the call to
206+
``add_vertex()``.

0 commit comments

Comments
 (0)