Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rendering point primitives with (vertex) shader

Discussion in 'Shaders' started by MarsAttax, Nov 22, 2014.

  1. MarsAttax

    MarsAttax

    Joined:
    Nov 22, 2014
    Posts:
    17
    Hey all!

    I've been scripting with Unity for a few years but just lately been studying shaders and GPU processing.

    I'm trying to render some points on GPU that are not based on objects vertices. What I want to do is to have arbitrary amount of global positions stored in an array of vectors and then make some deformations based on these positions. I'm able to store this data to a texture2D with SetPixel() etc. (I'm using Unity indie btw) and then pass the texture to shader with Material.SetTexture. After that I'm not really sure how to use texture lookups in the vertex shader (tex2Dlod) .

    Usually it's done like this:
    Code (CSharp):
    1. float4 tex = tex2Dlod (_MyPointPrimitiveTexture, float4(v.texcoord.xy,0,0));
    but I don't even want to render the original vertices so I guess I can't use those uv-coordinates.

    Like I said I'm a shader noob so if someone could point me to right direction I'd be very grateful.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    You can just calculate whatever texture coordinate you want to use, then use it. 0..1 is the normal range.
     
  3. MarsAttax

    MarsAttax

    Joined:
    Nov 22, 2014
    Posts:
    17
    Hi and thanks for the tip!

    Aren't texture coordinates usually part of the vertex data provided to the shader with Mesh.uv? Is there an alternative way of getting texture coordinates if I want just, let's say, coordinates for those 10 points I want to render? I could have thousands of vertices but only 10 points to render ( I know that sounds like a waste and maybe it is but I want to handle the rest of it in later shader passes ). The challenge here is with mapping each point to the correct coordinate.

    I'm not still quite getting it.. as far as I know the only way to pass an "array" of data to the shader is with vertex data or via texture2D, is that right?
     
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Generally yes you need a texture to contain the data.. .not sure about arrays, probably much smaller limit to how big an array can be and I dont know how you pass it.
     
  5. MarsAttax

    MarsAttax

    Joined:
    Nov 22, 2014
    Posts:
    17
    I've read some other threads and they say arrays are not possible. I would also need some kind of iterator to match the array index to the uv-array index. Isn't vertex shader kind of "foreach loop for vertices"? So following that idea maybe the solution would be to create another mesh procedurally so that it only consists of vertices for needed points, render them and then somehow fetch the result and use it with another mesh.
     
  6. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    You could look at the GL commands, they let you submit your own arrays of GL_POINTS to plot individual pixels. But that's doing much the same that Unity is doing with vertices.

    I think you will just need a texture that stores the coordinates of the points, and then every fragment in the fragment shader is going to have to read all of the pixels in that texture to find out if one of the points in it is at the fragments coordinates, and if so, output color. It's pretty intensive and not very efficient.
     
  7. MarsAttax

    MarsAttax

    Joined:
    Nov 22, 2014
    Posts:
    17
    Thank you for your suggestions! I will study them and if I find a decent solution I'll post it here.