private void PackingTexture(TexturePacker packer, DisposableList <HLODBuildInfo> targets, dynamic options, Action <float> onProgress) { List <TextureInfo> textureInfoList = options.TextureInfoList; using (MaterialTextureCache cache = new MaterialTextureCache(options)) { for (int i = 0; i < targets.Count; ++i) { var workingObjects = targets[i].WorkingObjects; Dictionary <Guid, TexturePacker.MaterialTexture> textures = new Dictionary <Guid, TexturePacker.MaterialTexture>(); for (int oi = 0; oi < workingObjects.Count; ++oi) { var materials = workingObjects[oi].Materials; for (int m = 0; m < materials.Count; ++m) { var materialTextures = cache.GetMaterialTextures(materials[m]); if (materialTextures == null) { continue; } if (textures.ContainsKey(materialTextures[0].GetGUID()) == true) { continue; } textures.Add(materialTextures[0].GetGUID(), materialTextures); } } packer.AddTextureGroup(targets[i], textures.Values.ToList()); if (onProgress != null) { onProgress(((float)i / targets.Count) * 0.1f); } } } packer.Pack(TextureFormat.RGBA32, options.PackTextureSize, options.LimitTextureSize, false); if (onProgress != null) { onProgress(0.3f); } int index = 1; var atlases = packer.GetAllAtlases(); foreach (var atlas in atlases) { Dictionary <string, WorkingTexture> textures = new Dictionary <string, WorkingTexture>(); for (int i = 0; i < atlas.Textures.Count; ++i) { WorkingTexture wt = atlas.Textures[i]; wt.Name = "CombinedTexture " + index + "_" + i; if (textureInfoList[i].Type == PackingType.Normal) { wt.Linear = true; } textures.Add(textureInfoList[i].OutputName, wt); } WorkingMaterial mat = CreateMaterial(options.MaterialGUID, textures); mat.Name = "CombinedMaterial " + index; m_createdMaterials.Add(atlas, mat); index += 1; } }
private void PackingTexture(List <HLODBuildInfo> targets, dynamic options, Action <float> onProgress) { List <TextureInfo> textureInfoList = options.TextureInfoList; for (int i = 0; i < targets.Count; ++i) { var renderers = targets[i].renderers; var textures = new HashSet <TexturePacker.MultipleTexture>(); for (int r = 0; r < renderers.Count; ++r) { var materials = renderers[r].sharedMaterials; for (int m = 0; m < materials.Length; ++m) { TexturePacker.MultipleTexture multipleTexture = new TexturePacker.MultipleTexture(); foreach (var info in textureInfoList) { Texture2D tex = materials[m].GetTexture(info.InputName) as Texture2D; if (tex == null) { multipleTexture.textureList.Add(GetDefaultTexture(info.Type)); } else { multipleTexture.textureList.Add(tex); } } textures.Add(multipleTexture); } } m_Packer.AddTextureGroup(targets[i], textures.ToArray()); if (onProgress != null) { onProgress(((float)i / targets.Count) * 0.1f); } } m_Packer.Pack(options.PackTextureSize, options.LimitTextureSize); if (onProgress != null) { onProgress(0.3f); } int index = 1; var atlases = m_Packer.GetAllAtlases(); foreach (var atlas in atlases) { for (int i = 0; i < atlas.PackedTexture.Length; ++i) { var name = GetPrefabDirectory() + m_hlod.name + index++ + "_" + i + ".png"; atlas.PackedTexture[i] = SaveTexture(atlas.PackedTexture[i], name, textureInfoList[i].Type == PackingType.Normal); Debug.Assert(atlas.PackedTexture[i]); } } if (onProgress != null) { onProgress(0.5f); } var savedAtlases = m_Packer.GetAllAtlases(); for (int i = 0; i < savedAtlases.Length; ++i) { m_hlod.GeneratedObjects.AddRange(savedAtlases[i].PackedTexture); } }