[XNA] Aligning texels to pixels doesn't work for small textures

Started by
11 comments, last by Spa8nky 12 years, 2 months ago
Ok I with you now. How can I determine which UV I am pinching without using conditional statements in HLSL?


u(0) += htw, v(0) += hth - u(1) -= htw, v(1) -= hth
[/quote]

Am I right in assuming that the pinching method will only work when passing in the UV coordinates as 4 corners of a quadrangle?

How would pinching work if the uv coordinates are not four corners of a quadrangle?
Advertisement

Ok I with you now. How can I determine which UV I am pinching without using conditional statements in HLSL?


You should pinch the data before it gets to the shader. The point where you fill in the UV data in the buffer.



Am I right in assuming that the pinching method will only work when passing in the UV coordinates as 4 corners of a quadrangle?

How would pinching work if the uv coordinates are not four corners of a quadrangle?


No idea, I've only tested with quadrangle (using two triangles), as the artefacts you are seeing are normally associated (or are more noticeable) with 2D UI elements. Chances are if you're using triangles as part of 3D meshes rather than sprite type primitives then it may not be noticeable.

However, the quadrangle method is because, on the top left side += relates to the normal texel mapping to primitive (.5 of a texel), the -= however is because we associate 1.0f as a bottom/right edge, but we know 1.0 = 0.0 :) so the right edge is really 1.0f - texel width + half texel width).
So I've been trying to implement your suggestion but it doesn't solve the problem:



v = quadrangles.Vertices;

halfTexel.X = 0.5f / quadrangles.Width;
halfTexel.Y = 0.5f / quadrangles.Height;

// [0] Lower Left [1] -- [3] (0,0)--(1,0)
// [1] Upper Left | | | |
// [2] Lower Right | | | |
// [3] Upper Right [0] -- [2] (0,1)--(1,1)

v[0].TexCoordDimension.X = halfTexel.X; // Texture Coordinates stored in X,Y Quadrangle Dimensions stored in Z,W
v[0].TexCoordDimension.Y = 1f - halfTexel.Y;
v[1].TexCoordDimension.X = halfTexel.X;
v[1].TexCoordDimension.Y = halfTexel.Y;
v[2].TexCoordDimension.X = 1f - halfTexel.X;
v[2].TexCoordDimension.Y = 1f - halfTexel.Y;
v[3].TexCoordDimension.X = 1f - halfTexel.X;
v[3].TexCoordDimension.Y = halfTexel.Y;

// Copy the vertex data to the drawer's large vertices array
v.CopyTo(vertices, i * 4);


or



v[0].TexCoordDimension.X += halfTexel.X;
v[0].TexCoordDimension.Y -= halfTexel.Y;

v[1].TexCoordDimension.X += halfTexel.X;
v[1].TexCoordDimension.Y += halfTexel.Y;

v[2].TexCoordDimension.X -= halfTexel.X;
v[2].TexCoordDimension.Y -= halfTexel.Y;

v[3].TexCoordDimension.X -= halfTexel.X;
v[3].TexCoordDimension.Y += halfTexel.Y;


How do I calculate one texel?

I have tried:



halfTexel.X = 0.5f / game.GraphicsDevice.PresentationParameters.BackBufferWidth;
halfTexel.Y = 0.5f / game.GraphicsDevice.PresentationParameters.BackBufferHeight;


and



halfTexel.X = 0.5f / game.TextureAtlas.Texture.Width;
halfTexel.Y = 0.5f / game.TextureAtlas.Texture.Height;


None of the above produces the correct result sad.png

This topic is closed to new replies.

Advertisement