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.
Now we need to generate our mesh or quad if you want. ?
First, we need to create base of our code!
using UnityEngine; /// <summary> /// Script that generates a quad with width and height. /// </summary> public class QuadGenerator : MonoBehaviour { // Reference to the mesh filter. private MeshFilter meshFilter; // Width of our quad. [SerializeField] private float width = 2; // Height of our quad. [SerializeField] private float height = 2; /// <summary> /// Unity method called on first frame. /// </summary> void Start() { meshFilter = GetComponent<MeshFilter>(); GenerateQuad(); } /// <summary> /// Method which generates quad. /// </summary> private void GenerateQuad() { // Creating mesh object. Mesh mesh = new Mesh(); // Defining vertices. Vector3[] vertices // Defining triangles. int[] triangles // Defining UV. Vector2[] uv // Assigning vertices, triangles and UV to the mesh. mesh.vertices = vertices; mesh.triangles = triangles; mesh.uv = uv; // Assigning mesh to mesh filter to display it. meshFilter.mesh = mesh; } }
Second, we need to define the positions of vertices.
// 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.
// 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!
At this point, there is only one thing left. UV!
// 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! ?
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:
https://docs.unity3d.com/ScriptReference/Mesh.html
https://docs.unity3d.com/Manual/Example-CreatingaBillboardPlane.html