- Home /
How to get precise pixel values form a Texture2D using uv coordinates.
I'm having a piece of geometry where several polygons share the same uv coordinate. Each of vertices of the polygons have coordinates that are mapped to a single pixel in the Texture2D (128x128).
This is the result: Note that the Texture2D i'm using only contains fully red or blue pixels. The left image is filterMode set to Point, and the right is filterMode set to Billinear.
It seems to start interpolating, which I would believe is a result of inpresice uv-coordinates, but I don't understand why, mipmap is disabled and the UVs map to the exact pixel coordinate. What is strange is that if I "manually" define a float2( u, v ) inside the shader, the precise mapping seems to work.
Any ideas what causing this?
From your two screenshots it's not clear how your texture actually looks like and how your geometry of your sphere looks like. Am i right that the hexagons are actual geometry? You can't really map to exact "pixel coordinates" since uv coordinates are not bound to any kind of grid. Pixels / texels have a size. It makes a difference if you talk about the lower corner, the center or the upper corner of a texel. Pointfiltering usually corresponds to GL_Nearest.
Can you add your actual used texture and add more details on how you mapped it to the geometry? Is the sphere procedurally generated or in a modelling appllication?
the hexagons consist of groups of 4 triangles, all sharing the same UV coordinate. They do not share vertexes with neighbors hexagons. For the red vertex group (see illustration), each vertex would have the coordinate of { 3 / 128, 0 }, given that the image is 128 pixels wide.
The exact texture is arbitrary, its every second pixel red or blue, if the uv-coords where exact non of the hexagon would be purple.
The geometry is procedural modeled in an modelling application. this tho shouldn't matter.
If GL_Nearest worked the way I would have expected, at least vertices sharing the same uv coordinate would be rendered in the same color, ins$$anonymous$$d it seams to flicker between two values, depending on the camera angle.
Answer by smuseus · Dec 02, 2015 at 10:00 AM
Figured this out, I wasn't sampling the center of the pixel. The texel coordinate for a specific pixel at { x, y } would be :
u = x / width + 0.5 / width;
v = y / height + 0.5 / height;
Thanks Bunny83 for the reference.
Your answer
Follow this Question
Related Questions
Changing material in code doesn't update shader 3 Answers
Offset textures individually into specific world directions 2 Answers
Awesome Bump shader in Unty? 0 Answers
How to change a unique shaders property 1 Answer
Unity 1080p Video player 1 Answer