public static void CleanupStaleReferences(BakeSets bakeSets) { bakeSets.m_containers.RemoveAll(delegate(MeshContainer mc) { return(mc == null || string.IsNullOrEmpty(AssetDatabase.GetAssetPath(mc))); }); }
public int BakeFinish(OnFinishedUpdate onUpdate) { VertexBakerLib.Assert(this != null && !m_run, "BakeFinished called but bake is still in process"); string outputPath = BakeData.DataPath; if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } if (m_result != 0) { string error = VertexBakerLib.Instance.GetLastError(); VertexBakerLib.LogError(error); } else if (!m_cancel && m_outBasis0[0] != IntPtr.Zero && m_outBasis1[0] != IntPtr.Zero && m_outBasis2[0] != IntPtr.Zero) { string bakeSetId = BakeData.Instance().GetBakeSettings().SelectedBakeSet.m_settingsId; BakeSets bakeSets = BakeData.Instance().GetBakeSets(); MeshContainer meshContainer = BakeData.Instance().GetMeshContainer(bakeSetId); EditorUtility.SetDirty(meshContainer); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); AssetDatabase.StartAssetEditing(); try { int ptrOffset = 0; int meshOffset = 0; for (int m = 0; m < m_meshes.Count; ++m) { int count = m_vertCounts[m]; int floatCount = count * 3; // the ID used to look up this mesh later string objectId = "" + m_lightBakers[m].GetUniqueId(); m_lightBakers[m].m_currentContainer = meshContainer; Mesh outputMesh = meshContainer.m_list.Find(delegate(Mesh mesh) { if (mesh != null) { return(mesh.name == bakeSetId + "_" + objectId); } return(false); }); if (outputMesh == null) { if (m_lightBakers[m].VertexLighting != null) { // if we are here than the mesh name may have changed, try and remove the stale data string oldName = m_lightBakers[m].VertexLighting.name; Mesh found = meshContainer.m_list.Find(delegate(Mesh mesh) { // remove the old reference if (mesh != null) { return(mesh.name == oldName); } // remove null mesh return(false); }); if (found != null) { GameObject.DestroyImmediate(found, true); } } // if no mesh exists for this target create it here outputMesh = new Mesh(); BakeData.Instance().AddToMeshContainer(meshContainer, outputMesh); //meshContainer.m_list.Add(outputMesh); //// add to the container asset //string outputFileName = bakeSetId + "_lighting"; //AssetDatabase.AddObjectToAsset(outputMesh, outputPath + "/" + outputFileName + ".asset"); } outputMesh.name = bakeSetId + "_" + objectId; // HACK: Work around to make Unity happy. If vertices are not found the additional vertex stream fails //outMeshes[m].vertices = m_meshes[m].sharedMesh.vertices; outputMesh.vertices = m_meshes[m].sharedMesh.vertices; // 3 floats per vector float[] rawData0 = new float[floatCount]; float[] rawData1 = new float[floatCount]; float[] rawData2 = new float[floatCount]; // offset pointer to next mesh IntPtr basis0 = new IntPtr(m_outBasis0[0].ToInt64() + ptrOffset * SIZE_FLOAT); IntPtr basis1 = new IntPtr(m_outBasis1[0].ToInt64() + ptrOffset * SIZE_FLOAT); IntPtr basis2 = new IntPtr(m_outBasis2[0].ToInt64() + ptrOffset * SIZE_FLOAT); ptrOffset += floatCount; // marshal data into float arrays Marshal.Copy(basis0, rawData0, 0, floatCount); Marshal.Copy(basis1, rawData1, 0, floatCount); Marshal.Copy(basis2, rawData2, 0, floatCount); // lists to hold output vectors List <Color> colorList0 = new List <Color>(); colorList0.Resize(count, Color.black); List <Vector3> colorList1 = new List <Vector3>(); colorList1.Resize(count, Vector3.zero); List <Vector3> colorList2 = new List <Vector3>(); colorList2.Resize(count, Vector3.zero); // copy float arrays into mesh data for (int i = 0; i < count; ++i) { int idx = i * 3; colorList0[i] = new Color(rawData0[idx], rawData0[idx + 1], rawData0[idx + 2], 1.0f); colorList1[i] = new Vector3(rawData1[idx], rawData1[idx + 1], rawData1[idx + 2]); colorList2[i] = new Vector3(rawData2[idx], rawData2[idx + 1], rawData2[idx + 2]); } // this offset is target uv sets 1, 2, and 3 for data destination const int uvOffset = 1; outputMesh.SetColors(colorList0); outputMesh.SetUVs(uvOffset + 1, colorList1); outputMesh.SetUVs(uvOffset + 2, colorList2); //outputMesh.UploadMeshData(true); meshOffset += count; EditorUtility.SetDirty(meshContainer); m_meshRenderers[m].additionalVertexStreams = outputMesh; m_lightBakers[m].m_bakeSets = bakeSets; m_lightBakers[m].VertexLighting = outputMesh; m_lightBakers[m].m_bakeId = objectId; EditorUtility.SetDirty(m_lightBakers[m]); onUpdate("Uploading Mesh Data", m_meshCount / (float)m); } // remove any null slots meshContainer.m_list.RemoveAll(delegate(Mesh m) { return(m == null); }); // aggregate containers under one super container int existingIdx = bakeSets.m_containers.FindIndex(delegate(MeshContainer mc) { return(mc.name == meshContainer.name); }); if (existingIdx != -1) { // replace existing entry bakeSets.m_containers[existingIdx] = meshContainer; } else { bakeSets.m_containers.Add(meshContainer); } BakeSetsInspector.CleanupStaleReferences(bakeSets); EditorUtility.SetDirty(bakeSets); AssetDatabase.SaveAssets(); } finally { onUpdate("Uploading Mesh Data", 1f); AssetDatabase.StopAssetEditing(); } } else { VertexBakerLib.LogWarning("Bake completed successfully but there was no output data available"); } // free data FreeContext(true); // since basis memory was allocated in one chunk // freeing this handle frees all basis memory VertexBakerLib.Instance.Free(m_outBasis0[0]); EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); VertexBakerLib.Log("Bake time: " + (DateTime.Now - m_bakeStart).TotalSeconds + " seconds"); while (VertexBakerLib.Instance.GetErrorCount() > 0) { string err = VertexBakerLib.Instance.GetLastError(); VertexBakerLib.LogError(err); } GC.Collect(); return(m_result); }
void OnGUI() { if (SceneManager.GetActiveScene().name == "") { GUILayout.Space(50); EditorGUILayout.HelpBox("Save the scene to begin baking", MessageType.Info); return; } if (!DaydreamRendererImportSettings.BakingEnabledForScene) { GUILayout.Space(50); EditorGUILayout.HelpBox("Enable vertex baking to use Daydream Static Lighting", MessageType.Info); if (GUILayout.Button("Enable Vertex Baking For Scene")) { DaydreamRendererImportSettings.BakingEnabledForScene = true; } return; } if (!s_settingsRestored) { BakeData.Instance().SaveBakeSettings(); s_settingsRestored = true; } if (Event.current.rawType == EventType.MouseUp) { if (s_settingsDirty) { s_settingsDirty = false; BakeData.Instance().SaveBakeSettings(); } } if (s_bakeInProgress) { if (EditorUtility.DisplayCancelableProgressBar("Daydream Baker", "Baking meshes", VertexBakerLib.Instance.BakeProgress())) { VertexBakerLib.Instance.BakeCancel(); if (!s_bakeInProgress) { s_bakeIsFinished = true; } } if (s_bakeIsFinished) { s_bakeIsFinished = false; s_bakeInProgress = false; EditorUtility.ClearProgressBar(); VertexBakerLib.Instance.BakeFinish(delegate(string msg, float complete) { EditorUtility.DisplayProgressBar("Daydream Baker", msg, complete); }); EditorUtility.ClearProgressBar(); // queue up next bake if (s_bakeSetQueue.Count > 0) { int bakeSet = s_bakeSetQueue.Dequeue(); BakeData.Instance().GetBakeSettings().SetBakeSetIndex(bakeSet); BakeScene(); } } } EditorGUI.BeginChangeCheck(); #if DDR_RUNTIME_DLL_LINKING_ if (GUILayout.Button("Reload Library")) { if (VertexBakerLib.Instance.LibLoaded) { VertexBakerLib.Instance.UnloadLib(); } else { VertexBakerLib.Instance.LoadLib(); } } #endif DDRSettings settingsData = BakeData.Instance().GetBakeSettings(); string[] ids = new string[settingsData.m_settingsList.Count]; for (int i = 0, k = settingsData.m_settingsList.Count; i < k; ++i) { ids[i] = settingsData.m_settingsList[i].m_settingsId; } // update selected data GUILayout.Space(20); int settingsIndex = -1; int selected = DrawToolBar(settingsData.GetBakeSetIndex(), ids, true, out settingsIndex); if (selected >= 0) { int cur = settingsData.GetBakeSetIndex(); settingsData.SetBakeSetIndex(selected); if (cur != selected) { DaydreamVertexLighting.UpdateAllVertexLighting(settingsData.SelectedBakeSet.m_settingsId); } } BakeSettings settings = settingsData.SelectedBakeSet; if (selected == Toolbar.kAdd) { if (!BakeSetDialog.m_active) { BakeSetDialog.ShowDialog(delegate(bool result, string name) { if (result) { BakeSettings newSettings = new BakeSettings(name); settingsData.AddBakeSettings(newSettings); EditorUtility.SetDirty(settingsData); } }); } } else if (selected == Toolbar.kRemove) { if (settingsData.m_settingsList.Count > 1) { int current = settingsData.GetBakeSetIndex(); settingsData.RemoveBakeSetting(current); EditorUtility.SetDirty(settingsData); --current; if (current < 0) { current = 0; } settingsData.SetBakeSetIndex(current); DaydreamVertexLighting.UpdateAllVertexLighting(settingsData.SelectedBakeSet.m_settingsId); } } else if (selected == Toolbar.kSettings) { if (!BakeSetSettingsDialog.m_active) { BakeSettings curSettings = settingsData.m_settingsList[settingsIndex]; s_settingsDialog = BakeSetSettingsDialog.ShowDialog(curSettings.m_settingsId, curSettings.m_lightList , curSettings.m_activeSet , curSettings.m_forceAllLights , delegate(BakeSetSettingsDialog.Result result, string bakeSetName, List <LightEntry> selectedLights, bool activeSet, bool forceAllLights) { s_settingsDialog = null; if (settingsData.m_settingsList.Count > 1 && result == BakeSetSettingsDialog.Result.Remove) { settingsData.RemoveBakeSetting(settingsIndex); EditorUtility.SetDirty(settingsData); curSettings = settingsData.m_settingsList[settingsIndex]; } else if (result == BakeSetSettingsDialog.Result.Ok) { curSettings.m_settingsId = bakeSetName; curSettings.m_activeSet = activeSet; curSettings.m_forceAllLights = forceAllLights; if (selectedLights != null) { // remove empty or stale entries var idsInFile = Utilities.LightsByLocalFileId(); selectedLights.RemoveAll(delegate(LightEntry obj){ return(string.IsNullOrEmpty(obj.m_group) && obj.m_idInFile == 0 || (!idsInFile.ContainsKey(obj.m_idInFile) && (string.IsNullOrEmpty(obj.m_group) || GameObject.Find(obj.m_group) == null))); }); curSettings.m_lightList = selectedLights; } EditorUtility.SetDirty(settingsData); } }); } else if (s_settingsDialog != null) { s_settingsDialog.CancelDialog(); } } DrawShadowAndAOSettings(settings, settingsData); //settings.m_diffuseEnergyConservation = EditorGUILayout.Slider("Diffuse Conservation", settings.m_diffuseEnergyConservation, 0f, 1f); EditorGUILayout.LabelField(Styles.m_ambientHeader); EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); Color solidColor = settings.GetColorSolid(); Color gradSky = settings.GetColorGradient(GradientIndex.Sky); Color gradEquator = settings.GetColorGradient(GradientIndex.Equator); Color gradGround = settings.GetColorGradient(GradientIndex.Ground); Color posX = settings.GetColorCubeFace(Face.PosX); Color posY = settings.GetColorCubeFace(Face.PosY); Color posZ = settings.GetColorCubeFace(Face.PosZ); Color negX = settings.GetColorCubeFace(Face.NegX); Color negY = settings.GetColorCubeFace(Face.NegY); Color negZ = settings.GetColorCubeFace(Face.NegZ); settings.m_colorMode = (BakeSettings.AmbientColorMode)EditorGUILayout.EnumPopup(Styles.m_ambientSource, settings.m_colorMode); // Draw color EditorGUILayout.BeginHorizontal(); { //GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); if (settings.m_colorMode == BakeSettings.AmbientColorMode.kColorCube) { // Check for color value change EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); DrawColorCube(posX, negX, posY, negY, posZ, negZ); EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); } else if (settings.m_colorMode == BakeSettings.AmbientColorMode.kColorGradient) { // Check for color value change EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); DrawColorGradient(gradGround, gradEquator, gradSky); EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); Color c = EditorGUILayout.ColorField(Styles.m_ambientColor, solidColor); EditorGUILayout.EndHorizontal(); if (c != solidColor) { settings.SetColorSolid(c); } } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); float max = settings.m_ambientMax; settings.m_ambientMax = EditorGUILayout.Slider(Styles.m_max, settings.m_ambientMax, 0f, 1f); if (!s_maxSliderActive && max != settings.m_ambientMax) { s_control = EditorGUIUtility.hotControl; s_maxSliderActive = true; } if (s_maxSliderActive && s_control != EditorGUIUtility.hotControl) { s_control = -1; s_maxSliderActive = false; } float min = settings.m_ambientMin; settings.m_ambientMin = EditorGUILayout.Slider(Styles.m_min, settings.m_ambientMin, 0f, 1f); if (!s_minSliderActive && min != settings.m_ambientMin) { s_control = EditorGUIUtility.hotControl; s_minSliderActive = true; } if (s_minSliderActive && s_control != EditorGUIUtility.hotControl) { s_control = -1; s_minSliderActive = false; } settings.m_ambientMax = Mathf.Clamp(settings.m_ambientMax, settings.m_ambientMin, 1f); settings.m_ambientMin = Mathf.Clamp(settings.m_ambientMin, 0f, settings.m_ambientMax); if (s_minSliderActive || s_maxSliderActive) { Color a = Color.black; float t = settings.m_ambientMin; if (s_maxSliderActive) { t = settings.m_ambientMax; } if (settings.m_colorMode == BakeSettings.AmbientColorMode.kColorCube) { Color px = Color.Lerp(a, posX, t); Color nx = Color.Lerp(a, negX, t); Color py = Color.Lerp(a, posY, t); Color ny = Color.Lerp(a, negY, t); Color pz = Color.Lerp(a, posZ, t); Color nz = Color.Lerp(a, negZ, t); EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); DrawColorCube(px, nx, py, ny, pz, nz, false); EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); } else if (settings.m_colorMode == BakeSettings.AmbientColorMode.kColorGradient) { Color sky = Color.Lerp(a, gradSky, t); Color equator = Color.Lerp(a, gradEquator, t); Color ground = Color.Lerp(a, gradGround, t); EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); DrawColorGradient(ground, equator, sky, false); EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); } else { Color solid = Color.Lerp(a, solidColor, t); EditorGUILayout.BeginHorizontal(); GUILayout.Space(Styles.kIndent); EditorGUILayout.BeginVertical(); EditorGUILayout.ColorField(solid); EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); GUILayout.Space(50); if (VertexBakerLib.Instance.BakeInProgress()) { if (GUILayout.Button("Cancel")) { VertexBakerLib.Instance.BakeCancel(); } } else { settings.m_bakeAllLightSets = EditorGUILayout.ToggleLeft(Styles.m_bakeAll, settings.m_bakeAllLightSets); if (GUILayout.Button("Bake Scene")) { if (settings.m_bakeAllLightSets) { // enqueue all bake sets for (int i = 0, k = settingsData.m_settingsList.Count; i < k; ++i) { if (settingsData.m_settingsList[i].m_activeSet) { s_bakeSetQueue.Enqueue(i); } } // set the first bake set if (s_bakeSetQueue.Count > 0) { int bakeSet = s_bakeSetQueue.Dequeue(); settingsData.SetBakeSetIndex(bakeSet); } } BakeScene(); } } BakeSets bakeSets = BakeData.Instance().GetBakeSets(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (bakeSets.m_containers.Count > 0 && GUILayout.Button(Styles.m_clearAllBakeData, Styles.m_clearButton)) { if (EditorUtility.DisplayDialog(Styles.kEditorTitle, "Clear all data, are you sure?", "Yes", "No")) { for (int i = 0, k = bakeSets.m_containers.Count; i < k; ++i) { List <Mesh> meshes = bakeSets.m_containers[i].m_list; for (int j = 0; j < meshes.Count; ++j) { DestroyImmediate(meshes[j], true); } } } EditorUtility.SetDirty(bakeSets); AssetDatabase.SaveAssets(); } GUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { s_settingsDirty = true; Undo.RecordObject(settingsData, "SettingsUndo"); //VertexBakerLib.Instance.WriteSettings(); //VertexBakerLib.Instance.SaveSettings(); } }
void OnGUI() { GUILayout.Space(10); m_bakeSetName = EditorGUILayout.TextField(m_bakeSetName); m_forceAllLights = EditorGUILayout.ToggleLeft(Styles.m_bakeAllLights, m_forceAllLights); if (m_groups == null) { CloseDialog(); m_resultCallback(Result.Cancel, m_bakeSetName, null, m_activeSet, m_forceAllLights); } if (!m_forceAllLights) { EditorGUILayout.LabelField(Styles.m_lightGroups); m_scrollPosition = EditorGUILayout.BeginScrollView(m_scrollPosition); { // 'ungrouped' lights under root List <Light> rootLights = null; if (m_groups.TryGetValue(Constants.kRoot, out rootLights)) { for (int i = 0; i < rootLights.Count; ++i) { EditorGUILayout.BeginHorizontal(); GUILayout.Space(10); EditorGUILayout.LabelField(rootLights[i].name, Styles.m_ungroupedStyle); int idInFile = rootLights[i].GetLocalIDinFile(); // search to see if light already exists in the list bool included = m_selectedLights.Exists(delegate(LightEntry obj) { // find by object reference return(obj.m_idInFile == idInFile); }); // Create UI and list for toggle if (EditorGUILayout.Toggle(included)) { // added - toggle was checked and the item was not already selected if (!included) { m_selectedLights.Add(new LightEntry(rootLights[i], null)); } } else { // was included - it was unchecked and was previously selected so it needs to be removed if (included) { m_selectedLights.RemoveAll(delegate(LightEntry obj) { // find by object reference return(obj.m_idInFile == idInFile); }); } } EditorGUILayout.EndHorizontal(); } } // Lights contained under a parent group Dictionary <string, List <Light> > .Enumerator iter = m_groups.GetEnumerator(); while (iter.MoveNext()) { string groupPath = iter.Current.Key; if (groupPath == Constants.kRoot) { continue; } EditorGUILayout.BeginHorizontal(); GUILayout.Space(10); GUIContent groupName = new GUIContent(groupPath, "Lights grouped by parent"); EditorGUILayout.LabelField(groupName, Styles.m_groupedStyle); // search for group path bool included = m_selectedLights.Exists(delegate(LightEntry obj) { return(obj.m_group == groupPath); }); if (EditorGUILayout.Toggle(included)) { // added if (!included) { // add new group path m_selectedLights.Add(new LightEntry(null, groupPath)); } } else { // was included if (included) { int idx = m_selectedLights.FindIndex(delegate(LightEntry obj) { return(obj.m_group == groupPath); }); if (idx >= 0) { m_selectedLights.RemoveAt(idx); } } } EditorGUILayout.EndHorizontal(); } } EditorGUILayout.EndScrollView(); } GUILayout.Space(20); m_activeSet = EditorGUILayout.ToggleLeft(Styles.m_activeBake, m_activeSet); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("OK")) { m_resultCallback(Result.Ok, m_bakeSetName, m_selectedLights, m_activeSet, m_forceAllLights); CloseDialog(); } if (GUILayout.Button("Cancel")) { m_resultCallback(Result.Cancel, m_bakeSetName, m_selectedLights, m_activeSet, m_forceAllLights); CloseDialog(); } EditorGUILayout.EndHorizontal(); BakeSets bakeSets = BakeData.Instance().GetBakeSets(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (bakeSets.m_containers.Count > 0 && GUILayout.Button(Styles.m_clearBakeData, Styles.m_clearButton)) { if (EditorUtility.DisplayDialog(m_bakeSetName, "Clear data, are you sure?", "Yes", "No")) { for (int i = 0, k = bakeSets.m_containers.Count; i < k; ++i) { if (bakeSets.m_containers[i].m_bakeSetId == m_bakeSetName) { List <Mesh> meshes = bakeSets.m_containers[i].m_list; for (int j = 0; j < meshes.Count; ++j) { DestroyImmediate(meshes[j], true); } break; } } } EditorUtility.SetDirty(bakeSets); AssetDatabase.SaveAssets(); } GUILayout.EndHorizontal(); }