How to access Non-Interpolated Vertex Data in Fragment

Hi, I want to create a shader where each vertex has an index baked into color or uv that specifies which texture to use, then blend the triangle’s 3 vertices’ respective textures.

However, because all vertex data is interpolated, I can’t figure out a way of extracting whole numbers back out from the interpolated data.

For example,
vertex1: texture 3,
vertex2: texture 18,
vertex3: texture 43

I’ve considered packing this number into a different channel for each vertex, so
vertex1: (3,0,0),
vertex2: (0,18,0)
vertex3: (0,0,43)
but in the fragment shader they’re still interpolated and I can’t restore them as their original whole numbers.

In the unity docs, they show passing a Vertex ID into a custom interpolator,
but I figure that just gets interpolated too. And, it would only be useful if somebody knows a way of accessing vertex data in the fragment shader using a vertex ID.

Any ideas would be super helpful. If I could get something like this to work it would allow my smooth voxel program to support biomes, which would be very cool :). Unfortunately, I’ve been pretty stuck here…
I feel like there’s either gotta be an obvious way to do this, or a really clever one, lol.

Thanks!

Alrighty, I’m after digging through the depths of google I’m starting to think that maybe there’s not a straightforward way of doing this.

However, I figure if I start with vertex data represented by float3, and give each vertex in a triangle a different index,
vertex1: (1,0,0)
vertex2: (0,1,0)
vertex3: (0,0,1)
Then after interpolation I should have the weights or the barycentric coordinates.
Then if in another channel I have the index for each vertex
vertex1: (3,0,0)
vertex2: (0,18,0)
vertex3: (0,0,43)
So long as I have enough precision, in the fragment shader I divide the indices by the weights and round the outputs, I should have the 3 textures indices back again.

Example:
vertex shader input:
Barycentric Coords = v1: (1,0,0) v2: (0,1,0) v3: (0,0,1)
Texture Indexes = v1: (3,0,0) v2: (0,18,0) v3: (0,0,43)
Then a sample interpolated vertex shader output might look like
Barycentric Coords = (.22, .54, .24)
Texture Indices = (0.66, 9.72, 10.32)
To get the original texture indexes back
Texture Coordinates = round(Texture Indices / Barycentric Coords) = (3,18,43). So now I can sample textures 3, 18, and 43 and blend the results for each pixel.

It feels like I may be using excessive amounts of vertex data here, but I think it will work.

Please let me know if you have any ideas on how to improve this sort of approach, or let me know if I’m way off track here, lol!

Thanks!