private void Export2D() { Texture2D texture = null; try { string path = EditorUtility.SaveFilePanel("Export Noise To Texture2D", Application.dataPath, "New Noise Texture2D.png", "png"); if (!path.StartsWith(Application.dataPath)) { Debug.LogError("You must specificy a path in your project's Assets folder to export a Noise Texture"); } if (!string.IsNullOrEmpty(path)) { EditorUtility.DisplayProgressBar("Exporting Noise to Texture2D", "Making some noise...", 0.1f); texture = NoiseUtils.BakeToTexture2D(m_noise, dims2D.x, dims2D.y, m_format, TextureCreationFlags.None); byte[] bytes = ImageConversion.EncodeToPNG(texture); System.IO.File.WriteAllBytes(path, bytes); Texture2D.DestroyImmediate(texture); texture = null; string assetPath = path.Remove(0, Application.dataPath.Length - "Assets".Length); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); texture = AssetDatabase.LoadAssetAtPath <Texture2D>(assetPath); EditorGUIUtility.PingObject(texture); } } catch (Exception e) { Debug.LogError(e); if (texture != null) { Texture2D.DestroyImmediate(texture); } Debug.Log("Exception caught"); EditorUtility.ClearProgressBar(); } }
private void Export3D() { Texture3D texture = null; try { string path = EditorUtility.SaveFilePanel("Export Noise To Texture3D", Application.dataPath, "New Noise Texture3D.asset", "asset"); if (!path.StartsWith(Application.dataPath)) { Debug.LogError("You must specificy a path in your project's Assets folder to export a Noise Texture"); } if (!string.IsNullOrEmpty(path) && path.StartsWith(Application.dataPath)) { EditorUtility.DisplayProgressBar("Exporting Noise to Texture3D", "Making some noise...", 0.1f); texture = NoiseUtils.BakeToTexture3D(m_noise, dims3D.x, dims3D.y, dims3D.z, m_format, TextureCreationFlags.None); AssetDatabase.CreateAsset(texture, path.Remove(0, Application.dataPath.Length - "Assets".Length)); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); EditorGUIUtility.PingObject(texture); } } catch (Exception e) { Debug.LogError(e); if (texture != null) { Texture2D.DestroyImmediate(texture); } EditorUtility.ClearProgressBar(); } }
/// <summary> /// Renders an interactive Noise Preview along with tooltip icons and an optional Export button that opens a new ExportNoiseWindow. /// A background image is also rendered behind the preview that takes up the entire width of the EditorWindow currently being drawn. /// </summary> /// <param name = "minSize"> Minimum size for the Preview </param> /// <param name = "showExportButton"> Whether or not to render the Export button </param> public void DrawPreviewTexture(float minSize, ref bool isLocked) { // Draw label with tooltip //GUILayout.Label( Styles.noisePreview ); float padding = 4f; float iconWidth = 40f; int size = (int)Mathf.Min(minSize, EditorGUIUtility.currentViewWidth); Rect currentRect = rect; currentRect.y += EditorGUIUtility.singleLineHeight * 2f; Rect totalRect = new Rect(currentRect.x, currentRect.y, currentRect.width, size); //GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, size + padding * 2); // extra pixels for highlight border Color prev = GUI.color; GUI.color = new Color(.1f, .1f, .1f, 1f); GUI.DrawTexture(totalRect, Texture2D.whiteTexture, ScaleMode.StretchToFill, false); GUI.color = Color.white; // draw info icon // if(totalRect.Contains(Event.current.mousePosition)) { Rect infoIconRect = new Rect(totalRect.x + padding, totalRect.y + padding, iconWidth, iconWidth); GUI.Label(infoIconRect, Styles.infoIcon); // GUI.Label( infoIconRect, Styles.noiseTooltip ); } float buttonWidth = GUI.skin.button.CalcSize(Styles.buttonUnlocked).x; float buttonHeight = EditorGUIUtility.singleLineHeight; Rect lockButtonRect = new Rect(totalRect.x + padding, totalRect.y + padding * 2f + GUI.skin.button.CalcSize(Styles.infoIcon).y, buttonWidth, buttonHeight); if (isLocked) { if (GUI.Button(lockButtonRect, Styles.buttonLocked)) { isLocked = false; } } else { if (GUI.Button(lockButtonRect, Styles.buttonUnlocked)) { isLocked = true; } } m_noisePreviewIsLocked = isLocked; // draw export button //Rect exportRect = new Rect( totalRect.xMax - buttonWidth - padding, totalRect.yMax - buttonHeight - padding, buttonWidth, buttonHeight ); //if(GUI.Button(exportRect, Styles.export)) //{ // serializedNoise.ApplyModifiedProperties(); // serializedNoise.Update(); // ExportNoiseWindow.ShowWindow( serializedNoise.targetObject as NoiseSettings ); //} float safeSpace = Mathf.Max(iconWidth * 2, buttonWidth * 2) + padding * 4; float minWidth = Mathf.Min(size, totalRect.width - safeSpace); Rect previewRect = new Rect(totalRect.x + totalRect.width / 2 - minWidth / 2, totalRect.y + totalRect.height / 2 - minWidth / 2, minWidth, minWidth); EditorGUIUtility.AddCursorRect(previewRect, MouseCursor.Pan); if (previewRect.Contains(Event.current.mousePosition) && !isLocked) { serializedNoise.Update(); HandlePreviewTextureInput(previewRect); serializedNoise.ApplyModifiedProperties(); } if (Event.current.type == EventType.Repaint) { // create preview RT here and keep until the next Repaint if (m_previewRT != null) { RenderTexture.ReleaseTemporary(m_previewRT); } NoiseSettings noiseSettings = serializedNoise.targetObject as NoiseSettings; m_previewRT = RenderTexture.GetTemporary(512, 512, 0, RenderTextureFormat.ARGB32); RenderTexture tempRT = RenderTexture.GetTemporary(512, 512, 0, RenderTextureFormat.RFloat); RenderTexture prevActive = RenderTexture.active; NoiseUtils.Blit2D(noiseSettings, tempRT); NoiseUtils.BlitPreview2D(tempRT, m_previewRT); RenderTexture.active = prevActive; GUI.DrawTexture(previewRect, m_previewRT, ScaleMode.ScaleToFit, false); RenderTexture.ReleaseTemporary(tempRT); } GUI.color = prev; }