Search Unity

Procedural mesh on update without creating new arrays.

Discussion in 'General Graphics' started by SidarVasco, Nov 10, 2016.

  1. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    I'm trying to create lines ( quads ) and feed that into a vertex buffer. Since these lines only need to show up if certain points are in proximity the the amount of vertices to draw will vary.

    Dx and OpenGL have DrawIndexed and DrawElements but I can't find something similar for unity.
    I just want a fixed sized vertex and index buffer, keep those and tell the renderer to only draw X amount of elements.

    Any advice?

    Edit:
    I'm targetting mobile so I don't want to mess with resource allocation too much. I just want to set the data in the vertex buffer, upload it to the gpu and tell it to draw.
     
  2. Lost-in-the-Garden

    Lost-in-the-Garden

    Joined:
    Nov 18, 2015
    Posts:
    176
    To my knowledge unity does not support that atm.

    How many vertices does the whole grid have? Maybe you can get away with always rendering the full mesh, but blending it in the fragment buffer? If your mesh is small, then rendering a few vertices more/less does not matter much since most time is spent in the overhead anyway.
     
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    Besides the shader way above there are some other things (I'm assuming you're doing some of these):
    1) Reuse the mesh object, subsequent .Set* calls are faster so they seem to reuse the internal arrays
    2) Use mesh.Set* overloads with List<>s, they seem to be as fast but you don't have to allocate various arrays all the time
    3) Leave the full vertex buffer and only update the indices
    4) If you can calculate bounds manually in a quick way, use the SetTriangles overload that doesn't update the bounds.
     
  4. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    @Lost-in-the-Garden
    It's not going to be that many. Tops 50 lines ( x 4 vertices ). I'm kinda baffled there is no simple render method for it.

    @Zuntatos
    I can't leave the vertex buffer alone, I need to adjust the vertex positions every frame. The problem is that the renderer takes the whole indices. So I'd still have to make sure the list grows/shrinks properly which will cause at least some allocation/gc
     
  5. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    You can set the List<> capacity to 200 so it won't resize when you're filling it. A List<> doesn't shrink its capacity automatically, you have to explicitly call the TrimExcess() function to do so.
     
  6. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    Well I just settled with clearing the list and adding new data. If it causes issues ill look into a bit more.

    I still don't understand why there isn't a DrawIndexed method. WTF @Unity
     
    Zuntatos likes this.
  7. Lost-in-the-Garden

    Lost-in-the-Garden

    Joined:
    Nov 18, 2015
    Posts:
    176
    try measuring how long that single draw call takes. If shows up as a bleep, then continue to optimize it. If not, just go with the simplest solution :D