Search Unity

How to find the index of a vertex in a fragment shader

Discussion in 'Shaders' started by PiezPiedPy, Aug 28, 2019.

  1. PiezPiedPy

    PiezPiedPy

    Joined:
    Mar 22, 2019
    Posts:
    14
    Hi, I'm new to shaders and have got a small problem I cant solve.

    To explain better, my vertices are created in an Vector3[] array called vertices, it is then passed to a mesh with mesh.vertices = vertices;

    I also have a StructuredBuffer<float> _Strengths; I have created with a ComputeShader, is the same size as the vertices array and has data I want to extract and use for each vertex position in the vertices array.

    What I need is the index of the current vertex in the fragment stage of my shader so I can use it to access the data in my StructuredBuffer.

    i.e. float strength = _Strengths[index];

    the index which I cant find being the same index value the shader uses to grab the vertex data from the vertices array as in vertices[index].
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    You can’t get the current vertex index in the fragment shader stage because in the fragment there isn’t any information about individual vertices. Also, more importantly, the fragment shader is always getting data from 3 vertices (when using triangle topology, which is the default).

    Now, if you mean you want the vertex ID in the vertex shader stage, then that’s relatively straightforward.
    Code (csharp):
    1. v2f vertex(appdata v, uint vertexID : SV_VertexID)
    If you want access to that data in the fragment, then you’ll need to pass that data along in your v2f struct. Just know by default it’ll be an interpolated value between the three vertices of the triangle the fragment is currently rendering.
     
  3. PiezPiedPy

    PiezPiedPy

    Joined:
    Mar 22, 2019
    Posts:
    14
    Cheers, that seems to have done the trick ;)
     
  4. RavenTravelStudios

    RavenTravelStudios

    Joined:
    Oct 15, 2015
    Posts:
    100
    Hello, sorry for bumping this but I'm stuck with a similar issue.

    I would retrieve the vertexID while using the VaryingDefault struct and VertDefault vertex function. I dunno if that's really possible but I'm stuck with a shader that isn't working on console bcs of this function:

    Code (Boo):
    1.      
    2.         // Vertex shader that procedurally outputs a full screen triangle
    3.         Varyings Vertex(uint vertexID : SV_VertexID)
    4.         {
    5.             // Render settings
    6.             float far = _ProjectionParams.z;
    7.             float2 orthoSize = unity_OrthoParams.xy;
    8.             float isOrtho = unity_OrthoParams.w; // 0: perspective, 1: orthographic
    9.  
    10.             // Vertex ID -> clip space vertex position
    11.             float x = (vertexID != 1) ? -1 : 3;
    12.             float y = (vertexID == 2) ? -3 : 1;
    13.             float3 vpos = float3(x, y, 1.0);
    14.  
    15.             // Perspective: view space vertex position of the far plane
    16.             float3 rayPers = mul(unity_CameraInvProjection, vpos.xyzz * far).xyz;
    17.  
    18.             // Orthographic: view space vertex position
    19.             float3 rayOrtho = float3(orthoSize * vpos.xy, 0);
    20.  
    21.             Varyings o;
    22.             o.position = float4(vpos.x, -vpos.y, 1, 1);
    23.             o.texcoord = (vpos.xy + 1) / 4.0;
    24.             o.ray = lerp(rayPers, rayOrtho, isOrtho);
    25.             return o;
    26.         }
    27.  
    So this is using a VertexID to calculate some spawn points from the mesh vertices.
    What I need is a way to get the same spawn algorhytm for x and y but using a VertDefault vertex function. Since that function isn't expecting a uint vertexID, the subsequent space clip space can't work - i tried to swap x and y by passing the texcoord, it works until the camera stays on place, but as it starts to move the shader pattern scrolls inside the object due to bad clipping.

    Could you help me somehow? Thanks! :)
     
    Last edited: Apr 14, 2022