private void Combine(Vector3 rootPosition, TexturePacker packer, HLODBuildInfo info, dynamic options) { var atlas = packer.GetAtlas(info); if (atlas == null) { return; } List <TextureInfo> textureInfoList = options.TextureInfoList; List <MeshCombiner.CombineInfo> combineInfos = new List <MeshCombiner.CombineInfo>(); for (int i = 0; i < info.WorkingObjects.Count; ++i) { var obj = info.WorkingObjects[i]; ConvertMesh(obj.Mesh, obj.Materials, atlas, textureInfoList[0].InputName); for (int si = 0; si < obj.Mesh.subMeshCount; ++si) { var ci = new MeshCombiner.CombineInfo(); ci.Mesh = obj.Mesh; ci.MeshIndex = si; ci.Transform = obj.LocalToWorld; ci.Transform.m03 -= rootPosition.x; ci.Transform.m13 -= rootPosition.y; ci.Transform.m23 -= rootPosition.z; combineInfos.Add(ci); } } MeshCombiner combiner = new MeshCombiner(); WorkingMesh combinedMesh = combiner.CombineMesh(Allocator.Persistent, combineInfos); WorkingObject newObj = new WorkingObject(Allocator.Persistent); WorkingMaterial newMat = m_createdMaterials[atlas].Clone(); combinedMesh.name = info.Name + "_Mesh"; newObj.Name = info.Name; newObj.SetMesh(combinedMesh); newObj.Materials.Add(newMat); info.WorkingObjects.Dispose(); info.WorkingObjects = new DisposableList <WorkingObject>(); info.WorkingObjects.Add(newObj); }
public void Batch(Vector3 rootPosition, DisposableList <HLODBuildInfo> targets, Action <float> onProgress) { dynamic options = m_batcherOptions; if (onProgress != null) { onProgress(0.0f); } using (TexturePacker packer = new TexturePacker()) { PackingTexture(packer, targets, options, onProgress); for (int i = 0; i < targets.Count; ++i) { Combine(rootPosition, packer, targets[i], options); if (onProgress != null) { onProgress(0.5f + ((float)i / (float)targets.Count) * 0.5f); } } } }
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; } }