- Home /
C# Proceducal Mesh terrain
Hello guys,
I am trying to create a proceducal terrain. I created a script that should work (I my head, not in unity). What I want is just a lot of faces next to each other. Just like a plane in Unity.
This is the code that i have now: // Create terrain mesh Mesh mesh = GetComponent().mesh;
for (int _x = 0; _x < chunkSize; _x++)
{
for (int _z= 0; _z < chunkSize; _z++)
{
vertices.Add(new Vector3(_x, 0, _z));
vertices.Add(new Vector3(_x + 1, 0, _z));
vertices.Add(new Vector3(_x + 1, 0, _z + 1));
vertices.Add(new Vector3(_x, 0, _z + 1));
triangles.AddRange
(
new int[]
{
3 + (_z * 4), 1 + (_z * 4), 0 + (_z * 4),
3 + (_z * 4), 2 + (_z * 4), 1 + (_z * 4)
}
);
}
}
// Apply data to the mesh
mesh.Clear();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.Optimize();
mesh.RecalculateNormals();
But it only makes one line of faces: https://gyazo.com/266ffbcaa953bf711255713f94e58604
Does anyone know what the problem is?
Thanks in advance, Wiebren de Haan.
Have you tried inseting Debug.Log lines to check the x,z values that are caluclated? The problem might lie in the way you are assigning triangles.
There's a plane generation script on the User Wiki you can use as reference:
You were right, the problem was with the triangles. Thanks!
Answer by Bunny83 · Nov 05, 2015 at 05:57 PM
Since you seem to create seperate quads which doesn't share vertices with other quads the triangle generation would simply be:
int size = (chunkSize-1) * (chunkSize-1);
triangles = new int[size*6];
for (int i = 0; i < size; i++)
{
triangles[i*6 + 0] = i*4 + 3;
triangles[i*6 + 1] = i*4 + 1;
triangles[i*6 + 2] = i*4 + 0;
triangles[i*6 + 3] = i*4 + 3;
triangles[i*6 + 4] = i*4 + 2;
triangles[i*6 + 5] = i*4 + 1;
}
You don't need to use Lists in your case since you know the required size beforehand. Using Lists will just create additional garbage, especially your temp arrays when creating the triangles.
Usually you would create a plane with shared vertices. That will reduce the required vertices by quite a bit (4 times less vertices). Since Unity has a Vertex limit of 65000 vertices per mesh that's quite a difference. With single quads like in your code you can create a plane with 127x127 subquads. With shared verices about 254x254 Of course the mesh is one big shared quad so UVs and normals are continous across the mesh. If you need / want to assign individual UVs to a single subquad you can't share all vertices.
Vector3[] vertices = new Vector3[chunkSize * chunkSize];
Vector2[] uvs = new Vector2[vertices.Length];
int size = (chunkSize-1) * (chunkSize-1); // sub quad count
int[] triangles = new int[size * 6]; // 6 indices per quad
for (int _z= 0; _z < chunkSize; _z++)
{
for (int _x = 0; _x < chunkSize; _x++)
{
vertices[_z*chunkSize + _x] = new Vector3(_x, 0f, _z);
uvs[_z*chunkSize + _x] = new Vector2(_x, _z) / (chunkSize-1);
}
}
for (int i = 0; i < size; i++)
{
triangles[i*6 + 0] = i;
triangles[i*6 + 1] = i + chunkSize;
triangles[i*6 + 2] = i + 1;
triangles[i*6 + 3] = i + chunkSize;
triangles[i*6 + 4] = i + chunkSize + 1;
triangles[i*6 + 5] = i + 1;
}
Answer by Inxhaine · Nov 06, 2015 at 02:28 PM
I will recommend taking a look on the following site:
It has some excellent tutorials on procedural meshes in Unity....including terrain