@@ -69,11 +69,15 @@ private void OnGUI()
69
69
70
70
Color oldColour = GUI . color ; // Used for setting GUI.color when drawing UI elements
71
71
72
- float contentWidth = Mathf . Min ( this . position . width - 20.0f , ( this . position . height - 100.0f ) * 2.0f ) ;
72
+ float contentWidth = Mathf . Min ( this . position . width , ( this . position . height - 100.0f ) * 2.0f ) ;
73
73
float contentHeight = contentWidth * 0.5f ;
74
74
75
- Rect bgRect = new Rect ( 0.0f , 0.0f , contentWidth , contentHeight ) ;
76
- Rect paletteRect = new Rect ( bgRect . x , bgRect . y + bgRect . height + 20 , contentWidth , 20.0f ) ;
75
+ // Interaction area (slightly larger than the histogram rect)
76
+ Rect interactRect = new Rect ( 0.0f , 0.0f , contentWidth , contentHeight ) ;
77
+ // Histogram rect (histogram view and alpha control points)
78
+ Rect histRect = new Rect ( interactRect . x + 20.0f , interactRect . y + 20.0f , interactRect . width - 40.0f , interactRect . height - 40.0f ) ;
79
+ // Colour palette rect (colour control points)
80
+ Rect paletteRect = new Rect ( histRect . x , histRect . y + histRect . height + 20 , histRect . width , 20.0f ) ;
77
81
78
82
// TODO: Don't do this every frame
79
83
tf . GenerateTexture ( ) ;
@@ -90,23 +94,23 @@ private void OnGUI()
90
94
// Draw histogram
91
95
tfGUIMat . SetTexture ( "_TFTex" , tf . GetTexture ( ) ) ;
92
96
tfGUIMat . SetTexture ( "_HistTex" , histTex ) ;
93
- Graphics . DrawTexture ( bgRect , tf . GetTexture ( ) , tfGUIMat ) ;
97
+ Graphics . DrawTexture ( histRect , tf . GetTexture ( ) , tfGUIMat ) ;
94
98
95
99
// Draw colour palette
96
100
Texture2D tfTexture = tf . GetTexture ( ) ;
97
101
tfPaletteGUIMat . SetTexture ( "_TFTex" , tf . GetTexture ( ) ) ;
98
102
Graphics . DrawTexture ( new Rect ( paletteRect . x , paletteRect . y , paletteRect . width , paletteRect . height ) , tfTexture , tfPaletteGUIMat ) ;
99
103
100
104
// Release selected colour/alpha points if mouse leaves window
101
- if ( movingAlphaPointIndex != - 1 && ! bgRect . Contains ( currentEvent . mousePosition ) )
105
+ if ( movingAlphaPointIndex != - 1 && ! interactRect . Contains ( currentEvent . mousePosition ) )
102
106
movingAlphaPointIndex = - 1 ;
103
- if ( movingColPointIndex != - 1 && ! ( currentEvent . mousePosition . x >= bgRect . x && currentEvent . mousePosition . x <= bgRect . x + bgRect . width ) )
107
+ if ( movingColPointIndex != - 1 && ! ( currentEvent . mousePosition . x >= paletteRect . x && currentEvent . mousePosition . x <= paletteRect . x + paletteRect . width ) )
104
108
movingColPointIndex = - 1 ;
105
109
106
110
// Mouse down => Move or remove selected colour control point
107
111
if ( currentEvent . type == EventType . MouseDown && paletteRect . Contains ( currentEvent . mousePosition ) )
108
112
{
109
- float mousePos = ( currentEvent . mousePosition . x - bgRect . x ) / bgRect . width ;
113
+ float mousePos = ( currentEvent . mousePosition . x - paletteRect . x ) / paletteRect . width ;
110
114
int pointIndex = PickColourControlPoint ( mousePos ) ;
111
115
if ( pointIndex != - 1 )
112
116
{
@@ -130,7 +134,7 @@ private void OnGUI()
130
134
// Mouse down => Move or remove selected alpha control point
131
135
if ( currentEvent . type == EventType . MouseDown )
132
136
{
133
- Vector2 mousePos = new Vector2 ( ( currentEvent . mousePosition . x - bgRect . x ) / bgRect . width , 1.0f - ( currentEvent . mousePosition . y - bgRect . y ) / bgRect . height ) ;
137
+ Vector2 mousePos = new Vector2 ( ( currentEvent . mousePosition . x - histRect . x ) / histRect . width , 1.0f - ( currentEvent . mousePosition . y - histRect . y ) / histRect . height ) ;
134
138
int pointIndex = PickAlphaControlPoint ( mousePos ) ;
135
139
if ( pointIndex != - 1 )
136
140
{
@@ -153,24 +157,24 @@ private void OnGUI()
153
157
if ( movingAlphaPointIndex != - 1 )
154
158
{
155
159
TFAlphaControlPoint alphaPoint = tf . alphaControlPoints [ movingAlphaPointIndex ] ;
156
- alphaPoint . dataValue = Mathf . Clamp ( ( currentEvent . mousePosition . x - bgRect . x ) / bgRect . width , 0.0f , 1.0f ) ;
157
- alphaPoint . alphaValue = Mathf . Clamp ( 1.0f - ( currentEvent . mousePosition . y - bgRect . y ) / bgRect . height , 0.0f , 1.0f ) ;
160
+ alphaPoint . dataValue = Mathf . Clamp ( ( currentEvent . mousePosition . x - histRect . x ) / histRect . width , 0.0f , 1.0f ) ;
161
+ alphaPoint . alphaValue = Mathf . Clamp ( 1.0f - ( currentEvent . mousePosition . y - histRect . y ) / histRect . height , 0.0f , 1.0f ) ;
158
162
tf . alphaControlPoints [ movingAlphaPointIndex ] = alphaPoint ;
159
163
}
160
164
161
165
// Move selected colour control point
162
166
if ( movingColPointIndex != - 1 )
163
167
{
164
168
TFColourControlPoint colPoint = tf . colourControlPoints [ movingColPointIndex ] ;
165
- colPoint . dataValue = Mathf . Clamp ( ( currentEvent . mousePosition . x - bgRect . x ) / bgRect . width , 0.0f , 1.0f ) ;
169
+ colPoint . dataValue = Mathf . Clamp ( ( currentEvent . mousePosition . x - paletteRect . x ) / paletteRect . width , 0.0f , 1.0f ) ;
166
170
tf . colourControlPoints [ movingColPointIndex ] = colPoint ;
167
171
}
168
172
169
173
// Draw colour control points
170
174
for ( int iCol = 0 ; iCol < tf . colourControlPoints . Count ; iCol ++ )
171
175
{
172
176
TFColourControlPoint colPoint = tf . colourControlPoints [ iCol ] ;
173
- Rect ctrlBox = new Rect ( bgRect . x + bgRect . width * colPoint . dataValue , bgRect . y + bgRect . height + 20 , 10 , 20 ) ;
177
+ Rect ctrlBox = new Rect ( histRect . x + histRect . width * colPoint . dataValue , histRect . y + histRect . height + 20 , 10 , 20 ) ;
174
178
GUI . color = Color . red ;
175
179
GUI . skin . box . fontSize = 6 ;
176
180
GUI . Box ( ctrlBox , "*" ) ;
@@ -179,11 +183,13 @@ private void OnGUI()
179
183
// Draw alpha control points
180
184
for ( int iAlpha = 0 ; iAlpha < tf . alphaControlPoints . Count ; iAlpha ++ )
181
185
{
186
+ const int pointSize = 10 ;
182
187
TFAlphaControlPoint alphaPoint = tf . alphaControlPoints [ iAlpha ] ;
183
- Rect ctrlBox = new Rect ( bgRect . x + bgRect . width * alphaPoint . dataValue , bgRect . y + ( 1.0f - alphaPoint . alphaValue ) * bgRect . height , 10 , 10 ) ;
184
- GUI . color = oldColour ;
188
+ Rect ctrlBox = new Rect ( histRect . x + histRect . width * alphaPoint . dataValue - pointSize / 2 , histRect . y + ( 1.0f - alphaPoint . alphaValue ) * histRect . height - pointSize / 2 , pointSize , pointSize ) ;
189
+ GUI . color = Color . red ;
185
190
GUI . skin . box . fontSize = 6 ;
186
191
GUI . Box ( ctrlBox , "*" ) ;
192
+ GUI . color = oldColour ;
187
193
}
188
194
189
195
if ( currentEvent . type == EventType . MouseUp )
@@ -195,23 +201,23 @@ private void OnGUI()
195
201
// Add points
196
202
if ( currentEvent . type == EventType . MouseDown && currentEvent . button == 1 )
197
203
{
198
- if ( bgRect . Contains ( new Vector2 ( currentEvent . mousePosition . x , currentEvent . mousePosition . y ) ) )
199
- tf . alphaControlPoints . Add ( new TFAlphaControlPoint ( Mathf . Clamp ( ( currentEvent . mousePosition . x - bgRect . x ) / bgRect . width , 0.0f , 1.0f ) , Mathf . Clamp ( 1.0f - ( currentEvent . mousePosition . y - bgRect . y ) / bgRect . height , 0.0f , 1.0f ) ) ) ;
204
+ if ( histRect . Contains ( new Vector2 ( currentEvent . mousePosition . x , currentEvent . mousePosition . y ) ) )
205
+ tf . alphaControlPoints . Add ( new TFAlphaControlPoint ( Mathf . Clamp ( ( currentEvent . mousePosition . x - histRect . x ) / histRect . width , 0.0f , 1.0f ) , Mathf . Clamp ( 1.0f - ( currentEvent . mousePosition . y - histRect . y ) / histRect . height , 0.0f , 1.0f ) ) ) ;
200
206
else
201
- tf . colourControlPoints . Add ( new TFColourControlPoint ( Mathf . Clamp ( ( currentEvent . mousePosition . x - bgRect . x ) / bgRect . width , 0.0f , 1.0f ) , Random . ColorHSV ( ) ) ) ;
207
+ tf . colourControlPoints . Add ( new TFColourControlPoint ( Mathf . Clamp ( ( currentEvent . mousePosition . x - histRect . x ) / histRect . width , 0.0f , 1.0f ) , Random . ColorHSV ( ) ) ) ;
202
208
selectedColPointIndex = - 1 ;
203
209
}
204
210
205
211
// Save TF
206
- if ( GUI . Button ( new Rect ( 0.0f , bgRect . y + bgRect . height + 50.0f , 70.0f , 30.0f ) , "Save" ) )
212
+ if ( GUI . Button ( new Rect ( histRect . x , histRect . y + histRect . height + 50.0f , 70.0f , 30.0f ) , "Save" ) )
207
213
{
208
214
string filepath = EditorUtility . SaveFilePanel ( "Save transfer function" , "" , "default.tf" , "tf" ) ;
209
215
if ( filepath != "" )
210
216
TransferFunctionDatabase . SaveTransferFunction ( tf , filepath ) ;
211
217
}
212
218
213
219
// Load TF
214
- if ( GUI . Button ( new Rect ( 75.0f , bgRect . y + bgRect . height + 50.0f , 70.0f , 30.0f ) , "Load" ) )
220
+ if ( GUI . Button ( new Rect ( histRect . x + 75.0f , histRect . y + histRect . height + 50.0f , 70.0f , 30.0f ) , "Load" ) )
215
221
{
216
222
string filepath = EditorUtility . OpenFilePanel ( "Save transfer function" , "" , "tf" ) ;
217
223
if ( filepath != "" )
@@ -222,7 +228,7 @@ private void OnGUI()
222
228
}
223
229
}
224
230
// Clear TF
225
- if ( GUI . Button ( new Rect ( 150.0f , bgRect . y + bgRect . height + 50.0f , 70.0f , 30.0f ) , "Clear" ) )
231
+ if ( GUI . Button ( new Rect ( histRect . x + 150.0f , histRect . y + histRect . height + 50.0f , 70.0f , 30.0f ) , "Clear" ) )
226
232
{
227
233
tf = volRendObject . transferFunction = new TransferFunction ( ) ;
228
234
tf . alphaControlPoints . Add ( new TFAlphaControlPoint ( 0.1f , 0.0f ) ) ;
@@ -235,12 +241,12 @@ private void OnGUI()
235
241
if ( selectedColPointIndex != - 1 )
236
242
{
237
243
TFColourControlPoint colPoint = tf . colourControlPoints [ selectedColPointIndex ] ;
238
- colPoint . colourValue = EditorGUI . ColorField ( new Rect ( 225 , bgRect . y + bgRect . height + 50 , 100.0f , 40.0f ) , colPoint . colourValue ) ;
244
+ colPoint . colourValue = EditorGUI . ColorField ( new Rect ( histRect . x + 225 , histRect . y + histRect . height + 50 , 100.0f , 40.0f ) , colPoint . colourValue ) ;
239
245
tf . colourControlPoints [ selectedColPointIndex ] = colPoint ;
240
246
}
241
247
242
248
GUI . skin . label . wordWrap = false ;
243
- GUI . Label ( new Rect ( 0.0f , bgRect . y + bgRect . height + 85.0f , 700 .0f, 30.0f ) , "Left click to select and move a control point. Right click to add a control point, and ctrl + right click to delete." ) ;
249
+ GUI . Label ( new Rect ( histRect . x , histRect . y + histRect . height + 85.0f , 720 .0f, 30.0f ) , "Left click to select and move a control point. Right click to add a control point, and ctrl + right click to delete." ) ;
244
250
245
251
GUI . color = oldColour ;
246
252
}
0 commit comments