Texture Mapping

So I set myself the task of making a minecraft clone in unity to try and practice, but I got stuck on making different textures for each side of a cube. I’m trying to use an atlas map to project 3 different 16x16 textures onto my cube - one for the 4 sides and 2 for the top and bottom. The problem is that the west, east and bottom textures are stretched for some reason. all textures are 16x16

west, east and bottom texture in full (they are all the same)

relevant portion of code:

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

        // Create a new texture atlas
        Texture2D textureAtlas = new Texture2D(48, 16, TextureFormat.RGB24, false);
        textureAtlas.filterMode = FilterMode.Point;
        textureAtlas.wrapMode = TextureWrapMode.Clamp;

        Texture2D[] textures = { side, top, bottom };
        Rect[] atlasUVs = textureAtlas.PackTextures(textures, 0);

        model = GameObject.CreatePrimitive(PrimitiveType.Cube);

        Material material = new Material(Shader.Find("Standard"));
        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
        UVs[0] = new Vector2(0.0f, 0.0f);
        UVs[2] = new Vector2(0.0f, 0.5f);
        UVs[1] = new Vector2(0.5f, 0.0f);
        UVs[3] = new Vector2(0.5f, 0.5f);

        // Top
        UVs[4] = new Vector2(0.5f, 0f);
        UVs[5] = new Vector2(0.5f, 0.5f);
        UVs[8] = new Vector2(1.0f, 0f);
        UVs[9] = new Vector2(1.0f, 0.5f);

        // north
        UVs[6] = UVs[0];
        UVs[7] = UVs[1];
        UVs[10] = UVs[2];
        UVs[11] = UVs[3];

        //bottom
        UVs[12] = new Vector2(0.0f, 0.0f);
        UVs[13] = new Vector2(0.0f, 1.0f); 
        UVs[14] = new Vector2(1.0f, 0.0f); 
        UVs[15] = new Vector2(1.0f, 1.0f);

        // West
        UVs[16] = new Vector2(0.0f, 0.0f);
        UVs[17] = new Vector2(0.0f, 1.0f);
        UVs[18] = new Vector2(1.0f, 0.0f);
        UVs[19] = new Vector2(1.0f, 1.0f);

        // East
        UVs[20] = UVs[16];
        UVs[21] = UVs[17];
        UVs[22] = UVs[18];
        UVs[23] = UVs[19];

        mesh.uv = UVs;

The UVs are wrong. 0.5f means a face is mapped to half of the texture when it should be a third of the texture. And the bottom, west and east faces are mapped to the full texture. I think it’ll be easier to use an image editor to create your texture atlas and use Blender to create a correctly mapped cube.

I mapped the bottom, west and east textures to that because I couldn’t find a mapping that worked. Also for some reason, unity interpreted my 16x48 texture as a 32x32 cube, where the first quarter was side, second was top, and third was bottom. The fourth quarter was just black , as there was no fourth texture

I’ve never used the PackTextures method but it is possible that it only creates square textures. If this is the case then your 0.5f makes sense. But if you don’t want to use a modeling app to create your own UV mapped cube then you’re just going to have to fumble around until you’ve figured out which UVs belong to which triangle. Or perhaps program the whole mesh yourself.

I can’t understand why just for those 3 sides of the cube the whole texture is stretched though.