- Home /
Why Unity freezes while creating Meshes procedurally?
I'm creating a mesh procedurally with around 10000 vertices, the problem is that when I start the mesh generation it freezes for a few moments, I used the time method to get the number of frames that past but it appears that not a single one past, then the mesh is finished, but I dont want that the program freeze each time that I generate a new mesh, 'cause I'm doing it constantly.
The arrays fills automatically before creating the mesh, but just filling them without sending them to the mesh there's no lag, so I don't think the problem is there.
Also, if ,after generating the mesh, a method move all the vertices around there's no lag, so the manipulation of the mesh appears to be ok too, anyone has a clue of why is this happening?.
Here's the codo so you can take a look:
public void Build( GameObject GObject,
List<Vector3> chunkPosition,
List<Vector3> chunkRotation,
List<int> chunkSides,
List<int> chunkSpace){
float time1,time2,timeResult;
time1 = Time.time;
int arraySize = 0,
index = 0,
sidesToRender = 0,
tama = chunkBlockID.Count;
MeshFilter meshFilter = GObject.GetComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
mesh.Clear();
//Each position in each list represents information of a cube that occupies that place
//chunkSpace represent the number of triangles of the cube that are to be rendered
for (int i = 0; i < tama; i++)
arraySize += chunkSpace[i];
Vector3[] vertices = new Vector3[arraySize];
Vector2[] uv = new Vector2[arraySize];
int[] triangles = new int[arraySize];
Vector3 Temp = new Vector3(0,0,0);
////ChunkSides contains six glags, one per side, so this cycle decides the orientation of each triangle
for (int i = 0; i < tama; i++){//Todos los posibles bloques
sidesToRender = chunkSides[i];
for(int j = 0,selector = 1,Kstart = 0,Kend = 6;
j < 6;
j++ ,selector *= 2,Kstart += 6,Kend += 6){
//If the flag of a side is on, then add the respective rendering parts
if((sidesToRender&selector) != 0)
for(int k = Kstart; k < Kend; k++, index++){
vertices[index] = Shapes.CubeVx[k]+Temp;
triangles[index]= index;
uv[index] = Shapes.CubeUV[k];
}
}
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uv;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
time2 = Time.time;
timeResult = time2-time1;
print(timeResult);
//Get the total time elapsed (it always gives me 0)
}
Do you use a $$anonymous$$eshCollider? If so, that's your problem ;) Updating a $$anonymous$$eshCollider is very (very) slow. This was an old webplayer i created. The cube is made up of 6 independent meshes each 128x128 vertices (as far as i remember). The recreation is very fast, but i don't use a $$anonymous$$eshCollider. If i use one a single update might take up to 10 sec.
@Bunny83 - interesting about $$anonymous$$eshColliders - I kind of never use them for other reasons - but now I've got a new shiny reason!!
That really helped, I went down from 25 miliseconds, to 8, in average!
Answer by whydoidoit · Mar 19, 2013 at 03:04 PM
Firstly no frames will pass if you are taking a long time to do something, it will be a synchronous operation.
If you want to know how long individual parts take you could use a StopWatch to time them.
Without seeing your code it's hard to tell why its taking some time, but of course creating a mesh will potentially need to do lots of things with the GPU and allocate a significant number of objects. One solution would be to prepare a collection of meshes and then manipulate them as that seems to be a fast enough operation for you.
Well it would certainly be worth working out which parts of that caused the delay using a StopWatch as I suggested. You can't rely on the Time functions as they are part of the game loop.
I used stop watch, the major problem was the collider.
Your answer
Follow this Question
Related Questions
Flat shading procedural generated mesh? 1 Answer
Help in understanding Mesh Generation 1 Answer
generate procedural floating island 1 Answer
Generated mesh loses lighting at certain angles 2 Answers
Procedural Cylinder Generation 2 Answers