/// <summary> /// Generates a texture containing the given graph's noise output. /// If this is being called very often, create a permanent render target and material and /// use the other version of this method instead for much better performance. /// If an error occurred, outputs to the Unity debug console and returns "null". /// </summary> /// <param name="outputComponents"> /// The texture output. /// For example, pass "rgb" or "xyz" to output the noise into the red, green, and blue channels /// but not the alpha channel. /// </param> /// <param name="defaultColor"> /// The color (generally 0-1) of the color components which aren't set by the noise. /// </param> public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height, string outputComponents, float defaultColor, TextureFormat format = TextureFormat.RGBAFloat) { //Generate a shader from the graph and have Unity compile it. string shaderPath = Path.Combine(Application.dataPath, "gpuNoiseShaderTemp.shader"); Shader shader = SaveShader(g, shaderPath, "TempGPUNoiseShader", outputComponents, defaultColor); if (shader == null) { return null; } //Render the shader's output into a render texture and copy the data to a Texture2D. RenderTexture target = new RenderTexture(width, height, 16, RenderTextureFormat.ARGBFloat); target.Create(); Texture2D resultTex = new Texture2D(width, height, format, false, true); //Create the material and set its parameters. Material mat = new Material(shader); c.SetParams(mat); GraphUtils.GenerateToTexture(target, mat, resultTex); //Clean up. target.Release(); if (!AssetDatabase.DeleteAsset(StringUtils.GetRelativePath(shaderPath, "Assets"))) { Debug.LogError("Unable to delete temp file: " + shaderPath); } return resultTex; }
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); } } }
/// <summary> /// Gets all parameters from the given collection and recreates them for the given graph. /// </summary> public GraphParamCollection(Graph otherG, GraphParamCollection c) : this(otherG) { //Needed for lambdas. List<FloatParamInfo> myFloatParams = FloatParams; List<Texture2DParamInfo> myTex2DParams = Tex2DParams; for (int i = 0; i < FloatParams.Count; ++i) { int otherPMIndex = c.FloatParams.FindIndex(param2 => param2.Name == myFloatParams[i].Name); if (otherPMIndex == -1) Debug.LogError("Couldn't find an original value for scalar var '" + FloatParams[i].Name + "'"); else FloatParams[i] = new FloatParamInfo(FloatParams[i], c.FloatParams[otherPMIndex].DefaultValue); } for (int i = 0; i < Tex2DParams.Count; ++i) { int otherPMIndex = c.Tex2DParams.FindIndex(param2 => param2.Name == myTex2DParams[i].Name); if (otherPMIndex == -1) Debug.LogError("Couldn't find an original value for Tex2D var '" + Tex2DParams[i].Name + "'"); else Tex2DParams[i] = new Texture2DParamInfo(Tex2DParams[i].Name, c.Tex2DParams[otherPMIndex].DefaultVal); } }
/// <summary> /// Gets all the parameters in the given Graph. /// </summary> public GraphParamCollection(Graph g) { Graph gCopy = g.Clone(); gCopy.PreProcess(); FloatParams = new List<FloatParamInfo>(); Tex2DParams = new List<Texture2DParamInfo>(); foreach (Node n in gCopy.Nodes) { if (n is ParamNode_Float) FloatParams.Add(((ParamNode_Float)n).Param); else if (n is ParamNode_Texture2D) Tex2DParams.Add(((ParamNode_Texture2D)n).Param); } }
/// <summary> /// Returns an expession that evaluates to what this input represents. /// </summary> public string GetExpression(Graph g) { if (IsAConstant) { if (float.IsNaN(ConstantValue)) { return "0.0"; } else { return ConstantValue.ToCodeString(); } } else { return g.GetNode(nodeID).OutputName; } }
/// <summary> /// Generates a 2D grid of noise from the given graph. /// If an error occurred, outputs to the Unity debug console and returns "null". /// </summary> public static float[,] GenerateToArray(Graph g, GraphParamCollection c, int width, int height) { Texture2D t = GenerateToTexture(g, c, width, height, "r", 0.0f); if (t == null) { return null; } Color[] cols = t.GetPixels(); float[,] vals = new float[width, height]; for (int i = 0; i < cols.Length; ++i) { int x = i % width, y = i / width; vals[x, y] = cols[i].r; } return vals; }
public Graph Clone(int idOffset = 0) { Graph g = new Graph(); g.NextUID = NextUID + idOffset; g.FilePath = FilePath; g.Output = Output; g.OutputPos = OutputPos; g.Hash1 = Hash1; g.Hash2 = Hash2; g.Hash3 = Hash3; foreach (Node n in nodes) g.nodes.Add(n.Clone(g, false, idOffset)); foreach (Node n in g.nodes) g.uidToNode.Add(n.UID, n); return 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> /// Saves the shader for the given graph to the given file with the given name. /// Also forces Unity to immediately recognize and compile the shader /// so that it can be immediately used after calling this function. /// If the shader fails to load for some reason, an error is output to the Unity console /// and "null" is returned. /// </summary> /// <param name="gradientRampName">The name of the gradient ramp texture param.</param> public static Shader SaveShader(Graph g, string filePath, string shaderName, string gradientRampName) { string relativePath = StringUtils.GetRelativePath(filePath, "Assets"); try { //Get the shader code. string shad = g.GenerateShader(shaderName, gradientRampName); if (shad == null) { return null; } //Write to the file. DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(filePath)); if (!dir.Exists) dir.Create(); File.WriteAllText(filePath, shad); //Tell Unity to load/compile it. AssetDatabase.ImportAsset(relativePath, ImportAssetOptions.ForceSynchronousImport); } catch (Exception e) { Debug.LogError("Error saving/loading shader to/from file: " + e.Message); } return AssetDatabase.LoadAssetAtPath<Shader>(relativePath); }
void OnEnable() { wantsMouseMove = true; Grph = null; OnFocus(); titleContent = new GUIContent("GPUG Editor"); minSize = new Vector2(OptionsSpace + MinGraphSize.x, MinGraphSize.y); }
/// <summary> /// Updates the given RuntimeGraph's params to be consistent with the given GPUGraph. /// </summary> private void LoadParams(RuntimeGraph gR, Graph gE) { GraphParamCollection paramSet = new GraphParamCollection(gE); //Get all float params for the graph. var newFloatParams = new List <RuntimeGraph._SerializableFloatParamKVP>(); foreach (FloatParamInfo fp in paramSet.FloatParams) { RuntimeGraph._SerializableFloatParamKVP sfp = new RuntimeGraph._SerializableFloatParamKVP(); sfp.Key = fp.Name; sfp.Value = new RuntimeGraph._SerializableFloatParamInfo(); sfp.Value.IsSlider = fp.IsSlider; sfp.Value.SliderMin = fp.SliderMin; sfp.Value.SliderMax = fp.SliderMax; sfp.Value.DefaultValue = fp.DefaultValue; newFloatParams.Add(sfp); } //Remove vestigial params from the RuntimeGraph. for (int i = 0; i < gR.FloatParams.Count; ++i) { if (!newFloatParams.Any((kvp) => kvp.Key == gR.FloatParams[i].Key)) { gR.FloatParams.RemoveAt(i); gR._FloatParams.RemoveAt(i); i -= 1; } } //Add new params to the RuntimeGraph. for (int i = 0; i < newFloatParams.Count; ++i) { if (!gR.FloatParams.Any((kvp) => kvp.Key == newFloatParams[i].Key)) { gR.FloatParams.Add( new FloatParamKVP(newFloatParams[i].Key, (newFloatParams[i].Value.IsSlider ? Mathf.Lerp(newFloatParams[i].Value.SliderMin, newFloatParams[i].Value.SliderMax, newFloatParams[i].Value.DefaultValue) : newFloatParams[i].Value.DefaultValue))); gR._FloatParams.Add(newFloatParams[i]); } } //Get all Tex2D params for the graph var newTex2DParams = new List <RuntimeGraph._SerializableTex2DParamKVP>(); foreach (Texture2DParamInfo tp in paramSet.Tex2DParams) { RuntimeGraph._SerializableTex2DParamKVP stp = new RuntimeGraph._SerializableTex2DParamKVP(); stp.Key = tp.Name; stp.Value = new RuntimeGraph._SerializableTex2DParamInfo(); stp.Value.DefaultValue = tp.DefaultVal; newTex2DParams.Add(stp); } //Remove vestigial params from the RuntimeGraph. for (int i = 0; i < gR.Tex2DParams.Count; ++i) { if (!newTex2DParams.Any((kvp) => kvp.Key == gR.Tex2DParams[i].Key)) { gR.Tex2DParams.RemoveAt(i); gR._Tex2DParams.RemoveAt(i); i -= 1; } } //Add new params to the RuntimeGraph. for (int i = 0; i < newTex2DParams.Count; ++i) { if (!gR.Tex2DParams.Any((kvp) => kvp.Key == newTex2DParams[i].Key)) { gR.Tex2DParams.Add( new Tex2DParamKVP(newTex2DParams[i].Key, newTex2DParams[i].Value.DefaultValue)); gR._Tex2DParams.Add(newTex2DParams[i]); } } }
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!"); } }
void OnFocus() { //Check what graphs are available. GraphPaths = GraphEditorUtils.GetAllGraphsInProject(); Func<string, GUIContent> selector = (s => new GUIContent(Path.GetFileNameWithoutExtension(s), s)); graphSelections = GraphPaths.Select(selector).ToArray(); //Keep the same graph selected even when the list of graphs changes. selectedGraph = GraphPaths.IndexOf((Grph == null ? "" : Grph.FilePath)); //If this graph was deleted, reset the editor. if (selectedGraph == -1) { unsavedStr = ""; Grph = null; reconnectingOutput = -1; reconnectingInput = -2; reconnectingInput_Index = 0; activeWindowID = -1; } NewNodeOptions = NodeOptionsGenerator.GenerateList(); }
/// <summary> /// Generates a new node identical to this one, including the UID, /// but with a different graph owner. /// </summary> public Node Clone(Graph newOwner, bool addToOwner, int idOffset = 0) { Node n = MakeClone(); n.UID = UID + idOffset; n.Owner = newOwner; n.Pos = Pos; n.Inputs = Inputs.ToList(); n.InputNames = InputNames.ToList(); n.InputDefaultVals = InputDefaultVals.ToList(); for (int i = 0; i < n.Inputs.Count; ++i) if (!n.Inputs[i].IsAConstant) n.Inputs[i] = new NodeInput(n.Inputs[i].NodeID + idOffset); if (addToOwner) newOwner.AddNode(this); return n; }
/// <summary> /// Generates a texture containing the given graph's noise output. /// If this is being called very often, create a permanent render target and material and /// use the other version of this method instead for much better performance. /// If an error occurred, outputs to the Unity debug console and returns "null". /// </summary> /// <param name="gradientRampName">The name of the gradient ramp texture param.</param> public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height, Gradient gradientRamp, TextureFormat format = TextureFormat.RGBAFloat) { //Generate a shader from the graph and have Unity compile it. string shaderPath = Path.Combine(Application.dataPath, "gpuNoiseShaderTemp.shader"); Shader shader = SaveShader(g, shaderPath, "TempGPUNoiseShader", "_MyGradientRamp14123"); if (shader == null) { return null; } //Generate a texture from the gradient. Texture2D myRamp = new Texture2D(1024, 1, TextureFormat.RGBA32, false); Color[] cols = new Color[myRamp.width]; for (int i = 0; i < cols.Length; ++i) cols[i] = gradientRamp.Evaluate((float)i / (float)(cols.Length - 1)); myRamp.SetPixels(cols); myRamp.Apply(false, true); //Render the shader's output into a render texture and copy the data to a Texture2D. RenderTexture target = new RenderTexture(width, height, 16, RenderTextureFormat.ARGBFloat); target.Create(); Texture2D resultTex = new Texture2D(width, height, format, false, true); //Create the material and set its parameters. Material mat = new Material(shader); mat.SetTexture("_MyGradientRamp14123", myRamp); c.SetParams(mat); GraphUtils.GenerateToTexture(target, mat, resultTex); //Clean up. target.Release(); if (!AssetDatabase.DeleteAsset(StringUtils.GetRelativePath(shaderPath, "Assets"))) { Debug.LogError("Unable to delete temp file: " + shaderPath); } return resultTex; }
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 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)); } }