/
MaterialPostprocessor.cs
393 lines (334 loc) · 15.9 KB
/
MaterialPostprocessor.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Rendering.Universal.ShaderGUI;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class MaterialModificationProcessor : AssetModificationProcessor
{
static void OnWillCreateAsset(string asset)
{
if (!asset.ToLowerInvariant().EndsWith(".mat"))
{
return;
}
MaterialPostprocessor.s_CreatedAssets.Add(asset);
}
}
class MaterialReimporter : Editor
{
static bool s_NeedToCheckProjSettingExistence = true;
static void ReimportAllMaterials()
{
string[] guids = AssetDatabase.FindAssets("t:material", null);
// There can be several materials subAssets per guid ( ie : FBX files ), remove duplicate guids.
var distinctGuids = guids.Distinct();
int materialIdx = 0;
int totalMaterials = distinctGuids.Count();
foreach (var asset in distinctGuids)
{
materialIdx++;
var path = AssetDatabase.GUIDToAssetPath(asset);
EditorUtility.DisplayProgressBar("Material Upgrader re-import", string.Format("({0} of {1}) {2}", materialIdx, totalMaterials, path), (float)materialIdx / (float)totalMaterials);
AssetDatabase.ImportAsset(path);
}
EditorUtility.ClearProgressBar();
MaterialPostprocessor.s_NeedsSavingAssets = true;
}
[InitializeOnLoadMethod]
static void RegisterUpgraderReimport()
{
EditorApplication.update += () =>
{
if (Time.renderedFrameCount > 0)
{
bool fileExist = true;
// We check the file existence only once to avoid IO operations every frame.
if (s_NeedToCheckProjSettingExistence)
{
fileExist = System.IO.File.Exists(UniversalProjectSettings.filePath);
s_NeedToCheckProjSettingExistence = false;
}
//This method is called at opening and when URP package change (update of manifest.json)
var curUpgradeVersion = UniversalProjectSettings.materialVersionForUpgrade;
if (curUpgradeVersion != MaterialPostprocessor.k_Upgraders.Length)
{
string commandLineOptions = Environment.CommandLine;
bool inTestSuite = commandLineOptions.Contains("-testResults");
if (!inTestSuite && fileExist)
{
EditorUtility.DisplayDialog("URP Material upgrade", "The Materials in your Project were created using an older version of the Universal Render Pipeline (URP)." +
" Unity must upgrade them to be compatible with your current version of URP. \n" +
" Unity will re-import all of the Materials in your project, save the upgraded Materials to disk, and check them out in source control if needed.\n" +
" Please see the Material upgrade guide in the URP documentation for more information.", "Ok");
}
ReimportAllMaterials();
}
if (MaterialPostprocessor.s_NeedsSavingAssets)
MaterialPostprocessor.SaveAssetsToDisk();
}
};
}
}
class MaterialPostprocessor : AssetPostprocessor
{
public static List<string> s_CreatedAssets = new List<string>();
internal static List<string> s_ImportedAssetThatNeedSaving = new List<string>();
internal static bool s_NeedsSavingAssets = false;
internal static readonly Action<Material, ShaderPathID>[] k_Upgraders = { UpgradeV1, UpgradeV2, UpgradeV3, UpgradeV4 };
static internal void SaveAssetsToDisk()
{
string commandLineOptions = System.Environment.CommandLine;
bool inTestSuite = commandLineOptions.Contains("-testResults");
if (inTestSuite)
{
// Need to update material version to prevent infinite loop in the upgrader
// when running tests.
UniversalProjectSettings.materialVersionForUpgrade = k_Upgraders.Length;
return;
}
foreach (var asset in s_ImportedAssetThatNeedSaving)
{
AssetDatabase.MakeEditable(asset);
}
AssetDatabase.SaveAssets();
//to prevent data loss, only update the saved version if user applied change and assets are written to
UniversalProjectSettings.materialVersionForUpgrade = k_Upgraders.Length;
UniversalProjectSettings.Save();
s_ImportedAssetThatNeedSaving.Clear();
s_NeedsSavingAssets = false;
}
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
var upgradeLog = "UniversalRP Material log:";
var upgradeCount = 0;
foreach (var asset in importedAssets)
{
if (!asset.EndsWith(".mat", StringComparison.InvariantCultureIgnoreCase))
continue;
var material = (Material)AssetDatabase.LoadAssetAtPath(asset, typeof(Material));
if (!ShaderUtils.IsLWShader(material.shader))
continue;
ShaderPathID id = ShaderUtils.GetEnumFromPath(material.shader.name);
var wasUpgraded = false;
var debug = "\n" + material.name;
AssetVersion assetVersion = null;
var allAssets = AssetDatabase.LoadAllAssetsAtPath(asset);
foreach (var subAsset in allAssets)
{
if (subAsset is AssetVersion sub)
{
assetVersion = sub;
}
}
if (!assetVersion)
{
wasUpgraded = true;
assetVersion = ScriptableObject.CreateInstance<AssetVersion>();
if (s_CreatedAssets.Contains(asset))
{
assetVersion.version = k_Upgraders.Length;
s_CreatedAssets.Remove(asset);
InitializeLatest(material, id);
debug += " initialized.";
}
else
{
assetVersion.version = UniversalProjectSettings.materialVersionForUpgrade;
debug += $" assumed to be version {UniversalProjectSettings.materialVersionForUpgrade} due to missing version.";
}
assetVersion.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
AssetDatabase.AddObjectToAsset(assetVersion, asset);
}
while (assetVersion.version < k_Upgraders.Length)
{
k_Upgraders[assetVersion.version](material, id);
debug += $" upgrading:v{assetVersion.version} to v{assetVersion.version + 1}";
assetVersion.version++;
wasUpgraded = true;
}
if (wasUpgraded)
{
upgradeLog += debug;
upgradeCount++;
EditorUtility.SetDirty(assetVersion);
s_ImportedAssetThatNeedSaving.Add(asset);
s_NeedsSavingAssets = true;
}
}
}
static void InitializeLatest(Material material, ShaderPathID id)
{
}
static void UpgradeV1(Material material, ShaderPathID shaderID)
{
var shaderPath = ShaderUtils.GetShaderPath(shaderID);
var upgradeFlag = MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound;
switch (shaderID)
{
case ShaderPathID.Unlit:
MaterialUpgrader.Upgrade(material, new UnlitUpdaterV1(shaderPath), upgradeFlag);
UnlitShader.SetMaterialKeywords(material);
break;
case ShaderPathID.SimpleLit:
MaterialUpgrader.Upgrade(material, new SimpleLitUpdaterV1(shaderPath), upgradeFlag);
SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
break;
case ShaderPathID.Lit:
MaterialUpgrader.Upgrade(material, new LitUpdaterV1(shaderPath), upgradeFlag);
LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
break;
case ShaderPathID.ParticlesLit:
MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
break;
case ShaderPathID.ParticlesSimpleLit:
MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
break;
case ShaderPathID.ParticlesUnlit:
MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords);
break;
}
}
static void UpgradeV2(Material material, ShaderPathID shaderID)
{
// fix 50 offset on shaders
if(material.HasProperty("_QueueOffset"))
BaseShaderGUI.SetupMaterialBlendMode(material);
}
static void UpgradeV3(Material material, ShaderPathID shaderID)
{
switch (shaderID)
{
case ShaderPathID.Lit:
case ShaderPathID.SimpleLit:
case ShaderPathID.ParticlesLit:
case ShaderPathID.ParticlesSimpleLit:
case ShaderPathID.ParticlesUnlit:
var propertyID = Shader.PropertyToID("_EmissionColor");
if (material.HasProperty(propertyID))
{
// In older version there was a bug that these shaders did not had HDR attribute on emission property.
// This caused emission color to be converted from gamma to linear space.
// In order to avoid visual regression on older projects we will do gamma to linear conversion here.
var emissionGamma = material.GetColor(propertyID);
var emissionLinear = emissionGamma.linear;
material.SetColor(propertyID, emissionLinear);
}
break;
}
}
static void UpgradeV4(Material material, ShaderPathID shaderID)
{}
}
// Upgraders v1
#region UpgradersV1
internal class LitUpdaterV1 : MaterialUpgrader
{
public static void UpdateLitDetails(Material material)
{
if (material == null)
throw new ArgumentNullException("material");
if(material.GetTexture("_MetallicGlossMap") || material.GetTexture("_SpecGlossMap") || material.GetFloat("_SmoothnessTextureChannel") >= 0.5f)
material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
else
material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
}
public LitUpdaterV1(string oldShaderName)
{
if (oldShaderName == null)
throw new ArgumentNullException("oldShaderName");
string standardShaderPath = ShaderUtils.GetShaderPath(ShaderPathID.Lit);
RenameShader(oldShaderName, standardShaderPath, UpdateLitDetails);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_GlossyReflections", "_EnvironmentReflections");
}
}
internal class UnlitUpdaterV1 : MaterialUpgrader
{
static Shader bakedLit = Shader.Find(ShaderUtils.GetShaderPath(ShaderPathID.BakedLit));
public static void UpgradeToUnlit(Material material)
{
if (material == null)
throw new ArgumentNullException("material");
if (material.GetFloat("_SampleGI") != 0)
{
material.shader = bakedLit;
material.EnableKeyword("_NORMALMAP");
}
}
public UnlitUpdaterV1(string oldShaderName)
{
if (oldShaderName == null)
throw new ArgumentNullException("oldShaderName");
RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.Unlit), UpgradeToUnlit);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
}
}
internal class SimpleLitUpdaterV1 : MaterialUpgrader
{
public SimpleLitUpdaterV1(string oldShaderName)
{
if (oldShaderName == null)
throw new ArgumentNullException("oldShaderName");
RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SimpleLit), UpgradeToSimpleLit);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_SpecSource", "_SpecularHighlights");
RenameFloat("_Shininess", "_Smoothness");
}
public static void UpgradeToSimpleLit(Material material)
{
if (material == null)
throw new ArgumentNullException("material");
var smoothnessSource = 1 - (int)material.GetFloat("_GlossinessSource");
material.SetFloat("_SmoothnessSource" ,smoothnessSource);
if (material.GetTexture("_SpecGlossMap") == null)
{
var col = material.GetColor("_SpecColor");
var colBase = material.GetColor("_Color");
var smoothness = material.GetFloat("_Shininess");
if (material.GetFloat("_Surface") == 0)
{
if (smoothnessSource == 1)
colBase.a = smoothness;
else
col.a = smoothness;
material.SetColor("_BaseColor", colBase);
}
material.SetColor("_BaseColor", colBase);
material.SetColor("_SpecColor", col);
}
}
}
internal class ParticleUpdaterV1 : MaterialUpgrader
{
public ParticleUpdaterV1(string shaderName)
{
if (shaderName == null)
throw new ArgumentNullException("oldShaderName");
RenameShader(shaderName, shaderName, ParticleUpgrader.UpdateSurfaceBlendModes);
RenameTexture("_MainTex", "_BaseMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_FlipbookMode", "_FlipbookBlending");
switch (ShaderUtils.GetEnumFromPath(shaderName))
{
case ShaderPathID.ParticlesLit:
RenameFloat("_Glossiness", "_Smoothness");
break;
case ShaderPathID.ParticlesSimpleLit:
RenameFloat("_Glossiness", "_Smoothness");
break;
case ShaderPathID.ParticlesUnlit:
break;
}
}
}
#endregion
}