/// <summary> /// Updates the preview texture of the graph's noise. /// </summary> private void UpdatePreviewTex(RuntimeGraph graph) { //Make sure the Texture2D is up to date. if (graph._PreviewTex == null) { graph._PreviewTex = new Texture2D(graph._PreviewTexWidth, graph._PreviewTexHeight); } else if (graph._PreviewTex.width != graph._PreviewTexWidth || graph._PreviewTex.height != graph._PreviewTexHeight) { graph._PreviewTex.Resize(graph._PreviewTexWidth, graph._PreviewTexHeight); } graph.UpdateAllParams(); //TODO: Maybe using the instance's private fields like this is the cause of some display bugs? graph.GenerateToTexture(graph._PreviewTex); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { RuntimeGraph graph = (RuntimeGraph)fieldInfo.GetValue(property.serializedObject.targetObject); EditorGUI.BeginProperty(position, label, property); //Do any necessary initialization. if (graph._ShaderFile == null || graph._ShaderFile == "" || !File.Exists(Path.Combine(Application.dataPath, graph._ShaderFile))) { graph._ShaderFile = GetNewShaderFile(); IsEditorNew = true; } if (IsEditorNew) { IsEditorNew = false; RefreshGraphs(graph); } IsFolded = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, oneLine), IsFolded, label); if (IsFolded) { position.x += 20.0f; position.y += oneLine + 10.0f; float viewWidth = position.width; position.width = 50.0f; //If no graphs are available, say so. if (CurrentGraph < 0) { GUI.Label(new Rect(position.x, position.y, 150.0f, oneLine), "No graphs available"); } //Otherwise, allow the user to change selected graphs. else { GUI.Label(new Rect(position.x, position.y, 50.0f, oneLine), "Graph"); int newGraph = EditorGUI.Popup(new Rect(position.x + 45.0f, position.y, 150.0f, oneLine), CurrentGraph, AvailableGraphsGUI); if (CurrentGraph != newGraph) { Undo.RecordObject(property.serializedObject.targetObject, "Inspector"); CurrentGraph = newGraph; graph._GraphFile = AvailableGraphs[CurrentGraph]; UpdateGraph(graph); } } position.y += oneLine + 10.0f; //"Refresh Graphs" button. if (GUI.Button(new Rect(position.x, position.y, 150.0f, oneLine), "Refresh Graphs")) { RefreshGraphs(graph); } //If no graph is selected, stop here. if (CurrentGraph < 0) { EditorGUI.EndProperty(); return; } position.y += oneLine + 10.0f; //Shader path. GUI.Label(new Rect(position.x, position.y, 100.0f, oneLine), "Shader File Path"); string newPath = EditorGUI.TextField(new Rect(position.x + 105.0f, position.y, viewWidth - 200.0f, oneLine), graph._ShaderFile); if (newPath != graph._ShaderFile) { string dirName = Path.GetDirectoryName(graph._ShaderFile); if (!Directory.Exists(Path.Combine(Application.dataPath, dirName))) { Directory.CreateDirectory(Path.Combine(Application.dataPath, dirName)); } AssetDatabase.MoveAsset(graph._ShaderFile, newPath); graph._ShaderFile = newPath; } position.y += oneLine + 20.0f; //Parameters. bool paramsChanged = false; //Float. IsFloatParamListFolded = EditorGUI.Foldout(new Rect(position.x + 15.0f, position.y, position.width, oneLine), IsFloatParamListFolded, "Float Params"); position.y += oneLine; const float paramIndent = 20.0f; if (IsFloatParamListFolded) { for (int i = 0; i < graph.FloatParams.Count; ++i) { GUI.Label(new Rect(position.x + paramIndent, position.y, 100.0f, oneLine), graph.FloatParams[i].Key); float newVal; if (graph._FloatParams[i].Value.IsSlider) { newVal = EditorGUI.Slider(new Rect(position.x + 105.0f + paramIndent, position.y, 50.0f, oneLine), graph.FloatParams[i].Value, graph._FloatParams[i].Value.SliderMin, graph._FloatParams[i].Value.SliderMax); } else { newVal = EditorGUI.FloatField(new Rect(position.x + 105.0f + paramIndent, position.y, 25.0f, oneLine), graph.FloatParams[i].Value); } if (newVal != graph.FloatParams[i].Value) { paramsChanged = true; Undo.RecordObject(property.serializedObject.targetObject, "Inspector"); graph.FloatParams[i].Value = newVal; } position.y += oneLine + 5.0f; } } position.y += 10.0f; //Tex2D. IsTex2DParamListFolded = EditorGUI.Foldout(new Rect(position.x + 15.0f, position.y, position.width, oneLine), IsTex2DParamListFolded, "Tex2D Params"); position.y += oneLine; if (IsTex2DParamListFolded) { for (int i = 0; i < graph.Tex2DParams.Count; ++i) { GUI.Label(new Rect(position.x + paramIndent, position.y, 100.0f, oneLine), graph.Tex2DParams[i].Key); Texture2D newVal = (Texture2D)EditorGUI.ObjectField(new Rect(position.x + 105.0f + paramIndent, position.y, graph.Tex2DParams[i].Value.width, graph.Tex2DParams[i].Value.height), graph.Tex2DParams[i].Value, typeof(Texture2D), true); if (newVal != graph.Tex2DParams[i].Value) { paramsChanged = true; Undo.RecordObject(property.serializedObject.targetObject, "Inspector"); graph.Tex2DParams[i].Value = newVal; } position.y += graph.Tex2DParams[i].Value.height; } } position.y += 20.0f; if (paramsChanged) { graph.UpdateAllParams(); UpdatePreviewTex(graph); } //Preview texture stuff. if (graph._PreviewTex != null) { GUI.Label(new Rect(position.x, position.y, 90.0f, oneLine), "Preview Scale"); float newPreviewTexScale = EditorGUI.Slider(new Rect(position.x + 90.0f, position.y, position.width, oneLine), graph._PreviewTexScale, 0.1f, 10.0f); position.y += oneLine + 10.0f; const float labelWidth = 78.0f, intBoxWidth = 40.0f; GUI.Label(new Rect(position.x, position.y, labelWidth, oneLine), "Preview Size"); int newPreviewTexWidth = Math.Max(1, EditorGUI.IntField(new Rect(position.x + labelWidth + 10.0f, position.y, intBoxWidth, oneLine), graph._PreviewTexWidth)); int newPreviewTexHeight = Math.Max(1, EditorGUI.IntField(new Rect(position.x + labelWidth + intBoxWidth + 20.0f, position.y, intBoxWidth, oneLine), graph._PreviewTexHeight)); if (newPreviewTexScale != graph._PreviewTexScale || newPreviewTexWidth != graph._PreviewTexWidth || newPreviewTexHeight != graph._PreviewTexHeight) { bool updatePreview = (newPreviewTexWidth != graph._PreviewTexWidth || newPreviewTexHeight != graph._PreviewTexHeight); Undo.RecordObject(property.serializedObject.targetObject, "Inspector"); graph._PreviewTexScale = newPreviewTexScale; graph._PreviewTexWidth = newPreviewTexWidth; graph._PreviewTexHeight = newPreviewTexHeight; if (updatePreview) { UpdatePreviewTex(graph); } } position.y += oneLine + 15.0f; EditorGUI.DrawPreviewTexture(new Rect(position.x, position.y, graph._PreviewTex.width * graph._PreviewTexScale, graph._PreviewTex.height * graph._PreviewTexScale), graph._PreviewTex); position.y += graph._PreviewTex.height * graph._PreviewTexScale; } } //Occasionally refresh the texture preview. //This lets the window "react" to undo/redo changes. //Have to do it this way because UnityEditor.Undo is a huge PITA. if (graph._ShaderFile != null && UnityEngine.Random.Range(0.0f, 1.0f) > 0.85f) { UpdatePreviewTex(graph); } EditorGUI.EndProperty(); }