Example #1
0
        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((graph == null ? "" : graph.FilePath));


            //If this graph was deleted, reset the editor.
            if (selectedGraph == -1)
            {
                unsavedStr = "";

                graph = null;

                reconnectingOutput      = -1;
                reconnectingInput       = -2;
                reconnectingInput_Index = 0;

                activeWindowID = -1;
            }

            NewNodeOptions = NodeOptionsGenerator.GenerateList();
        }
        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 void Generate()
        {
            Terrain     terr = Selection.activeGameObject.GetComponent <Terrain>();
            TerrainData dat  = terr.terrainData;
            Graph       g    = new Graph(graphPaths[selectedGraphIndex]);

            string err = g.Load();

            if (err.Length > 0)
            {
                Debug.LogError("Error loading graph: " + err);
                return;
            }

            float[,] heights = GraphEditorUtils.GenerateToArray(g, new GraphParamCollection(g, gParams),
                                                                dat.heightmapWidth,
                                                                dat.heightmapHeight);
            if (heights == null)
            {
                return;
            }

            for (int x = 0; x < heights.GetLength(0); ++x)
            {
                for (int y = 0; y < heights.GetLength(1); ++y)
                {
                    heights[x, y] *= heightScale;
                }
            }
            dat.SetHeights(0, 0, heights);
        }
Example #4
0
        protected virtual void OnEnable()
        {
            graphPaths.Clear();
            graphPaths = GraphEditorUtils.GetAllGraphsInProject();

            Func <string, GUIContent> selector = (gp => new GUIContent(Path.GetFileNameWithoutExtension(gp), gp));

            graphNameOptions = graphPaths.Select(selector).ToArray();

            if (!isLoaded)
            {
                isLoaded = true;
                chosenGraphs.Clear();
                if (graphPaths.Count > 0)
                {
                    var graph = new Graph(graphPaths[0]);
                    if (graph.Load().Length == 0)
                    {
                        chosenGraphs.Add(new GraphSelection()
                        {
                            Index = 0, Params = new GraphParamCollection(graph)
                        });
                    }
                }
            }
        }
Example #5
0
        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("Terrain Gen");
            this.minSize      = new Vector2(200.0f, 250.0f);

            selectedGraphIndex = 0;
            if (graphPaths.Count > 0)
            {
                graph = new Graph(graphPaths[selectedGraphIndex]);
                if (graph.Load().Length == 0)
                {
                    gParams = new GraphParamCollection(graph);
                    GeneratePreview();
                }
                else
                {
                    graph = null;
                }
            }
        }
Example #6
0
        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("Shader Gen");
            this.minSize      = new Vector2(200.0f, 250.0f);

            selectedGraphIndex = 0;
        }
Example #7
0
        private void Generate()
        {
            Terrain     terr = Selection.activeGameObject.GetComponent <Terrain>();
            TerrainData dat  = terr.terrainData;

            float[,] heights = GraphEditorUtils.GenerateToArray(graph, gParams,
                                                                dat.heightmapWidth,
                                                                dat.heightmapHeight);
            if (heights == null)
            {
                return;
            }

            for (int x = 0; x < heights.GetLength(0); ++x)
            {
                for (int y = 0; y < heights.GetLength(1); ++y)
                {
                    heights[x, y] *= heightScale;
                }
            }
            dat.SetHeights(0, 0, heights);
        }
