- Home /
How to split up a procedural mesh so you don't reach the vertex limit
I am creating a procedural mesh and to make it bigger I need to split it up into separate meshes so I don't get the vertex limit error. I thought I could just call the following method as much as I needed and it would create a new separate mesh each time but unfortunately it just overwrites the previously assigned vertices and does not create a new mesh.
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize();
mesh.RecalculateNormals();
MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
meshc.sharedMesh = mesh;
newVertices.Clear();
newUV.Clear();
newTriangles.Clear();
faceCount = 0;
}
would I just need to create a new instance of newVerticies and newTriangles? I have tried doing that like this but it gives me the same results (I have also tried making new instances of newVerticies and newTriangles etc. in the updateMesh method but nothing showed).
private void generateTestIsland()
{
for (int x = 0; x < grid.rows; x++)
{
for (int y = 0; y < grid.columns; y++)
{
grid.FindTile(x, y).elevation += 3;
if (x % 50 == 0 && y % 50 == 0 && x != 0 && y != 0)
{
newVertices = new List<Vector3>();
newTriangles = new List<int>();
newUV = new List<Vector2>();
UpdateMesh();
}
}
}
}
No matter how many arrays of vertices and triangles you create, everything depends on what you put in a mesh.
You have to understand what you are doing ins$$anonymous$$d of copy-pasting and expecting things to work.
Answer by jpthek9 · Feb 09, 2015 at 08:08 AM
int VCount = 0;
int MaxV = 64000; //The maximum vertices in 1 mesh
List<Mesh> Meshes = new List<Mesh>();
Mesh currentMesh = new Mesh();
Meshes.Add(currentMesh);
for (int i; i < mesh.verticies.Count; i++)
{
if (VCount < MaxV)
{
currentMesh.verticies.Add (mesh[i]);
}
else{
currentMesh = new Mesh();
Meshes.Add (currentMesh);
currentMesh.Add (mesh[i]);
VCount = 0;
}
}
I'm not entirely sure if this will work but it shows the concept. Generate a new mesh every time that mesh has its fill of vertices.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182840im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Scaling and rotaion of terrain trees 1 Answer
SetAlphaMaps Opacity Gradient 0 Answers
How would you go about 'painting' images onto terrain 0 Answers