//posibilities // using fixOutOfBoundsUVs or not // /// <summary> /// 根据合并列表物体配置多材质 /// </summary> /// <param name="mom"></param> /// <param name="resultMaterials"></param> /// <param name="textureBaker"></param> public static void ConfigureMutiMaterialsFromObjsToCombine(TextureCombineEntrance mom, SerializedProperty resultMaterials, SerializedObject textureBaker) { if (mom.GetObjectsToCombine().Count == 0) { Debug.LogError("合并列表为空"); return; } if (resultMaterials.arraySize > 0) { Debug.LogError("多材质映射已配置,在进行当前操作前需清除旧配置"); return; } if (mom.textureBakeResults == null) { Debug.LogError("Texture Bake Result 为空"); return; } //validate that the objects to be combined are valid //验证合并列表内物体有效性 for (int i = 0; i < mom.GetObjectsToCombine().Count; i++) { GameObject go = mom.GetObjectsToCombine()[i]; if (go == null) { Debug.LogError("合并列表中有空物体于 " + i); return; } Renderer r = go.GetComponent <Renderer>(); if (r == null || (!(r is MeshRenderer) && !(r is SkinnedMeshRenderer))) { Debug.LogError("合并列表中第 " + i + " 个游戏物体没有 renderer 组件"); return; } if (r.sharedMaterial == null) { Debug.LogError("合并列表中第 " + i + " 个游戏物体没有材质"); return; } } Dictionary <Material, Mesh> obUVobjectToMesh_map = new Dictionary <Material, Mesh>(); //first pass put any meshes with obUVs on their own submesh if not fixing OB uvs //1.将所有带有obUV的网格置于单独的材质中,(如果不固定OB UV) if (mom.doMultiMaterialSplitAtlasesIfOBUVs) { for (int i = 0; i < mom.GetObjectsToCombine().Count; i++) { GameObject go = mom.GetObjectsToCombine()[i]; Mesh m = MeshBakerUtility.GetMesh(go); MeshAnalysisResult dummyMar = new MeshAnalysisResult(); Renderer r = go.GetComponent <Renderer>(); for (int j = 0; j < r.sharedMaterials.Length; j++) { if (MeshBakerUtility.hasOutOfBoundsUVs(m, ref dummyMar, j)) { if (!obUVobjectToMesh_map.ContainsKey(r.sharedMaterials[j])) { Debug.LogWarning("Object " + go + " submesh " + j + " uses UVs outside the range 0,0..1,1 to generate tiling. " + "This object has been mapped to its own submesh in the combined mesh. " + "It can share a submesh with other objects that use different materials " + "if you use the fix out of bounds UVs feature which will bake the tiling"); obUVobjectToMesh_map.Add(r.sharedMaterials[j], m); } } } } } Dictionary <MultiMatSubmeshInfo, List <List <Material> > > shaderToMaterial_map = new Dictionary <MultiMatSubmeshInfo, List <List <Material> > >(); //2.没有 OB uvs 的材质按 shader 添加到 shader2Material_map 中 for (int i = 0; i < mom.GetObjectsToCombine().Count; i++) { Renderer r = mom.GetObjectsToCombine()[i].GetComponent <Renderer>(); for (int j = 0; j < r.sharedMaterials.Length; j++) { if (!obUVobjectToMesh_map.ContainsKey(r.sharedMaterials[j])) { //if not already added if (r.sharedMaterials[j] == null) { continue; } List <List <Material> > binsOfMatsThatUseShader = null; MultiMatSubmeshInfo newKey = new MultiMatSubmeshInfo(r.sharedMaterials[j].shader, r.sharedMaterials[j]); if (!shaderToMaterial_map.TryGetValue(newKey, out binsOfMatsThatUseShader)) { binsOfMatsThatUseShader = new List <List <Material> >(); binsOfMatsThatUseShader.Add(new List <Material>()); shaderToMaterial_map.Add(newKey, binsOfMatsThatUseShader); } if (!binsOfMatsThatUseShader[0].Contains(r.sharedMaterials[j])) { binsOfMatsThatUseShader[0].Add(r.sharedMaterials[j]); } } } } int ResultMatsCount = shaderToMaterial_map.Count; //third pass for each shader grouping check how big the atlas would be and group into bins that would fit in an atlas // 3.检查每个 Shader-Mat 组的最终打包的 atlas 大小,过大则分到另一个图集中 if (mom.doMultiMaterialSplitAtlasesIfTooBig) { if (mom.packingAlgorithm == PackingAlgorithmEnum.UnitysPackTextures) { Debug.LogWarning("Unity texture packer does not support splitting atlases if too big. Atlases will not be split."); } else { ResultMatsCount = 0; foreach (MultiMatSubmeshInfo sh in shaderToMaterial_map.Keys) { List <List <Material> > binsOfMatsThatUseShader = shaderToMaterial_map[sh]; List <Material> allMatsThatUserShader = binsOfMatsThatUseShader[0];//at this point everything is in the same list binsOfMatsThatUseShader.RemoveAt(0); TextureCombineHandler combiner = mom.CreateAndConfigureTextureCombiner(); combiner.saveAtlasesAsAssets = false; if (allMatsThatUserShader.Count > 1) { combiner.fixOutOfBoundsUVs = mom.fixOutOfBoundsUVs; } else { combiner.fixOutOfBoundsUVs = false; } // Do the texture pack List <AtlasPackingResult> packingResults = new List <AtlasPackingResult>(); Material tempMat = new Material(sh.shader); combiner.CombineTexturesIntoAtlases(null, null, tempMat, mom.GetObjectsToCombine(), allMatsThatUserShader, null, packingResults, true, true); for (int i = 0; i < packingResults.Count; i++) { List <MatsAndGOs> matsData = (List <MatsAndGOs>)packingResults[i].data; List <Material> mats = new List <Material>(); for (int j = 0; j < matsData.Count; j++) { for (int kk = 0; kk < matsData[j].mats.Count; kk++) { if (!mats.Contains(matsData[j].mats[kk].mat)) { mats.Add(matsData[j].mats[kk].mat); } } } binsOfMatsThatUseShader.Add(mats); } ResultMatsCount += binsOfMatsThatUseShader.Count; } } } //build the result materials if (shaderToMaterial_map.Count == 0 && obUVobjectToMesh_map.Count == 0) { Debug.LogError("合并列表中没有材质"); } mom.resultMaterials = new MultiMaterial[ResultMatsCount + obUVobjectToMesh_map.Count]; string pth = AssetDatabase.GetAssetPath(mom.textureBakeResults); string baseName = Path.GetFileNameWithoutExtension(pth); string folderPath = pth.Substring(0, pth.Length - baseName.Length - 6); int k = 0; foreach (MultiMatSubmeshInfo sh in shaderToMaterial_map.Keys) { foreach (List <Material> sourseMatsThatUseSameShader in shaderToMaterial_map[sh]) { MultiMaterial mm = mom.resultMaterials[k] = new MultiMaterial(); mm.sourceMaterials = sourseMatsThatUseSameShader; if (mm.sourceMaterials.Count == 1) { mm.considerMeshUVs = false; } else { mm.considerMeshUVs = mom.fixOutOfBoundsUVs; } string matName = folderPath + baseName + "-mat" + k + ".mat"; Material newMat = new Material(Shader.Find("Diffuse")); if (sourseMatsThatUseSameShader.Count > 0 && sourseMatsThatUseSameShader[0] != null) { //复制参数值 TextureCombineEntrance.ConfigureNewMaterialToMatchOld(newMat, sourseMatsThatUseSameShader[0]); } AssetDatabase.CreateAsset(newMat, matName); //合并材质初始值为源材质列表第一个的值 mm.combinedMaterial = (Material)AssetDatabase.LoadAssetAtPath(matName, typeof(Material)); k++; } } foreach (Material m in obUVobjectToMesh_map.Keys) { MultiMaterial mm = mom.resultMaterials[k] = new MultiMaterial(); mm.sourceMaterials = new List <Material>(); mm.sourceMaterials.Add(m); mm.considerMeshUVs = false; string matName = folderPath + baseName + "-mat" + k + ".mat"; Material newMat = new Material(Shader.Find("Diffuse")); TextureCombineEntrance.ConfigureNewMaterialToMatchOld(newMat, m); AssetDatabase.CreateAsset(newMat, matName); mm.combinedMaterial = (Material)AssetDatabase.LoadAssetAtPath(matName, typeof(Material)); k++; } SceneBakerUtilityInEditor.UpdateIfDirtyOrScript(textureBaker); }
/// <summary> /// 创建合并材质资源 /// </summary> /// <param name="target"></param> /// <param name="pth"></param> public static void CreateCombinedMaterialAssets(TextureCombineEntrance target, string pth) { TextureCombineEntrance mom = (TextureCombineEntrance)target; string baseName = Path.GetFileNameWithoutExtension(pth); if (baseName == null || baseName.Length == 0) { return; } string folderPath = pth.Substring(0, pth.Length - baseName.Length - 6); List <string> matNames = new List <string>(); //多材质 if (mom.doMultiMaterial) { for (int i = 0; i < mom.resultMaterials.Length; i++) { matNames.Add(folderPath + baseName + "-mat" + i + ".mat"); AssetDatabase.CreateAsset(new Material(Shader.Find("Diffuse")), matNames[i]); mom.resultMaterials[i].combinedMaterial = (Material)AssetDatabase.LoadAssetAtPath(matNames[i], typeof(Material)); } } else { matNames.Add(folderPath + baseName + "-mat.mat"); Material newMat = null; if (mom.GetObjectsToCombine().Count > 0 && mom.GetObjectsToCombine()[0] != null) { Renderer r = mom.GetObjectsToCombine()[0].GetComponent <Renderer>(); if (r == null) { Debug.LogWarning("Object " + mom.GetObjectsToCombine()[0] + " does not have a Renderer on it."); } else { if (r.sharedMaterial != null) { newMat = new Material(r.sharedMaterial); TextureCombineEntrance.ConfigureNewMaterialToMatchOld(newMat, r.sharedMaterial); } } } else { Debug.Log("If you add objects to be combined before creating the Combined Material Assets. " + "Then Mesh Baker will create a result material that is a duplicate of the material on the first object to be combined. " + "This saves time configuring the shader."); } if (newMat == null) { newMat = new Material(Shader.Find("Diffuse")); } AssetDatabase.CreateAsset(newMat, matNames[0]); mom.resultMaterial = (Material)AssetDatabase.LoadAssetAtPath(matNames[0], typeof(Material)); } //create the TextureBakeResults AssetDatabase.CreateAsset(ScriptableObject.CreateInstance <TextureBakeResults>(), pth); mom.textureBakeResults = (TextureBakeResults)AssetDatabase.LoadAssetAtPath(pth, typeof(TextureBakeResults)); AssetDatabase.Refresh(); }
/* tried to see if the MultiMaterialConfig could be done using the GroupBy filters. Saddly it didn't work -_- */ public static void ConfigureMutiMaterialsFromObjsToCombine2(TextureCombineEntrance mom, SerializedProperty resultMaterials, SerializedObject textureBaker) { if (mom.GetObjectsToCombine().Count == 0) { Debug.LogError("You need to add some objects to combine before building the multi material list."); return; } if (resultMaterials.arraySize > 0) { Debug.LogError("You already have some source to combined material mappings configured. You must remove these before doing this operation."); return; } if (mom.textureBakeResults == null) { Debug.LogError("Texture Bake Result asset must be set before using this operation."); return; } //validate that the objects to be combined are valid for (int i = 0; i < mom.GetObjectsToCombine().Count; i++) { GameObject go = mom.GetObjectsToCombine()[i]; if (go == null) { Debug.LogError("Null object in list of objects to combine at position " + i); return; } Renderer r = go.GetComponent <Renderer>(); if (r == null || (!(r is MeshRenderer) && !(r is SkinnedMeshRenderer))) { Debug.LogError("GameObject at position " + i + " in list of objects to combine did not have a renderer"); return; } if (r.sharedMaterial == null) { Debug.LogError("GameObject at position " + i + " in list of objects to combine has a null material"); return; } } IGroupByFilter[] filters = new IGroupByFilter[3]; filters[0] = new GroupByOutOfBoundsUVs(); filters[1] = new GroupByShader(); filters[2] = new MB3_GroupByStandardShaderType(); List <GameObjectFilterInfo> gameObjects = new List <GameObjectFilterInfo>(); HashSet <GameObject> objectsAlreadyIncludedInBakers = new HashSet <GameObject>(); for (int i = 0; i < mom.GetObjectsToCombine().Count; i++) { GameObjectFilterInfo goaw = new GameObjectFilterInfo(mom.GetObjectsToCombine()[i], objectsAlreadyIncludedInBakers, filters); if (goaw.materials.Length > 0) //don't consider renderers with no materials { gameObjects.Add(goaw); } } //analyse meshes Dictionary <int, MeshAnalysisResult> meshAnalysisResultCache = new Dictionary <int, MeshAnalysisResult>(); int totalVerts = 0; for (int i = 0; i < gameObjects.Count; i++) { //string rpt = String.Format("Processing {0} [{1} of {2}]", gameObjects[i].go.name, i, gameObjects.Count); //EditorUtility.DisplayProgressBar("Analysing Scene", rpt + " A", .6f); Mesh mm = MeshBakerUtility.GetMesh(gameObjects[i].go); int nVerts = 0; if (mm != null) { nVerts += mm.vertexCount; MeshAnalysisResult mar; if (!meshAnalysisResultCache.TryGetValue(mm.GetInstanceID(), out mar)) { //EditorUtility.DisplayProgressBar("Analysing Scene", rpt + " Check Out Of Bounds UVs", .6f); MeshBakerUtility.hasOutOfBoundsUVs(mm, ref mar); //Rect dummy = mar.uvRect; MeshBakerUtility.doSubmeshesShareVertsOrTris(mm, ref mar); meshAnalysisResultCache.Add(mm.GetInstanceID(), mar); } if (mar.hasOutOfBoundsUVs) { int w = (int)mar.uvRect.width; int h = (int)mar.uvRect.height; gameObjects[i].outOfBoundsUVs = true; gameObjects[i].warning += " [WARNING: has uvs outside the range (0,1) tex is tiled " + w + "x" + h + " times]"; } if (mar.hasOverlappingSubmeshVerts) { gameObjects[i].submeshesOverlap = true; gameObjects[i].warning += " [WARNING: Submeshes share verts or triangles. 'Multiple Combined Materials' feature may not work.]"; } } totalVerts += nVerts; //EditorUtility.DisplayProgressBar("Analysing Scene", rpt + " Validate OBuvs Multi Material", .6f); Renderer mr = gameObjects[i].go.GetComponent <Renderer>(); if (!MeshBakerUtility.AreAllSharedMaterialsDistinct(mr.sharedMaterials)) { gameObjects[i].warning += " [WARNING: Object uses same material on multiple submeshes. This may produce poor results when used with multiple materials or fix out of bounds uvs.]"; } } List <GameObjectFilterInfo> objsNotAddedToBaker = new List <GameObjectFilterInfo>(); Dictionary <GameObjectFilterInfo, List <List <GameObjectFilterInfo> > > gs2bakeGroupMap = null;//MB3_MeshBakerEditorWindow.sortIntoBakeGroups3(gameObjects, objsNotAddedToBaker, filters, false, mom.maxAtlasSize); mom.resultMaterials = new MultiMaterial[gs2bakeGroupMap.Keys.Count]; string pth = AssetDatabase.GetAssetPath(mom.textureBakeResults); string baseName = Path.GetFileNameWithoutExtension(pth); string folderPath = pth.Substring(0, pth.Length - baseName.Length - 6); int k = 0; foreach (GameObjectFilterInfo m in gs2bakeGroupMap.Keys) { MultiMaterial mm = mom.resultMaterials[k] = new MultiMaterial(); mm.sourceMaterials = new List <Material>(); mm.sourceMaterials.Add(m.materials[0]); string matName = folderPath + baseName + "-mat" + k + ".mat"; Material newMat = new Material(Shader.Find("Diffuse")); TextureCombineEntrance.ConfigureNewMaterialToMatchOld(newMat, m.materials[0]); AssetDatabase.CreateAsset(newMat, matName); mm.combinedMaterial = (Material)AssetDatabase.LoadAssetAtPath(matName, typeof(Material)); k++; } SceneBakerUtilityInEditor.UpdateIfDirtyOrScript(textureBaker); }