Skip to content

Commit 5e68807

Browse files
marrinightMarites HendrixCalinouAThousandShips
authored
Add Rectangle Example to ArrayMesh (#11247)
* add rectangle arraymesh example * make webp image Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> * spelling --------- Co-authored-by: Marites Hendrix <github_username@users.noreply.github.com> Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro> Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
1 parent f9730cf commit 5e68807

File tree

2 files changed

+223
-2
lines changed

2 files changed

+223
-2
lines changed

tutorials/3d/procedural_geometry/arraymesh.rst

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,230 @@ Put together, the full code looks like:
227227

228228

229229
The code that goes in the middle can be whatever you want. Below we will present some
230-
example code for generating a sphere.
230+
example code for generating shapes, starting with a rectangle.
231231

232-
Generating geometry
232+
Generating a rectangle
233+
----------------------
234+
235+
Since we are using ``Mesh.PRIMITIVE_TRIANGLES`` to render, we will construct a rectangle
236+
with triangles.
237+
238+
A rectangle is formed by two triangles sharing four vertices. For our example, we will create
239+
a rectangle with its top left point at ``(0, 0, 0)`` with a width and length of one as shown below:
240+
241+
.. image:: img/array_mesh_rectangle_as_triangles.webp
242+
:scale: 33%
243+
:alt: A rectangle made of two triangles sharing four vertices.
244+
245+
To draw this rectangle, define the coordinates of each vertex in the ``verts`` array.
246+
247+
.. tabs::
248+
.. code-tab:: gdscript GDScript
249+
250+
verts = PackedVector3Array([
251+
Vector3(0, 0, 0),
252+
Vector3(0, 0, 1),
253+
Vector3(1, 0, 0),
254+
Vector3(1, 0, 1),
255+
])
256+
257+
.. code-tab:: csharp C#
258+
259+
verts.AddRange(new Vector3[]
260+
{
261+
new Vector3(0, 0, 0),
262+
new Vector3(0, 0, 1),
263+
new Vector3(1, 0, 0),
264+
new Vector3(1, 0, 1),
265+
});
266+
267+
The ``uvs`` array helps describe where parts of a texture should go onto the mesh. The values
268+
range from 0 to 1. Depending on your texture, you may want to change these values.
269+
270+
.. tabs::
271+
.. code-tab:: gdscript GDScript
272+
273+
uvs = PackedVector2Array([
274+
Vector2(0, 0),
275+
Vector2(1, 0),
276+
Vector2(0, 1),
277+
Vector2(1, 1),
278+
])
279+
280+
.. code-tab:: csharp C#
281+
282+
uvs.AddRange(new Vector2[]
283+
{
284+
new Vector2(0, 0),
285+
new Vector2(1, 0),
286+
new Vector2(0, 1),
287+
new Vector2(1, 1),
288+
});
289+
290+
The ``normals`` array is used to describe the direction the vertices face and is
291+
used in lighting calculations. For this example, we will default to the ``Vector3.UP``
292+
direction.
293+
294+
.. tabs::
295+
.. code-tab:: gdscript GDScript
296+
297+
normals = PackedVector3Array([
298+
Vector3.UP,
299+
Vector3.UP,
300+
Vector3.UP,
301+
Vector3.UP,
302+
])
303+
304+
.. code-tab:: csharp C#
305+
306+
normals.AddRange(new Vector3[]
307+
{
308+
Vector3.Up,
309+
Vector3.Up,
310+
Vector3.Up,
311+
Vector3.Up,
312+
});
313+
314+
The ``indices`` array defines the order vertices are drawn. Godot
315+
renders in a *clockwise* direction, meaning that we must specify the vertices
316+
of a triangle we want to draw in clockwise order.
317+
318+
For example, to draw the first triangle, we will want to draw the vertices ``(0, 0, 0)``,
319+
``(1, 0, 0)``, and ``(0, 0, 1)`` in that order. This is the same as drawing ``vert[0]``, ``vert[2]``, and
320+
``vert[1]``, i.e., indices 0, 2, and 1, in the ``verts`` array. These index values are what the
321+
``indices`` array defines.
322+
323+
.. list-table::
324+
:header-rows: 1
325+
:widths: auto
326+
327+
* - Index
328+
- ``verts[Index]``
329+
- ``uvs[Index]``
330+
- ``normals[Index]``
331+
332+
* - 0
333+
- (0, 0, 0)
334+
- (0, 0)
335+
- Vector3.UP
336+
337+
* - 1
338+
- (0, 0, 1)
339+
- (1, 0)
340+
- Vector3.UP
341+
342+
* - 2
343+
- (1, 0, 0)
344+
- (0, 1)
345+
- Vector3.UP
346+
347+
* - 3
348+
- (1, 0, 1)
349+
- (1, 1)
350+
- Vector3.UP
351+
352+
.. tabs::
353+
.. code-tab:: gdscript GDScript
354+
355+
indices = PackedInt32Array([
356+
0, 2, 1, # Draw the first triangle.
357+
2, 3, 1, # Draw the second triangle.
358+
])
359+
360+
.. code-tab:: csharp C#
361+
362+
indices.AddRange(new int[]
363+
{
364+
0, 2, 1, // Draw the first triangle.
365+
2, 3, 1, // Draw the second triangle.
366+
});
367+
368+
Put together, the rectangle generation code looks like:
369+
370+
.. tabs::
371+
.. code-tab:: gdscript GDScript
372+
373+
extends MeshInstance3D
374+
375+
func _ready():
376+
377+
# Insert setting up the PackedVector**Arrays here.
378+
379+
verts = PackedVector3Array([
380+
Vector3(0, 0, 0),
381+
Vector3(0, 0, 1),
382+
Vector3(1, 0, 0),
383+
Vector3(1, 0, 1),
384+
])
385+
386+
uvs = PackedVector2Array([
387+
Vector2(0, 0),
388+
Vector2(1, 0),
389+
Vector2(0, 1),
390+
Vector2(1, 1),
391+
])
392+
393+
normals = PackedVector3Array([
394+
Vector3.UP,
395+
Vector3.UP,
396+
Vector3.UP,
397+
Vector3.UP,
398+
])
399+
400+
indices = PackedInt32Array([
401+
0, 2, 1,
402+
2, 3, 1,
403+
])
404+
405+
# Insert committing to the ArrayMesh here.
406+
407+
.. code-tab:: csharp C#
408+
409+
using System.Collections.Generic;
410+
411+
public partial class MeshInstance3d : MeshInstance3D
412+
{
413+
public override void _Ready()
414+
{
415+
// Insert setting up the surface array and lists here.
416+
417+
verts.AddRange(new Vector3[]
418+
{
419+
new Vector3(0, 0, 0),
420+
new Vector3(0, 0, 1),
421+
new Vector3(1, 0, 0),
422+
new Vector3(1, 0, 1),
423+
});
424+
425+
uvs.AddRange(new Vector2[]
426+
{
427+
new Vector2(0, 0),
428+
new Vector2(1, 0),
429+
new Vector2(0, 1),
430+
new Vector2(1, 1),
431+
});
432+
433+
normals.AddRange(new Vector3[]
434+
{
435+
Vector3.Up,
436+
Vector3.Up,
437+
Vector3.Up,
438+
Vector3.Up,
439+
});
440+
441+
indices.AddRange(new int[]
442+
{
443+
0, 2, 1,
444+
2, 3, 1,
445+
});
446+
447+
// Insert committing to the ArrayMesh here.
448+
}
449+
}
450+
451+
For a more complex example, see the sphere generation section below.
452+
453+
Generating a sphere
233454
-------------------
234455

235456
Here is sample code for generating a sphere. Although the code is presented in
14.6 KB
Loading

0 commit comments

Comments
 (0)