浏览代码

First version of a material upgrader system (With upgrade for EmissiveColor)

/main
Sebastien Lagarde 7 年前
当前提交
bac8ca96
共有 5 个文件被更改,包括 136 次插入11 次删除
  1. 136
      com.unity.render-pipelines.high-definition/HDRP/Editor/Upgraders/UpgradeMenuItem.cs
  2. 1
      com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DefaultHDDecalMaterial.mat
  3. 1
      com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DefaultHDMaterial.mat
  4. 1
      com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DefaultHDMirrorMaterial.mat
  5. 8
      com.unity.render-pipelines.high-definition/HDRP/Editor/Decal.meta

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


using UnityEngine.Experimental.Rendering.HDPipeline;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
using System.Linq;
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{

{
EditorUtility.ClearProgressBar();
}
}
// Update EmissiveColor after we remove EmissiveIntensity from all shaders in 2018.2
// Now EmissiveColor is HDR and it must be update to the value new EmissiveColor = old EmissiveColor * EmissiveIntensity
static bool UpdateMaterial_EmissiveColor_2018_2(string path, Material mat)
{
// Find the missing property in the file and update EmissiveColor
string[] readText = File.ReadAllLines(path);
foreach (string line in readText)
{
if (line.Contains("_EmissiveIntensity:"))
{
int startPos = line.IndexOf(":") + 1;
string sub = line.Substring(startPos);
float emissiveIntensity = float.Parse(sub);
Color emissiveColor = Color.black;
if (mat.HasProperty("_EmissiveColor"))
{
emissiveColor = mat.GetColor("_EmissiveColor");
}
emissiveColor *= emissiveIntensity;
emissiveColor.a = 1.0f;
mat.SetColor("_EmissiveColor", emissiveColor);
// Also fix EmissionColor if needed (Allow to let HD handle GI, if black GI is disabled by legacy)
mat.SetColor("_EmissionColor", Color.white);
return true;
}
}
return false;
}
static void UpdateMaterialFile_EmissiveColor_2018_2(string path)
{
string[] readText = File.ReadAllLines(path);
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;
}
delegate bool UpdateMaterial(string path, Material mat);
delegate void UpdateMaterialFile(string path);
static void UpdateMaterialToNewerVersion(string caption, UpdateMaterial updateMaterial, UpdateMaterialFile updateMaterialFile = null)
{
bool VSCEnabled = (UnityEditor.VersionControl.Provider.enabled && UnityEditor.VersionControl.Provider.isActive);
var matIds = AssetDatabase.FindAssets("t:Material");
List<string> materialFiles = new List<string>(); // Contain the list dirty files
try
{
for (int i = 0, length = matIds.Length; i < length; i++)
{
var path = AssetDatabase.GUIDToAssetPath(matIds[i]);
var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
EditorUtility.DisplayProgressBar(
"Update material to new version " + caption + "...",
string.Format("{0} / {1} materials updated.", i, length),
i / (float)(length - 1));
if (mat.shader.name == "HDRenderPipeline/LitTessellation" ||
mat.shader.name == "HDRenderPipeline/Lit" ||
mat.shader.name == "HDRenderPipeline/LayeredLit" ||
mat.shader.name == "HDRenderPipeline/LayeredLitTessellation" ||
mat.shader.name == "HDRenderPipeline/StackLit" ||
mat.shader.name == "HDRenderPipeline/Unlit"
)
{
// 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);
if (dirty)
{
// Checkout the file and tag it as dirty
CoreUtils.CheckOutFile(VSCEnabled, mat);
EditorUtility.SetDirty(mat);
materialFiles.Add(path);
}
}
}
}
finally
{
EditorUtility.ClearProgressBar();
// Save all dirty assets
AssetDatabase.SaveAssets();
}
if (updateMaterialFile == null)
return;
// Now that all the asset have been modified and save, we can safely update the .mat file and remove removed property
try
{
for (int i = 0, length = materialFiles.Count; i < length; i++)
{
string path = materialFiles[i];
EditorUtility.DisplayProgressBar(
"Update .mat files...",
string.Format("{0} / {1} materials .mat file updated.", i, length),
i / (float)(length - 1));
// Note: The file is supposed to be checkout by the previous loop
updateMaterialFile(path);
}
}
finally
{
EditorUtility.ClearProgressBar();
// No need to save in this case
}
}
[MenuItem("Edit/Render Pipeline/Update all Materials to latest version", priority = CoreUtils.editMenuPriority3)]
static void UpdateMaterialToNewerVersion()
{
// Add here all the material upgrade function supported in this version
UpdateMaterialToNewerVersion("(EmissiveColor)", UpdateMaterial_EmissiveColor_2018_2, UpdateMaterialFile_EmissiveColor_2018_2);
}
}
}

1
com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DefaultHDDecalMaterial.mat


- _Drag: 1
- _DstBlend: 0
- _EmissiveColorMode: 1
- _EmissiveIntensity: 0
- _EnableBlendModeAccurateLighting: 1
- _EnableBlendModePreserveSpecularLighting: 1
- _EnableFogOnTransparent: 1

1
com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DefaultHDMaterial.mat


- _Drag: 1
- _DstBlend: 0
- _EmissiveColorMode: 1
- _EmissiveIntensity: 0
- _EnableBlendModeAccurateLighting: 1
- _EnableBlendModePreserveSpecularLighting: 1
- _EnableFogOnTransparent: 1

1
com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DefaultHDMirrorMaterial.mat


- _Drag: 1
- _DstBlend: 0
- _EmissiveColorMode: 1
- _EmissiveIntensity: 0
- _EnableBlendModeAccurateLighting: 1
- _EnableBlendModePreserveSpecularLighting: 1
- _EnableFogOnTransparent: 1

8
com.unity.render-pipelines.high-definition/HDRP/Editor/Decal.meta


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