Texture UV coords not lining up as expected in Unity3D

unity uv editor
unity uv mapping
unity texture not showing on mesh

I'm creating a mesh programmatically in Unity3D. Each section of the mesh is setup with 4 verts:

// 1 ----- 2 
// | a  /  |
// |  /  b |
// 3 ----- 4

My UVs are:

1 = 0, 1
2 = 0.125, 1
3 = 0, 0.875
4 = 0.125, 0.875

My texture is 2048x2048. The tile is in the top left of the image and is 256x256 in size. I've updated the texture a bit with some additional tiles to better give an idea of what I'm trying to do.

Im getting white edges around each tile on the mesh.

I'm importing the image with the following settings.

The code the generate the mesh (manual normals):

IEnumerator BuildMesh (int width, int height, float tileSize) {

    float halfSize = tileSize / 2f;

    List<Vector3> vertices = new List<Vector3> ();
    List<Vector3> normals = new List<Vector3> ();
    List<Vector2> uv = new List<Vector2> ();
    List<int> triangles = new List<int> ();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int tileVertIdx = vertices.Count;

            float h = map.GetHeight(startX + x, startY + y);
            float n = map.GetHeight(startX + x, startY + y - 1);
            float s = map.GetHeight(startX + x, startY + y + 1);
            float e = map.GetHeight(startX + x + 1, startY + y);
            float w = map.GetHeight(startX + x - 1, startY + y);
            float ne = map.GetHeight(startX + x + 1, startY + y - 1);
            float nw = map.GetHeight(startX + x - 1, startY + y - 1);
            float se = map.GetHeight(startX + x + 1, startY + y + 1);
            float sw = map.GetHeight(startX + x - 1, startY + y + 1);

            Vector3 tileCenter = new Vector3 (x * tileSize, h, y * tileSize);
            Rect topUV = map.GetTopUV ((int)h);

            if (n == h && s == h && e == h && w == h && ne == h && nw == h && se == h && sw == h) {
                // Each tile
                // 1 ----- 2 
                // | a  /  |
                // |  /  b |
                // 3 ----- 4

                // top row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z - halfSize));

                // bottom row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z + halfSize));

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                uv.Add (new Vector2(topUV.x, topUV.y));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y));

                uv.Add (new Vector2(topUV.x, topUV.y - topUV.height));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y - topUV.height));

                // a
                triangles.Add (tileVertIdx + 0);
                triangles.Add (tileVertIdx + 2);
                triangles.Add (tileVertIdx + 1);

                // b
                triangles.Add (tileVertIdx + 1);
                triangles.Add (tileVertIdx + 2);
                triangles.Add (tileVertIdx + 3);
            } else {
                // Each tile
                // 1 - 2 - 3 14 --- 16
                // |a\b|c/d\ |  k / |
                // 4 - 5 - 6 |   /  |
                // |e/f|g\h| |  / l |
                // 7 - 8 - 9 15 --- 17
                // 10 --- 11
                // | i / j |
                // 12 --- 13

                // top row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x, tileCenter.y, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z - halfSize));

                // middle row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z));
                vertices.Add (new Vector3 (tileCenter.x, tileCenter.y, tileCenter.z));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z));

                // bottom row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x, tileCenter.y, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z + halfSize));

                // bottom square
                vertices.Add (new Vector3 (tileCenter.x - halfSize, h, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, h, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x - halfSize, s, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, s, tileCenter.z + halfSize));

                // right square
                vertices.Add (new Vector3 (tileCenter.x + halfSize, h, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, h, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, e, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, e, tileCenter.z - halfSize));

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);
                normals.Add (Vector3.up);

                uv.Add (new Vector2(topUV.x, topUV.y));
                uv.Add (new Vector2(topUV.x - (topUV.width / 2), topUV.y));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y));

                uv.Add (new Vector2(topUV.x, topUV.y - (topUV.height / 2)));
                uv.Add (new Vector2(topUV.x - (topUV.width / 2), topUV.y - (topUV.height / 2)));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y - (topUV.height / 2)));

                uv.Add (new Vector2(topUV.x, topUV.y - topUV.height));
                uv.Add (new Vector2(topUV.x - (topUV.width / 2), topUV.y - topUV.height));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y - topUV.height));

                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);

                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);

                // a
                triangles.Add (tileVertIdx + 0);
                triangles.Add (tileVertIdx + 3);
                triangles.Add (tileVertIdx + 4);

                // b
                triangles.Add (tileVertIdx + 0);
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 1);

                // c
                triangles.Add (tileVertIdx + 1);
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 2);

                // d
                triangles.Add (tileVertIdx + 2);
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 5);

                // e
                triangles.Add (tileVertIdx + 3);
                triangles.Add (tileVertIdx + 6);
                triangles.Add (tileVertIdx + 4);

                // f
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 6);
                triangles.Add (tileVertIdx + 7);

                // g
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 7);
                triangles.Add (tileVertIdx + 8);

                // h
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 8);
                triangles.Add (tileVertIdx + 5);

                // i
                triangles.Add (tileVertIdx + 9);
                triangles.Add (tileVertIdx + 11);
                triangles.Add (tileVertIdx + 10);

                // j
                triangles.Add (tileVertIdx + 10);
                triangles.Add (tileVertIdx + 11);
                triangles.Add (tileVertIdx + 12);

                // k
                triangles.Add (tileVertIdx + 13);
                triangles.Add (tileVertIdx + 15);
                triangles.Add (tileVertIdx + 14);

                // l
                triangles.Add (tileVertIdx + 15);
                triangles.Add (tileVertIdx + 16);
                triangles.Add (tileVertIdx + 14);
            }
        }
        yield return null;
    }

    Mesh mesh = _meshFilter.mesh;
    mesh.vertices = vertices.ToArray ();
    mesh.triangles = triangles.ToArray ();
    mesh.normals = normals.ToArray ();
    mesh.uv = uv.ToArray ();
