Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved New Mesh.SetVertices vs. Old Mesh.vertices - using array versus list, and mobile performance

Discussion in 'Authoring Dev Blitz Day 2023' started by bigpants, Jan 26, 2023.

  1. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    49
    It's a general question about Unity which I originally posted here:
    https://forum.unity.com/threads/new...-and-mobile-performance.1390564/#post-8754019

    Old ".vertices" forces me to send the entire array.
    New ".SetVertices" allows me to send part of the array!
    AWESOME! This solves a huge problem for me and I want to use .SetVertices.
    IMO, there's NO reason to use ".vertices" anymore
    Is that correct?


    My 2 Questions (repeated from the original forum posting)

    Question 1
    Is there a difference in performance between passing an array "Vector3[]" or a list "List<Vector3>" to "Mesh.SetVertices"? I thought array would be best BUT "Mesh.GetVertices" returns a list, implying list is the way to go. Ideally I'd like to switch to "List<Vector3>" but not if it costs performance. I'm doing a significant amount of procedural generation, so this is important to me.

    Question 2
    On older mobile, using "Mesh.vertices = MyArray",
    the size of MyArray mattered EVEN if there was nothing in it.
    Example:
    If I wanted to render 100 cubes, I would need to pass in an array perfectly sized to 100 cubes.
    Using an array of 200 cubes, with the last 100 zeroed out, would render perfectly BUT perform poorly on mobile.

    I'm ASSUMING Mesh.SetVertices FINALLY SOLVES this problem
    (since it can be passed the length of the array/list) BUT is that true?
    Specifically, on mobile, if MyArray contains 200 elements,
    does "Mesh.SetVertices(MyArray, 0, 100)"
    perform as well as "Mesh.vertices = Array-of-JUST-100-Elements"?

    thankyou!
     
    DevDunk likes this.
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    `Mesh.vertices` and `Mesh.SetVertices` internally end up through the same call, `SetSizedArrayForChannel`. There's no reason to use the `vertices` setter over `Mesh.SetVertices`.

    As noted above, they both end up going through the same call to native code. Internally we are extracting the array from the managed list before marshalling, so there likely isn't a perceivable difference.

    GetVertices exists to avoid managed allocations. It doesn't return a new list, it appends to user-provided list instead. If you are reading vertices back from native to managed often then this can avoid lots of unnecessary garbage.

    I'm not sure, this is more of a questions for graphics. You could always just give a try!

    My best guess would be that as with all things performance, it depends on your target platform and device.
     
  3. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    49
    THANKYOU!