浏览代码
Merge branch 'MaterialUpdater' of https://github.com/Unity-Technologies/ScriptableRenderLoop
/main
Merge branch 'MaterialUpdater' of https://github.com/Unity-Technologies/ScriptableRenderLoop
/main
vlad-andreev
8 年前
当前提交
5b31375a
共有 20 个文件被更改,包括 446 次插入 和 34 次删除
-
2Assets/ScriptableRenderLoop/HDRenderLoop/Material/LayeredLit/Editor/LayeredLitUI.cs
-
2Assets/ScriptableRenderLoop/HDRenderLoop/Material/LayeredLit/LayeredLit.shader
-
2Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/LitUI.cs
-
2Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.shader
-
2Assets/ScriptableRenderLoop/HDRenderLoop/Material/Unlit/Editor/UnlitUI.cs
-
9Assets/ScriptableRenderLoop/ScriptableRenderLoopPicker.cs
-
9Assets/ScriptableRenderLoop/Editor.meta
-
42Assets/ScriptableRenderLoop/HDRenderLoop/Editor/UpgradeStandardShaderMaterials.cs
-
12Assets/ScriptableRenderLoop/HDRenderLoop/Editor/UpgradeStandardShaderMaterials.cs.meta
-
63Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/StandardSpecularToHDLitMaterialUpgrader.cs
-
12Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/StandardSpecularToHDLitMaterialUpgrader.cs.meta
-
69Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/StandardToHDLitMaterialUpgrader.cs
-
12Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Editor/StandardToHDLitMaterialUpgrader.cs.meta
-
20Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/PatchStandardShaderToNewNamingConvention.cginc
-
9Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/PatchStandardShaderToNewNamingConvention.cginc.meta
-
12Assets/ScriptableRenderLoop/Editor/MaterialUpgrader.cs.meta
-
173Assets/ScriptableRenderLoop/Editor/MaterialUpgrader.cs
-
20Assets/forwardrenderloop.asset
-
8Assets/forwardrenderloop.asset.meta
|
|||
fileFormatVersion: 2 |
|||
guid: 74ae8146f4a01491ba1306f3db78139d |
|||
folderAsset: yes |
|||
timeCreated: 1479851675 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using UnityEditor; |
|||
using System; |
|||
using NUnit.Framework; |
|||
|
|||
namespace UnityEditor.Experimental.ScriptableRenderLoop |
|||
{ |
|||
public class UpgradeStandardShaderMaterials |
|||
{ |
|||
static List<MaterialUpgrader> GetHDUpgraders() |
|||
{ |
|||
var upgraders = new List<MaterialUpgrader>(); |
|||
upgraders.Add(new StandardToHDLitMaterialUpgrader()); |
|||
upgraders.Add(new StandardSpecularToHDLitMaterialUpgrader()); |
|||
return upgraders; |
|||
} |
|||
|
|||
[MenuItem("HDRenderLoop/Upgrade Materials - Project")] |
|||
static void UpgradeMaterialsProject() |
|||
{ |
|||
MaterialUpgrader.UpgradeProjectFolder(GetHDUpgraders(), "Upgrade to HD Material"); |
|||
} |
|||
|
|||
[MenuItem("HDRenderLoop/Upgrade Materials - Selection")] |
|||
static void UpgradeMaterialsSelection() |
|||
{ |
|||
MaterialUpgrader.UpgradeSelection(GetHDUpgraders(), "Upgrade to HD Material"); |
|||
} |
|||
|
|||
[MenuItem("HDRenderLoop/Modify Light Intensity for Upgrade - Scene Only")] |
|||
static void UpgradeLights() |
|||
{ |
|||
Light[] lights = Light.GetLights(LightType.Directional, 0); |
|||
foreach (var l in lights) |
|||
{ |
|||
l.intensity *= Mathf.PI; |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9384735d0665f47539990919bf72504e |
|||
timeCreated: 1479839793 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using NUnit.Framework; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Experimental.ScriptableRenderLoop |
|||
{ |
|||
class StandardSpecularToHDLitMaterialUpgrader : MaterialUpgrader |
|||
{ |
|||
public StandardSpecularToHDLitMaterialUpgrader() |
|||
{ |
|||
RenameShader("Standard (Specular setup)", "HDRenderLoop/LitLegacySupport"); |
|||
|
|||
RenameTexture("_MainTex", "_BaseColorMap"); |
|||
RenameColor("_Color", "_BaseColor"); |
|||
RenameFloat("_Glossiness", "_Smoothness"); |
|||
RenameTexture("_BumpMap", "_NormalMap"); |
|||
RenameColor("_EmissionColor", "_EmissiveColor"); |
|||
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale"); |
|||
RenameTexture("_DetailNormalMap", "_DetailMap"); |
|||
|
|||
//@Seb: Bumpmap scale doesn't exist in new shader
|
|||
//_BumpScale("Scale", Float) = 1.0
|
|||
|
|||
// Anything reasonable that can be done here?
|
|||
//RenameFloat("_SpecColor", ...);
|
|||
|
|||
//@TODO: Seb. Why do we multiply color by intensity
|
|||
// in shader when we can just store a color?
|
|||
// builtinData.emissiveColor * builtinData.emissiveIntensity
|
|||
} |
|||
|
|||
public override void Convert(Material srcMaterial, Material dstMaterial) |
|||
{ |
|||
base.Convert(srcMaterial, dstMaterial); |
|||
//@TODO: Find a good way of setting up keywords etc from properties.
|
|||
// Code should be shared with material UI code.
|
|||
} |
|||
|
|||
[Test] |
|||
public void UpgradeMaterial() |
|||
{ |
|||
var newShader = Shader.Find("HDRenderLoop/LitLegacySupport"); |
|||
var mat = new Material(Shader.Find("Standard (Specular setup)")); |
|||
var albedo = new Texture2D(1, 1); |
|||
var normals = new Texture2D(1, 1); |
|||
var baseScale = new Vector2(1, 1); |
|||
var color = Color.red; |
|||
mat.mainTexture = albedo; |
|||
mat.SetTexture("_BumpMap", normals); |
|||
mat.color = color; |
|||
mat.SetTextureScale("_MainTex", baseScale); |
|||
|
|||
MaterialUpgrader.Upgrade(mat, new StandardSpecularToHDLitMaterialUpgrader(), MaterialUpgrader.UpgradeFlags.CleanupNonUpgradedProperties); |
|||
|
|||
Assert.AreEqual(newShader, mat.shader); |
|||
Assert.AreEqual(albedo, mat.GetTexture("_BaseColorMap")); |
|||
Assert.AreEqual(color, mat.GetColor("_BaseColor")); |
|||
Assert.AreEqual(baseScale, mat.GetTextureScale("_BaseColorMap")); |
|||
Assert.AreEqual(normals, mat.GetTexture("_NormalMap")); |
|||
} |
|||
} |
|||
} |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: 775bef174b1e2ed47972a143845e1191 |
|||
timeCreated: 1479911618 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using NUnit.Framework; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEditor.Experimental.ScriptableRenderLoop |
|||
{ |
|||
class StandardToHDLitMaterialUpgrader : MaterialUpgrader |
|||
{ |
|||
public StandardToHDLitMaterialUpgrader() |
|||
{ |
|||
RenameShader("Standard", "HDRenderLoop/LitLegacySupport"); |
|||
|
|||
RenameTexture("_MainTex", "_BaseColorMap"); |
|||
RenameColor("_Color", "_BaseColor"); |
|||
RenameFloat("_Glossiness", "_Smoothness"); |
|||
RenameTexture("_BumpMap", "_NormalMap"); |
|||
RenameColor("_EmissionColor", "_EmissiveColor"); |
|||
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale"); |
|||
|
|||
// the HD renderloop packs detail albedo and detail normals into a single texture.
|
|||
// mapping the detail normal map, if any, to the detail map, should do the right thing if
|
|||
// there is no detail albedo.
|
|||
RenameTexture("_DetailNormalMap", "_DetailMap"); |
|||
|
|||
//@Seb: Bumpmap scale doesn't exist in new shader
|
|||
//_BumpScale("Scale", Float) = 1.0
|
|||
|
|||
// Metallic uses [Gamma] attribute in standard shader but not in Lit.
|
|||
// @Seb: Should we convert?
|
|||
RenameFloat("_Metallic", "_Metallic"); |
|||
|
|||
//@TODO: Seb. Why do we multiply color by intensity
|
|||
// in shader when we can just store a color?
|
|||
// builtinData.emissiveColor * builtinData.emissiveIntensity
|
|||
} |
|||
|
|||
public override void Convert(Material srcMaterial, Material dstMaterial) |
|||
{ |
|||
base.Convert(srcMaterial, dstMaterial); |
|||
//@TODO: Find a good way of setting up keywords etc from properties.
|
|||
// Code should be shared with material UI code.
|
|||
} |
|||
|
|||
[Test] |
|||
public void UpgradeMaterial() |
|||
{ |
|||
var newShader = Shader.Find("HDRenderLoop/LitLegacySupport"); |
|||
var mat = new Material(Shader.Find("Standard")); |
|||
var albedo = new Texture2D(1, 1); |
|||
var normals = new Texture2D(1, 1); |
|||
var baseScale = new Vector2(1, 1); |
|||
var color = Color.red; |
|||
mat.mainTexture = albedo; |
|||
mat.SetTexture("_BumpMap", normals); |
|||
mat.color = color; |
|||
mat.SetTextureScale("_MainTex", baseScale); |
|||
|
|||
|
|||
MaterialUpgrader.Upgrade(mat, new StandardToHDLitMaterialUpgrader(), MaterialUpgrader.UpgradeFlags.CleanupNonUpgradedProperties); |
|||
|
|||
Assert.AreEqual(newShader, mat.shader); |
|||
Assert.AreEqual(albedo, mat.GetTexture("_BaseColorMap")); |
|||
Assert.AreEqual(color, mat.GetColor("_BaseColor")); |
|||
Assert.AreEqual(baseScale, mat.GetTextureScale("_BaseColorMap")); |
|||
Assert.AreEqual(normals, mat.GetTexture("_NormalMap")); |
|||
} |
|||
} |
|||
} |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: fedbd6bc5d95f4d768cb8077e8454b4e |
|||
timeCreated: 1479851295 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
// NOTE: For performing a project upgrade where you temporarily support both old and new renderloops |
|||
// in the same sahder |
|||
// |
|||
// The basic approach is: |
|||
// Upgrade all your shaders to the new naming convention, using a SubShader that also contains the legacy // renderloop code. |
|||
// |
|||
// 1. Copy HDRenderloop Lit.shader into your project |
|||
// 2. Add a SubShader and copy old Standard shader passes into it. |
|||
// 2. Set LOD on subshader to make Unity pick at runtime to use new renderloop shaders or |
|||
// legacy standard shaders based on if SRL is enabled or not. |
|||
// In the legacy standard shader section add |
|||
// #include "PatchStandardShaderToNewNamingConvention.cginc" |
|||
|
|||
// List of name remaps |
|||
#define _MainTex _BaseColorMap |
|||
#define _MainTex_ST _BaseColorMap_ST |
|||
#define _BumpMap _NormalMap |
|||
#define _ParallaxMap _HeightMap |
|||
#define _Parallax _HeightScale |
|||
#define _Glossiness _Smoothness |
|
|||
fileFormatVersion: 2 |
|||
guid: a9b4e2b7ef9a45f49834b052e7722acb |
|||
timeCreated: 1480085057 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 67b996d0727a74b029aff1545143be17 |
|||
timeCreated: 1479851694 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using System; |
|||
|
|||
namespace UnityEditor.Experimental.ScriptableRenderLoop |
|||
{ |
|||
public class MaterialUpgrader |
|||
{ |
|||
string m_OldShader; |
|||
string m_NewShader; |
|||
|
|||
Dictionary<string, string> m_TextureRename = new Dictionary<string, string>(); |
|||
Dictionary<string, string> m_FloatRename = new Dictionary<string, string>(); |
|||
Dictionary<string, string> m_ColorRename = new Dictionary<string, string>(); |
|||
|
|||
[Flags] |
|||
public enum UpgradeFlags |
|||
{ |
|||
None = 0, |
|||
LogErrorOnNonExistingProperty = 1, |
|||
CleanupNonUpgradedProperties = 2, |
|||
} |
|||
|
|||
public void Upgrade(Material material, UpgradeFlags flags) |
|||
{ |
|||
Material newMaterial; |
|||
if ((flags & UpgradeFlags.CleanupNonUpgradedProperties) != 0) |
|||
{ |
|||
newMaterial = new Material(Shader.Find(m_NewShader)); |
|||
} |
|||
else |
|||
{ |
|||
newMaterial = UnityEngine.Object.Instantiate(material) as Material; |
|||
newMaterial.shader = Shader.Find(m_NewShader); |
|||
} |
|||
|
|||
Convert(material, newMaterial); |
|||
|
|||
material.shader = Shader.Find(m_NewShader); |
|||
material.CopyPropertiesFromMaterial(newMaterial); |
|||
UnityEngine.Object.DestroyImmediate(newMaterial); |
|||
} |
|||
|
|||
// Overridable function to implement custom material upgrading functionality
|
|||
public virtual void Convert(Material srcMaterial, Material dstMaterial) |
|||
{ |
|||
foreach (var t in m_TextureRename) |
|||
{ |
|||
dstMaterial.SetTextureScale(t.Value, srcMaterial.GetTextureScale(t.Key)); |
|||
dstMaterial.SetTextureOffset(t.Value, srcMaterial.GetTextureOffset(t.Key)); |
|||
dstMaterial.SetTexture(t.Value, srcMaterial.GetTexture(t.Key)); |
|||
} |
|||
|
|||
foreach (var t in m_FloatRename) |
|||
dstMaterial.SetFloat(t.Value, srcMaterial.GetFloat(t.Key)); |
|||
|
|||
foreach (var t in m_ColorRename) |
|||
dstMaterial.SetColor(t.Value, srcMaterial.GetColor(t.Key)); |
|||
} |
|||
|
|||
public void RenameShader(string oldName, string newName) |
|||
{ |
|||
m_OldShader = oldName; |
|||
m_NewShader = newName; |
|||
} |
|||
|
|||
public void RenameTexture(string oldName, string newName) |
|||
{ |
|||
m_TextureRename[oldName] = newName; |
|||
} |
|||
|
|||
public void RenameFloat(string oldName, string newName) |
|||
{ |
|||
m_FloatRename[oldName] = newName; |
|||
} |
|||
|
|||
public void RenameColor(string oldName, string newName) |
|||
{ |
|||
m_ColorRename[oldName] = newName; |
|||
} |
|||
|
|||
static bool IsMaterialPath(string path) |
|||
{ |
|||
return path.EndsWith(".mat", StringComparison.OrdinalIgnoreCase); |
|||
} |
|||
|
|||
static MaterialUpgrader GetUpgrader(List<MaterialUpgrader> upgraders, Material material) |
|||
{ |
|||
if (material == null || material.shader == null) |
|||
return null; |
|||
|
|||
string shaderName = material.shader.name; |
|||
for (int i = 0; i != upgraders.Count; i++) |
|||
{ |
|||
if (upgraders[i].m_OldShader == shaderName) |
|||
return upgraders[i]; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
//@TODO: Only do this when it exceeds memory consumption...
|
|||
static void SaveAssetsAndFreeMemory() |
|||
{ |
|||
AssetDatabase.SaveAssets(); |
|||
GC.Collect(); |
|||
EditorUtility.UnloadUnusedAssetsImmediate(); |
|||
AssetDatabase.Refresh(); |
|||
} |
|||
|
|||
public static void UpgradeProjectFolder(List<MaterialUpgrader> upgraders, string progressBarName) |
|||
{ |
|||
int totalMaterialCount = 0; |
|||
foreach (string s in UnityEditor.AssetDatabase.GetAllAssetPaths()) |
|||
{ |
|||
if (IsMaterialPath(s)) |
|||
totalMaterialCount++; |
|||
} |
|||
|
|||
int materialIndex = 0; |
|||
foreach (string path in UnityEditor.AssetDatabase.GetAllAssetPaths()) |
|||
{ |
|||
if (IsMaterialPath(path)) |
|||
{ |
|||
materialIndex++; |
|||
if (UnityEditor.EditorUtility.DisplayCancelableProgressBar(progressBarName, string.Format("({0} of {1}) {2}", materialIndex, totalMaterialCount, path), (float)materialIndex / (float)totalMaterialCount)) |
|||
break; |
|||
|
|||
Material m = UnityEditor.AssetDatabase.LoadMainAssetAtPath(path) as Material; |
|||
Upgrade(m, upgraders, UpgradeFlags.None); |
|||
|
|||
//SaveAssetsAndFreeMemory();
|
|||
} |
|||
} |
|||
|
|||
UnityEditor.EditorUtility.ClearProgressBar(); |
|||
} |
|||
|
|||
public static void Upgrade(Material material, MaterialUpgrader upgrader, UpgradeFlags flags) |
|||
{ |
|||
var upgraders = new List<MaterialUpgrader>(); |
|||
upgraders.Add(upgrader); |
|||
Upgrade(material, upgraders, flags); |
|||
} |
|||
|
|||
public static void Upgrade(Material material, List<MaterialUpgrader> upgraders, UpgradeFlags flags) |
|||
{ |
|||
var upgrader = GetUpgrader(upgraders, material); |
|||
if (upgrader != null) |
|||
upgrader.Upgrade(material, flags); |
|||
} |
|||
|
|||
public static void UpgradeSelection(List<MaterialUpgrader> upgraders, string progressBarName) |
|||
{ |
|||
string lastMaterialName = ""; |
|||
var selection = Selection.objects; |
|||
for (int i = 0; i < selection.Length; i++) |
|||
{ |
|||
|
|||
if (UnityEditor.EditorUtility.DisplayCancelableProgressBar(progressBarName, string.Format("({0} of {1}) {2}", i, selection.Length, lastMaterialName), (float)i / (float)selection.Length)) |
|||
break; |
|||
|
|||
var material = selection[i] as Material; |
|||
Upgrade(material, upgraders, UpgradeFlags.None); |
|||
if (material != null) |
|||
lastMaterialName = material.name; |
|||
} |
|||
|
|||
UnityEditor.EditorUtility.ClearProgressBar(); |
|||
} |
|||
} |
|||
} |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!114 &11400000 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_PrefabParentObject: {fileID: 0} |
|||
m_PrefabInternal: {fileID: 0} |
|||
m_GameObject: {fileID: 0} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 98fe12104b04e43dd9a963f3f7b26e8e, type: 3} |
|||
m_Name: forwardrenderloop |
|||
m_EditorClassIdentifier: |
|||
m_ShadowSettings: |
|||
enabled: 1 |
|||
shadowAtlasWidth: 4096 |
|||
shadowAtlasHeight: 4096 |
|||
maxShadowDistance: 1000 |
|||
directionalLightCascadeCount: 4 |
|||
directionalLightCascades: {x: 0.05, y: 0.2, z: 0.3} |
|
|||
fileFormatVersion: 2 |
|||
guid: bfc231f294556ca4799e7f7feddee50d |
|||
timeCreated: 1475076344 |
|||
licenseType: Pro |
|||
NativeFormatImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue