- Home /
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[i] += normals[i] * Mathf.PerlinNoise(vertices[i].x, vertices[i].z);
i++;
}
mesh.vertices = vertices;
}
Thanks in advance!
Answer by Enum · Jun 08, 2015 at 09:31 AM
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.
Answer by cjdev · Jan 21, 2016 at 03:54 AM
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++)
{
verts.Add(new Vector3(i, 0, j)); // Add noise function into y
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");
terrain.AddComponent<MeshFilter>();
terrain.AddComponent<MeshRenderer>();
terrain.GetComponent<MeshFilter>().mesh = mesh;
// Load a material named "TerrainMat" from a folder named "Resources"
Material terrainMat = Resources.Load<Material>("TerrainMat");
terrain.GetComponent<Renderer>().material = terrainMat;
}
The thing to remember about generating meshes procedurally is that the order of the vertices matters and the triangles have to be made clockwise. If you're interested in other applications of procedural meshes you can still use this method but switch out the i and j variables in the for loops or in the vertex declaration. Besides the terrain method using noise you can also get things like curves, circles, and even spheres.
Uhm, you also generate shared vertices and not unique vertices per quad / triangle which was the question.
If you needed the individual vertices per quad you'd only have to build the mesh with a size of 1 and loop over that, my point was just to show how to make the basic mesh.
Your answer
Follow this Question
Related Questions
Shadow artifects on mesh with Standard shader 1 Answer
Seam on Procedural Mesh 1 Answer
how to make an horizontal mesh? c# 0 Answers
How to overlap tiles in procedural meshes? 1 Answer
Procedural model turns black up close 0 Answers