Example #1
0
            /// <summary>
            /// Does the GUI for this graph.
            /// Returns what changed.
            /// </summary>
            public ChangeTypes DoGUILayout(string label, List <string> graphPaths, GUIContent[] graphOptions)
            {
                var changes = ChangeTypes.None;

                GUILayout.BeginHorizontal();
                GUILayout.Label(label);
                int oldIndex = Index;

                Index = EditorGUILayout.Popup(oldIndex, graphOptions);
                if (oldIndex != Index)
                {
                    Graph g = new Graph(graphPaths[Index]);
                    if (g.Load().Length > 0)
                    {
                        Index = oldIndex;
                    }
                    else
                    {
                        Params  = new GraphParamCollection(g);
                        changes = ChangeTypes.Everything;
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout_TabIn(25.0f);

                if (Params != null && Params.ParamEditorGUI() && changes != ChangeTypes.Everything)
                {
                    changes = ChangeTypes.Params;
                }

                GUILayout_TabOut(25.0f);

                return(changes);
            }
        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 #3
0
        private void OnGUI()
        {
            GUILayout.Space(15.0f);

            //Choose the graph to use.
            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);
                }

                GetPreview(true);
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(10.0f);

            OnGUI_BelowGraphSelection();

            //Choose the color gradient.
            GUILayout.Label("Gradient");
            GUILayout.BeginHorizontal();
            GUILayout.Space(15.0f);
            GUILayout.BeginVertical();
            for (int i = 0; i < GradientRamp_Colors.Count; ++i)
            {
                GUILayout.BeginHorizontal();

                //Edit the color value.
                EditorGUI.BeginChangeCheck();
                GradientRamp_Colors[i] = EditorGUILayout.ColorField(GradientRamp_Colors[i]);
                if (i > 0)
                {
                    GradientRamp_Times[i] = EditorGUILayout.Slider(GradientRamp_Times[i],
                                                                   0.0f, 1.0f);
                }
                if (EditorGUI.EndChangeCheck())
                {
                    GetPreview(true);
                }

                //Button to insert a new element.
                if (i > 0 && GUILayout.Button("+"))
                {
                    GradientRamp_Colors.Insert(i, GradientRamp_Colors[i]);
                    GradientRamp_Times.Insert(i, GradientRamp_Times[i] - 0.00000001f);

                    GetPreview(true);
                }
                //Button to remove this element.
                if (i > 0 && GradientRamp_Colors.Count > 2 && GUILayout.Button("-"))
                {
                    GradientRamp_Colors.RemoveAt(i);
                    GradientRamp_Times.RemoveAt(i);
                    i -= 1;

                    GetPreview(true);
                }
                GUILayout.EndHorizontal();

                //Make sure elements are in order.
                if (i > 0 && GradientRamp_Times[i] < GradientRamp_Times[i - 1])
                {
                    GradientRamp_Times[i] = GradientRamp_Times[i - 1] + 0.000001f;
                    GetPreview(true);
                }
                else if (i < GradientRamp_Colors.Count - 1 &&
                         GradientRamp_Times[i] > GradientRamp_Times[i + 1])
                {
                    GradientRamp_Times[i] = GradientRamp_Times[i + 1] - 0.00001f;
                    GetPreview(true);
                }
            }
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();

            GUILayout.Space(15.0f);

            OnGUI_BelowGradientAboveParams();

            //Edit the graph's parameters.
            GUILayout.Label("Graph Parameters:");
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();
            {
                if (graphPaths.Count > 0 && gParams.ParamEditorGUI())
                {
                    GetPreview(false);
                }
            }
            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.Space(10.0f);

            OnGUI_AboveGenerationButton();

            //Show the preview texture.
            if (previewTex != null)
            {
                //Preview scale slider.
                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label("Preview Scale");

                    //Use a nonlinear scale for the slider.
                    float t       = Mathf.Log10(previewScale);
                    float resultT = GUILayout.HorizontalSlider(t, -2.0f, 2.0f,
                                                               GUILayout.Width(position.width - 40.0f));
                    previewScale = Mathf.Pow(10.0f, resultT);
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                {
                    //Slider for preview UV z.
                    GUILayout.BeginVertical();
                    {
                        float oldZ = previewUVz;

                        GUILayout.Label("Z");
                        previewUVzMax = EditorGUILayout.FloatField(previewUVzMax, GUILayout.Width(20.0f));
                        previewUVz    = GUILayout.VerticalSlider(previewUVz, previewUVzMin, previewUVzMax,
                                                                 GUILayout.Height(previewTex.height * previewScale - (15.0f * 3)));
                        previewUVzMin = EditorGUILayout.FloatField(previewUVzMin, GUILayout.Width(20.0f));

                        if (oldZ != previewUVz)
                        {
                            GetPreview(false);
                        }
                    }
                    GUILayout.EndVertical();

                    Rect texPos = EditorGUILayout.GetControlRect(GUILayout.Width(previewTex.width * previewScale),
                                                                 GUILayout.Height(previewTex.height * previewScale));
                    EditorGUI.DrawPreviewTexture(texPos, previewTex);

                    GUILayout.FlexibleSpace();
                }
                GUILayout.EndHorizontal();
            }

            //If a graph is selected, display a button to generate the texture.
            if (graphPaths.Count > 0)
            {
                if (GUILayout.Button("Generate Texture"))
                {
                    GenerateTexture();
                }
            }
            else
            {
                GUILayout.Space(15.0f);
                GUILayout.Label("No graph files detected in the project!");
            }
        }
        void OnGUI()
        {
            GUILayout.Space(10.0f);

            if (Selection.activeGameObject == null ||
                Selection.activeGameObject.GetComponent <Terrain>() == null)
            {
                EditorGUILayout.LabelField("No terrain is selected in the editor.");
            }
            else
            {
                //Graph selection.
                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(15.0f);

                //Graph params.
                if (graphPaths.Count > 0)
                {
                    gParams.ParamEditorGUI();
                }

                GUILayout.Space(15.0f);

                //Other params.
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Height scale:");
                heightScale = EditorGUILayout.FloatField(heightScale);
                EditorGUILayout.EndHorizontal();

                GUILayout.Space(15.0f);

                //Button to generate heightmap.
                if (graphPaths.Count > 0)
                {
                    if (GUILayout.Button("Generate Heightmap"))
                    {
                        Generate();
                    }
                }
                else
                {
                    GUILayout.Space(15.0f);
                    GUILayout.Label("No graph files detected in the project!");
                }
            }
        }
