Aliased edges with NEAREST sampling

Sorry for yet another question, it’s a bit of a general one this time. I saw a similar post made over at HTML5 Game Dev’s but the issue wasn’t really resolved.

I have a custom material that is using NEAREST sampling on a texture, all well and good. The thing is… when faces are rendered, if you zoom in on their edges you can notice these weird aliased seams (sometimes interspersed pixel dots, depending on viewing angle) . A bit of texture bleed (probably not the right word for NEAREST) into neighboring pixels. This was a problem when the neighboring pixels contrasted with what was being rendered (e.g. I used to have a red tile next to the grass tile, so red specks would often appear at the edges from certain angles).

I thought I could overcome this issue by having a 1-pixel ‘pad’ around each tile, with contents being a repeat of the tile edges. This only served to make the issue less pronounced. No longer are there red seams, there are instead seams of a similar color (but not equal) to the tile being rendered. Shown below.
why

My question is, why is this happening? It looks like there’s some linear interpolation between colors going on at the boundaries, when I should be seeing flat colors like with the rest of the texture. Is there some rendering option that’s causing this, an attempted anti-aliasing at the edges or something? I’m not using any post-processing. Also not sure if relevant, but none of these rectangular face-regions share vertices with their neighboring rectangles (they often can’t, greedy meshed).

My guess is it’s because of the UV. UV are float values that are never the exact value you want. Floats are an approximation. You can’t get 0.25 with a float. You’ll either 0.24999 or 0.25001. Usually, this is fixed by adding a border (like what you did). Or if you generate your UV, add a small epsylon so the texture tap will occur inside the texel you want.
Be aware of issues like halftexel that are related to the API you use, IIRC. If you use a Direct3D UV mapper tool and then display your mesh using OpenGL, you may see small differences in the UVs. Halftexel on a 2Kx2K map is barely visible. If it’s 64x64 with nearest, it might be more visible.
http://drilian.com/2008/11/25/understanding-half-pixel-and-half-texel-offsets/

And there is also clamping texcoord

3 Likes

I think you’re right, definitely a UV issue. I’ll try the clamping function and get back :slight_smile: