void saveMeshToAssetDatabase(MB2_MeshBakerCommon mom, Mesh mesh) { //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset."); return; } string baseName = System.IO.Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7); string newFilename = folderPath + baseName + "-mesh.asset"; //check if mesh is already an asset string ap = AssetDatabase.GetAssetPath(mesh); if (ap == null || ap.Equals("")) { Debug.Log("Saving mesh asset to " + newFilename); AssetDatabase.CreateAsset(mesh, newFilename); } else { Debug.Log("Mesh is an asset at " + ap); } }
public static void RebuildPrefab(MB2_MeshBakerCommon mom) { if (MB2_MeshCombiner.EVAL_VERSION) { return; } if (mom is MB2_MeshBaker) { MB2_MeshBaker mb = (MB2_MeshBaker)mom; GameObject prefabRoot = mom.resultPrefab; GameObject rootGO = (GameObject)PrefabUtility.InstantiatePrefab(prefabRoot); mb.meshCombiner.buildSceneMeshObject(rootGO, mb.meshCombiner.GetMesh(), true); string prefabPth = AssetDatabase.GetAssetPath(prefabRoot); PrefabUtility.ReplacePrefab(rootGO, AssetDatabase.LoadAssetAtPath(prefabPth, typeof(GameObject)), ReplacePrefabOptions.ConnectToPrefab); Editor.DestroyImmediate(rootGO); } else if (mom is MB2_MultiMeshBaker) { MB2_MultiMeshBaker mmb = (MB2_MultiMeshBaker)mom; GameObject prefabRoot = mom.resultPrefab; GameObject rootGO = (GameObject)PrefabUtility.InstantiatePrefab(prefabRoot); for (int i = 0; i < mmb.meshCombiner.meshCombiners.Count; i++) { mmb.meshCombiner.meshCombiners[i].combinedMesh.buildSceneMeshObject(rootGO, mmb.meshCombiner.meshCombiners[i].combinedMesh.GetMesh(), true); } string prefabPth = AssetDatabase.GetAssetPath(prefabRoot); PrefabUtility.ReplacePrefab(rootGO, AssetDatabase.LoadAssetAtPath(prefabPth, typeof(GameObject)), ReplacePrefabOptions.ConnectToPrefab); Editor.DestroyImmediate(rootGO); } else { Debug.LogError("Argument was not a MB2_MeshBaker or an MB2_MultiMeshBaker."); } }
void createNewPrefab(MB2_MeshBakerCommon mom, string pth) { //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; string baseName = System.IO.Path.GetFileNameWithoutExtension(pth); string folderPath = pth.Substring(0, pth.Length - baseName.Length - 7); List <string> matNames = new List <string>(); if (mom.textureBakeResults.doMultiMaterial) { for (int i = 0; i < mom.textureBakeResults.resultMaterials.Length; i++) { matNames.Add(folderPath + baseName + "-mat" + i + ".mat"); AssetDatabase.CreateAsset(new Material(Shader.Find("Diffuse")), matNames[i]); mom.textureBakeResults.resultMaterials[i].combinedMaterial = (Material)AssetDatabase.LoadAssetAtPath(matNames[i], typeof(Material)); } } else { matNames.Add(folderPath + baseName + "-mat.mat"); AssetDatabase.CreateAsset(new Material(Shader.Find("Diffuse")), matNames[0]); mom.textureBakeResults.resultMaterial = (Material)AssetDatabase.LoadAssetAtPath(matNames[0], typeof(Material)); } //create the prefab UnityEngine.Object prefabAsset = PrefabUtility.CreateEmptyPrefab(pth); GameObject rootGO = new GameObject("combinedMesh-" + mom.name); PrefabUtility.ReplacePrefab(rootGO, prefabAsset, ReplacePrefabOptions.ConnectToPrefab); Editor.DestroyImmediate(rootGO); mom.resultPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(pth, typeof(GameObject)); AssetDatabase.Refresh(); }
public static void SaveMeshsToAssetDatabase(MB2_MeshBakerCommon mom, string folderPath, string newFileNameBase){ if (MB2_MeshCombiner.EVAL_VERSION) return; if (mom is MB2_MeshBaker){ MB2_MeshBaker mb = (MB2_MeshBaker) mom; string newFilename = newFileNameBase + ".asset"; string ap = AssetDatabase.GetAssetPath(mb.meshCombiner.GetMesh()); if (ap == null || ap.Equals("")){ Debug.Log("Saving mesh asset to " + newFilename); AssetDatabase.CreateAsset(mb.meshCombiner.GetMesh(), newFilename); } else { Debug.Log("Mesh is an asset at " + ap); } } else if (mom is MB2_MultiMeshBaker){ MB2_MultiMeshBaker mmb = (MB2_MultiMeshBaker) mom; for (int i = 0; i < mmb.meshCombiner.meshCombiners.Count; i++){ string newFilename = newFileNameBase + i + ".asset"; Mesh mesh = mmb.meshCombiner.meshCombiners[i].combinedMesh.GetMesh(); string ap = AssetDatabase.GetAssetPath(mesh); if (ap == null || ap.Equals("")){ Debug.Log("Saving mesh asset to " + newFilename); AssetDatabase.CreateAsset(mesh, newFilename); } else { Debug.Log("Mesh is an asset at " + ap); } } } else { Debug.LogError("Argument was not a MB2_MeshBaker or an MB2_MultiMeshBaker."); } }
void bake(MB2_MeshBakerCommon mom) { try{ //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; if (mom.outputOption == MB2_OutputOptions.bakeIntoSceneObject) { _bakeIntoCombined(mom, MB_OutputOptions.bakeIntoSceneObject); } else if (mom.outputOption == MB2_OutputOptions.bakeIntoPrefab) { _bakeIntoCombined(mom, MB_OutputOptions.bakeIntoPrefab); } else { if (mom is MB2_MeshBaker) { _bakeMeshesInPlace(mom); } else { Debug.LogError("Multi-mesh Baker components cannot be used for Bake In Place. Use an ordinary Mesh Baker object instead."); } } } catch (Exception e) { Debug.LogError(e); } finally { EditorUtility.ClearProgressBar(); } }
void _init (MB2_MeshBakerCommon target) { meshBaker = new SerializedObject(target); outputOptions = meshBaker.FindProperty("outputOption"); objsToMesh = meshBaker.FindProperty("objsToMesh"); renderType = meshBaker.FindProperty("renderType"); useObjsToMeshFromTexBaker = meshBaker.FindProperty("useObjsToMeshFromTexBaker"); textureBakeResults = meshBaker.FindProperty("textureBakeResults"); lightmappingOption = meshBaker.FindProperty("lightmapOption"); doNorm = meshBaker.FindProperty("doNorm"); doTan = meshBaker.FindProperty("doTan"); doUV = meshBaker.FindProperty("doUV"); doUV1 = meshBaker.FindProperty("doUV1"); doCol = meshBaker.FindProperty("doCol"); }
void _init(MB2_MeshBakerCommon target) { meshBaker = new SerializedObject(target); outputOptions = meshBaker.FindProperty("outputOption"); objsToMesh = meshBaker.FindProperty("objsToMesh"); renderType = meshBaker.FindProperty("renderType"); useObjsToMeshFromTexBaker = meshBaker.FindProperty("useObjsToMeshFromTexBaker"); textureBakeResults = meshBaker.FindProperty("textureBakeResults"); lightmappingOption = meshBaker.FindProperty("lightmapOption"); doNorm = meshBaker.FindProperty("doNorm"); doTan = meshBaker.FindProperty("doTan"); doUV = meshBaker.FindProperty("doUV"); doUV1 = meshBaker.FindProperty("doUV1"); doCol = meshBaker.FindProperty("doCol"); }
void bake(MB2_MeshBakerCommon mom) { try{ if (mom.outputOption == MB2_OutputOptions.bakeIntoSceneObject) { MB2_MeshBakerEditorFunctions._bakeIntoCombined(mom, MB_OutputOptions.bakeIntoSceneObject); } else if (mom.outputOption == MB2_OutputOptions.bakeIntoPrefab) { MB2_MeshBakerEditorFunctions._bakeIntoCombined(mom, MB_OutputOptions.bakeIntoPrefab); } else { if (mom is MB2_MeshBaker) { if (MB2_MeshCombiner.EVAL_VERSION) { Debug.LogError("Bake Meshes In Place is disabled in the evaluation version."); } else { if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly, new MB2_EditorMethods())) { return; } List <GameObject> objsToMesh = mom.objsToMesh; if (mom.useObjsToMeshFromTexBaker && mom.GetComponent <MB2_TextureBaker>() != null) { objsToMesh = mom.GetComponent <MB2_TextureBaker>().objsToMesh; } ((MB2_MeshBaker)mom)._update_MB2_MeshCombiner(); MB2_BakeInPlace.BakeMeshesInPlace(((MB2_MeshBaker)mom).meshCombiner, objsToMesh, mom.bakeAssetsInPlaceFolderPath, updateProgressBar); } } else { Debug.LogError("Multi-mesh Baker components cannot be used for Bake In Place. Use an ordinary Mesh Baker object instead."); } } } catch (Exception e) { Debug.LogError(e); } finally { EditorUtility.ClearProgressBar(); } }
public static void SaveMeshsToAssetDatabase(MB2_MeshBakerCommon mom, string folderPath, string newFileNameBase) { if (MB2_MeshCombiner.EVAL_VERSION) { return; } if (mom is MB2_MeshBaker) { MB2_MeshBaker mb = (MB2_MeshBaker)mom; string newFilename = newFileNameBase + ".asset"; string ap = AssetDatabase.GetAssetPath(mb.meshCombiner.GetMesh()); if (ap == null || ap.Equals("")) { Debug.Log("Saving mesh asset to " + newFilename); AssetDatabase.CreateAsset(mb.meshCombiner.GetMesh(), newFilename); } else { Debug.Log("Mesh is an asset at " + ap); } } else if (mom is MB2_MultiMeshBaker) { MB2_MultiMeshBaker mmb = (MB2_MultiMeshBaker)mom; for (int i = 0; i < mmb.meshCombiner.meshCombiners.Count; i++) { string newFilename = newFileNameBase + i + ".asset"; Mesh mesh = mmb.meshCombiner.meshCombiners[i].combinedMesh.GetMesh(); string ap = AssetDatabase.GetAssetPath(mesh); if (ap == null || ap.Equals("")) { Debug.Log("Saving mesh asset to " + newFilename); AssetDatabase.CreateAsset(mesh, newFilename); } else { Debug.Log("Mesh is an asset at " + ap); } } } else { Debug.LogError("Argument was not a MB2_MeshBaker or an MB2_MultiMeshBaker."); } }
/** * pass in System.IO.File.WriteAllBytes for parameter fileSaveFunction. This is necessary because on Web Player file saving * functions only exist for Editor classes */ public void CreateAndSaveAtlases(ProgressUpdateDelegate progressInfo, MB_TextureCombiner.FileSaveFunction fileSaveFunction) { MB_AtlasesAndRects[] mAndAs = null; try{ if (doCombinedValidate(this, MB_ObjsToCombineTypes.dontCare)) { mAndAs = CreateAtlases(progressInfo, true, fileSaveFunction); if (mAndAs != null) { MB2_MeshBakerCommon mb = GetComponent <MB2_MeshBakerCommon>(); if (mb != null) { mb.textureBakeResults = textureBakeResults; } } } } catch (Exception e) { Debug.LogError(e); } finally { if (mAndAs != null) { for (int j = 0; j < mAndAs.Length; j++) { MB_AtlasesAndRects mAndA = mAndAs[j]; if (mAndA != null && mAndA.atlases != null) { for (int i = 0; i < mAndA.atlases.Length; i++) { if (mAndA.atlases[i] != null) { MB_Utility.Destroy(mAndA.atlases[i]); } } } } } } }
public void OnInspectorGUI(MB2_MeshBakerCommon target) { DrawGUI(target); }
public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject) { if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab) { Debug.LogError("Cannot BakeIntoPrefab with evaluation version."); return; } if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject) { Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject"); return; } //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>(); if (mom.textureBakeResults == null && tb != null) { Debug.Log("setting results"); mom.textureBakeResults = tb.textureBakeResults; } if (mom.useObjsToMeshFromTexBaker) { if (tb != null) { mom.GetObjectsToCombine().Clear(); mom.GetObjectsToCombine().AddRange(tb.GetObjectsToCombine()); } } Mesh mesh; if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly, new MB2_EditorMethods())) { return; } if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab && mom.resultPrefab == null) { Debug.LogError("Need to set the Combined Mesh Prefab field. Create a prefab asset, drag an empty game object into it, and drag it to the 'Combined Mesh Prefab' field."); return; } mom.ClearMesh(); mesh = mom.AddDeleteGameObjects(mom.GetObjectsToCombine().ToArray(), null, false, true); if (mesh != null) { mom.Apply(Unwrapping.GenerateSecondaryUVSet); Debug.Log(String.Format("Successfully baked {0} meshes into a combined mesh", mom.GetObjectsToCombine().Count)); if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject) { PrefabType pt = PrefabUtility.GetPrefabType(mom.resultSceneObject); if (pt == PrefabType.Prefab || pt == PrefabType.ModelPrefab) { Debug.LogError("Combined Mesh Object is a prefab asset. If output option bakeIntoSceneObject then this must be an instance in the scene."); return; } } else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab) { string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset."); return; } string baseName = Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7); string newFilename = folderPath + baseName + "-mesh"; SaveMeshsToAssetDatabase(mom, folderPath, newFilename); if (mom.renderType == MB_RenderType.skinnedMeshRenderer) { Debug.LogWarning("Render type is skinned mesh renderer. " + "Can't create prefab until all bones have been added to the combined mesh object " + mom.resultPrefab + " Add the bones then drag the combined mesh object to the prefab."); } else { RebuildPrefab(mom); } } else { Debug.LogError("Unknown parameter"); } } }
void bake(MB2_MeshBakerCommon mom){ try{ if (mom.outputOption == MB2_OutputOptions.bakeIntoSceneObject){ MB2_MeshBakerEditorFunctions._bakeIntoCombined(mom, MB_OutputOptions.bakeIntoSceneObject); } else if (mom.outputOption == MB2_OutputOptions.bakeIntoPrefab){ MB2_MeshBakerEditorFunctions._bakeIntoCombined(mom, MB_OutputOptions.bakeIntoPrefab); } else { if (mom is MB2_MeshBaker){ if (MB2_MeshCombiner.EVAL_VERSION){ Debug.LogError("Bake Meshes In Place is disabled in the evaluation version."); } else { if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly, new MB2_EditorMethods())) return; List<GameObject> objsToMesh = mom.objsToMesh; if (mom.useObjsToMeshFromTexBaker && mom.GetComponent<MB2_TextureBaker>() != null){ objsToMesh = mom.GetComponent<MB2_TextureBaker>().objsToMesh; } ((MB2_MeshBaker)mom)._update_MB2_MeshCombiner(); MB2_BakeInPlace.BakeMeshesInPlace(((MB2_MeshBaker)mom).meshCombiner, objsToMesh, mom.bakeAssetsInPlaceFolderPath, updateProgressBar); } } else { Debug.LogError("Multi-mesh Baker components cannot be used for Bake In Place. Use an ordinary Mesh Baker object instead."); } } } catch(Exception e){ Debug.LogError(e); } finally { EditorUtility.ClearProgressBar(); } }
void createAndSetupBaker(List <_GameObjectAndWarning> gaws, string pthRoot) { if (gaws.Count < 1) { return; } int numVerts = 0; for (int i = 0; i < gaws.Count; i++) { numVerts = gaws[i].numVerts; } UnityEngine.GameObject newMeshBaker = null; if (numVerts >= 65535) { newMeshBaker = MB2_MultiMeshBakerEditor.CreateNewMeshBaker(); } else { newMeshBaker = MB2_MeshBakerEditor.CreateNewMeshBaker(); } newMeshBaker.name = ("MeshBaker-" + gaws[0].shader.name + "-LM" + gaws[0].lightmapIndex).ToString().Replace("/", "-"); MB2_TextureBaker tb = newMeshBaker.GetComponent <MB2_TextureBaker>(); //string pth = AssetDatabase.GenerateUniqueAssetPath(pthRoot); //create result material string pthMat = AssetDatabase.GenerateUniqueAssetPath(pthRoot + newMeshBaker.name + ".mat"); AssetDatabase.CreateAsset(new Material(UnityEngine.Shader.Find("Diffuse")), pthMat); tb.resultMaterial = (Material)AssetDatabase.LoadAssetAtPath(pthMat, typeof(Material)); //create the MB2_TextureBakeResults string pthAsset = AssetDatabase.GenerateUniqueAssetPath(pthRoot + newMeshBaker.name + ".asset"); AssetDatabase.CreateAsset(ScriptableObject.CreateInstance <MB2_TextureBakeResults>(), pthAsset); tb.textureBakeResults = (MB2_TextureBakeResults)AssetDatabase.LoadAssetAtPath(pthAsset, typeof(MB2_TextureBakeResults)); AssetDatabase.Refresh(); tb.resultMaterial.shader = gaws[0].shader; tb.objsToMesh = new List <UnityEngine.GameObject>(); for (int i = 0; i < gaws.Count; i++) { tb.objsToMesh.Add(gaws[i].go); } MB2_MeshBakerCommon mb = newMeshBaker.GetComponent <MB2_MeshBakerCommon>(); if (generate_LightmapOption == LightMapOption.ignore) { mb.lightmapOption = MB2_LightmapOptions.ignore_UV2; } else { if (gaws[0].lightmapIndex == -1 || gaws[0].lightmapIndex == -2) { mb.lightmapOption = MB2_LightmapOptions.ignore_UV2; } else { mb.lightmapOption = MB2_LightmapOptions.preserve_current_lightmapping; } } }
public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject) { if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab) { Debug.LogError("Cannot BakeIntoPrefab with evaluation version."); return; } if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject) { Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject"); return; } //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>(); if (mom.textureBakeResults == null && tb != null) { Debug.Log("setting results"); mom.textureBakeResults = tb.textureBakeResults; } if (mom.useObjsToMeshFromTexBaker) { if (tb != null) { mom.objsToMesh.Clear(); mom.objsToMesh.AddRange(tb.objsToMesh); } } Mesh mesh; if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly)) { return; } if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab && mom.resultPrefab == null) { Debug.LogError("Need to set the Combined Mesh Prefab field."); return; } mom.ClearMesh(); mesh = mom.AddDeleteGameObjects(mom.objsToMesh.ToArray(), null, false, true); if (mesh != null) { mom.Apply(); // updateProgressBar("Created mesh saving assets",.6f); if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject) { // mom.BuildSceneMeshObject(); } else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab) { string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset."); return; } string baseName = Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7); string newFilename = folderPath + baseName + "-mesh"; mom.SaveMeshsToAssetDatabase(folderPath, newFilename); if (mom.renderType == MB_RenderType.skinnedMeshRenderer) { Debug.LogWarning("Render type is skinned mesh renderer. " + "Can't create prefab until all bones have been added to the combined mesh object " + mom.resultPrefab + " Add the bones then drag the combined mesh object to the prefab."); } else { mom.RebuildPrefab(); } } else { Debug.LogError("Unknown parameter"); } } }
void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject) { if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject) { Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject"); return; } //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>(); if (mom.textureBakeResults == null && tb != null) { Debug.Log("setting results"); mom.textureBakeResults = tb.textureBakeResults; } if (mom.useObjsToMeshFromTexBaker) { if (tb != null) { mom.objsToMesh.Clear(); mom.objsToMesh.AddRange(tb.objsToMesh); } } Mesh mesh; if (!MB_Utility.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly)) { return; } if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab && mom.resultPrefab == null) { Debug.LogError("Need to set the Combined Mesh Prefab field."); return; } mom.ClearMesh(); mesh = mom.AddDeleteGameObjects(mom.objsToMesh.ToArray(), null, false, true); if (mesh != null) { mom.Apply(); updateProgressBar("Created mesh saving assets", .6f); if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject) { // mom.BuildSceneMeshObject(); } else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab) { string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab); if (prefabPth == null || prefabPth.Length == 0) { Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset."); return; } string baseName = System.IO.Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7); string newFilename = folderPath + baseName + "-mesh"; mom.SaveMeshsToAssetDatabase(folderPath, newFilename); mom.RebuildPrefab(); // saveMeshToAssetDatabase(mom, mesh); // if (mom.renderType == MB_RenderType.skinnedMeshRenderer){ // Debug.LogWarning("Prefab will not be updated for skinned mesh. This is because all bones need to be included in the prefab for it to be usefull."); // } else { // rebuildPrefab(mom, mesh); // } } else { Debug.LogError("Unknown parameter"); } } }
void _bakeMeshesInPlace(MB2_MeshBakerCommon mom) { Mesh mesh; //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; if (!MB_Utility.doCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly)) { return; } mom.DestroyMesh(); List <GameObject> objsToMesh = mom.objsToMesh; if (mom.useObjsToMeshFromTexBaker && mom.GetComponent <MB2_TextureBaker>() != null) { objsToMesh = mom.GetComponent <MB2_TextureBaker>().objsToMesh; } GameObject[] objs = new GameObject[1]; List <string> usedNames = new List <string>(); MB_RenderType originalRenderType = mom.renderType; for (int i = 0; i < objsToMesh.Count; i++) { if (objsToMesh[i] == null) { Debug.LogError("The " + i + "th object on the list of objects to combine is 'None'. Use Command-Delete on Mac OS X; Delete or Shift-Delete on Windows to remove this one element."); return; } objs[0] = objsToMesh[i]; Renderer r = MB_Utility.GetRenderer(objsToMesh[i]); if (r is SkinnedMeshRenderer) { mom.renderType = MB_RenderType.skinnedMeshRenderer; } else { mom.renderType = MB_RenderType.meshRenderer; } mesh = mom.AddDeleteGameObjects(objs, null, false); if (mesh != null) { //mom.ApplyAll(); mom.Apply(); Mesh mf = MB_Utility.GetMesh(objs[0]); if (mf != null) { string baseName, folderPath, newFilename; string pth = AssetDatabase.GetAssetPath(mf); if (pth != null && pth.Length != 0) { baseName = System.IO.Path.GetFileNameWithoutExtension(pth) + "_" + objs[0].name + "_MB"; folderPath = System.IO.Path.GetDirectoryName(pth); } else //try to get the name from prefab { pth = AssetDatabase.GetAssetPath(objs[0]); //get prefab name if (pth != null && pth.Length != 0) { baseName = System.IO.Path.GetFileNameWithoutExtension(pth) + "_" + objs[0].name + "_MB"; folderPath = System.IO.Path.GetDirectoryName(pth); } else //save in root { baseName = objs[0].name + "mesh_MB"; folderPath = "Assets"; } } //make name unique newFilename = System.IO.Path.Combine(folderPath, baseName + ".asset"); int j = 0; while (usedNames.Contains(newFilename)) { newFilename = System.IO.Path.Combine(folderPath, baseName + j + ".asset"); j++; } usedNames.Add(newFilename); updateProgressBar("Created mesh saving mesh on " + objs[0].name + " to asset " + newFilename, .6f); if (newFilename != null && newFilename.Length != 0) { Debug.Log("Creating mesh for " + objs[0].name + " with adjusted UVs at: " + newFilename); AssetDatabase.CreateAsset(mesh, newFilename); } else { Debug.LogWarning("Could not save mesh for " + objs[0].name); } } } mom.DestroyMesh(); } mom.renderType = originalRenderType; return; }
public void OnInspectorGUI(MB2_MeshBakerCommon target, System.Type editorWindowType) { DrawGUI(target, editorWindowType); }
public void DrawGUI(MB2_MeshBakerCommon target) { if (meshBaker == null) { _init(target); } meshBaker.Update(); showInstructions = EditorGUILayout.Foldout(showInstructions, "Instructions:"); if (showInstructions) { EditorGUILayout.HelpBox("1. Bake combined material(s).\n\n" + "2. If necessary set the 'Material Bake Results' field.\n\n" + "3. Add scene objects or prefabs to combine or check 'Same As Texture Baker'. For best results these should use the same shader as result material.\n\n" + "4. Select options and 'Bake'.\n\n" + "6. Look at warnings/errors in console. Decide if action needs to be taken.\n\n" + "7. (optional) Disable renderers in source objects.", UnityEditor.MessageType.None); EditorGUILayout.Separator(); } MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon)target; EditorGUILayout.PropertyField(textureBakeResults, textureBakeResultsGUIContent); EditorGUILayout.LabelField("Objects To Be Combined", EditorStyles.boldLabel); if (mom.GetComponent <MB2_TextureBaker>() != null) { EditorGUILayout.PropertyField(useObjsToMeshFromTexBaker, useTextureBakerObjsGUIContent); } else { useObjsToMeshFromTexBaker.boolValue = false; } if (!mom.useObjsToMeshFromTexBaker) { if (GUILayout.Button(openToolsWindowLabelContent)) { MB_MeshBakerEditorWindow mmWin = (MB_MeshBakerEditorWindow)EditorWindow.GetWindow(typeof(MB_MeshBakerEditorWindow)); mmWin.target = (MB2_MeshBakerRoot)target; } EditorGUILayout.PropertyField(objsToMesh, objectsToCombineGUIContent, true); } EditorGUILayout.LabelField("Output", EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(doNorm, doNormGUIContent); EditorGUILayout.PropertyField(doTan, doTanGUIContent); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(doUV, doUVGUIContent); EditorGUILayout.PropertyField(doUV1, doUV1GUIContent); EditorGUILayout.EndHorizontal(); EditorGUILayout.PropertyField(doCol, doColGUIContent); if (mom.lightmapOption == MB2_LightmapOptions.generate_new_UV2_layout) { EditorGUILayout.HelpBox("Generating new lightmap UVs can split vertices which can push the number of vertices over the 64k limit.", MessageType.Warning); } EditorGUILayout.PropertyField(lightmappingOption, lightmappingOptionGUIContent); EditorGUILayout.PropertyField(outputOptions, outputOptoinsGUIContent); EditorGUILayout.PropertyField(renderType, renderTypeGUIContent); if (mom.outputOption == MB2_OutputOptions.bakeIntoSceneObject) { mom.resultSceneObject = (GameObject)EditorGUILayout.ObjectField("Combined Mesh Object", mom.resultSceneObject, typeof(GameObject), true); } else if (mom.outputOption == MB2_OutputOptions.bakeIntoPrefab) { mom.resultPrefab = (GameObject)EditorGUILayout.ObjectField("Combined Mesh Prefab", mom.resultPrefab, typeof(GameObject), true); } if (GUILayout.Button("Bake")) { bake(mom); } string enableRenderersLabel; bool disableRendererInSource = false; if (mom.objsToMesh.Count > 0) { Renderer r = MB_Utility.GetRenderer(mom.objsToMesh[0]); if (r != null && r.enabled) { disableRendererInSource = true; } } if (disableRendererInSource) { enableRenderersLabel = "Disable Renderers On Combined Objects"; } else { enableRenderersLabel = "Enable Renderers On Combined Objects"; } if (GUILayout.Button(enableRenderersLabel)) { mom.EnableDisableSourceObjectRenderers(!disableRendererInSource); } meshBaker.ApplyModifiedProperties(); meshBaker.SetIsDifferentCacheDirty(); }
public static void RebuildPrefab(MB2_MeshBakerCommon mom){ if (MB2_MeshCombiner.EVAL_VERSION) return; if (mom is MB2_MeshBaker){ MB2_MeshBaker mb = (MB2_MeshBaker) mom; GameObject prefabRoot = mom.resultPrefab; GameObject rootGO = (GameObject) PrefabUtility.InstantiatePrefab(prefabRoot); mb.meshCombiner.buildSceneMeshObject(rootGO,mb.meshCombiner.GetMesh(),true); string prefabPth = AssetDatabase.GetAssetPath(prefabRoot); PrefabUtility.ReplacePrefab(rootGO,AssetDatabase.LoadAssetAtPath(prefabPth,typeof(GameObject)),ReplacePrefabOptions.ConnectToPrefab); Editor.DestroyImmediate(rootGO); } else if (mom is MB2_MultiMeshBaker){ MB2_MultiMeshBaker mmb = (MB2_MultiMeshBaker) mom; GameObject prefabRoot = mom.resultPrefab; GameObject rootGO = (GameObject) PrefabUtility.InstantiatePrefab(prefabRoot); for (int i = 0; i < mmb.meshCombiner.meshCombiners.Count; i++){ mmb.meshCombiner.meshCombiners[i].combinedMesh.buildSceneMeshObject(rootGO,mmb.meshCombiner.meshCombiners[i].combinedMesh.GetMesh(),true); } string prefabPth = AssetDatabase.GetAssetPath(prefabRoot); PrefabUtility.ReplacePrefab(rootGO,AssetDatabase.LoadAssetAtPath(prefabPth,typeof(GameObject)),ReplacePrefabOptions.ConnectToPrefab); Editor.DestroyImmediate(rootGO); } else { Debug.LogError("Argument was not a MB2_MeshBaker or an MB2_MultiMeshBaker."); } }
public void OnInspectorGUI(MB2_MeshBakerCommon target, System.Type editorWindowType){ DrawGUI(target, editorWindowType); }
MB_AtlasesAndRects[] _CreateAtlases(ProgressUpdateDelegate progressInfo, bool saveAtlasesAsAssets = false, MB2_EditorMethodsInterface textureFormatTracker = null) { //validation if (saveAtlasesAsAssets && textureFormatTracker == null) { Debug.LogError("Error in CreateAtlases If saveAtlasesAsAssets = true then textureFormatTracker cannot be null."); return(null); } if (saveAtlasesAsAssets && !Application.isEditor) { Debug.LogError("Error in CreateAtlases If saveAtlasesAsAssets = true it must be called from the Unity Editor."); return(null); } if (!doCombinedValidate(this, MB_ObjsToCombineTypes.dontCare, textureFormatTracker)) { return(null); } if (doMultiMaterial && !_ValidateResultMaterials()) { return(null); } else if (!doMultiMaterial) { if (resultMaterial == null) { Debug.LogError("Combined Material is null please create and assign a result material."); return(null); } Shader targShader = resultMaterial.shader; for (int i = 0; i < objsToMesh.Count; i++) { Material[] ms = MB_Utility.GetGOMaterials(objsToMesh[i]); for (int j = 0; j < ms.Length; j++) { Material m = ms[j]; if (m != null && m.shader != targShader) { Debug.LogWarning("Game object " + objsToMesh[i] + " does not use shader " + targShader + " it may not have the required textures. If not 2x2 clear textures will be generated."); } } } } for (int i = 0; i < objsToMesh.Count; i++) { Material[] ms = MB_Utility.GetGOMaterials(objsToMesh[i]); for (int j = 0; j < ms.Length; j++) { Material m = ms[j]; if (m == null) { Debug.LogError("Game object " + objsToMesh[i] + " has a null material. Can't build atlases"); return(null); } } } MB_TextureCombiner combiner = new MB_TextureCombiner(); // if editor analyse meshes and suggest treatment if (!Application.isPlaying) { Material[] rms; if (doMultiMaterial) { rms = new Material[resultMaterials.Length]; for (int i = 0; i < rms.Length; i++) { rms[i] = resultMaterials[i].combinedMaterial; } } else { rms = new Material[1]; rms[0] = resultMaterial; } combiner.SuggestTreatment(objsToMesh, rms, customShaderPropNames); } //initialize structure to store results int numResults = 1; if (doMultiMaterial) { numResults = resultMaterials.Length; } MB_AtlasesAndRects[] resultAtlasesAndRects = new MB_AtlasesAndRects[numResults]; for (int i = 0; i < resultAtlasesAndRects.Length; i++) { resultAtlasesAndRects[i] = new MB_AtlasesAndRects(); } //Do the material combining. for (int i = 0; i < resultAtlasesAndRects.Length; i++) { Material resMatToPass = null; List <Material> sourceMats = null; if (doMultiMaterial) { sourceMats = resultMaterials[i].sourceMaterials; resMatToPass = resultMaterials[i].combinedMaterial; } else { resMatToPass = resultMaterial; } Debug.Log("Creating atlases for result material " + resMatToPass); if (!combiner.combineTexturesIntoAtlases(progressInfo, resultAtlasesAndRects[i], resMatToPass, objsToMesh, sourceMats, atlasPadding, customShaderPropNames, resizePowerOfTwoTextures, fixOutOfBoundsUVs, maxTilingBakeSize, saveAtlasesAsAssets, texturePackingAlgorithm, textureFormatTracker)) { return(null); } } //Save the results textureBakeResults.combinedMaterialInfo = resultAtlasesAndRects; textureBakeResults.doMultiMaterial = doMultiMaterial; textureBakeResults.resultMaterial = resultMaterial; textureBakeResults.resultMaterials = resultMaterials; textureBakeResults.fixOutOfBoundsUVs = fixOutOfBoundsUVs; unpackMat2RectMap(textureBakeResults); //originally did all the assign of atlases to the result materials here //don't touch result material assets until we are sure atlas creation worked. //unfortunatly Unity has a bug where it Destroys Texture2Ds without warning when memory gets low and generates MissingReferenceException //so if generating assets in editor then save and assign atlases as soon as each atlas is created // if (Application.isPlaying){ // if (doMultiMaterial){ // for (int j = 0; j < resultMaterials.Length; j++){ // Material resMat = resultMaterials[j].combinedMaterial; //resultMaterials[j].combinedMaterial; // Texture2D[] atlases = resultAtlasesAndRects[j].atlases; // for(int i = 0; i < atlases.Length;i++){ // resMat.SetTexture(resultAtlasesAndRects[j].texPropertyNames[i], atlases[i]); // } // } // } else { // Material resMat = resultMaterial; //resultMaterials[j].combinedMaterial; // Texture2D[] atlases = resultAtlasesAndRects[0].atlases; // for(int i = 0; i < atlases.Length;i++){ // resMat.SetTexture(resultAtlasesAndRects[0].texPropertyNames[i], atlases[i]); // } // } // } //set the texture bake resultAtlasesAndRects on the Mesh Baker component if it exists MB2_MeshBakerCommon mb = GetComponent <MB2_MeshBakerCommon>(); if (mb != null) { mb.textureBakeResults = textureBakeResults; } if (VERBOSE) { Debug.Log("Created Atlases"); } return(resultAtlasesAndRects); }
public void DrawGUI(MB2_MeshBakerCommon target, System.Type editorWindowType){ if (meshBaker == null){ _init(target); } meshBaker.Update(); showInstructions = EditorGUILayout.Foldout(showInstructions,"Instructions:"); if (showInstructions){ EditorGUILayout.HelpBox("1. Bake combined material(s).\n\n" + "2. If necessary set the 'Material Bake Results' field.\n\n" + "3. Add scene objects or prefabs to combine or check 'Same As Texture Baker'. For best results these should use the same shader as result material.\n\n" + "4. Select options and 'Bake'.\n\n" + "6. Look at warnings/errors in console. Decide if action needs to be taken.\n\n" + "7. (optional) Disable renderers in source objects.", UnityEditor.MessageType.None); EditorGUILayout.Separator(); } MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; EditorGUILayout.PropertyField(textureBakeResults, textureBakeResultsGUIContent); if (textureBakeResults.objectReferenceValue != null){ showContainsReport = EditorGUILayout.Foldout(showContainsReport, "Shaders & Materials Contained"); if (showContainsReport){ EditorGUILayout.HelpBox(((MB2_TextureBakeResults)textureBakeResults.objectReferenceValue).GetDescription(), MessageType.Info); } } EditorGUILayout.LabelField("Objects To Be Combined",EditorStyles.boldLabel); if (mom.GetComponent<MB2_TextureBaker>() != null){ EditorGUILayout.PropertyField(useObjsToMeshFromTexBaker, useTextureBakerObjsGUIContent); } else { useObjsToMeshFromTexBaker.boolValue = false; } if (!mom.useObjsToMeshFromTexBaker){ if (GUILayout.Button(openToolsWindowLabelContent)){ MB2_MeshBakerEditorWindowInterface mmWin = (MB2_MeshBakerEditorWindowInterface) EditorWindow.GetWindow(editorWindowType); mmWin.target = (MB2_MeshBakerRoot) target; } EditorGUILayout.PropertyField(objsToMesh,objectsToCombineGUIContent, true); } EditorGUILayout.LabelField("Output",EditorStyles.boldLabel); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(doNorm,doNormGUIContent); EditorGUILayout.PropertyField(doTan,doTanGUIContent); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PropertyField(doUV,doUVGUIContent); EditorGUILayout.PropertyField(doUV1,doUV1GUIContent); EditorGUILayout.EndHorizontal(); EditorGUILayout.PropertyField(doCol,doColGUIContent); if (mom.lightmapOption == MB2_LightmapOptions.generate_new_UV2_layout){ EditorGUILayout.HelpBox("Generating new lightmap UVs can split vertices which can push the number of vertices over the 64k limit.",MessageType.Warning); } EditorGUILayout.PropertyField(lightmappingOption,lightmappingOptionGUIContent); EditorGUILayout.PropertyField(outputOptions,outputOptoinsGUIContent); EditorGUILayout.PropertyField(renderType, renderTypeGUIContent); if (mom.outputOption == MB2_OutputOptions.bakeIntoSceneObject){ mom.resultSceneObject = (GameObject) EditorGUILayout.ObjectField("Combined Mesh Object", mom.resultSceneObject, typeof(GameObject), true); } else if (mom.outputOption == MB2_OutputOptions.bakeIntoPrefab){ mom.resultPrefab = (GameObject) EditorGUILayout.ObjectField("Combined Mesh Prefab", mom.resultPrefab, typeof(GameObject), true); } else if (mom.outputOption == MB2_OutputOptions.bakeMeshAssetsInPlace){ if (GUILayout.Button("Choose Folder For Bake In Place Meshes") ){ string newFolder = EditorUtility.SaveFolderPanel("Folder For Bake In Place Meshes", Application.dataPath, ""); if (!newFolder.Contains(Application.dataPath)) Debug.LogWarning("The chosen folder must be in your assets folder."); mom.bakeAssetsInPlaceFolderPath = "Assets" + newFolder.Replace(Application.dataPath, ""); } EditorGUILayout.LabelField("Folder For Meshes: " + mom.bakeAssetsInPlaceFolderPath); } if (GUILayout.Button("Bake")){ bake(mom); } string enableRenderersLabel; bool disableRendererInSource = false; if (mom.objsToMesh.Count > 0){ Renderer r = MB_Utility.GetRenderer(mom.objsToMesh[0]); if (r != null && r.enabled) disableRendererInSource = true; } if (disableRendererInSource){ enableRenderersLabel = "Disable Renderers On Source Objects"; } else { enableRenderersLabel = "Enable Renderers On Source Objects"; } if (GUILayout.Button(enableRenderersLabel)){ mom.EnableDisableSourceObjectRenderers(!disableRendererInSource); } meshBaker.ApplyModifiedProperties(); meshBaker.SetIsDifferentCacheDirty(); }
public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject){ if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab){ Debug.LogError("Cannot BakeIntoPrefab with evaluation version."); return; } if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject){ Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject"); return; } //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target; MB2_TextureBaker tb = mom.GetComponent<MB2_TextureBaker>(); if (mom.textureBakeResults == null && tb != null){ Debug.Log("setting results"); mom.textureBakeResults = tb.textureBakeResults; } if (mom.useObjsToMeshFromTexBaker){ if (tb != null){ mom.GetObjectsToCombine().Clear(); mom.GetObjectsToCombine().AddRange(tb.GetObjectsToCombine()); } } Mesh mesh; if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly, new MB2_EditorMethods())) return; if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab && mom.resultPrefab == null){ Debug.LogError("Need to set the Combined Mesh Prefab field. Create a prefab asset, drag an empty game object into it, and drag it to the 'Combined Mesh Prefab' field."); return; } mom.ClearMesh(); mesh = mom.AddDeleteGameObjects(mom.GetObjectsToCombine().ToArray(),null,false, true); if (mesh != null){ mom.Apply( Unwrapping.GenerateSecondaryUVSet ); Debug.Log(String.Format("Successfully baked {0} meshes into a combined mesh",mom.GetObjectsToCombine().Count)); if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject){ PrefabType pt = PrefabUtility.GetPrefabType(mom.resultSceneObject); if (pt == PrefabType.Prefab || pt == PrefabType.ModelPrefab){ Debug.LogError("Combined Mesh Object is a prefab asset. If output option bakeIntoSceneObject then this must be an instance in the scene."); return; } } else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab){ string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab); if (prefabPth == null || prefabPth.Length == 0){ Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset."); return; } string baseName = Path.GetFileNameWithoutExtension(prefabPth); string folderPath = prefabPth.Substring(0,prefabPth.Length - baseName.Length - 7); string newFilename = folderPath + baseName + "-mesh"; SaveMeshsToAssetDatabase(mom, folderPath,newFilename); if (mom.renderType == MB_RenderType.skinnedMeshRenderer){ Debug.LogWarning("Render type is skinned mesh renderer. " + "Can't create prefab until all bones have been added to the combined mesh object " + mom.resultPrefab + " Add the bones then drag the combined mesh object to the prefab."); } else { RebuildPrefab(mom); } } else { Debug.LogError("Unknown parameter"); } } }