Example #8
0
        /// <summary>
        /// Generates both textures and saves them to the given file paths.
        /// Returns whether the operation succeeded.
        /// </summary>
        private bool GenerateTextures(string texPath, string normalTexPath = null)
        {
            //The base data set should be large enough for both textures.
            int baseDataSizeX = X,
                baseDataSizeY = Y,
                baseDataSizeZ = Z;

            if (GenerateNormals)
            {
                baseDataSizeX = Math.Max(baseDataSizeX, Normals_X);
                baseDataSizeY = Math.Max(baseDataSizeY, Normals_Y);
                baseDataSizeZ = Math.Max(baseDataSizeZ, Normals_Z);
            }

            Graph graph = LoadGraph();
            GraphParamCollection graphParams = GraphParams;

            //Generate the initial data.
            float[,,] baseData = GraphEditorUtils.GenerateToArray(graph, graphParams,
                                                                  baseDataSizeX,
                                                                  baseDataSizeY,
                                                                  baseDataSizeZ);


            //Generate the value texture.

            float[,,] valueTexData = baseData.ResampleFull(Mathf.Lerp, X, Y, Z);
            Gradient colorGradient = MakeGradient();

            Color[] valueTexPixels = new Color[X * Y * Z];

            for (int z = 0; z < Z; ++z)
            {
                for (int y = 0; y < Y; ++y)
                {
                    for (int x = 0; x < X; ++x)
                    {
                        float value = valueTexData[x, y, z];
                        int   index = x +
                                      (y * X) +
                                      (z * X * Y);

                        valueTexPixels[index] = colorGradient.Evaluate(value);
                    }
                }
            }

            Texture3D valueTex = new Texture3D(X, Y, Z, TextureFormat.ARGB32, false);

            valueTex.wrapMode   = Wrapping;
            valueTex.filterMode = Filtering;
            valueTex.SetPixels(valueTexPixels);
            valueTex.Apply(UseMipmaps, !LeaveReadable);
            AssetDatabase.CreateAsset(valueTex, StringUtils.GetRelativePath(texPath, "Assets"));


            //Generate the normals texture.

            if (normalTexPath == null)
            {
                return(true);
            }

            float[,,] normalsValueData = baseData.ResampleFull(Mathf.Lerp,
                                                               Normals_X, Normals_Y, Normals_Z);
            Color[] normalsPixels = new Color[Normals_X * Normals_Y * Normals_Z];

            for (int z = 0; z < Normals_Z; ++z)
            {
                for (int y = 0; y < Normals_Y; ++y)
                {
                    for (int x = 0; x < Normals_X; ++x)
                    {
                        //The first digit is the X, the second is the Y, the third is the Z.
                        float _000, _100, _010, _110, _001, _101, _011, _111;
                        normalsValueData.Sample(x, y, z, true,
                                                out _000, out _100, out _010, out _110,
                                                out _001, out _101, out _011, out _111);

                        Vector3 gradient = new Vector3();
                        gradient.x = (_100 - _000) +
                                     (_110 - _010) +
                                     (_101 - _001) +
                                     (_111 - _011);
                        gradient.y = (_010 - _000) +
                                     (_110 - _100) +
                                     (_011 - _001) +
                                     (_111 - _101);
                        gradient.z = (_001 - _000) +
                                     (_101 - _100) +
                                     (_011 - _010) +
                                     (_111 - _110);

                        //Normalize.
                        if (gradient == Vector3.zero)
                        {
                            gradient = Vector3.up;
                        }
                        else
                        {
                            gradient = gradient.normalized;
                        }

                        //Pack into a color.
                        int index = x + (y * Normals_X) + (z * Normals_X * Normals_Y);
                        normalsPixels[index] = new Color(0.5f + (0.5f * gradient.x),
                                                         0.5f + (0.5f * gradient.y),
                                                         0.5f + (0.5f * gradient.z));
                    }
                }
            }

            Texture3D normalsTex = new Texture3D(Normals_X, Normals_Y, Normals_Z,
                                                 Normals_Format, Normals_UseMipmaps);

            normalsTex.wrapMode   = Normals_Wrapping;
            normalsTex.filterMode = Normals_Filtering;
            normalsTex.SetPixels(normalsPixels);
            normalsTex.Apply(Normals_UseMipmaps, !Normals_LeaveReadable);
            AssetDatabase.CreateAsset(normalsTex, StringUtils.GetRelativePath(normalTexPath, "Assets"));


            return(true);
        }
