# Generating unique vertices for a procedural mesh

I’m having an issue with a procedurally generated terrain (made from a pre-made plane), the vertices are shared, and I don’t really know how to fix it…

As the vertices are shared it gives a smoothing effect, which looks really bad.

Here is the simple code for generating the terrain:

``````void Start()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int i = 0;
while (i < vertices.Length)
{
vertices <em>+= normals <em>* Mathf.PerlinNoise(vertices_.x, vertices*.z);*_</em></em>
``````

i++;
}
mesh.vertices = vertices;
}

As the predefined plain mesh is not intended for such a usage, I think you will need to generate your own mesh. This will mean not much more effort for you, because you are already manipulating the vertices yourself. As long as one triangle has different normals at its corners, they will be interpolated per fragment. That’s how the pipeline works.

Here is the link from Unity Manual that you might already know: GeneratingMeshGeometryProcedurally

It is really simple: Create vertices, create triangles by giving the indices of 3 vertices each and creating the normals.

Even though this thread was resurrected a bit late I’ll leave this here for anyone interested. To create a procedural plane useful for terrain generation it’s easiest to iterate over the vertices and create quads of two tris each for every vertex. One way looks like this:

``````public void CreateMesh(int size)
{
List<Vector3> verts = new List<Vector3>(); // Index used in tri list
List<int> tris = new List<int>(); // Every 3 ints represents a triangle
List<Vector2> uvs = new List<Vector2>(); // Vertex in 0-1 UV space
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
uvs.Add(new Vector2((float)i / size, (float)j / size));
if (i == 0 || j == 0) continue; // First bottom and left skipped
tris.Add(size * i + j); //Top right
tris.Add(size * i + (j - 1)); //Bottom right
tris.Add(size * (i - 1) + (j - 1)); //Bottom left - First triangle
tris.Add(size * (i - 1) + (j - 1)); //Bottom left
tris.Add(size * (i - 1) + j); //Top left
tris.Add(size * i + j); //Top right - Second triangle
}
}
Mesh mesh = new Mesh();
mesh.vertices = verts.ToArray();
mesh.uv = uvs.ToArray();
mesh.triangles = tris.ToArray();
mesh.RecalculateNormals();

GameObject terrain = new GameObject("Terrain");