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;
