Skip to content

Commit 09ec657

Browse files
committed
Implemented AssignNewShader callback to update shader keyword upon material upgrade.
1 parent f4d4900 commit 09ec657

3 files changed

Lines changed: 69 additions & 69 deletions

File tree

Assets/LowEndMobilePipeline/Editor/LegacyShadersToLowEndUpgrader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static void UpgradeMaterialsToLDProject()
105105
MaterialUpgrader.UpgradeProjectFolder(materialUpgraders, "Upgrade to LD Materials");
106106
}
107107

108+
// TODO: Replace this logic with AssignNewShaderToMaterial
108109
private static void GetUpgraders(ref List<MaterialUpgrader> materialUpgraders)
109110
{
110111
/////////////////////////////////////
@@ -126,9 +127,9 @@ private static void GetUpgraders(ref List<MaterialUpgrader> materialUpgraders)
126127

127128
// Alpha Blended
128129
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Diffuse", SupportedUpgradeParams.diffuseAlpha));
129-
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Specular", SupportedUpgradeParams.specularCubemap));
130+
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Specular", SupportedUpgradeParams.specularAlpha));
130131
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Bumped Diffuse", SupportedUpgradeParams.diffuseAlpha));
131-
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Bumped Specular", SupportedUpgradeParams.specularCubemap));
132+
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Bumped Specular", SupportedUpgradeParams.specularAlpha));
132133

133134
// Cutout
134135
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Cutout/Diffuse", SupportedUpgradeParams.diffuseAlphaCutout));

Assets/LowEndMobilePipeline/Editor/LowendMobilePipelineMaterialEditor.cs

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using UnityEditor;
33
using UnityEngine;
44

5-
public class LowendMobilePipelineMaterialEditor : MaterialEditor
5+
public class LowendMobilePipelineMaterialEditor : ShaderGUI
66
{
77
private MaterialProperty blendModeProp = null;
88
private MaterialProperty albedoMapProp = null;
@@ -20,6 +20,8 @@ public class LowendMobilePipelineMaterialEditor : MaterialEditor
2020
private MaterialProperty reflectionMapProp = null;
2121
private MaterialProperty reflectionColorProp = null;
2222

23+
private MaterialEditor m_MaterialEditor = null;
24+
2325
public enum BlendMode
2426
{
2527
Opaque,
@@ -47,15 +49,6 @@ public enum ReflectionSource
4749
ReflectionProbe
4850
}
4951

50-
private void Awake()
51-
{
52-
Material material = target as Material;
53-
FindMaterialProperties(material);
54-
UpdateMaterialKeywords(material);
55-
56-
Styles.warningStyle.normal.textColor = Color.yellow;
57-
}
58-
5952
private static class Styles
6053
{
6154
public static GUIContent[] albedoGlosinessLabels =
@@ -101,65 +94,68 @@ private static class Styles
10194
public static string reflectionSourceLabel = "Reflection Source";
10295
}
10396

104-
private void FindMaterialProperties(Material material)
97+
private void FindMaterialProperties(MaterialProperty[] properties)
10598
{
106-
Material[] mats = { material };
107-
blendModeProp = GetMaterialProperty(mats, "_Mode");
108-
albedoMapProp = GetMaterialProperty(mats, "_MainTex");
109-
albedoColorProp = GetMaterialProperty(mats, "_Color");
110-
111-
alphaCutoffProp = GetMaterialProperty(mats, "_Cutoff");
112-
specularSourceProp = GetMaterialProperty(mats, "_SpecSource");
113-
glossinessSourceProp = GetMaterialProperty(mats, "_GlossinessSource");
114-
specularGlossMapProp = GetMaterialProperty(mats, "_SpecGlossMap");
115-
specularColorProp = GetMaterialProperty(mats, "_SpecColor");
116-
shininessProp = GetMaterialProperty(mats, "_Shininess");
117-
bumpMapProp = GetMaterialProperty(mats, "_BumpMap");
118-
emissionMapProp = GetMaterialProperty(mats, "_EmissionMap");
119-
emissionColorProp = GetMaterialProperty(mats, "_EmissionColor");
120-
reflectionMapProp = GetMaterialProperty(mats, "_Cube");
121-
reflectionColorProp = GetMaterialProperty(mats, "_ReflectColor");
122-
reflectionSourceProp = GetMaterialProperty(mats, "_ReflectionSource");
99+
blendModeProp = FindProperty("_Mode", properties);
100+
albedoMapProp = FindProperty("_MainTex", properties);
101+
albedoColorProp = FindProperty("_Color", properties);
102+
103+
alphaCutoffProp = FindProperty("_Cutoff", properties);
104+
specularSourceProp = FindProperty("_SpecSource", properties);
105+
glossinessSourceProp = FindProperty("_GlossinessSource", properties);
106+
specularGlossMapProp = FindProperty("_SpecGlossMap", properties);
107+
specularColorProp = FindProperty("_SpecColor", properties);
108+
shininessProp = FindProperty("_Shininess", properties);
109+
bumpMapProp = FindProperty("_BumpMap", properties);
110+
emissionMapProp = FindProperty("_EmissionMap", properties);
111+
emissionColorProp = FindProperty("_EmissionColor", properties);
112+
reflectionMapProp = FindProperty("_Cube", properties);
113+
reflectionColorProp = FindProperty("_ReflectColor", properties);
114+
reflectionSourceProp = FindProperty("_ReflectionSource", properties);
123115
}
124116

125-
public override void OnInspectorGUI()
117+
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
126118
{
127-
serializedObject.Update();
128-
var shaderObj = serializedObject.FindProperty("m_Shader");
129-
if (isVisible && shaderObj.objectReferenceValue != null)
130-
{
131-
Material material = target as Material;
132-
FindMaterialProperties(material);
119+
Material material = materialEditor.target as Material;
120+
m_MaterialEditor = materialEditor;
133121

134-
EditorGUI.BeginChangeCheck();
135-
DoBlendMode();
122+
FindMaterialProperties(properties);
136123

137-
EditorGUILayout.Space();
138-
DoSpecular();
124+
EditorGUI.BeginChangeCheck();
125+
DoBlendMode();
139126

140-
EditorGUILayout.Space();
141-
TexturePropertySingleLine(Styles.normalMapText, bumpMapProp);
127+
EditorGUILayout.Space();
128+
DoSpecular();
142129

143-
EditorGUILayout.Space();
144-
TexturePropertySingleLine(Styles.emissionMapLabel, emissionMapProp, emissionColorProp);
130+
EditorGUILayout.Space();
131+
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMapProp);
145132

146-
EditorGUILayout.Space();
147-
DoReflection();
133+
EditorGUILayout.Space();
134+
m_MaterialEditor.TexturePropertySingleLine(Styles.emissionMapLabel, emissionMapProp, emissionColorProp);
148135

149-
if (EditorGUI.EndChangeCheck())
150-
UpdateMaterialKeywords(material);
136+
EditorGUILayout.Space();
137+
DoReflection();
151138

152-
EditorGUILayout.Space();
153-
EditorGUILayout.Space();
139+
if (EditorGUI.EndChangeCheck())
140+
UpdateMaterialKeywords(material);
141+
142+
EditorGUILayout.Space();
143+
EditorGUILayout.Space();
154144

155-
if ((BlendMode)blendModeProp.floatValue == BlendMode.Cutout)
156-
{
157-
Styles.warningStyle.normal.textColor = Color.yellow;
158-
EditorGUILayout.LabelField(Styles.alphaCutoutWarning, Styles.warningStyle);
159-
}
145+
if ((BlendMode)blendModeProp.floatValue == BlendMode.Cutout)
146+
{
147+
Styles.warningStyle.normal.textColor = Color.yellow;
148+
EditorGUILayout.LabelField(Styles.alphaCutoutWarning, Styles.warningStyle);
160149
}
161150
}
162151

152+
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
153+
{
154+
base.AssignNewShaderToMaterial(material, oldShader, newShader);
155+
156+
UpdateMaterialKeywords(material);
157+
}
158+
163159
private void DoBlendMode()
164160
{
165161
int modeValue = (int)blendModeProp.floatValue;
@@ -175,15 +171,15 @@ private void DoBlendMode()
175171
if (mode == BlendMode.Opaque)
176172
{
177173
int glossSource = (int)glossinessSourceProp.floatValue;
178-
TexturePropertySingleLine(Styles.albedoGlosinessLabels[glossSource], albedoMapProp, albedoColorProp);
179-
TextureScaleOffsetProperty(albedoMapProp);
174+
m_MaterialEditor.TexturePropertySingleLine(Styles.albedoGlosinessLabels[glossSource], albedoMapProp, albedoColorProp);
175+
m_MaterialEditor.TextureScaleOffsetProperty(albedoMapProp);
180176
}
181177
else
182178
{
183-
TexturePropertySingleLine(Styles.albedoAlphaLabel, albedoMapProp, albedoColorProp);
184-
TextureScaleOffsetProperty(albedoMapProp);
179+
m_MaterialEditor.TexturePropertySingleLine(Styles.albedoAlphaLabel, albedoMapProp, albedoColorProp);
180+
m_MaterialEditor.TextureScaleOffsetProperty(albedoMapProp);
185181
if (mode == BlendMode.Cutout)
186-
RangeProperty(alphaCutoffProp, "Cutoff");
182+
m_MaterialEditor.RangeProperty(alphaCutoffProp, "Cutoff");
187183
}
188184
}
189185

@@ -215,12 +211,12 @@ private void DoSpecular()
215211
int glossSource = (int)glossinessSourceProp.floatValue;
216212
if (specSource == SpecularSource.SpecularTextureAndColor)
217213
{
218-
TexturePropertySingleLine(Styles.specularGlossMapLabels[glossSource], specularGlossMapProp, specularColorProp);
214+
m_MaterialEditor.TexturePropertySingleLine(Styles.specularGlossMapLabels[glossSource], specularGlossMapProp, specularColorProp);
219215
}
220216

221217
if (specSource != SpecularSource.NoSpecular)
222218
{
223-
RangeProperty(shininessProp, Styles.shininessLabel);
219+
m_MaterialEditor.RangeProperty(shininessProp, Styles.shininessLabel);
224220
}
225221
}
226222

@@ -237,7 +233,7 @@ private void DoReflection()
237233
EditorGUILayout.Space();
238234
ReflectionSource reflectionSource = (ReflectionSource) reflectionSourceProp.floatValue;
239235
if (reflectionSource == ReflectionSource.Cubemap)
240-
TexturePropertySingleLine(Styles.reflectionMapLabel, reflectionMapProp, reflectionColorProp);
236+
m_MaterialEditor.TexturePropertySingleLine(Styles.reflectionMapLabel, reflectionMapProp, reflectionColorProp);
241237
}
242238