//  mesh.RecalculateNormals ();
//  mesh.Optimize ();
    map.ChunkFinished ();
}

The code to generate the mesh (auto normals):

IEnumerator BuildMesh (int width, int height, float tileSize) {

    float halfSize = tileSize / 2f;

    List<Vector3> vertices = new List<Vector3> ();
    List<Vector3> normals = new List<Vector3> ();
    List<Vector2> uv = new List<Vector2> ();
    List<int> triangles = new List<int> ();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int tileVertIdx = vertices.Count;

            float h = map.GetHeight(startX + x, startY + y);
            float n = map.GetHeight(startX + x, startY + y - 1);
            float s = map.GetHeight(startX + x, startY + y + 1);
            float e = map.GetHeight(startX + x + 1, startY + y);
            float w = map.GetHeight(startX + x - 1, startY + y);
            float ne = map.GetHeight(startX + x + 1, startY + y - 1);
            float nw = map.GetHeight(startX + x - 1, startY + y - 1);
            float se = map.GetHeight(startX + x + 1, startY + y + 1);
            float sw = map.GetHeight(startX + x - 1, startY + y + 1);

            Vector3 tileCenter = new Vector3 (x * tileSize, h, y * tileSize);
            Rect topUV = map.GetTopUV ((int)h);

            if (n == h && s == h && e == h && w == h && ne == h && nw == h && se == h && sw == h) {
                // Each tile
                // 1 ----- 2 
                // | a  /  |
                // |  /  b |
                // 3 ----- 4

                // top row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z - halfSize));

                // bottom row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z + halfSize));

                uv.Add (new Vector2(topUV.x, topUV.y));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y));

                uv.Add (new Vector2(topUV.x, topUV.y - topUV.height));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y - topUV.height));

                // a
                triangles.Add (tileVertIdx + 0);
                triangles.Add (tileVertIdx + 2);
                triangles.Add (tileVertIdx + 1);

                // b
                triangles.Add (tileVertIdx + 1);
                triangles.Add (tileVertIdx + 2);
                triangles.Add (tileVertIdx + 3);
            } else {
                // Each tile
                // 1 - 2 - 3 14 --- 16
                // |a\b|c/d\ |  k / |
                // 4 - 5 - 6 |   /  |
                // |e/f|g\h| |  / l |
                // 7 - 8 - 9 15 --- 17
                // 10 --- 11
                // | i / j |
                // 12 --- 13

                // top row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x, tileCenter.y, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z - halfSize));

                // middle row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z));
                vertices.Add (new Vector3 (tileCenter.x, tileCenter.y, tileCenter.z));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z));

                // bottom row
                vertices.Add (new Vector3 (tileCenter.x - halfSize, tileCenter.y, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x, tileCenter.y, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, tileCenter.y, tileCenter.z + halfSize));

                // bottom square
                vertices.Add (new Vector3 (tileCenter.x - halfSize, h, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, h, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x - halfSize, s, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, s, tileCenter.z + halfSize));

                // right square
                vertices.Add (new Vector3 (tileCenter.x + halfSize, h, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, h, tileCenter.z - halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, e, tileCenter.z + halfSize));
                vertices.Add (new Vector3 (tileCenter.x + halfSize, e, tileCenter.z - halfSize));

                uv.Add (new Vector2(topUV.x, topUV.y));
                uv.Add (new Vector2(topUV.x - (topUV.width / 2), topUV.y));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y));

                uv.Add (new Vector2(topUV.x, topUV.y - (topUV.height / 2)));
                uv.Add (new Vector2(topUV.x - (topUV.width / 2), topUV.y - (topUV.height / 2)));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y - (topUV.height / 2)));

                uv.Add (new Vector2(topUV.x, topUV.y - topUV.height));
                uv.Add (new Vector2(topUV.x - (topUV.width / 2), topUV.y - topUV.height));
                uv.Add (new Vector2(topUV.x - topUV.width, topUV.y - topUV.height));

                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);

                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);
                uv.Add (Vector2.up);

                // a
                triangles.Add (tileVertIdx + 0);
                triangles.Add (tileVertIdx + 3);
                triangles.Add (tileVertIdx + 4);

                // b
                triangles.Add (tileVertIdx + 0);
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 1);

                // c
                triangles.Add (tileVertIdx + 1);
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 2);

                // d
                triangles.Add (tileVertIdx + 2);
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 5);

                // e
                triangles.Add (tileVertIdx + 3);
                triangles.Add (tileVertIdx + 6);
                triangles.Add (tileVertIdx + 4);

                // f
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 6);
                triangles.Add (tileVertIdx + 7);

                // g
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 7);
                triangles.Add (tileVertIdx + 8);

                // h
                triangles.Add (tileVertIdx + 4);
                triangles.Add (tileVertIdx + 8);
                triangles.Add (tileVertIdx + 5);

                // i
                triangles.Add (tileVertIdx + 9);
                triangles.Add (tileVertIdx + 11);
                triangles.Add (tileVertIdx + 10);

                // j
                triangles.Add (tileVertIdx + 10);
                triangles.Add (tileVertIdx + 11);
                triangles.Add (tileVertIdx + 12);

                // k
                triangles.Add (tileVertIdx + 13);
                triangles.Add (tileVertIdx + 15);
                triangles.Add (tileVertIdx + 14);

                // l
                triangles.Add (tileVertIdx + 15);
                triangles.Add (tileVertIdx + 16);
                triangles.Add (tileVertIdx + 14);
            }
        }
        yield return null;
    }

    Mesh mesh = _meshFilter.mesh;
    mesh.vertices = vertices.ToArray ();
    mesh.triangles = triangles.ToArray ();
    mesh.uv = uv.ToArray ();
    mesh.RecalculateNormals ();
    mesh.Optimize ();

    map.ChunkFinished ();
}

