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

Is it possible to get back data for a specific vertex from a shader ?

Discussion in 'Shaders' started by nasos_333, Feb 4, 2015.

  1. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    I have come across this problem twice, in two major systems i am working on.

    I would like to find the color (or pixel color near the vertex) or height in world space of a specific vertex that a shader is applied to.

    Is it possible to have one public shader value (vector3) and when i am in the vertex of interest (also an issue of how to identify that vertex) assign a property directly from the vertex shader code to that public value and read it in script ?

    Thanks in advance for any help
     
  2. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    If you were unconstrained by the engine and could access the graphics library api directly, it should be possible. I don't know about OpenGL, but DirectX supports a thing called Stream-out stage, where the gpu stores the output of any vertex or geometry shader in a buffer that can be read from a script.

    If you don't mind the extra draw call though, you could do the same with compute shaders: Just pass it the vertices, transform them and output the desired information into a buffer.

    However, if you're interested in just a single vertex, why not compute that directly in a script? You have access to everything you need for that.
     
    nasos_333 likes this.
  3. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    Thanks for the info, very usefull.

    Will compute shader limit the usage to DX11 ?

    The DirectX way was what i was looking for, but as you suggested i am trying to do it on script now and i have got the up/down motion already working and now adjusting the normalization factors since my calcs use stuff that are not directly accessible in script, like the surface normals and texture uvs.
     
  4. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Unfortunately yes.. maybe you could fake it through the 3d pipeline instead by rendering each vertex as a point into a 1d texture as an array, but I think that would be more complex and error prone than what you'd have to do by calculating what you need directly in the script.
     
  5. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,842
    Indeed, that would be rather complex and harder to setup.

    Another way i am considering is by updating the mesh collider somehow, though this is not trivial either and i read that it is slow generally.