- Home /
Question by
Rustam-Ganeyev · Mar 05, 2013 at 06:40 AM ·
meshmaterialsuvsubmesh
Change uvs on texture packing
Hi. I'm doing some optimization stuff and trying to pack all materials to one material. Here's my problem: I have mesh with 5 submeshes(5 materials with different textures). I can pack all textures to one atlas and use just that texture. Problem is I don't know how to recalculate new uvs to my new atlas. Here's the code:
private void CreateMeshTexture ()
{
//get all textures
var rend = renderer;
var atlasMaterial = new Material (Shader.Find ("Mobile/Diffuse"));
var meshFilter = GetComponent<MeshFilter> ();
var textures = new List<Texture2D> ();
foreach (var mat in rend.materials) {
textures.Add ((Texture2D)mat.mainTexture);
}
//create texture atlas
textureAtlas = new Texture2D (maxSquareTextureSize, maxSquareTextureSize);
Rect[] coords = textureAtlas.PackTextures (textures.ToArray (), 0, maxSquareTextureSize);
atlasMaterial.mainTexture = textureAtlas;
//update materials
rend.material = atlasMaterial;
for (int i = 0; i < rend.materials.Length; ++i) {
rend.materials[i] = atlasMaterial;
}
//correct uvs
Vector2[] oldUV, newUV;
oldUV = meshFilter.mesh.uv;
newUV = new Vector2[oldUV.Length];
//how to set new uvs?
}
Does anybody know how to recalculate uvs? Is that possible?
Comment
Your answer
Follow this Question
Related Questions
uv sub meshes in cube c# 0 Answers
Submeshs doesn't combine with materials 1 Answer
Updating UV in imported mesh without having to re-material 0 Answers
How do I properly apply a transparent texture to a mesh? 2 Answers
Materials on an imported mesh 1 Answer