243239
private void UpdateMaterialKeywords(Material material)
@@ -252,7 +248,8 @@ private void UpdateMaterialKeywords(Material material)
252248

253249
private void UpdateMaterialBlendMode(Material material)
254250
{
255-
BlendMode mode = (BlendMode)blendModeProp.floatValue;
251+
Debug.Log("BlendMode");
252+
BlendMode mode = (BlendMode) material.GetFloat("_Mode");
256253
switch (mode)
257254
{
258255
case BlendMode.Opaque:
@@ -286,7 +283,8 @@ private void UpdateMaterialBlendMode(Material material)
286283

287284
private void UpdateMaterialSpecularSource(Material material)
288285
{
289-
SpecularSource specSource = (SpecularSource)specularSourceProp.floatValue;
286+
Debug.Log("SpecularSource");
287+
SpecularSource specSource = (SpecularSource) material.GetFloat("_SpecSource");
290288
if (specSource == SpecularSource.NoSpecular)
291289
{
292290
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);
@@ -312,7 +310,7 @@ private void UpdateMaterialSpecularSource(Material material)
312310
SetKeyword(material, "_SPECULAR_COLOR", true);
313311
}
314312

315-
GlossinessSource glossSource = (GlossinessSource)glossinessSourceProp.floatValue;
313+
GlossinessSource glossSource = (GlossinessSource) material.GetFloat("_GlossinessSource");
316314
if (glossSource == GlossinessSource.BaseAlpha)
317315
SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", true);
318316
else
@@ -321,7 +319,8 @@ private void UpdateMaterialSpecularSource(Material material)
321319

322320
private void UpdateMaterialReflectionSource(Material material)
323321
{
324-
ReflectionSource reflectionSource = (ReflectionSource) reflectionSourceProp.floatValue;
322+
Debug.Log("ReflectionSource");
323+
ReflectionSource reflectionSource = (ReflectionSource) material.GetFloat("_ReflectionSource");
325324
if (reflectionSource == ReflectionSource.NoReflection)
326325
{
327326
SetKeyword(material, "_CUBEMAP_REFLECTION", false);

Assets/ScriptableRenderPipeline/Editor/MaterialUpgrader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void Upgrade(Material material, UpgradeFlags flags)
4747
material.shaderKeywords = new string[0];
4848

4949
var matEditor = Editor.CreateEditor(material) as MaterialEditor;
50-
matEditor.SetShader(Shader.Find(m_NewShader), false);
50+
matEditor.SetShader(material.shader, false);
5151
matEditor.serializedObject.ApplyModifiedPropertiesWithoutUndo();
5252
}
5353

0 commit comments

Comments
 (0)