-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathTransferFunctionEditorWindow.cs
More file actions
135 lines (111 loc) · 5.67 KB
/
Copy pathTransferFunctionEditorWindow.cs
File metadata and controls
135 lines (111 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using UnityEngine;
using UnityEditor;
namespace UnityVolumeRendering
{
public class TransferFunctionEditorWindow : EditorWindow
{
private TransferFunctionInstance tfInstance = null;
private VolumeRenderedObject volRendObject = null;
private TransferFunctionEditor tfEditor = new TransferFunctionEditor();
public static void ShowWindow(VolumeRenderedObject volRendObj)
{
// Close all (if any) 2D TF editor windows
TransferFunction2DEditorWindow[] tf2dWnds = Resources.FindObjectsOfTypeAll<TransferFunction2DEditorWindow>();
foreach (TransferFunction2DEditorWindow tf2dWnd in tf2dWnds)
tf2dWnd.Close();
TransferFunctionEditorWindow wnd = (TransferFunctionEditorWindow)EditorWindow.GetWindow(typeof(TransferFunctionEditorWindow));
if (volRendObj)
wnd.volRendObject = volRendObj;
wnd.Show();
wnd.SetInitialPosition();
}
private void SetInitialPosition()
{
Rect rect = this.position;
rect.width = 800.0f;
rect.height = 500.0f;
this.position = rect;
}
private void OnEnable()
{
tfEditor.Initialise();
}
private void OnGUI()
{
wantsMouseEnterLeaveWindow = true;
// Update selected object
if (volRendObject == null)
volRendObject = SelectionHelper.GetSelectedVolumeObject();
if (volRendObject == null)
return;
tfInstance = volRendObject.transferFunctionInstance;
TransferFunction tf = tfInstance.transferFunction;
Event currentEvent = new Event(Event.current);
Color oldColour = GUI.color; // Used for setting GUI.color when drawing UI elements
float contentWidth = Mathf.Min(this.position.width, (this.position.height - 100.0f) * 2.0f);
float contentHeight = contentWidth * 0.5f;
// Interaction area (slightly larger than the histogram rect)
Rect outerRect = new Rect(0.0f, 0.0f, contentWidth, contentHeight);
Rect tfEditorRect = new Rect(outerRect.x + 20.0f, outerRect.y + 20.0f, outerRect.width - 40.0f, outerRect.height - 50.0f);
tfEditor.SetTransferFunctionInstnace(tfInstance);
tfEditor.DrawOnGUI(tfEditorRect);
// Save TF
if(GUI.Button(new Rect(tfEditorRect.x, tfEditorRect.y + tfEditorRect.height + 20.0f, 70.0f, 30.0f), "Save"))
{
string filepath = EditorUtility.SaveFilePanel("Save transfer function", "", "default.tf", "tf");
if(filepath != "")
TransferFunctionDatabase.SaveTransferFunction(tf, filepath);
}
// Load TF
if(GUI.Button(new Rect(tfEditorRect.x + 75.0f, tfEditorRect.y + tfEditorRect.height + 20.0f, 70.0f, 30.0f), "Load"))
{
string filepath = EditorUtility.OpenFilePanel("Load transfer function", "", "tf");
if(filepath != "")
{
TransferFunction newTF = TransferFunctionDatabase.LoadTransferFunction(filepath);
if(newTF != null)
{
tf = newTF;
volRendObject.SetTransferFunction(tf);
tfEditor.ClearSelection();
}
}
}
// Clear TF
if(GUI.Button(new Rect(tfEditorRect.x + 150.0f, tfEditorRect.y + tfEditorRect.height + 20.0f, 70.0f, 30.0f), "Clear"))
{
tf = ScriptableObject.CreateInstance<TransferFunction>();
tf.relativeScale = true;
tf.alphaControlPoints.Add(new TFAlphaControlPoint(0.2f, 0.0f));
tf.alphaControlPoints.Add(new TFAlphaControlPoint(0.8f, 1.0f));
tf.colourControlPoints.Add(new TFColourControlPoint(0.5f, new Color(0.469f, 0.354f, 0.223f, 1.0f)));
volRendObject.SetTransferFunction(tf);
tfEditor.ClearSelection();
}
Color? selectedColour = tfEditor.GetSelectedColour();
if (selectedColour != null)
{
// Colour picker
Color newColour = EditorGUI.ColorField(new Rect(tfEditorRect.x + 245, tfEditorRect.y + tfEditorRect.height + 20.0f, 100.0f, 40.0f), selectedColour.Value);
tfEditor.SetSelectedColour(newColour);
// Remove colour
if (GUI.Button(new Rect(tfEditorRect.x + 350.0f, tfEditorRect.y + tfEditorRect.height + 20.0f, 70.0f, 30.0f), "Remove"))
tfEditor.RemoveSelectedColour();
}
GUI.skin.label.wordWrap = false;
GUI.Label(new Rect(tfEditorRect.x, tfEditorRect.y + tfEditorRect.height + 55.0f, 720.0f, 50.0f), "Left click to select and move a control point.\nRight click to add a control point, and ctrl + right click to delete.");
GUI.color = oldColour;
}
private void OnSelectionChange()
{
VolumeRenderedObject newVolRendObj = Selection.activeGameObject?.GetComponent<VolumeRenderedObject>();
// If we selected another volume object than the one previously edited in this GUI
if (volRendObject != null && newVolRendObj != null && newVolRendObj != volRendObject)
this.Close();
}
public void OnInspectorUpdate()
{
Repaint();
}
}
}