Example #5
0
        void OnGUI()
        {
            GUILayout.Space(10.0f);
            scrollPos = GUILayout.BeginScrollView(scrollPos);

            if (Selection.activeGameObject == null ||
                Selection.activeGameObject.GetComponent <Terrain>() == null)
            {
                EditorGUILayout.LabelField("No terrain is selected in the editor.");
            }
            else
            {
                //Graph selection.
                GUILayout.BeginHorizontal();
                GUILayout.Label("Graph:");
                int oldIndex = selectedGraphIndex;
                selectedGraphIndex = EditorGUILayout.Popup(selectedGraphIndex, graphNameOptions);
                if (oldIndex != selectedGraphIndex)
                {
                    var newGraph = new Graph(graphPaths[selectedGraphIndex]);
                    var errMsg   = newGraph.Load();
                    if (errMsg.Length > 0)
                    {
                        Debug.LogError(errMsg);
                        selectedGraphIndex = oldIndex;
                    }
                    else
                    {
                        gParams = new GraphParamCollection(newGraph);
                        graph   = newGraph;
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(15.0f);

                //Graph params.
                if (graphPaths.Count > 0)
                {
                    gParams.ParamEditorGUI();
                }

                GUILayout.Space(15.0f);

                //Other params.
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Height scale:");
                heightScale = EditorGUILayout.FloatField(heightScale);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                uvz = EditorGUILayout.Slider("UV.z", uvz, 0, 1);
                EditorGUILayout.EndHorizontal();

                GUILayout.Space(15.0f);

                //Button to generate heightmap.
                if (graphPaths.Count > 0)
                {
                    if (GUILayout.Button("Generate Heightmap"))
                    {
                        Generate();
                    }
                }
                else
                {
                    GUILayout.Space(15.0f);
                    GUILayout.Label("No graph files detected in the project!");
                }

                //Preview texture.
                if (GUILayout.Button("Update Preview"))
                {
                    GeneratePreview();
                }
                if (previewTex != null)
                {
                    GUILayout.Box(previewTex, GUILayout.Width(256), GUILayout.Height(256));
                }
            }

            GUILayout.EndScrollView();
        }