- Home /
Procedural Mesh Generation - Split Arrays into sections
I am using a modified version of a procedural generation asset which generates a mesh at runtime, but for performance i am trying to split the mesh into pieces.
Currently im dividing the arrays for the vertices, uv's and triangles into 6 as thats how many pieces i want and then looping six times and each time copying the section from the arrays that i need and using it to make a mesh which would then be applied to a gameobject. However when i run the code i get the following error:
"Failed setting triangles: Some indices are referencing out of bounds vertices. UnityEngine.Mesh:set_Triangles(Int32[])"
Any help resolving this issue would be greatly appreciated.
This is the code im currently using:
for (int i = 0; i < 6; i++)
{
//UnityEngine.Debug.Log("i is: "+i+" "+(i*vertsChunk));
Array.Copy(vectorArray, (i * vertsChunk), chunkverts, 0, vertsChunk);
Array.Copy(vectorArray2, (i * uvchunk), chunkUv, 0, uvchunk);
Array.Copy(vectorArray3, (i * uvchunk2), chunkUv2, 0, uvchunk2);
Array.Copy(numArray, (i*triChunk), chunktris, 0, triChunk);
Mesh m = new Mesh
{
vertices = chunkverts,
uv = chunkUv,
tangents = new Vector4[vertsChunk],
uv2 = chunkUv2,
triangles = chunktris
};
m.RecalculateNormals();
}
Sounds like either one of the triangles doesn't have exactly three vertex indices, or one of the triangles' vertex indices is higher than the vertex count.
i have checked both of those and they are both set correctly.
Answer by incorrect · Jan 11, 2015 at 01:27 PM
That's probably because in triangles[] you just enumerate vertices by their indexes in vertices[] and you can not just divide those arrays because that mathematical coherence will be broken. Also note that in triangles[] polygons can be enumerated in any order, so if you take first half of vertices that does not mean that polygons that use those vertices will be in first half of triangles[]. You have to do it smarter: for each vertice in separate part you have to check through triangles[] and copy those polygons to that part's triangles[]. And you still have to solve the problem wich will occur with polygons that have vertices in different parts. Try reading about meshes and how they work here and there.
Oh, btw, the error appears because renderer picks an int from triangles[] that is greater than vertices.Length. That's what I mean by saying 'mathematical coherency will be broken'. :)
your suggestion of looping through each vertice in a section and copy those polygons. How would i do that, a simple for loop or would i have to try and work out which polygons match the vertice.
sudo code would be really helpful in understanding your idea, if possible ?
Well, it's not very straightforward for a 'for loop'.
You have to divide vertices[] on several parts.
You have to run through all of those vertices and check their presence in triangles[], picking each separate triangle and checking if all other vertices are in this part of vertices[] division. If some vertices appear to be in another part of division (and there will be many of them), you have to add them in that part. (Btw, that will cause duplication of vertices, because some of them have to be in more than one mesh after division)
Then you have to recalculate indexes and make new meshes with resulting arrays. Remember that indexes of vertices[], uv[], triangles[] and all other arrays that form mesh are the core of how mesh works.
The reason why meshes are made index-based is in the efficiency of this method, but it makes working with meshes quite complicated. Anyway, if you can avoid getting deep into meshes, try to make it, else it's not a question for Unity Answers and you have to start a discussion on Forums.
Your answer
Follow this Question
Related Questions
Highmap on a cube. 0 Answers
Best Way to Link Vertices into Mesh 0 Answers
Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer
Procedural cube code 0 Answers