0
\$\begingroup\$

In my voxel game, this is how I'm calculating UV coordinates for my cubes' faces:

public static Vector2[] faceUVs(Direction dir)
{

    Vector2[] sideUV = new Vector2[4]{
        new Vector2(0, 1),
        new Vector2(1f/3, 1),
        new Vector2(1f/3, 0),
        new Vector2(0, 0)
    };
    Vector2[] topUV = new Vector2[4]{
        new Vector2(1f/3, 0),
        new Vector2(1f/3, 1),
        new Vector2(2f/3, 1),
        new Vector2(2f/3, 0)
    };
    Vector2[] bottomUV = new Vector2[4]{
        new Vector2(2f/3, 0),
        new Vector2(2f/3, 1),
        new Vector2(1, 1),
        new Vector2(1, 0)
    };
    if ((int)dir == 4)
    {
        return topUV;
    }
    if ((int)dir == 5)
    {
        return bottomUV;
    }

    return sideUV;
}

Then depending on which face is being generated, I return appropriate UV coordinates for it.

This is the texture itself(pretty clean, each block is exactly 16 pixels wide and 16 pixels tall. Total length of picture is 48 pixels):

enter image description here

But the result is terrible(Faces are almost correctly aligned with appropriate texture part, but the annoying keyword here is "almost"):

enter image description here

As you can see, edges of side faces contain few pixels of what should have been the texture of top face.

I've set filter mode to point too.

If I instead use 16x16 pixel texture, for example only side, then I get good result:

enter image description here

I suspect the problem here could be floating point precision. In that case, how do i resolve this error?

\$\endgroup\$
3
  • \$\begingroup\$ It looks like your texture's import settings are scaling it to a power of 2 size since your input was a different size. Additionally, 1/3 isn't exactly representable as a decimal, let alone binary floating point. Have you tried making your image a power of two width and adding padding between the tiles, so the pixel edges you want to reference fall on representable floating point numbers? \$\endgroup\$
    – DMGregory
    Mar 4, 2020 at 12:14
  • \$\begingroup\$ @DMGregory Epic! Thanks. Changed image width to 64, didn't even need additional padding between tiles. Works seamlessly! \$\endgroup\$
    – Nick
    Mar 4, 2020 at 12:35
  • 1
    \$\begingroup\$ If this solved your problem, please post it as an Answer below. \$\endgroup\$
    – DMGregory
    Mar 4, 2020 at 12:37

1 Answer 1

1
\$\begingroup\$

Problem was that 1/3 can't be represented as finite and accurate decimal, so it caused miss-alignments on the face.

I fixed it by increasing width of whole texture to next closest power of 2(Both width and height need to be power of 2, but I already had 16 as height), which was 64 for me.

New texture:

enter image description here

Result:

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .