-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathMaterialEditor.cs
More file actions
435 lines (353 loc) · 18.5 KB
/
MaterialEditor.cs
File metadata and controls
435 lines (353 loc) · 18.5 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#pragma warning disable 0618
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ShortcutManagement;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;
using System;
namespace UnityEditor.ProBuilder
{
/// <summary>
/// Assign materials to faces and objects.
/// </summary>
sealed class MaterialEditor : ConfigurableWindow
{
class MaterialShortcutContext : IShortcutContext
{
public bool active
=> EditorWindow.focusedWindow is SceneView
&& instance != null && MeshSelection.selectedObjectCount > 0
&& instance.m_QueuedMaterial.value != null;
}
// Reference to pb_Editor instance.
static ProBuilderEditor editor { get { return ProBuilderEditor.instance; } }
// Reference to the currently open pb_Material_Editor
public static MaterialEditor instance { get; private set; }
const string k_QuickMaterialPath = "Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Quick Material";
[MenuItem(k_QuickMaterialPath, true, PreferenceKeys.menuMaterialColors)]
public static bool VerifyQuickMaterialAction()
{
return ProBuilderEditor.instance != null && MeshSelection.selectedObjectCount > 0 && instance != null && instance.m_QueuedMaterial.value != null;
}
[MenuItem(k_QuickMaterialPath, false, PreferenceKeys.menuMaterialColors)]
public static void ApplyQuickMaterial()
{
ApplyMaterial(MeshSelection.topInternal, instance.m_QueuedMaterial.value);
}
[Shortcut("ProBuilder/Apply Quick Material", typeof(MaterialShortcutContext), KeyCode.Mouse2, defaultShortcutModifiers: ShortcutModifiers.Shift | ShortcutModifiers.Control)]
public static void ApplyQuickMaterialShortcut()
{
ApplyMaterial(MeshSelection.topInternal, instance.m_QueuedMaterial.value);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 1", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 2", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 3", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 4", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 5", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 6", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 7", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 8", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 9", true, PreferenceKeys.menuMaterialColors)]
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 10", true, PreferenceKeys.menuMaterialColors)]
public static bool VerifyMaterialAction()
{
return ProBuilderEditor.instance != null && MeshSelection.selectedObjectCount > 0;
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 1", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial0()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[0]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 2", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial1()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[1]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 3", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial2()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[2]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 4", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial3()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[3]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 5", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial4()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[4]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 6", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial5()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[5]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 7", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial6()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[6]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 8", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial7()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[7]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 9", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial8()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[8]);
}
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset 10", false, PreferenceKeys.menuMaterialColors)]
public static void ApplyMaterial9()
{
ApplyMaterial(MeshSelection.topInternal, CurrentPalette[9]);
}
// Path to the required default material palette. If not valid material palettes are
// found a new one will be created with this path (relative to ProBuilder folder).
static Pref<string> s_MaterialPalettePath = new Pref<string>("editor.materialPalettePath", FileUtility.GetLocalDataDirectory() + "/Default Material Palette.asset");
// The currently loaded material palette asset.
static MaterialPalette s_CurrentPalette = null;
// The user set "quick material"
Pref<Material> m_QueuedMaterial = new Pref<Material>("materialEditor.quickMaterial", null, SettingsScope.User);
// Custom style for material row background
GUIStyle m_RowBackgroundStyle;
// The view scroll position.
Vector2 m_ViewScroll = Vector2.zero;
// All available material palettes
MaterialPalette[] m_AvailablePalettes = null;
// List of string names for all available palettes (plus one entry for 'Add New')
string[] m_AvailablePalettes_Str = null;
// The index of the currently loaded material palette in m_AvailablePalettes
int m_CurrentPaletteIndex = 0;
MaterialShortcutContext m_ShortcutContext;
/// <summary>
/// The currently loaded material palette, or a default.
/// </summary>
public static MaterialPalette CurrentPalette
{
get
{
if (s_CurrentPalette == null)
{
// Attempt to load the last user-set material palette
s_CurrentPalette = AssetDatabase.LoadAssetAtPath<MaterialPalette>(s_MaterialPalettePath);
// If not set (or deleted), fall back on default
if (s_CurrentPalette != null)
return s_CurrentPalette;
// No dice - iterate any other pb_MaterialPalette objects in the project (favoring first found)
s_CurrentPalette = FileUtility.FindAssetOfType<MaterialPalette>();
if (s_CurrentPalette != null)
return s_CurrentPalette;
// If no existing pb_MaterialPalette objects in project:
// - create a new one
// - check for the older pb_ObjectArray and copy data to new default
s_CurrentPalette = FileUtility.LoadRequired<MaterialPalette>(s_MaterialPalettePath);
string[] m_LegacyMaterialArrays = AssetDatabase.FindAssets("t:pb_ObjectArray");
for (int i = 0; m_LegacyMaterialArrays != null && i < m_LegacyMaterialArrays.Length; i++)
{
pb_ObjectArray poa = AssetDatabase.LoadAssetAtPath<pb_ObjectArray>(AssetDatabase.GUIDToAssetPath(m_LegacyMaterialArrays[i]));
// Make sure there's actually something worth copying
if (poa != null && poa.array != null && poa.array.Any(x => x != null && x is Material))
{
s_CurrentPalette.array = poa.GetArray<Material>();
break;
}
}
}
return s_CurrentPalette;
}
}
public static void MenuOpenMaterialEditor()
{
GetWindow<MaterialEditor>("Material Editor");
}
void OnEnable()
{
instance = this;
this.autoRepaintOnSceneChange = true;
this.minSize = new Vector2(236, 250);
m_RowBackgroundStyle = new GUIStyle();
m_RowBackgroundStyle.normal.background = EditorGUIUtility.whiteTexture;
s_CurrentPalette = null;
RefreshAvailablePalettes();
ShortcutManager.RegisterContext(m_ShortcutContext ??= new MaterialShortcutContext());
}
void OnDisable()
{
ShortcutManager.UnregisterContext(m_ShortcutContext);
instance = null;
}
void OnGUI()
{
DoContextMenu();
GUILayout.Label("Quick Material", EditorStyles.boldLabel);
Rect r = GUILayoutUtility.GetLastRect();
int left = (int)position.width - 68;
GUILayout.BeginHorizontal(GUILayout.MaxWidth(position.width - 74), GUILayout.MinHeight(64));
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
m_QueuedMaterial.value = (Material)EditorGUILayout.ObjectField(m_QueuedMaterial.value, typeof(Material), true);
GUILayout.Space(2);
GUI.enabled = editor != null && MeshSelection.selectedFaceCount > 0;
if (GUILayout.Button("Match Selection", GUILayout.MaxWidth(120)))
{
m_QueuedMaterial.SetValue(EditorMaterialUtility.GetActiveSelection());
}
GUI.enabled = true;
GUILayout.EndHorizontal();
var quickMaterialShortcut = string.Empty;
try
{
quickMaterialShortcut = ShortcutManager.instance.GetShortcutBinding("Main Menu/" + k_QuickMaterialPath).ToString();
}
catch (Exception)
{
// Do nothing.
}
var quickMaterialButtonLabel = string.IsNullOrEmpty(quickMaterialShortcut) ? "Apply" : $"Apply ({quickMaterialShortcut})";
if (GUILayout.Button(quickMaterialButtonLabel))
ApplyMaterial(MeshSelection.topInternal, m_QueuedMaterial.value);
GUILayout.EndVertical();
GUI.Box(new Rect(left, r.y + r.height + 2, 64, 64), "");
var previewTexture = EditorMaterialUtility.GetPreviewTexture(m_QueuedMaterial.value);
if (previewTexture != null)
{
GUI.Label(new Rect(left + 2, r.y + r.height + 4, 60, 60), previewTexture);
}
else
{
GUI.Box(new Rect(left + 2, r.y + r.height + 4, 60, 60), "");
GUI.Label(new Rect(left + 2, r.height + 28, 120, 32), "None\n(Texture)");
}
GUILayout.EndHorizontal();
GUILayout.Space(4);
GUI.backgroundColor = PreferenceKeys.proBuilderDarkGray;
UI.EditorGUIUtility.DrawSeparator(2);
GUI.backgroundColor = Color.white;
GUILayout.Label("Material Palette", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
m_CurrentPaletteIndex = EditorGUILayout.Popup("", m_CurrentPaletteIndex, m_AvailablePalettes_Str);
if (EditorGUI.EndChangeCheck())
{
MaterialPalette newPalette = null;
// Add a new material palette
if (m_CurrentPaletteIndex >= m_AvailablePalettes.Length)
{
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/Material Palette.asset");
newPalette = FileUtility.LoadRequired<MaterialPalette>(path);
EditorGUIUtility.PingObject(newPalette);
}
else
{
newPalette = m_AvailablePalettes[m_CurrentPaletteIndex];
}
SetMaterialPalette(newPalette);
}
EditorGUI.BeginChangeCheck();
s_CurrentPalette = (MaterialPalette)EditorGUILayout.ObjectField(s_CurrentPalette, typeof(MaterialPalette), false);
if (EditorGUI.EndChangeCheck())
SetMaterialPalette(s_CurrentPalette);
GUILayout.Space(4);
Material[] materials = CurrentPalette;
m_ViewScroll = GUILayout.BeginScrollView(m_ViewScroll);
for (int i = 0; i < materials.Length; i++)
{
if (i == 10)
{
GUILayout.Space(2);
GUI.backgroundColor = PreferenceKeys.proBuilderLightGray;
UI.EditorGUIUtility.DrawSeparator(1);
GUI.backgroundColor = Color.white;
GUILayout.Space(2);
}
GUILayout.BeginHorizontal();
if (i < 10)
{
var applyMaterialPresetShortcut = string.Empty;
try
{
var shortcutPath = "Main Menu/Tools/" + PreferenceKeys.pluginTitle + "/Materials/Apply Material Preset " + (i + 1);
applyMaterialPresetShortcut = ShortcutManager.instance.GetShortcutBinding(shortcutPath).ToString();
}
catch (Exception)
{
// Do nothing.
}
var applyMaterialPresetButtonLabel = string.IsNullOrEmpty(applyMaterialPresetShortcut)
? $"Apply {i}"
: $"Apply {i} ({applyMaterialPresetShortcut})";
if (GUILayout.Button(applyMaterialPresetButtonLabel, EditorStyles.miniButton, GUILayout.MinWidth(50), GUILayout.MaxWidth(150)))
ApplyMaterial(MeshSelection.topInternal, materials[i]);
}
else
{
if (GUILayout.Button("Apply", EditorStyles.miniButtonLeft, GUILayout.MaxWidth(44)))
ApplyMaterial(MeshSelection.topInternal, materials[i]);
GUI.backgroundColor = Color.red;
if (GUILayout.Button("", EditorStyles.miniButtonRight, GUILayout.MaxWidth(14)))
{
Material[] temp = new Material[materials.Length - 1];
System.Array.Copy(materials, 0, temp, 0, materials.Length - 1);
materials = temp;
SaveUserMaterials(materials);
GUIUtility.ExitGUI();
return;
}
GUI.backgroundColor = Color.white;
}
EditorGUI.BeginChangeCheck();
materials[i] = (Material)EditorGUILayout.ObjectField(materials[i], typeof(Material), false);
if (EditorGUI.EndChangeCheck())
SaveUserMaterials(materials);
GUILayout.EndHorizontal();
}
if (GUILayout.Button("Add"))
{
Material[] temp = new Material[materials.Length + 1];
System.Array.Copy(materials, 0, temp, 0, materials.Length);
materials = temp;
SaveUserMaterials(materials);
}
GUILayout.EndScrollView();
}
static void ApplyMaterial(IEnumerable<ProBuilderMesh> selection, Material mat)
{
if (mat == null)
return;
UndoUtility.RecordComponents<MeshRenderer, ProBuilderMesh>(selection, "Set Material");
foreach (var mesh in selection)
{
var applyPerFace = ProBuilderEditor.instance != null && ProBuilderEditor.selectMode.ContainsFlag(SelectMode.Face) && mesh.faceCount > 0;
mesh.SetMaterial(applyPerFace ? mesh.GetSelectedFaces() : mesh.facesInternal, mat);
InternalMeshUtility.FilterUnusedSubmeshIndexes(mesh);
mesh.Rebuild();
mesh.Optimize();
}
if (ProBuilderEditor.instance != null && MeshSelection.selectedFaceCount > 0)
EditorUtility.ShowNotification("Set Material\n" + mat.name);
}
static void SaveUserMaterials(Material[] materials)
{
s_CurrentPalette.array = materials;
UnityEditor.EditorUtility.SetDirty(s_CurrentPalette);
AssetDatabase.SaveAssets();
}
void SetMaterialPalette(MaterialPalette palette)
{
s_CurrentPalette = palette;
RefreshAvailablePalettes();
}
void RefreshAvailablePalettes()
{
MaterialPalette cur = CurrentPalette;
m_AvailablePalettes = FileUtility.FindAndLoadAssets<MaterialPalette>();
m_AvailablePalettes_Str = m_AvailablePalettes.Select(x => x.name).ToArray();
ArrayUtility.Add<string>(ref m_AvailablePalettes_Str, string.Empty);
ArrayUtility.Add<string>(ref m_AvailablePalettes_Str, "New Material Palette...");
m_CurrentPaletteIndex = System.Array.IndexOf(m_AvailablePalettes, cur);
s_MaterialPalettePath.SetValue(AssetDatabase.GetAssetPath(cur), true);
}
}
}