Search Unity

Get mesh triangle

Discussion in 'Getting Started' started by rhughes, Nov 6, 2015.

  1. rhughes

    rhughes

    Joined:
    Jun 26, 2013
    Posts:
    5
    If I want to get a triangle's vertices from a mesh, I currently need to call mesh.vertices. The problem with this is if I call this each frame and the mesh is big, it allocates a lot of memory each frame - which adds up to increase GC calls.

    Is there a way to get a triangle's vertices from a mesh without calling Mesh.vertices? Or is there a more memory-efficient way of getting the mesh vertices?

    I am currently doing this based on a RaycastHit.

    Thanks!
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Can you cache the mesh.vertices?
     
  3. rhughes

    rhughes

    Joined:
    Jun 26, 2013
    Posts:
    5
    There is that option. I would like to avoid doing this though as the mesh can deform during runtime, so a cached set of vertices even of the same length is no guarantee of being a current representation, unless I implement a timestamp of some kind.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    If the mesh can deform at any time, then I can't see any way to avoid calling mesh.vertices whenever you need to know where the vertices are.

    But if you can hook into whatever's deforming the mesh, maybe you can have it do the same operation to your triangle points, or otherwise have it report back where those three vertices are. (Of course it's calling mesh.vertices whenever it's deforming the mesh anyway.)