So i’ve had a problem for the past few days… I am trying to handle having a plane of 400 cubes on mid-tier(?) mobile at 60FPS (Samsung J5 2015). The problem is it needs to allow 20+ cubes to be moved to a new position/material changed every say 10 frames minimum… (Only 2 different materials it can be)
The first approach I spawned 400 prefabs and batched the rendering… This handles 60FPS for a little bit but after a minute or so the phone will heat up and drop to around 40FPS.
Second approach I used the unity MeshCombine function on the 400 cubes after an edit, but this results in upto a 0.10 second freeze in gameplay due to the large amount of cubes.
This was suppose to be a simple project but it quickly became an optimisation headache. I can run all my progress so far easily on an S9 but not on my reference device J5. My last resort is some sort of lower level voxel system but at that point I question whether its worth the time, I haven’t dived into that area yet. Does anyone have any suggestions?
Generate mesh yourself. 400 cubes == 4800 triangles, mid tier mobile will handle this with two materials without any problem.
Before you go any deeper into optimizations - have you run the profiler in order to determine where your best optimization targets are?
-ch
Alright I am generating the mesh myself procedurally but I have an issue now with removing triangles… I can’t keep track of what triangles are part of what cube because as soon as I delete triangles for one cube the triangle indexes for every other cube change. Probably needs another post…
You don’t need to remove/add triangles. Just generate whole new mesh every time. It should not be slow if you reuse mesh instance and lists used to generate it.
1 Like
What do you mean by reuse lists used to generate it.
I have set it to refresh the entire mesh as you mention but when it change I am getting 50KB of garbage collection because it has to recreate the vertices/triangles list etc each time because they are different.
How can I generate a new mesh each time AND reuse the vertex/triangle lists. Is that what you are suggesting?
All I can think of is reuse a cached untouched list of triangles, and loop from end to beginning somehow removing the ones we dont need (since indexes wont change when removing in reverse order) but I dont think thats what you meant…
Declare readonly lists as fields in the class generating mesh like this
private static readonly List<Vector3> Vertices = new List<Vector3>(4800 /* maximum number of vertices expected */);
Every time you want to generate new mesh, call
Vertices.Clear();
Same with other lists. After that fill them again. List is designed to reuse it’s memory in this scenario. After that clear the mesh and fill again.