Unity Community


Results 1 to 5 of 5

  1. Posts
    30

    Any approaches C# Struct Caching for Memory and Performance?

    Is there a good approach to recycling thousands of small structs, a subset of which get modified per frame.

    Take something where i am creating a dynamic mesh on the fly. Some of the vertices and some of the tris get modified but I'd like to be able cache what I have.

    conventionally, I might create an array of these objects and cycle through the array as a primitive factory. Is there any way to encapsulate this in say a generic class or anything?

    I realize that structs are copied by value.

    I'm just not sure what the best performance solution is to this.

    Cheers

  2. Volunteer Moderator
    Posts
    23,737
    Keep the vertex and triangle arrays, modify whatever you need, and upload them. When you do "mesh.vertices = myVertices", that's actually uploading the vertices to the graphics card. So you don't need a new vertex array every frame, just use the one you already have.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  3. Posts
    30
    Cool... but what about needing to modify the size of my array? The model ad or remove verts as it goes. Is there a clean way of doing this?

  4. Volunteer Moderator
    Posts
    23,737
    You can choose a maximum size for the vertex array, and set any unused vertices to Vector3.zero (or any value really as long as it's the same). Those are skipped over when rendered, although they still count toward the upload size.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  5. Posts
    30
    What about structures outside the mesh itself? I have an abstract model is an array of structs. I'm still trying to get a good sense of when I can reasonably use a struct.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •