Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Mesh API - Efficient way to obtain global vertex position given a vertex index

Discussion in 'Scripting' started by lcompton, Jul 2, 2023.

  1. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    79
    I'm looking for an efficient way to obtain the global vertex position given a mesh vertex index. Mesh.vertices and Mesh.GetVertices() give you a copy of the entire array, which would work, but that's overkill for what I'm trying to do. I just need a simple lookup method, something like... Vector3 GetVertexFromIndex(int index).
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Read it once at startup, get the vert you want, then use the Transform at that moment to compute the actual world space.

    If you are modifying the mesh at runtime then record the position of the vert in question.

    A far more robust solution is to put a blank GameObject at the "interesting" vert in question and use that GameObject's Transform to find position.
     
  3. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    79
    I'm picking random vertices throughout the lifetime of the object. I found a workaround for what I'm trying to do, but let me restate the core question...

    Is there a way to lookup vertex coordinates given a vertex index (local space is fine) without making a copy of the mesh vertex array?

    Sadly, it would appear the answer is no. I just want to verify.
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    The answer is a no. I was certain at some stage Unity added the ability to do a partial read of mesh data. I know this feature exists in graphics APIs. But I think I may have confused Unity's GetSubMesh with OpenGL's glBufferSubData.
     
    lcompton likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Well, there is a way using the GetVertexBuffer method which returns a GraphicBuffer object. You can use the GetData method of that buffer to only copy one element from the buffer into a fix managed array.

    Though if you aim for a garbage free solution you probably want to cache the GraphicBuffer as well as the intermediate array that receives the data. Also since the vertex buffer contains actual vertices, you also want to cache the actual layout of a vertex, so you know what you're actually reading. A vertex on the GPU consists of several things (normal, uv, tangent, ...) and the position is just one of them.

    You haven't really said why caching the vertex array / list is an issue and what your actual usecase is.. Do you talk about a single object / mesh? Or do you want to get vertex positions of arbitrary objects in your scene? Context generally helps to understand what you want to do, why and if there might be a better solution.
     
  6. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    79
    Thanks for the replies. No need to explain the use case in detail. It's for a minor feature and I've already found an adequate compromise/workaround. I was simply trying to make sure I wasn't missing anything obvious in the API docs.