Both versions of the code produce the same issue.

Dude are your normals ok?

They should all be pointing straight up for this jobbie right?

If you're using some default normal calculation, could it be they're trying to gently wrap the edge thinking it's a cube?

UV coordinates aren't imported/are screwy, saved it as an .OBJ, and imported it into Unity(along with the texture). However, the UV cords are all messed up. It should be work. This is� Note: Texture coordinates must be created or modified externally from the mesh. The script example above creates the uvs array, assigns texture coordinates to the uvs array, and then assigns the array to the Mesh.

Try setting the format to Truecolor. It might be that due to the compression the borders between the different textures are not sharp anymore but blurred, causing them to bleed into each other.

RaycastHit.textureCoord --- does the reverse exist?, textureCoord to collect the UV coordinate based on a point on the in-world Just searched for this old answer where i put an barycentric calculation routine up; triangle and use the barycentric coordinates to get the world position. default sphere / cube mesh and like expected on the sphere i get one� You can scale up NPOT Texture Assets at import time using the Non Power of 2 option in the Advanced section of the Texture Importer. UV mapping. When mapping a 2D Texture onto a 3D model, your 3D modelling application does a type of wrapping called UV mapping. Inside Unity, you can scale and move the Texture using Materials. Scaling normal and

i have the similar problem no filter (point) is not realy no filter there is a nearest neighbour unity builtin filter behind no filter

