void OnEnable() { graphPaths.Clear(); graphPaths = GraphEditorUtils.GetAllGraphsInProject(); Func<string, GUIContent> selector = (gp => new GUIContent(Path.GetFileNameWithoutExtension(gp), gp)); graphNameOptions = graphPaths.Select(selector).ToArray(); this.titleContent = new GUIContent("Texture Gen"); this.minSize = new Vector2(200.0f, 270.0f); gParams = new GraphParamCollection(); GradientRamp_Colors = new List<Color>() { Color.black, Color.white }; GradientRamp_Times = new List<float>() { 0.0f, 1.0f }; SelectedGraphIndex = 0; if (graphPaths.Count > 0) { Graph g = new Graph(graphPaths[SelectedGraphIndex]); if (g.Load().Length == 0) { gParams = new GraphParamCollection(g); } } }
private Graph TryLoadGraph() { Graph g = new Graph(GraphGUID, true); string err = g.Load(); if (err.Length > 0) { Debug.LogError("Error opening graph " + g.FilePath + ": " + err); return(null); } else { return(g); } }
/// <summary> /// Reloads the graph used by the given RuntimeGraph. /// Returns the loaded graph in case any other code wants to use it. /// </summary> private Graph UpdateGraph(RuntimeGraph graph) { Graph gpuG = null; gpuG = new Graph(graph._GraphFile); string err = gpuG.Load(); if (err.Length > 0) { return(null); } //Generate the shader. graph.GraphShader = GPUGraph.GraphEditorUtils.SaveShader(gpuG, Path.Combine(Application.dataPath, graph._ShaderFile), "Hidden/" + Path.GetFileNameWithoutExtension(graph._ShaderFile), "rgb", 0.0f); if (graph.GraphShader == null) { return(gpuG); } LoadParams(graph, gpuG); //Make sure the material is up-to-date. if (graph._PreviewMat == null) { graph._PreviewMat = new Material(graph.GraphShader); } else { graph._PreviewMat.shader = graph.GraphShader; } //Update the preview of the graph's output. UpdatePreviewTex(graph); return(gpuG); }
void OnGUI() { useRed = GUILayout.Toggle(useRed, "Use Red?"); useGreen = GUILayout.Toggle(useGreen, "Use Green?"); useBlue = GUILayout.Toggle(useBlue, "Use Blue?"); useAlpha = GUILayout.Toggle(useAlpha, "Use Alpha?"); if (!useRed && !useGreen && !useBlue && !useAlpha) { useRed = true; } unusedColor = EditorGUILayout.FloatField("Unused color value", unusedColor); GUILayout.Space(10.0f); int oldIndex = selectedGraphIndex; selectedGraphIndex = EditorGUILayout.Popup(selectedGraphIndex, graphNameOptions); if (oldIndex != selectedGraphIndex) { Graph g = new Graph(graphPaths[selectedGraphIndex]); string err = g.Load(); if (err.Length > 0) { selectedGraphIndex = oldIndex; Debug.LogError("Error reading graph: " + err); } } GUILayout.Space(10.0f); GUILayout.BeginHorizontal(); GUILayout.Label("Shader name:"); shaderName = GUILayout.TextField(shaderName); GUILayout.EndHorizontal(); if (graphPaths.Count > 0) { if (GUILayout.Button("Generate Shader")) { string savePath = EditorUtility.SaveFilePanel("Choose where to save the shader.", Application.dataPath, "MyNoiseShader.shader", "shader"); if (savePath.Length > 0) { Graph g = new Graph(graphPaths[selectedGraphIndex]); if (g.Load().Length == 0) { string outComponents = ""; if (useRed) outComponents += "r"; if (useGreen) outComponents += "g"; if (useBlue) outComponents += "b"; if (useAlpha) outComponents += "a"; GraphEditorUtils.SaveShader(g, savePath, shaderName, outComponents, unusedColor); } } } } else { GUILayout.Space(25.0f); GUILayout.Label("No graph files detected in the project!"); } EditorGUILayout.Space(); }
private Graph TryLoadGraph() { Graph g = new Graph(GraphGUID, true); string err = g.Load(); if (err.Length > 0) { Debug.LogError("Error opening graph " + g.FilePath + ": " + err); return null; } else { return g; } }
void OnGUI() { GUILayout.Space(10.0f); X = EditorGUILayout.IntField("Width", X); Y = EditorGUILayout.IntField("Height", Y); GUILayout.Space(15.0f); GUILayout.Label("Gradient"); GUILayout.BeginHorizontal(); GUILayout.Space(15.0f); GUILayout.BeginVertical(); for (int i = 0; i < GradientRamp_Colors.Count; ++i) { GUILayout.BeginHorizontal(); GradientRamp_Colors[i] = EditorGUILayout.ColorField(GradientRamp_Colors[i]); if (i > 0) GradientRamp_Times[i] = EditorGUILayout.Slider(GradientRamp_Times[i], 0.0f, 1.0f); if (i > 0 && GUILayout.Button("+")) { GradientRamp_Colors.Insert(i, GradientRamp_Colors[i]); GradientRamp_Times.Insert(i, GradientRamp_Times[i] - 0.00000001f); } if (i > 0 && GradientRamp_Colors.Count > 2 && GUILayout.Button("-")) { GradientRamp_Colors.RemoveAt(i); GradientRamp_Times.RemoveAt(i); i -= 1; } GUILayout.EndHorizontal(); if (i > 0 && GradientRamp_Times[i] < GradientRamp_Times[i - 1]) { GradientRamp_Times[i] = GradientRamp_Times[i - 1] + 0.000001f; } else if (i < GradientRamp_Colors.Count - 1 && GradientRamp_Times[i] > GradientRamp_Times[i + 1]) { GradientRamp_Times[i] = GradientRamp_Times[i + 1] - 0.00001f; } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.Space(15.0f); GUILayout.BeginHorizontal(); GUILayout.Label("Graph:"); int oldIndex = SelectedGraphIndex; SelectedGraphIndex = EditorGUILayout.Popup(SelectedGraphIndex, graphNameOptions); if (oldIndex != SelectedGraphIndex) { Graph g = new Graph(graphPaths[SelectedGraphIndex]); if (g.Load().Length == 0) { SelectedGraphIndex = oldIndex; } else { gParams = new GraphParamCollection(g); } } GUILayout.EndHorizontal(); GUILayout.Space(10.0f); //Show some GUI elements for changing the parameters. if (graphPaths.Count > 0) gParams.ParamEditorGUI(); GUILayout.Space(10.0f); //If a graph is selected, display a button to generate the texture. if (graphPaths.Count > 0) { if (GUILayout.Button("Generate Texture")) { string savePath = EditorUtility.SaveFilePanel("Choose where to save the texture.", Application.dataPath, "MyTex.png", "png"); if (savePath.Length > 0) { //Load the graph. Graph g = new Graph(graphPaths[SelectedGraphIndex]); if (g.Load().Length > 0) { return; } //Render the gradient ramp to a texture, // then render the graph's noise to a texture. Gradient grd = new Gradient(); grd.SetKeys(GradientRamp_Colors.Select((c, i) => new GradientColorKey(c, GradientRamp_Times[i])).ToArray(), new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f) }); Texture2D tex = GraphEditorUtils.GenerateToTexture(g, new GraphParamCollection(g, gParams), X, Y, grd); if (tex == null) { return; } //Write out the texture as a PNG. try { File.WriteAllBytes(savePath, tex.EncodeToPNG()); } catch (Exception e) { Debug.LogError("Unable to save texture to file: " + e.Message); } //Now that we're finished, clean up. AssetDatabase.ImportAsset(StringUtils.GetRelativePath(savePath, "Assets")); //Finally, open explorer to show the user the texture. if (Application.platform == RuntimePlatform.WindowsEditor) { System.Diagnostics.Process.Start("explorer.exe", "/select," + StringUtils.FixDirectorySeparators(savePath)); } else if (Application.platform == RuntimePlatform.OSXEditor) { try { System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "open"; proc.StartInfo.Arguments = "-n -R \"" + StringUtils.FixDirectorySeparators(savePath) + "\""; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = false; proc.StartInfo.RedirectStandardOutput = false; proc.ErrorDataReceived += (s, a) => Debug.Log(a.Data); if (proc.Start()) { proc.BeginErrorReadLine(); proc.BeginOutputReadLine(); } else { Debug.LogError("Error opening Finder to show texture file"); } } catch (Exception e) { Debug.LogError("Error opening Finder to show texture file: " + e.Message); } } } } } else { GUILayout.Space(15.0f); GUILayout.Label("No graph files detected in the project!"); } }
private void GUILeftArea() { GUILayout.Space(10.0f); GUILayout.Label("Graphs:"); int oldVal = selectedGraph; selectedGraph = EditorGUILayout.Popup(selectedGraph, graphSelections); if (selectedGraph != oldVal) { if (ConfirmLoseUnsavedChanges()) { Grph = new Graph(GraphPaths[selectedGraph]); string err = Grph.Load(); CamOffset = Grph.OutputPos.position - new Vector2(Mathf.RoundToInt(position.width * 0.5f), Mathf.RoundToInt(position.height * 0.5f)); if (err.Length > 0) { Debug.LogError("Error loading graph: " + err); } } else { selectedGraph = oldVal; } } GUILayout.Space(35.0f); if (GUILayout.Button("New Graph") && ConfirmLoseUnsavedChanges()) { string savePath = EditorUtility.SaveFilePanelInProject("Choose Graph location", "MyGraph.gpug", "gpug", "Choose where to save the graph."); if (savePath != "") { Graph g = new Graph(savePath); string err = g.Save(); if (err.Length > 0) { EditorUtility.DisplayDialog("Error saving new graph", "Error saving graph " + g.FilePath + ": " + err, "OK"); } else { Grph = g; CamOffset = Grph.OutputPos.position - new Vector2(Mathf.RoundToInt(position.width * 0.5f), Mathf.RoundToInt(position.height * 0.5f)); GraphPaths = GraphEditorUtils.GetAllGraphsInProject(); NewNodeOptions = NodeOptionsGenerator.GenerateList(); Func<string, GUIContent> selector = (s => new GUIContent(Path.GetFileNameWithoutExtension(s), s)); graphSelections = GraphPaths.Select(selector).ToArray(); selectedGraph = -1; string toFind = Path.GetFileNameWithoutExtension(Grph.FilePath); for (int i = 0; i < graphSelections.Length; ++i) { if (graphSelections[i].text == toFind) { selectedGraph = i; break; } } UpdatePreview(); } } } GUILayout.Space(30.0f); if (Grph != null && unsavedStr.Length > 0) { if (GUILayout.Button("Save Changes")) { string err = Grph.Save(); if (err.Length > 0) { Debug.LogError("Error saving graph: " + err); } unsavedStr = ""; } if (GUILayout.Button("Discard Changes")) { if (ConfirmLoseUnsavedChanges()) { string err = Grph.Load(); if (err.Length > 0) { Debug.LogError("Unable to reload graph: " + err); } else { UpdatePreview(); } } } } else { //Leave extra space for the buttons to appear once a change is made. GUILayout.Space(42.0f); } GUILayout.Space(35.0f); if (Grph != null) { GUILayout.Label("1D Hash:"); string oldHash = Grph.Hash1; Grph.Hash1 = GUILayout.TextField(Grph.Hash1); if (oldHash != Grph.Hash1) { if (!unsavedStr.Contains("1D hash func")) unsavedStr += "1D hash func, "; if (autoUpdatePreview) UpdatePreview(); } GUILayout.Space(10.0f); GUILayout.Label("2D Hash:"); oldHash = Grph.Hash2; Grph.Hash2 = GUILayout.TextField(Grph.Hash2); if (oldHash != Grph.Hash2) { if (!unsavedStr.Contains("2D hash func")) unsavedStr += "2D hash func, "; if (autoUpdatePreview) UpdatePreview(); } GUILayout.Space(10.0f); GUILayout.Label("3D Hash:"); oldHash = Grph.Hash3; Grph.Hash3 = GUILayout.TextField(Grph.Hash3); if (oldHash != Grph.Hash3) { if (!unsavedStr.Contains("3D hash func")) unsavedStr += "3D hash func, "; if (autoUpdatePreview) UpdatePreview(); } } GUILayout.Space(30.0f); //Noise previewing. if (Grph != null) { bool oldAutoUpdate = autoUpdatePreview; autoUpdatePreview = GUILayout.Toggle(autoUpdatePreview, "Auto-Update Preview"); if (autoUpdatePreview && !oldAutoUpdate) { UpdatePreview(); } if (!autoUpdatePreview) { if (GUILayout.Button("Update Preview")) { UpdatePreview(); } } if (previewNoise != null) { //Flip the image vertically for unity GUI. Rect texR = EditorGUILayout.GetControlRect(GUILayout.Width(previewNoise.width), GUILayout.Height(previewNoise.height)); GUI.DrawTextureWithTexCoords(texR, previewNoise, new Rect(0.0f, 1.0f, 1.0f, -1.0f)); } } //Update the title bar as well. if (Grph == null) { titleContent = new GUIContent("GPUG Editor"); } else if (unsavedStr.Length > 0) { titleContent = new GUIContent("*" + Path.GetFileNameWithoutExtension(Grph.FilePath) + "*"); } else { titleContent = new GUIContent(Path.GetFileNameWithoutExtension(Grph.FilePath)); } }