Search Unity

How do I assign Vector3() values to individuals mesh vertices?

Discussion in 'General Graphics' started by Gigabitten_Gaming, May 3, 2020.

  1. Gigabitten_Gaming

    Gigabitten_Gaming

    Joined:
    Jul 10, 2017
    Posts:
    32
    I phrased the title of this post in a very particular way. I didn't say: "is is possible to to assign Vector3() values to individual mesh vertices?" Because among all the other posts with similar questions, a lot of people are saying that this isn't possible. Perhaps this is a hard problem, but saying that it's impossible is unintelligent. I mean, it's not like there is some universal scientific law of computer science that makes this problem unsolvable. It's not like I'm metaphorically trying to move an object faster than the speed of light.

    What I've figured out so far:
    • In OpenGL, mesh vertices are wrote from RAM into GPU memory using the "GPU memory buffer".
    • Using OpenGL, a developer has the option to "lock" the GPU memory buffer for certain memory values, which makes things a little bit faster in various ways. However, once a memory buffer is locked, its memory values cannot be changed until it gets unlocked. (I'm sure its possible to use a more basic API to brute force the locked memory values to change, but I assume that this would probably cause OpenGL to crash.)
    • Because Unity keeps all of its OpenGL/DirectX stuff under the hook, there's no easy way to unlock the GPU memory buffer manually. This is a major bummer. (As a side note, Unity's lack of flexibility, as exemplified here, is a big reason why games developed on custom game engines tend to be faster than game made by Unity.)
    • Assuming that there is a way to work around this and unlock the memory buffer, changing the mesh vertices would be as simple as getting the pointers and modifying the memory at the correct pointer index in both the RAM and GPU memory.
    Given what I've figured out so far, does anybody have any ideas on how to accomplish this?
     
  2. Gigabitten_Gaming

    Gigabitten_Gaming

    Joined:
    Jul 10, 2017
    Posts:
    32
    Or perhaps a better question might be whether or not going through all this effort would actually make the game run faster. Does anybody know anything that might answer this question as well?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,350
    I'm not entirely sure what the end goal is.

    If you want to modify a single vertex, modify the vertex on the mesh.
    Code (csharp):
    1. var vertices = mesh.vertices;
    2. vertices[vertexIndex] = newVertexPosition;
    3. mesh.vertices = vertices;
    4. mesh.UploadMeshData();
    If you're starting with a mesh you're importing, make sure Read/Write Enabled is check on in the import settings.

    Direct memory mapping of the mesh might technically be faster, but probably not appreciably so. It's not uncommon for Unity to upload thousands or even millions of vertices every frame for things like UI, sprites, or particles, and all of that is usually only a small fraction of the amount of data normally needed to be sent to the GPU every frame.