Unity, can I create my own mesh?

Hello everyone

In the next three posts, I will explain how to develop a dynamic mesh with a Pelin noise high maps and a custom shader to draw our mesh with the high map. This tree post will be, «Unity, can I create my own mesh?», «Perlin you are making a lot of noise» and «Color without if sentence». Well, let’s start with «Unity, can I create my own mesh?»

First of all, you need to understand the correct behavior and concept about what is a mesh. A polygon mesh is a collection of vertices, edges, and faces that define the shape of a polyhedral object in 3D computer graphics and solid modeling. The faces usually consist of triangles (triangle mesh), quadrilaterals, or other simple convex polygons, since this simplifies rendering, but may also be of more general concave polygons or polygons with holes.

In our case, we will use a triangle mesh that means that we must divide our mesh, at this moment a plane, in xparts and yparts (vertex) and join all these parts in triangles. The first step will create the vertex, to do that we just need four parameters:

  • Width
  • Height
  • Xvertex (how many parts we want to divide the width)
  • Yvertex (how many parts we want to divide the height)

The process is simple, just divide the width for the xVertex to get de distance between vertex y x and the same for the height with the yVertex. The next image is an example of a mesh with 5 points as width division and 3 as height.

With two simple «for loop», we create all of our vertices position. The first for will be used the height division and the second for the width division.

float xSpace = (Width / (XVertex - 1));
float ySpace = (Hehght / (Yvertex - 1));
for (int j = 0; j < Yvertex; j++) {
	for (int i = 0; i < XVertex; i++) {
		pos = new Vector3(i * xSpace,0, j * ySpace);

Now we have all vertex position created, the next step is to create the triangulation. This is simple; just take the vertex index… wow, vertex index? What is this? All the vertex are saved into an array, the index is the position of each vertex in this array. So let’s begin to create our triangles.

To create a triangle we need only three vertices, but they may be in clockwise. If you look the below image you can understand what are I said. For example, the triangle number one is composed by the vertices 5,1,0 in this order, or 0,5,1 or 1,0,5, the three combination are ok.

Well the code to make this is the next:

for (int j = 0; j < Yvertex - 1; j++) {
	for (int i = 0; i < XVertex - 1; i++) {
		triangulation.Add(i + (j * XVertex) + XVertex);
		triangulation.Add(i + (j * XVertex) + 1);
		triangulation.Add(i + (j * XVertex));
		triangulation.Add(i + (j * XVertex) + 1);
		triangulation.Add(i + (j * XVertex) + XVertex);
		triangulation.Add(i + (j * XVertex) + XVertex + 1);

Well now, we have our data, but it necessary set these values to a mesh. This mesh is part of component meshFilter. Therefore, you only pass these arrays to the mesh component like this

if (GeneratedMesh == null) {
	GeneratedMesh = new Mesh();
	this.gameObject.GetComponent().mesh = GeneratedMesh;
GeneratedMesh.vertices = vertex.toArray();
GeneratedMesh.triangles = triangulation.ToArray();

At the end, you only set a material to avoid the horrible pink colour.
In the next post, I explain how to pass from to a static mesh from a dynamic mesh.

To check all code, please visit my bitbucket repository or click here.


  • All not so is simple, as it seems

    • admin

      Tell me what are you don’t understand and I will help you 😉

Leave a Comment