浏览代码

Update the material upgrader system to be per material and not per project

/main
Sebastien Lagarde 6 年前
当前提交
65ea94ef
共有 10 个文件被更改,包括 110 次插入186 次删除
  1. 4
      com.unity.render-pipelines.high-definition/HDRP/Editor/Upgraders/HDRPVersion.cs
  2. 269
      com.unity.render-pipelines.high-definition/HDRP/Editor/Upgraders/UpgradeMenuItem.cs
  3. 3
      com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader
  4. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLit.shader
  5. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLitTessellation.shader
  6. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader
  7. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitTessellation.shader
  8. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.shader
  9. 2
      com.unity.render-pipelines.high-definition/HDRP/Material/Unlit/Unlit.shader
  10. 8
      com.unity.render-pipelines.high-definition/HDRP/Sky/AtmosphericScattering.meta

4
com.unity.render-pipelines.high-definition/HDRP/Editor/Upgraders/HDRPVersion.cs


[InitializeOnLoad]
public class HDRPVersion
{
// 1 changed emissive color
// 2 add decal mode in decal material
static public int hdrpVersion = 2;
static public int hdrpVersion = 1;
static public int GetCurrentHDRPProjectVersion()
{

269
com.unity.render-pipelines.high-definition/HDRP/Editor/Upgraders/UpgradeMenuItem.cs


{
public class UpgradeMenuItems
{
// Remove a set of variables from the text file target by path
static void UpdateMaterialFile_RemoveLines(string path, string[] variableNames)
{
string[] readText = File.ReadAllLines(path);
List<string> writeText = new List<string>();
//[MenuItem("Internal/HDRenderPipeline/Update/Update material for subsurface")]
static void UpdateMaterialForSubsurface()
{
try
foreach (string line in readText)
var matIds = AssetDatabase.FindAssets("t:Material");
bool found = false;
for (int i = 0, length = matIds.Length; i < length; i++)
foreach (string str in variableNames)
var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
EditorUtility.DisplayProgressBar(
"Setup materials Keywords...",
string.Format("{0} / {1} materials subsurface updated.", i, length),
i / (float)(length - 1));
bool VCSEnabled = (UnityEditor.VersionControl.Provider.enabled && UnityEditor.VersionControl.Provider.isActive);
if (mat.shader.name == "HDRenderPipeline/LitTessellation" ||
mat.shader.name == "HDRenderPipeline/Lit" ||
mat.shader.name == "HDRenderPipeline/LayeredLit" ||
mat.shader.name == "HDRenderPipeline/LayeredLitTessellation")
if (line.Contains(str))
float materialID = mat.GetInt("_MaterialID");
if (materialID != 0.0)
continue;
if (mat.HasProperty("_SSSAndTransmissionType"))
{
CoreEditorUtils.CheckOutFile(VCSEnabled, mat);
int materialSSSAndTransmissionID = mat.GetInt("_SSSAndTransmissionType");
// Both;, SSS only, Transmission only
if (materialSSSAndTransmissionID == 2.0)
{
mat.SetInt("_MaterialID", 5);
}
else
{
if (materialSSSAndTransmissionID == 0.0)
mat.SetFloat("_TransmissionEnable", 1.0f);
else
mat.SetFloat("_TransmissionEnable", 0.0f);
}
EditorUtility.SetDirty(mat);
}
found = true;
if (!found)
writeText.Add(line);
finally
{
EditorUtility.ClearProgressBar();
}
File.WriteAllLines(path, writeText.ToArray());
return;
//[MenuItem("Internal/HDRenderPipeline/Update/Update Height Maps parametrization")]
static void UpdateHeightMapParametrization()
// It is a pity but if we call mat.GetFloat("_hdrpVersion"), this return the default value
// if the _hdrpVersion was not written. So older material that haven't been updated can't be detected.
// so for now we must check for _hdrpVersion in the .txt
// maybe in a far future we can rely on just mat.GetFloat("_hdrpVersion")
static float UpdateMaterial_GetVersion(string path)
try
// Find the missing property in the file and update EmissiveColor
string[] readText = File.ReadAllLines(path);
foreach (string line in readText)
var matIds = AssetDatabase.FindAssets("t:Material");
for (int i = 0, length = matIds.Length; i < length; i++)
if (line.Contains("_HdrpVersion:"))
var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
EditorUtility.DisplayProgressBar(
"Updating Materials...",
string.Format("{0} / {1} materials updated.", i, length),
i / (float)(length - 1));
bool VCSEnabled = (UnityEditor.VersionControl.Provider.enabled && UnityEditor.VersionControl.Provider.isActive);
if (mat.shader.name == "HDRenderPipeline/LitTessellation" ||
mat.shader.name == "HDRenderPipeline/Lit")
{
// Need only test one of the new properties
if (mat.HasProperty("_HeightPoMAmplitude"))
{
CoreEditorUtils.CheckOutFile(VCSEnabled, mat);
float valueMax = mat.GetFloat("_HeightMax");
float valueMin = mat.GetFloat("_HeightMin");
float center = mat.GetFloat("_HeightCenter");
float amplitude = valueMax - valueMin;
mat.SetInt("_HeightMapParametrization", 1);
mat.SetFloat("_HeightPoMAmplitude", amplitude);
mat.SetFloat("_HeightTessAmplitude", amplitude);
mat.SetFloat("_HeightOffset", 0.0f);
mat.SetFloat("_HeightTessCenter", center);
BaseLitGUI.DisplacementMode displaceMode = (BaseLitGUI.DisplacementMode)mat.GetInt("_DisplacementMode");
if (displaceMode == BaseLitGUI.DisplacementMode.Pixel)
{
mat.SetFloat("_HeightCenter", 1.0f); // With PoM this is always 1.0f. We set it here to avoid having to open the UI to update it.
}
EditorUtility.SetDirty(mat);
}
}
else if (mat.shader.name == "HDRenderPipeline/LayeredLit" ||
mat.shader.name == "HDRenderPipeline/LayeredLitTessellation")
{
int numLayer = (int)mat.GetFloat("_LayerCount");
if (mat.HasProperty("_HeightPoMAmplitude0"))
{
CoreEditorUtils.CheckOutFile(VCSEnabled, mat);
for (int x = 0; x < numLayer; ++x)
{
float valueMax = mat.GetFloat("_HeightMax" + x);
float valueMin = mat.GetFloat("_HeightMin" + x);
float center = mat.GetFloat("_HeightCenter" + x);
float amplitude = valueMax - valueMin;
mat.SetInt("_HeightMapParametrization" + x, 1);
mat.SetFloat("_HeightPoMAmplitude" + x, amplitude);
mat.SetFloat("_HeightTessAmplitude" + x, amplitude);
mat.SetFloat("_HeightOffset" + x, 0.0f);
mat.SetFloat("_HeightTessCenter" + x, center);
BaseLitGUI.DisplacementMode displaceMode = (BaseLitGUI.DisplacementMode)mat.GetInt("_DisplacementMode");
if (displaceMode == BaseLitGUI.DisplacementMode.Pixel)
{
mat.SetFloat("_HeightCenter" + x, 1.0f); // With PoM this is always 1.0f. We set it here to avoid having to open the UI to update it.
}
}
EditorUtility.SetDirty(mat);
}
}
int startPos = line.IndexOf(":") + 1;
string sub = line.Substring(startPos);
return float.Parse(sub);
finally
{
EditorUtility.ClearProgressBar();
}
return 0.0f;
// Version 1
static bool UpdateMaterial_EmissiveColor(string path, Material mat)
static bool UpdateMaterial_EmissiveColor_1(string path, Material mat)
{
// Find the missing property in the file and update EmissiveColor
string[] readText = File.ReadAllLines(path);

return false;
}
static void UpdateMaterialFile_EmissiveColor(string path)
static void UpdateMaterialFile_EmissiveColor_1(string path)
string[] readText = File.ReadAllLines(path);
string[] variablesNames = new string[1];
variablesNames[0] = "_EmissiveIntensity:";
UpdateMaterialFile_RemoveLines(path, variablesNames);
}
foreach (string line in readText)
{
if (line.Contains("_EmissiveIntensity:"))
{
// Remove emissive intensity line
string[] writeText = readText.Where(l => l != line).ToArray();
File.WriteAllLines(path, writeText);
return;
}
}
return;
}
// Version 2
static bool UpdateMaterial_DecalBlendMode(string path, Material mat)
// Also we have rename _SupportDBuffer to _SupportDecals
static bool UpdateMaterial_Decals_2(string path, Material mat)
bool dirty = false;
if (mat.shader.name == "HDRenderPipeline/Decal")
{
float maskBlendMode = mat.GetFloat("_MaskBlendMode");

mat.SetFloat("_MaskBlendMode", (float)Decal.MaskBlendFlags.Smoothness);
return true;
dirty = true;
else
{
// Find the missing property in the file and update EmissiveColor
string[] readText = File.ReadAllLines(path);
return false;
foreach (string line in readText)
{
if (line.Contains("_SupportDBuffer:"))
{
int startPos = line.IndexOf(":") + 1;
string sub = line.Substring(startPos);
float enableDecal = float.Parse(sub);
mat.SetFloat("_SupportDecals", enableDecal);
dirty = true;
}
}
}
return dirty;
}
static void UpdateMaterialFile_Decals_2(string path)
{
string[] variablesNames = new string[1];
variablesNames[0] = "_SupportDBuffer:";
UpdateMaterialFile_RemoveLines(path, variablesNames);
static void UpdateMaterialToNewerVersion(string caption, UpdateMaterial updateMaterial, UpdateMaterialFile updateMaterialFile = null)
static void UpdateMaterialToNewerVersion(string caption, float scriptVersion, UpdateMaterial updateMaterial, UpdateMaterialFile updateMaterialFile = null)
{
bool VCSEnabled = (UnityEditor.VersionControl.Provider.enabled && UnityEditor.VersionControl.Provider.isActive);
var matIds = AssetDatabase.FindAssets("t:Material");

var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
EditorUtility.DisplayProgressBar(
"Update material to new version " + caption + "...",
"Update material to new version...",
string.Format("{0} / {1} materials updated.", i, length),
i / (float)(length - 1));

mat.shader.name == "HDRenderPipeline/Decal"
)
{
// Need to be processed in order - All function here should be re-entrant (i.e after upgrade it can be recall)
bool dirty = updateMaterial(path, mat);
// Get current version
float materialVersion = UpdateMaterial_GetVersion(path);
if (dirty)
if (materialVersion < scriptVersion)
updateMaterial(path, mat);
// Update version number to script number (so next script can upgrade correctly)
mat.SetFloat("_HdrpVersion", scriptVersion);
// Checkout the file and tag it as dirty
CoreEditorUtils.CheckOutFile(VCSEnabled, mat);
EditorUtility.SetDirty(mat);

}
}
[MenuItem("Edit/Render Pipeline/Single step upgrade script/Upgrade all Materials EmissionColor", priority = CoreUtils.editMenuPriority3)]
static public void UpdateMaterialToNewerVersionEmissiveColor()
[MenuItem("Edit/Render Pipeline/Upgrade all Materials to newer version", priority = CoreUtils.editMenuPriority3)]
static public void UpdateMaterialToNewerVersion()
UpdateMaterialToNewerVersion("(EmissiveColor)", UpdateMaterial_EmissiveColor, UpdateMaterialFile_EmissiveColor);
}
// TODO: We need to handle material that are embed inside scene!
[MenuItem("Edit/Render Pipeline/Single step upgrade script/Upgrade all DecalMaterial MaskBlendMode", priority = CoreUtils.editMenuPriority3)]
static public void UpdateMaterialToNewerVersionDecalMaterialMaskBlendMode()
{
UpdateMaterialToNewerVersion("(DecalMaterial)", UpdateMaterial_DecalBlendMode);
}
// Add here all the material upgrade functions
// Note: This is a slow path as we go through all files for each script + update the version number after each script execution,
// but it is the safest way to do it currently for incremental upgrade
UpdateMaterialToNewerVersion("(EmissiveColor_1)", 1.0f, UpdateMaterial_EmissiveColor_1, UpdateMaterialFile_EmissiveColor_1);
UpdateMaterialToNewerVersion("(Decals_2)", 2.0f, UpdateMaterial_Decals_2, UpdateMaterialFile_Decals_2);
[MenuItem("Edit/Render Pipeline/Upgrade all Materials to latest version", priority = CoreUtils.editMenuPriority3)]
static public void UpdateMaterialToNewerVersion()
{
// Add here all the material upgrade function supported in this version
// Caution: All the functions here MUST be re-entrant (call multiple time) without failing.
int currentVersion = HDRPVersion.GetCurrentHDRPProjectVersion();
if (currentVersion < 1)
{
// Appear in hdrp version 1.0
UpdateMaterialToNewerVersion("(EmissiveColor)", UpdateMaterial_EmissiveColor, UpdateMaterialFile_EmissiveColor);
}
if (currentVersion < 2)
{
// Appear in hdrp version 2.0
UpdateMaterialToNewerVersion("(DecalMaterial)", UpdateMaterial_DecalBlendMode);
}
// Caution: Version of latest script and default version in all HDRP shader must match
}
}
}

3
com.unity.render-pipelines.high-definition/HDRP/Material/Decal/Decal.shader


{
Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
_BaseColor("_BaseColor", Color) = (1,1,1,1)
_BaseColorMap("BaseColorMap", 2D) = "white" {}
_NormalMap("NormalMap", 2D) = "bump" {} // Tangent space normal map

2
com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLit.shader


Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
// Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph this should not exist.

2
com.unity.render-pipelines.high-definition/HDRP/Material/LayeredLit/LayeredLitTessellation.shader


Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
// Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph this should not exist.

2
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/Lit.shader


Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
// Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph this should not exist.

2
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitTessellation.shader


Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
// Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph this should not exist.

2
com.unity.render-pipelines.high-definition/HDRP/Material/StackLit/StackLit.shader


Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
// Following set of parameters represent the parameters node inside the MaterialGraph.
// They are use to fill a SurfaceData. With a MaterialGraph this should not exist.

2
com.unity.render-pipelines.high-definition/HDRP/Material/Unlit/Unlit.shader


Properties
{
// Versioning of material to help for upgrading
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 1
[HideInInspector] _HdrpVersion("_HdrpVersion", Float) = 2
// Be careful, do not change the name here to _Color. It will conflict with the "fake" parameters (see end of properties) required for GI.
_UnlitColor("Color", Color) = (1,1,1,1)

8
com.unity.render-pipelines.high-definition/HDRP/Sky/AtmosphericScattering.meta


fileFormatVersion: 2
guid: d522828bc0314e14cb6faa4291bb64da
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存