3
\$\begingroup\$

The game I am developing is in unity and I want to make a shader which can be put on a mesh that only 'draws' color on each vertex point. I am not proficient with shaders, and out of all my searching I have come up empty handed; I unfortunately don't even know how to look up this question (I seem to only find results about point clouds, which only serves to confuse and scare me more ;P).

enter image description here

Any help or if you can point me in the right direction on what to search, I would be very much greatful.

\$\endgroup\$
  • \$\begingroup\$ draw with GL_POINTS instead of GL_TRIANGLES? \$\endgroup\$ – ratchet freak Jan 29 '15 at 8:32
1
\$\begingroup\$

If you're writing shaders, you should know a bit about the world, view, and projection matrices. I'm assuming you do, so if you don't, either read up on it or let me know. For every vertex, you can determine its position on the screen by creating a float4, with the first 3 parameters being the vertex position, and the last being 1. When you have this float4, multiply it by the world, view and projection matrices. You will now end up with another float4. Divide this one by its own w value, and you'll get a float4 with the following:

The x value will be between -1 (left edge of the screen) and 1 (right edge of the screen), the y value will be between -1 (top edge of the screen) and 1 (bottom edge of the screen), and the z value will be between -1 (camera's far plane) and 1 (camera's near plane). If any value is out of those bounds, the vertex is not on screen.

With this float4, and knowing the game's resolution, you can determine the exact pixel location of the vertex (I'm going to leave that up to you because it's quite a simple division of the resolution and the x and y coordinates, and I believe you can do that.) When you have the x,y coordinate of the vertex on screen, you can just draw a circle at that point (use a sprite and you can draw whatever you want there, little stars, pacmen, anything)

\$\endgroup\$
  • \$\begingroup\$ What I'm missing here is how to draw the sprite. How is this done, in general, in a shader? I've only ever been able to reproduce this by generating quads at the points. \$\endgroup\$ – mklingen Mar 31 '15 at 2:44
  • \$\begingroup\$ What you're looking for is a point sprite. mmmovania.blogspot.nl/2010/12/… shows a little on how to do it in openGl. msdn.microsoft.com/en-us/library/windows/desktop/… shows how to do it in directX. Basically, you let your vertex shader output it's screen position to your pixel shader. Your pixel shader will look at every pixel and can determine its distance from your vertex' on-screen coordinates. If it's close, render it a color. This should give you dots. \$\endgroup\$ – Peethor Mar 31 '15 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.