Example #9
0
        private void GUILeftArea()
        {
            GUILayout.Space(10.0f);

            GUILayout.Label("Graphs:");

            int oldVal = selectedGraph;

            selectedGraph = EditorGUILayout.Popup(selectedGraph, graphSelections);
            if (selectedGraph != oldVal)
            {
                if (ConfirmLoseUnsavedChanges())
                {
                    graph = new Graph(GraphPaths[selectedGraph]);
                    string err = graph.Load();
                    CamOffset = graph.OutputPos.position - new Vector2(Mathf.RoundToInt(position.width * 0.5f),
                                                                       Mathf.RoundToInt(position.height * 0.5f));
                    if (err.Length > 0)
                    {
                        graphParams = new GPUGraph.GraphParamCollection(graph);
                        Debug.LogError("Error loading graph: " + err);
                    }
                    else
                    {
                        graphParams = new GraphParamCollection(graph);
                    }

                    UpdatePreview();
                }
                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
                    {
                        graph     = g;
                        CamOffset = graph.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(graph.FilePath);
                        for (int i = 0; i < graphSelections.Length; ++i)
                        {
                            if (graphSelections[i].text == toFind)
                            {
                                selectedGraph = i;
                                break;
                            }
                        }

                        UpdatePreview();
                    }
                }
            }

            GUILayout.Space(30.0f);

            if (graph != null && unsavedStr.Length > 0)
            {
                if (GUILayout.Button("Save Changes"))
                {
                    string err = graph.Save();
                    if (err.Length > 0)
                    {
                        Debug.LogError("Error saving graph: " + err);
                    }
                    unsavedStr = "";
                }

                if (GUILayout.Button("Discard Changes"))
                {
                    if (ConfirmLoseUnsavedChanges())
                    {
                        string err = graph.Load();
                        if (err.Length > 0)
                        {
                            graphParams = new GPUGraph.GraphParamCollection()
                            {
                                FloatParams = new List <GPUGraph.FloatParamInfo>(),
                                Tex2DParams = new List <GPUGraph.Texture2DParamInfo>(),
                            };
                            Debug.LogError("Unable to reload graph: " + err);
                        }
                        else
                        {
                            graphParams = new GraphParamCollection(graph);
                            UpdatePreview();
                        }
                    }
                }
            }
            else
            {
                //Leave extra space for the buttons to appear once a change is made.
                GUILayout.Space(42.0f);
            }

            GUILayout.Space(35.0f);


            //Noise previewing.
            if (graph != 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));
                    EditorGUI.DrawPreviewTexture(texR, previewNoise);

                    //Draw a slider for the UV.z coordinate.
                    GUILayout.BeginHorizontal();
                    {
                        float oldUVz = uvZ;

                        GUILayout.Label("UV.z coord:");
                        GUILayout.Space(10.0f);
                        GUILayout.Label("0");
                        uvZ    = GUILayout.HorizontalSlider(uvZ, 0.0f, uvZMax, GUILayout.Width(80.0f));
                        uvZMax = EditorGUILayout.FloatField(uvZMax, GUILayout.Width(25.0f));
                        GUILayout.FlexibleSpace();

                        if (oldUVz != uvZ)
                        {
                            UpdatePreview(false);
                        }
                    }
                    GUILayout.EndHorizontal();

                    //Edit parameters.
                    EditorGUI.BeginChangeCheck();
                    for (int i = 0; i < graphParams.FloatParams.Count; ++i)
                    {
                        var param = graphParams.FloatParams[i];

                        GUILayout.BeginHorizontal();
                        GUILayout.Label(param.Name);

                        if (param.IsSlider)
                        {
                            param.DefaultValue =
                                GUILayout.HorizontalSlider(Mathf.Lerp(param.SliderMin, param.SliderMax,
                                                                      param.DefaultValue),
                                                           param.SliderMin, param.SliderMax,
                                                           GUILayout.ExpandWidth(true),
                                                           GUILayout.MinWidth(80.0f));
                            param.DefaultValue = Mathf.InverseLerp(param.SliderMin, param.SliderMax,
                                                                   param.DefaultValue);

                            GUILayout.FlexibleSpace();
                        }
                        else
                        {
                            param.DefaultValue = EditorGUILayout.DelayedFloatField(param.DefaultValue);
                        }

                        GUILayout.EndHorizontal();

                        graphParams.FloatParams[i] = param;
                    }
                    for (int i = 0; i < graphParams.Tex2DParams.Count; ++i)
                    {
                        var param = graphParams.Tex2DParams[i];

                        GUILayout.BeginHorizontal();
                        GUILayout.Label(param.Name);

                        param.DefaultVal = (Texture2D)EditorGUILayout.ObjectField(param.Name,
                                                                                  param.DefaultVal,
                                                                                  typeof(Texture2D),
                                                                                  false);

                        GUILayout.EndHorizontal();

                        graphParams.Tex2DParams[i] = param;
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        UpdatePreview(false);
                    }
                }
            }


            //Update the title bar as well.
            if (graph == null)
            {
                titleContent = new GUIContent("GPUG Editor");
            }
            else if (unsavedStr.Length > 0)
            {
                titleContent = new GUIContent("*" + Path.GetFileNameWithoutExtension(graph.FilePath) + "*");
            }
            else
            {
                titleContent = new GUIContent(Path.GetFileNameWithoutExtension(graph.FilePath));
            }
        }
        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!");
            }
        }
Example #11
0
 private void GeneratePreview()
 {
     previewTex = GraphEditorUtils.GenerateToTexture(graph, gParams, 1024, 1024, uvz,
                                                     "rgb", 1.0f, TextureFormat.RGBAFloat);
 }
Example #12
0
        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();
        }
Example #13
0
 private void UpdatePreview()
 {
     previewNoise = GraphEditorUtils.GenerateToTexture(Grph, new GraphParamCollection(Grph),
                                                       256, 256, "rgb", 1.0f,
                                                       TextureFormat.RGBAFloat);
 }
Example #14
0
        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));
            }
        }