Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

400 cubes on mid tier mobile... Optimisation assistance required

Discussion in 'Scripting' started by chipity, Jul 23, 2019.

  1. chipity

    chipity

    Joined:
    Dec 21, 2016
    Posts:
    7
    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?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Generate mesh yourself. 400 cubes == 4800 triangles, mid tier mobile will handle this with two materials without any problem.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Before you go any deeper into optimizations - have you run the profiler in order to determine where your best optimization targets are?

    -ch
     
  4. chipity

    chipity

    Joined:
    Dec 21, 2016
    Posts:
    7
    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..
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    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.
     
    chipity likes this.
  6. chipity

    chipity

    Joined:
    Dec 21, 2016
    Posts:
    7
    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..
     
    Last edited: Jul 26, 2019
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Declare readonly lists as fields in the class generating mesh like this

    Code (CSharp):
    1. private static readonly List<Vector3> Vertices = new List<Vector3>(4800 /* maximum number of vertices expected */);
    Every time you want to generate new mesh, call

    Code (CSharp):
    1. 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.