Search Issue Tracker

Not Reproducible

Votes

0

Found in

2021.3.41f1

2022.3.39f1

6000.0.12f1

Issue ID

UUM-76667

Regression

No

The textures get stretched when using an Atlas to UV map the 2D textures to the sides of a Cube

--

-

How to reproduce:
1. Open the “ReproBlockProject“ project
2. Open the “SampleScene“
3. Enter the Play Mode
4. Observe the west and bottom side of the “Cube(Clone)“ GameObjects in the Scene view

Expected result: The texture of the west and the bottom side of the “Cube(Clone)“ GameObject is not stretched
Actual result: The texture of the west and the bottom side of the “Cube(Clone)“ GameObject is stretched

Reproducible with: 2021.3.41f1, 2022.3.39f1, 6000.0.12f1

Reproducible on: macOS 14.5 (Intel), Windows 10 Pro (22H2)
Not reproducible on: No other environments tested

  1. Resolution Note:

    Hi, the issue reported is not a bug with the engine but the provided script is not mapping the UV's correctly.

    Changing the provided function as follows does resolve the problem:

    public static GameObject multiTextures(string sideID, string topID, string bottomID)
    {
    GameObject model = GameObject.CreatePrimitive(PrimitiveType.Cube);
    Material material = new Material(Shader.Find("Standard"));

    Texture2D side = Resources.Load<Texture2D>("Texture/Block/" + sideID);
    Texture2D top = Resources.Load<Texture2D>("Texture/Block/" + topID);
    Texture2D bottom = Resources.Load<Texture2D>("Texture/Block/" + bottomID);

    Texture2D textureAtlas = new Texture2D(32, 32, TextureFormat.RGB24, false);
    textureAtlas.filterMode = FilterMode.Point;
    textureAtlas.wrapMode = TextureWrapMode.Clamp;

    Texture2D[] textures = { side, top, bottom };
    Rect[] atlasUVs = textureAtlas.PackTextures(textures, 0);
    byte[] bytes = textureAtlas.EncodeToPNG();
    System.IO.File.WriteAllBytes(Application.dataPath + "/Resources/Texture/BlockAtlas.png", bytes);

    material.mainTexture = textureAtlas;
    material.SetFloat("_Metallic", 0);
    material.SetFloat("_Glossiness", 0);

    MeshRenderer meshRenderer = model.GetComponent<MeshRenderer>();
    meshRenderer.material = material;
    Mesh mesh = model.GetComponent<MeshFilter>().mesh;
    Vector2[] UVs = new Vector2[mesh.vertices.Length];

    // South (front)
    UVs[0] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMin);
    UVs[1] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMin);
    UVs[2] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMax);
    UVs[3] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMax);

    // Top
    UVs[4] = new Vector2(atlasUVs[1].xMin, atlasUVs[1].yMin);
    UVs[5] = new Vector2(atlasUVs[1].xMin, atlasUVs[1].yMax);
    UVs[8] = new Vector2(atlasUVs[1].xMax, atlasUVs[1].yMin);
    UVs[9] = new Vector2(atlasUVs[1].xMax, atlasUVs[1].yMax);

    // North (back)
    UVs[6] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMin);
    UVs[7] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMin);
    UVs[10] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMax);
    UVs[11] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMax);

    // Bottom
    UVs[15] = new Vector2(atlasUVs[2].xMin, atlasUVs[2].yMin);
    UVs[14] = new Vector2(atlasUVs[2].xMin, atlasUVs[2].yMax);
    UVs[12] = new Vector2(atlasUVs[2].xMax, atlasUVs[2].yMin);
    UVs[13] = new Vector2(atlasUVs[2].xMax, atlasUVs[2].yMax);

    // West (left)
    UVs[19] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMin);
    UVs[18] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMax);
    UVs[16] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMin);
    UVs[17] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMax);

    // East (right)
    UVs[23] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMin);
    UVs[22] = new Vector2(atlasUVs[0].xMin, atlasUVs[0].yMax);
    UVs[20] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMin);
    UVs[21] = new Vector2(atlasUVs[0].xMax, atlasUVs[0].yMax);

    mesh.uv = UVs;
    return model;
    }

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.