In sense, i have some same meshes (two big one small)and have a one Point light .look at 1.png,
then i combined mesh and textures, both meshes be combined one mesh and one texture. look at 12.png and 2.png
sense look ok,then Lightmap sense finished,but sense get wrong render look at 3.png.
my code is
static void Merge (GameObject scene) {
int uvCount = 0;
Texture2D[] textures;
int uvOffset = 0;
int meshOffset = 0;
int tNum = 0;
MeshFilter[] MFs =scene.GetComponentsInChildren<MeshFilter>();
if (MFs == null) {
return;
}
foreach (MeshFilter mf in MFs)
{
tNum++;
}
textures = new Texture2D[tNum];
int[] meshIndex = new int[tNum];
List<Vector2[]> uvList1 = new List<Vector2[]>();
List<Vector2[]> uvList2 = new List<Vector2[]>();
List<Vector2[]> uvList3 = new List<Vector2[]>();
int uvCount1 = 0;
int uvCount2 = 0;
int uvCount3 = 0;
CombineInstance[] combine = new CombineInstance[tNum];
foreach (MeshFilter mf in MFs)
{
combine[meshOffset] = new CombineInstance();
combine[meshOffset].mesh = mf.sharedMesh;
combine[meshOffset].transform = mf.transform.localToWorldMatrix;
meshIndex[meshOffset] = mf.sharedMesh.vertexCount;
textures[meshOffset] = (Texture2D) mf.GetComponent<MeshRenderer>().sharedMaterial.GetTexture("_MainTex");
uvList1.Add(mf.sharedMesh.uv);
uvList2.Add(mf.sharedMesh.uv2);
uvList3.Add(mf.sharedMesh.uv1);
uvCount1+=mf.sharedMesh.uv.Length;
uvCount2+=mf.sharedMesh.uv2.Length;
uvCount3+=mf.sharedMesh.uv1.Length;
meshOffset++;
if(destroy)
{
EditorWindow.DestroyImmediate(mf.gameObject);
}
}
Texture2D tx = new Texture2D (maxAtlasSize,maxAtlasSize);
Rect[] r = tx.PackTextures (textures, 0,maxAtlasSize);
uvOffset = 0;
meshOffset = 0;
MeshFilter filter = scene.AddComponent<MeshFilter>();
MeshRenderer renderer = scene.AddComponent<MeshRenderer>();
filter.sharedMesh = new Mesh();
filter.sharedMesh.CombineMeshes( combine, true, true );
Vector2[] atlasUVs = new Vector2[uvCount1];
Vector2[] lightUVs = new Vector2[uvCount2];
Vector2[] xxUVs = new Vector2[uvCount3];
int j = 0;
for (int i = 0; i < uvList1.Count; i++)
{
foreach (Vector2 uv in uvList1[i])
{
atlasUVs[j].x = Mathf.Lerp(r[i].xMin, r[i].xMax, uv.x);
atlasUVs[j].y = Mathf.Lerp(r[i].yMin, r[i].yMax, uv.y);
j++;
}
}
j = 0;
for (int i = 0; i < uvList2.Count; i++)
{
foreach (Vector2 uv in uvList2[i])
{
lightUVs[j].x = Mathf.Lerp(r[i].xMin, r[i].xMax, uv.x);
lightUVs[j].y = Mathf.Lerp(r[i].yMin, r[i].yMax, uv.y);
j++;
}
}
// j = 0;
// for (int i = 0; i < uvList3.Count; i++)
// {
// foreach (Vector2 uv in uvList3[i])
// {
// xxUVs[j].x = Mathf.Lerp(r[i].xMin, r[i].xMax, uv.x);
// xxUVs[j].y = Mathf.Lerp(r[i].yMin, r[i].yMax, uv.y);
// j++;
// }
// }
Material mat;
mat = new Material(Shader.Find("Transparent/Cutout/Diffuse"));
mat.SetTexture("_MainTex", tx);
mat.SetColor ("_Color", new Color32 (221,229,229,255));
filter.sharedMesh.uv = atlasUVs;
filter.sharedMesh.uv2 = lightUVs;
// filter.sharedMesh.uv1 = xxUVs;
renderer.sharedMaterial = mat;
uvOffset = 0;
for (int i = 0;i<meshOffset;i++) {
EditorWindow.DestroyImmediate(MFs[i].gameObject,true);
Resources.UnloadAsset (textures[i]);
textures[i] = null;
}
MFs = null;
textures = null;
atlasUVs = null;
lightUVs = null;
Resources.UnloadUnusedAssets();
}
some body tall me, my sense have some the same mesh same uv2. so it wrong, but how can i get diffence uv2