for resolve your issue you must split your texture into 4 splited textures by this way create 4 new texture2D() store all pixels of orginal texture into an 2D array by get pixel method https://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html and create 4 new texture and use use 4 for loop to get quarter of orginal texture pixels and set the pixels to one of destination textures https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html

Texture not mapped to UV CG shader, Texture not mapped to UV CG shader I just want it to map once across the uv space but it seems to be So, firstly try changing that last line to: the correct types, or else you're likely to run into further problems in the future. sampler lookup with uv coordinates passed straight through from the vertex� Watch this video in context on Unity's learning pages here - http://unity3d.com/learn/tutorials/modules/beginner/graphics/textures What are Textures? How doe

Material not showing on some objects, So I just downloaded a seamless texture, slapped it on the "Albedo" of a If the model does not have a propper uv map unity can not display the texture the the uv coordinates in some 3D modelling software, such as Blender, into Blender and import the UV texture (which is likely a .png) once in Unity. The Texture Importer opens in the Inspector window. Note that some of the less commonly used properties are hidden by default. Click Advanced in the Inspector window to view these. The first property in the Texture Importer is the Texture Type. Use this to select the type of Texture you want to create from the source image file.

Creating cube in script/ texture stretches on all sides but top and , The texture appears correctly on Unity's default cube, but I create the cubes in a way that not all faces are drawn. I believe the problem may be incorrect UV mapping but I'm finding it quite difficult to understand how that works. Either way you'll likely need to split it up as I said because when you add UVs� TEXCOORD0 is the first UV coordinate, typically float2, float3 or float4. TEXCOORD1, TEXCOORD2 and TEXCOORD3 are the 2nd, 3rd and 4th UV coordinates, respectively. TANGENT is the tangent vector (used for normal mapping), typically a float4. COLOR is the per-vertex color, typically a float4.

How to get texture UV tiling and offset in a Surface Shader?, You can declare UV coordinates as input to the Surface Shader, but that only seems to include UV, not tiling and offset. I looked at the complied� Im not sure whats going on with the textures. Ive been having this issue all day. Any object that I export from blender causes the textures added to become smudged and or don't even line up. EDIT: I forgot to add that the textures work fine on terrain. Any object causes these issues. Textures not lining up: Smudged:

Comments
  • what's up with the .125 business? wouldn't it just be 1,1 etc .. all 1/0??
  • @JoeBlow - Its not the full image size. I only have 1 tile on there at the moment but there will be a lot more, grass, dirt, water, etc. I'm just trying to get the texture to line up properly at the moment.
  • ok - just a suggestion: just make a normal quad in unity (ie use menu). put your texture on it. fool with the values until they are as you wish. Absolutely check it looks correct ON THE UNITY SUPPLIED MESH!
  • I'm trying to make a tiled map, on a single mesh. The mesh is not just a plane so I can't use the supplied mesh. If it helps I'll post my code.
  • did you google these lame ideas ? forum.unity3d.com/threads/…
  • I've tried both ways, everything pointing up and using the build in RecalculateNormals
  • oh don't use Recalc. normals. you're 1000% certain your normals were correct??
  • are you totally sure it is not working over you and doing RecalculateStupidNormals on its own? there's some point where it does that for you "helpfully" automatically. drives people nuts
  • I'm 99% certain it's your normals. To test: just zoom in really close and see if Unity is trying to make it a nice smooth edge. Print out with debug or something the actual final normals (don't trust that you set them as you wish)