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 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 _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; }
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"); } } }
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 _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"); } } }
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"); } } }
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"); } } }