Generate mesh yourself!

Let’s talk about meshes! Did you try to generate one yourself? Well, you are about to do that!

Meshes are used to display 3D models in a virtual environment. They are built from polygons which are covered with texture, based on the UV map. Its role is to give a player or a user impression of an object.

When an object has a lot of polygons, it’s called hi-poly. Usually, these are counted in hundreds of thousands or even in millions. Often, you can see them in movies or some AAA games.

On the other hand, there are objects with much fewer polygons, and they are called low-poly. Usually, low-poly models are a style choice, especially when you can see polygons on the model without any problem.
Low-poly models can also be used as an optimization technique, with baked textures from hi-poly models.

Anatomy of Mesh

For today’s post, we need to know about 3 elements of the mesh. These elements are verticles, triangles, and UV.

Vertices (Vector3) are points in 3D space. Which are later connected by…

Triangles (int) indicates which of 3 vertices should create triangle. They are all assigned later into one big list. Order is important here, as a triangle might be facing in the wrong direction.

UV (Vector2) tells how texture should be displayed on the model. It’s using coordinates from 0.0 to 1.0, which are later translates to position on the texture.

With that knowledge, we can try to generate our first mesh!

How to generate mesh?

Let’s make a little setup here. We will need a GameObject with MeshRenderer and MeshFilter. You can also attach the material to the renderer so you will be able to see it on the mesh.

GameObject components.

Now we need to generate our mesh or quad if you want. πŸ˜…

First, we need to create base of our code!

Second, we need to define the positions of vertices.

Vertices for the quad.
// Defining vertices.
Vector3[] vertices = new Vector3[4]
    new Vector3(-width/2, height/2,0),
    new Vector3(width/2, height/2,0),
    new Vector3(-width/2, -height/2,0),
    new Vector3(width/2, -height/2,0)

We won’t see anything yet, because we need to define triangles as well.

Triangles in the quad.
// Defining triangles.
int[] triangles = new int[6]
    0,1,2, // first triangle
    1,3,2 // second triangle

Now we will be able to see our mesh!

Mesh built from vertices and triangles. It’s missing UV map.

At this point, there is only one thing left. UV!

UV for the quad.
// Defining UV.
Vector2[] uv = new Vector2[4]
    new Vector2(0,1),
    new Vector2(1,1),
    new Vector2(0,0),
    new Vector2(1,0)

Great! Maybe it’s not much, but this is the beginning of building something greater! πŸ”₯

Generated mesh with UV and texture on it.

What do you think about it? Have you tried generating meshes earlier? Let me know in the comment section below!

If you want to get notified on future content, sign up for the newsletter!

As always, the whole project is available at my public repository. πŸ”—

And I hope to see you next time! πŸ€“

Additional references:

Notify of