# how does a pixel shader calculate tex coordinates

## Recommended Posts

How does a pixel shader calculate the interpolated texture coordinates? You assign texture coordinates to the vertices, so I assume the pixel shader has to calculate an interpolated value; how does the pixel shader know which vertex(s) it is attached to? I ask because I'm programming a pixel shader that needs to know which vertex it is most associated with. Such that I could know in the pixel shader that I am on vertex 5, or 27, or whatever. Or even all 3 vertices of the face the pixel shader is being used on, and then of course the weighting.

##### Share on other sites
Through rasterization; the pixel doesn't really "know" which vertex it's attached to. Instead, the rasterizer takes a triangle at a time, scans it, then fills it in. As it's filling it in, it's interpolating all the vertex data to that pixel, then passing it to the pixel shader.

To handle your case, you'd need to somehow store that information in the vertex itself, then pass it down to the pixel shader. The problem there is still interpolation, the vertex value from each vertex will be interpolated into that pixel, so you wouldn't end up with the right values. Offhand, I'm really not sure if you CAN do what you want to do (without some really complex chickanery).

##### Share on other sites
If you can store and interpolate an extra RGB color per vertex, you could set the color of one of the vertices of a triangle to red, another to green and the third to blue.. Then check which channel has the highest value in the interpolated color. This will be the vertex you're closest to. You could perhaps then use this to look up a "real" vertex index.

Not sure if this is at all practical though. Setting it up could be difficult and you couldn't share vertices between triangles.

##### Share on other sites
Quote:
 Original post by FunkymunkyHow does a pixel shader calculate the interpolated texture coordinates? You assign texture coordinates to the vertices, so I assume the pixel shader has to calculate an interpolated value; how does the pixel shader know which vertex(s) it is attached to?

As said before, but simply put : you can't, the rasterizer interpolates and feeds these values to the pixel shader, where you can take matters into your own hands. However, Fingers_ tip sounds like a good solution for triangle primitives.

##### Share on other sites
Ah! Or how about this. If I make sure the mesh I'm using is uv mapped, and I also assure that each face is uniquely mapped, then I can make a new texture map of indexes to use in the pixel shader. The map will basically set each pixel as an index to the nearest vertex based on proximity to the texture coordinates of the regular uv map. Then I can use those regular texture coordinates on the map of indexes to find the nearest vertex to the fragment, even after interpolation.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628356
• Total Posts
2982253

• 10
• 9
• 13
• 24
• 11