纹理UV坐标不在Unity3D中排列

时间:2016-02-28 22:52:26

标签: unity3d textures uv-mapping

我正在Unity3D中以编程方式创建网格。网格的每个部分都设置有4个顶点:

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

我的紫外线是:

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

我的纹理是2048x2048。磁贴位于图像的左上角,大小为256x256。我用一些额外的瓷砖更新了纹理,以便更好地了解我正在尝试做什么。

Tilemap

我在网格上的每个图块周围都会出现白边。

Screenshot

我正在使用以下设置导入图像。

Import settings

生成网格的代码(手动法线):

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 ();
}

生成网格的代码(自动法线):

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 ();
}

这两个版本的代码都会产生同样的问题。

3 个答案:

答案 0 :(得分:1)

老兄你的常态好吗?

他们应该直接指向这个职业吧?

如果你正在使用一些默认的正常计算,那么他们是不是试图轻轻地包裹边缘,认为它是一个立方体?

答案 1 :(得分:0)

尝试将格式设置为Truecolor。可能是由于压缩,不同纹理之间的边界不再尖锐,而是模糊,导致它们相互渗透。

答案 2 :(得分:0)

我有类似的问题 没有过滤器(点)不是真的没有过滤器 在没有过滤器的后面有一个最近的邻居内置过滤器

为解决您的问题,您必须将纹理分成4个分割的纹理 通过这种方式 创建4个新的texture2D() 通过获取像素方法将原始纹理的所有像素存储到2D数组中 https://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html 并创建4个新纹理并使用 使用4 for循环获取四分之一的原始纹理像素并将像素设置为目标纹理之一 https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html