/
MaterialEditorGUIHelpers.cs
410 lines (342 loc) · 16.9 KB
/
MaterialEditorGUIHelpers.cs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEngine.Rendering;
using System;
using System.Collections.Generic;
namespace UnityEditor
{
public partial class MaterialEditor
{
public const int kMiniTextureFieldLabelIndentLevel = 2;
const float kSpaceBetweenFlexibleAreaAndField = 5f;
const float kQueuePopupWidth = 100f;
const float kCustomQueuePopupWidth = kQueuePopupWidth + 15f;
private bool isPrefabAsset
{
get
{
if (m_SerializedObject == null || m_SerializedObject.targetObject == null)
return false;
return PrefabUtility.IsPartOfPrefabAsset(m_SerializedObject.targetObject);
}
}
// Field for editing render queue value, with an automatically calculated rect
public void RenderQueueField()
{
Rect r = GetControlRectForSingleLine();
RenderQueueField(r);
}
// Field for editing render queue value, with an explicit rect
public void RenderQueueField(Rect r)
{
BeginProperty(r, MaterialSerializedProperty.CustomRenderQueue, targets);
var mat = targets[0] as Material;
int curRawQueue = mat.rawRenderQueue;
int curDisplayQueue = mat.renderQueue; // this gets final queue value used for rendering, taking shader's queue into account
// Figure out if we're using one of common queues, or a custom one
GUIContent[] queueNames = null;
int[] queueValues = null;
float labelWidth;
// If we use queue value that is not available, lets switch to the custom one
bool useCustomQueue = Array.IndexOf(Styles.queueValues, curRawQueue) < 0;
if (useCustomQueue)
{
// It is a big chance that we already have this custom queue value available
bool updateNewCustomQueueValue = Array.IndexOf(Styles.customQueueNames, curRawQueue) < 0;
if (updateNewCustomQueueValue)
{
int targetQueueIndex = CalculateClosestQueueIndexToValue(curRawQueue);
string targetQueueName = Styles.queueNames[targetQueueIndex].text;
int targetQueueValueOverflow = curRawQueue - Styles.queueValues[targetQueueIndex];
string newQueueName = string.Format(
targetQueueValueOverflow > 0 ? "{0}+{1}" : "{0}{1}",
targetQueueName,
targetQueueValueOverflow);
Styles.customQueueNames[Styles.kCustomQueueIndex].text = newQueueName;
Styles.customQueueValues[Styles.kCustomQueueIndex] = curRawQueue;
}
queueNames = Styles.customQueueNames;
queueValues = Styles.customQueueValues;
labelWidth = kCustomQueuePopupWidth;
}
else
{
queueNames = Styles.queueNames;
queueValues = Styles.queueValues;
labelWidth = kQueuePopupWidth;
}
// We want the custom queue number field to line up with thumbnails & other value fields
// (on the right side), and common queues popup to be on the left of that.
float oldLabelWidth = EditorGUIUtility.labelWidth;
float oldFieldWidth = EditorGUIUtility.fieldWidth;
SetDefaultGUIWidths();
EditorGUIUtility.labelWidth -= labelWidth;
Rect popupRect = r;
popupRect.width -= EditorGUIUtility.fieldWidth + 2;
Rect numberRect = r;
numberRect.xMin = numberRect.xMax - EditorGUIUtility.fieldWidth;
numberRect.height = EditorGUI.kSingleLineHeight;
// Queues popup
int curPopupValue = curRawQueue;
int newPopupValue = EditorGUI.IntPopup(popupRect, Styles.queueLabel, curRawQueue, queueNames, queueValues);
// Custom queue field
int newDisplayQueue = EditorGUI.DelayedIntField(numberRect, curDisplayQueue);
// If popup or custom field changed, set the new queue
if (curPopupValue != newPopupValue || curDisplayQueue != newDisplayQueue)
{
RegisterPropertyChangeUndo("Render Queue");
// Take the value from the number field,
int newQueue = newDisplayQueue;
// But if it's the popup that was changed
if (newPopupValue != curPopupValue)
newQueue = newPopupValue;
newQueue = Mathf.Clamp(newQueue, -1, 5000); // clamp to valid queue ranges
// Change the material queues
foreach (var m in targets)
{
((Material)m).renderQueue = newQueue;
}
}
EditorGUIUtility.labelWidth = oldLabelWidth;
EditorGUIUtility.fieldWidth = oldFieldWidth;
EndProperty();
}
public bool EnableInstancingField()
{
if (!ShaderUtil.HasInstancing(m_Shader))
return false;
Rect r = GetControlRectForSingleLine();
EnableInstancingField(r);
return true;
}
public void EnableInstancingField(Rect r)
{
BeginProperty(r, MaterialSerializedProperty.EnableInstancingVariants, targets);
using (var scope = new EditorGUI.ChangeCheckScope())
{
bool enableInstancing = EditorGUI.Toggle(r, Styles.enableInstancingLabel, (targets[0] as Material).enableInstancing);
if (scope.changed)
{
foreach (Material material in targets)
material.enableInstancing = enableInstancing;
}
}
EndProperty();
}
public bool IsInstancingEnabled()
{
return ShaderUtil.HasInstancing(m_Shader) && (targets[0] as Material).enableInstancing;
}
public bool DoubleSidedGIField()
{
Rect r = GetControlRectForSingleLine();
BeginProperty(r, MaterialSerializedProperty.DoubleSidedGI, targets);
EditorGUI.BeginChangeCheck();
bool doubleSidedGI = EditorGUI.Toggle(r, Styles.doubleSidedGILabel, (targets[0] as Material).doubleSidedGI);
if (EditorGUI.EndChangeCheck())
{
foreach (Material material in targets)
material.doubleSidedGI = doubleSidedGI;
}
EndProperty();
return true;
}
private int CalculateClosestQueueIndexToValue(int requestedValue)
{
int bestCloseByDiff = int.MaxValue;
int result = 1;
for (int i = 1; i < Styles.queueValues.Length; i++)
{
int queueValue = Styles.queueValues[i];
int closeByDiff = Mathf.Abs(queueValue - requestedValue);
if (closeByDiff < bestCloseByDiff)
{
result = i;
bestCloseByDiff = closeByDiff;
}
}
return result;
}
public Rect TexturePropertySingleLine(GUIContent label, MaterialProperty textureProp)
{
return TexturePropertySingleLine(label, textureProp, null, null);
}
public Rect TexturePropertySingleLine(GUIContent label, MaterialProperty textureProp, MaterialProperty extraProperty1)
{
return TexturePropertySingleLine(label, textureProp, extraProperty1, null);
}
// Mini texture slot, with two extra controls on the same line (allocates rect in GUILayout)
// Have up to 3 controls on one line
public Rect TexturePropertySingleLine(GUIContent label, MaterialProperty textureProp, MaterialProperty extraProperty1, MaterialProperty extraProperty2)
{
Rect r = GetControlRectForSingleLine();
bool hasExtraProp = !(extraProperty1 == null && extraProperty2 == null);
if (hasExtraProp) BeginProperty(r, textureProp);
if (extraProperty1 != null) BeginProperty(r, extraProperty1);
if (extraProperty2 != null) BeginProperty(r, extraProperty2);
TexturePropertyMiniThumbnail(r, textureProp, label.text, label.tooltip);
// No extra properties: early out
if (!hasExtraProp)
return r;
// Temporarily reset the indent level as it was already used earlier to compute the positions of the layout items. See issue 946082.
int oldIndentLevel = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// One extra property
if (extraProperty1 == null || extraProperty2 == null)
{
var prop = extraProperty1 ?? extraProperty2;
ExtraPropertyAfterTexture(GetRectAfterLabelWidth(r), prop, false);
}
else // Two extra properties
{
if (extraProperty1.type == MaterialProperty.PropType.Color)
{
ExtraPropertyAfterTexture(GetFlexibleRectBetweenFieldAndRightEdge(r), extraProperty2);
ExtraPropertyAfterTexture(GetLeftAlignedFieldRect(r), extraProperty1);
}
else
{
ExtraPropertyAfterTexture(GetRightAlignedFieldRect(r), extraProperty2);
ExtraPropertyAfterTexture(GetFlexibleRectBetweenLabelAndField(r), extraProperty1);
}
}
// Restore the indent level
EditorGUI.indentLevel = oldIndentLevel;
if (extraProperty2 != null) EndProperty();
if (extraProperty1 != null) EndProperty();
if (hasExtraProp) EndProperty();
return r;
}
[Obsolete("Use TexturePropertyWithHDRColor(GUIContent label, MaterialProperty textureProp, MaterialProperty colorProperty, bool showAlpha)")]
public Rect TexturePropertyWithHDRColor(
GUIContent label, MaterialProperty textureProp, MaterialProperty colorProperty, ColorPickerHDRConfig hdrConfig, bool showAlpha
)
{
return TexturePropertyWithHDRColor(label, textureProp, colorProperty, showAlpha);
}
public Rect TexturePropertyWithHDRColor(GUIContent label, MaterialProperty textureProp, MaterialProperty colorProperty, bool showAlpha)
{
Rect r = GetControlRectForSingleLine();
bool isColorProperty = colorProperty.type == MaterialProperty.PropType.Color;
if (isColorProperty)
{
BeginProperty(r, textureProp);
BeginProperty(r, colorProperty);
}
TexturePropertyMiniThumbnail(r, textureProp, label.text, label.tooltip);
if (!isColorProperty)
{
Debug.LogError("Assuming MaterialProperty.PropType.Color (was " + colorProperty.type + ")");
return r;
}
// Temporarily reset the indent level. See issue 946082.
int oldIndentLevel = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
BeginAnimatedCheck(r, colorProperty);
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = colorProperty.hasMixedValue;
Color newValue = EditorGUI.ColorField(GetRectAfterLabelWidth(r), GUIContent.none, colorProperty.colorValue, true, showAlpha, true);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
colorProperty.colorValue = newValue;
EndAnimatedCheck();
// Restore the indent level
EditorGUI.indentLevel = oldIndentLevel;
if (isColorProperty)
{
EndProperty();
EndProperty();
}
return r;
}
public Rect TexturePropertyTwoLines(GUIContent label, MaterialProperty textureProp, MaterialProperty extraProperty1, GUIContent label2, MaterialProperty extraProperty2)
{
// If not using the second extra property then use the single line version as
// the first extra property is always inlined with the the texture slot
if (extraProperty2 == null)
{
return TexturePropertySingleLine(label, textureProp, extraProperty1);
}
Rect r = GetControlRectForSingleLine();
BeginProperty(r, textureProp);
BeginProperty(r, extraProperty1);
TexturePropertyMiniThumbnail(r, textureProp, label.text, label.tooltip);
// Temporarily reset the indent level. See issue 946082.
int oldIndentLevel = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// First extra control on the same line as the texture
Rect r1 = GetRectAfterLabelWidth(r);
if (extraProperty1.type == MaterialProperty.PropType.Color)
r1 = GetLeftAlignedFieldRect(r);
ExtraPropertyAfterTexture(r1, extraProperty1);
EndProperty();
EndProperty();
// New line for extraProperty2
Rect r2 = GetControlRectForSingleLine();
ShaderProperty(r2, extraProperty2, label2.text, MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1);
// Restore the indent level
EditorGUI.indentLevel = oldIndentLevel;
// Return total rect
r.height += r2.height;
return r;
}
Rect GetControlRectForSingleLine()
{
const float extraSpacing = 2f; // The shader properties needs a little more vertical spacing due to the mini texture field (looks cramped without)
return EditorGUILayout.GetControlRect(true, EditorGUI.kSingleLineHeight + extraSpacing, EditorStyles.layerMaskField);
}
void ExtraPropertyAfterTexture(Rect r, MaterialProperty property, bool adjustLabelWidth = true)
{
if (adjustLabelWidth && (property.type == MaterialProperty.PropType.Float || property.type == MaterialProperty.PropType.Color) && r.width > EditorGUIUtility.fieldWidth)
{
// We want color fields and float fields to have same width as EditorGUIUtility.fieldWidth
// so controls aligns vertically.
// This also makes us able to have a draggable area in front of the float fields. We therefore ensures
// the property has a label (here we use a whitespace) and adjust label width.
float oldLabelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = r.width - EditorGUIUtility.fieldWidth;
ShaderProperty(r, property, " ");
EditorGUIUtility.labelWidth = oldLabelWidth;
return;
}
ShaderProperty(r, property, string.Empty);
}
static public Rect GetRightAlignedFieldRect(Rect r)
{
return new Rect(r.xMax - EditorGUIUtility.fieldWidth, r.y, EditorGUIUtility.fieldWidth, EditorGUIUtility.singleLineHeight);
}
static public Rect GetLeftAlignedFieldRect(Rect r)
{
return new Rect(r.x + EditorGUIUtility.labelWidth, r.y, EditorGUIUtility.fieldWidth, EditorGUIUtility.singleLineHeight);
}
static public Rect GetFlexibleRectBetweenLabelAndField(Rect r)
{
return new Rect(r.x + EditorGUIUtility.labelWidth, r.y, r.width - EditorGUIUtility.labelWidth - EditorGUIUtility.fieldWidth - kSpaceBetweenFlexibleAreaAndField, EditorGUIUtility.singleLineHeight);
}
static public Rect GetFlexibleRectBetweenFieldAndRightEdge(Rect r)
{
Rect r2 = GetRectAfterLabelWidth(r);
r2.xMin += EditorGUIUtility.fieldWidth + kSpaceBetweenFlexibleAreaAndField;
return r2;
}
static public Rect GetRectAfterLabelWidth(Rect r)
{
return new Rect(r.x + EditorGUIUtility.labelWidth, r.y, r.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight);
}
static internal System.Type GetTextureTypeFromDimension(TextureDimension dim)
{
switch (dim)
{
case TextureDimension.Tex2D: return typeof(Texture); // common use case is RenderTextures too, so return base class
case TextureDimension.Cube: return typeof(Cubemap);
case TextureDimension.Tex3D: return typeof(Texture3D);
case TextureDimension.Tex2DArray: return typeof(Texture2DArray);
case TextureDimension.CubeArray: return typeof(CubemapArray);
case TextureDimension.Any: return typeof(Texture);
default: return null; // Unknown, None etc.
}
}
}
} // namespace UnityEditor