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*;*
-
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*;_
_ 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 MeshCollider? If so, that's your problem ;) Updating a MeshCollider is very (very) slow. <a href="https://dl.dropbox.com/u/7761356/UnityAnswers/Web/SphereTerrain/WebPlayer.html ">This</a> 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 MeshCollider. If i use one a single update might take up to 10 sec.
– Bunny83@Bunny83 - interesting about MeshColliders - I kind of never use them for other reasons - but now I've got a new shiny reason!!
– whydoidoitThat really helped, I went down from 25 miliseconds, to 8, in average!
– WdarkfenixI assume you've already realized this, but SystemInfo.supportsGyroscope returning false means that your hardware does not have a gyroscope to read from (or at least doesn't have one that Unity supports).
– CraigGraffBuilding to pc did not work. building to UWP did. The hardware turned out to work, just not in the IDE. same issue as above. :(
– DanVioletSagmiller