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

Vertex Index in Vertex Program

Discussion in 'Shaders' started by vladstorm_, Apr 26, 2016.

  1. vladstorm_

    vladstorm_

    Joined:
    Nov 26, 2013
    Posts:
    184
    Hi,

    How can I get a vertex number (or vertex index) inside vertex part of the shader?
     
  2. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    You can use VertexID if your target platform supports it.
    Code (CSharp):
    1. uint id : SV_VertexID
    Otherwise, you'll have to store the index as part of each vertex (as additional uv set for example).
     
    chrismarch likes this.
  3. vladstorm_

    vladstorm_

    Joined:
    Nov 26, 2013
    Posts:
    184
    @Michal_ interesting coz i didnt find this anywhere in the documentation.
    So is it a part of appdata_full ?
    or should I manually pass it
    or is it already there? i can just grab it.
    I have no idea because I'm using default Mesh and I dunno how exactly it works (i mean on the code side).
     
  4. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    appdata_full probably doesn't have it. You have to create your own input structure with vertexId and anything else you need. Something like:
    Code (CSharp):
    1. struct myAppData
    2. {
    3.     float4 position : POSITION
    4.     float3 normal : NORMAL;
    5.     uint vertexId : SV_VertexID;
    6.     ...
    7. }
     
    imaewyn likes this.
  5. vladstorm_

    vladstorm_

    Joined:
    Nov 26, 2013
    Posts:
    184
    @Michal_ wow/ didnt know u can do it. thank you
    and SV_VertexID will be automatically set if i use standard mesh right?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
  7. vladstorm_

    vladstorm_

    Joined:
    Nov 26, 2013
    Posts:
    184
    @bgolus but isnt it like mesh is giving all this stuff to shader ?
    and since i dont have the source for mesh class - i have no idea which data is in shader in the end.

    anyways / thank you / ill test it
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Most of it comes through as part of the mesh through API calls to Direct3D, yes, but stuff like BINORMAL doesn't have valid data in it since Unity's mesh format doesn't fill in that information since they reconstruct it in-shader. And BLENDINDICES and BLENDWEIGHT I believe are also unfilled because by the time the shader gets the mesh it's been through a Geometry Shader StreamOut that pre-deforms and strips this data.
     
    vladstorm_ likes this.
  9. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    SV_VertexID (and many others) is system-generated value. It is not part of the source mesh, it is generated by the gpu on the fly.