浏览代码

Implemented AssignNewShader callback to update shader keyword upon material upgrade.

/vr_sandbox
Felipe Lira 8 年前
当前提交
8753342a
共有 3 个文件被更改,包括 68 次插入68 次删除
  1. 5
      Assets/LowEndMobilePipeline/Editor/LegacyShadersToLowEndUpgrader.cs
  2. 129
      Assets/LowEndMobilePipeline/Editor/LowendMobilePipelineMaterialEditor.cs
  3. 2
      Assets/ScriptableRenderPipeline/Editor/MaterialUpgrader.cs

5
Assets/LowEndMobilePipeline/Editor/LegacyShadersToLowEndUpgrader.cs


MaterialUpgrader.UpgradeProjectFolder(materialUpgraders, "Upgrade to LD Materials");
}
// TODO: Replace this logic with AssignNewShaderToMaterial
private static void GetUpgraders(ref List<MaterialUpgrader> materialUpgraders)
{
/////////////////////////////////////

// Alpha Blended
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Diffuse", SupportedUpgradeParams.diffuseAlpha));
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Specular", SupportedUpgradeParams.specularCubemap));
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Specular", SupportedUpgradeParams.specularAlpha));
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Bumped Specular", SupportedUpgradeParams.specularCubemap));
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Bumped Specular", SupportedUpgradeParams.specularAlpha));
// Cutout
materialUpgraders.Add(new LegacyShadersToLowEndUpgrader("Legacy Shaders/Transparent/Cutout/Diffuse", SupportedUpgradeParams.diffuseAlphaCutout));

129
Assets/LowEndMobilePipeline/Editor/LowendMobilePipelineMaterialEditor.cs


using UnityEditor;
using UnityEngine;
public class LowendMobilePipelineMaterialEditor : MaterialEditor
public class LowendMobilePipelineMaterialEditor : ShaderGUI
{
private MaterialProperty blendModeProp = null;
private MaterialProperty albedoMapProp = null;

private MaterialProperty emissionColorProp = null;
private MaterialProperty reflectionMapProp = null;
private MaterialProperty reflectionColorProp = null;
private MaterialEditor m_MaterialEditor = null;
public enum BlendMode
{

ReflectionProbe
}
private void Awake()
{
Material material = target as Material;
FindMaterialProperties(material);
UpdateMaterialKeywords(material);
Styles.warningStyle.normal.textColor = Color.yellow;
}
private static class Styles
{
public static GUIContent[] albedoGlosinessLabels =

public static string reflectionSourceLabel = "Reflection Source";
}
private void FindMaterialProperties(Material material)
private void FindMaterialProperties(MaterialProperty[] properties)
Material[] mats = { material };
blendModeProp = GetMaterialProperty(mats, "_Mode");
albedoMapProp = GetMaterialProperty(mats, "_MainTex");
albedoColorProp = GetMaterialProperty(mats, "_Color");
blendModeProp = FindProperty("_Mode", properties);
albedoMapProp = FindProperty("_MainTex", properties);
albedoColorProp = FindProperty("_Color", properties);
alphaCutoffProp = GetMaterialProperty(mats, "_Cutoff");
specularSourceProp = GetMaterialProperty(mats, "_SpecSource");
glossinessSourceProp = GetMaterialProperty(mats, "_GlossinessSource");
specularGlossMapProp = GetMaterialProperty(mats, "_SpecGlossMap");
specularColorProp = GetMaterialProperty(mats, "_SpecColor");
shininessProp = GetMaterialProperty(mats, "_Shininess");
bumpMapProp = GetMaterialProperty(mats, "_BumpMap");
emissionMapProp = GetMaterialProperty(mats, "_EmissionMap");
emissionColorProp = GetMaterialProperty(mats, "_EmissionColor");
reflectionMapProp = GetMaterialProperty(mats, "_Cube");
reflectionColorProp = GetMaterialProperty(mats, "_ReflectColor");
reflectionSourceProp = GetMaterialProperty(mats, "_ReflectionSource");
alphaCutoffProp = FindProperty("_Cutoff", properties);
specularSourceProp = FindProperty("_SpecSource", properties);
glossinessSourceProp = FindProperty("_GlossinessSource", properties);
specularGlossMapProp = FindProperty("_SpecGlossMap", properties);
specularColorProp = FindProperty("_SpecColor", properties);
shininessProp = FindProperty("_Shininess", properties);
bumpMapProp = FindProperty("_BumpMap", properties);
emissionMapProp = FindProperty("_EmissionMap", properties);
emissionColorProp = FindProperty("_EmissionColor", properties);
reflectionMapProp = FindProperty("_Cube", properties);
reflectionColorProp = FindProperty("_ReflectColor", properties);
reflectionSourceProp = FindProperty("_ReflectionSource", properties);
public override void OnInspectorGUI()
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
serializedObject.Update();
var shaderObj = serializedObject.FindProperty("m_Shader");
if (isVisible && shaderObj.objectReferenceValue != null)
{
Material material = target as Material;
FindMaterialProperties(material);
Material material = materialEditor.target as Material;
m_MaterialEditor = materialEditor;
EditorGUI.BeginChangeCheck();
DoBlendMode();
FindMaterialProperties(properties);
EditorGUILayout.Space();
DoSpecular();
EditorGUI.BeginChangeCheck();
DoBlendMode();
EditorGUILayout.Space();
DoSpecular();
EditorGUILayout.Space();
TexturePropertySingleLine(Styles.normalMapText, bumpMapProp);
EditorGUILayout.Space();
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMapProp);
EditorGUILayout.Space();
TexturePropertySingleLine(Styles.emissionMapLabel, emissionMapProp, emissionColorProp);
EditorGUILayout.Space();
m_MaterialEditor.TexturePropertySingleLine(Styles.emissionMapLabel, emissionMapProp, emissionColorProp);
EditorGUILayout.Space();
DoReflection();
EditorGUILayout.Space();
DoReflection();
if (EditorGUI.EndChangeCheck())
UpdateMaterialKeywords(material);
if (EditorGUI.EndChangeCheck())
UpdateMaterialKeywords(material);
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space();
if ((BlendMode)blendModeProp.floatValue == BlendMode.Cutout)
{
Styles.warningStyle.normal.textColor = Color.yellow;
EditorGUILayout.LabelField(Styles.alphaCutoutWarning, Styles.warningStyle);
}
if ((BlendMode)blendModeProp.floatValue == BlendMode.Cutout)
{
Styles.warningStyle.normal.textColor = Color.yellow;
EditorGUILayout.LabelField(Styles.alphaCutoutWarning, Styles.warningStyle);
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
{
base.AssignNewShaderToMaterial(material, oldShader, newShader);
UpdateMaterialKeywords(material);
}
private void DoBlendMode()
{
int modeValue = (int)blendModeProp.floatValue;

if (mode == BlendMode.Opaque)
{
int glossSource = (int)glossinessSourceProp.floatValue;
TexturePropertySingleLine(Styles.albedoGlosinessLabels[glossSource], albedoMapProp, albedoColorProp);
TextureScaleOffsetProperty(albedoMapProp);
m_MaterialEditor.TexturePropertySingleLine(Styles.albedoGlosinessLabels[glossSource], albedoMapProp, albedoColorProp);
m_MaterialEditor.TextureScaleOffsetProperty(albedoMapProp);
TexturePropertySingleLine(Styles.albedoAlphaLabel, albedoMapProp, albedoColorProp);
TextureScaleOffsetProperty(albedoMapProp);
m_MaterialEditor.TexturePropertySingleLine(Styles.albedoAlphaLabel, albedoMapProp, albedoColorProp);
m_MaterialEditor.TextureScaleOffsetProperty(albedoMapProp);
RangeProperty(alphaCutoffProp, "Cutoff");
m_MaterialEditor.RangeProperty(alphaCutoffProp, "Cutoff");
}
}

int glossSource = (int)glossinessSourceProp.floatValue;
if (specSource == SpecularSource.SpecularTextureAndColor)
{
TexturePropertySingleLine(Styles.specularGlossMapLabels[glossSource], specularGlossMapProp, specularColorProp);
m_MaterialEditor.TexturePropertySingleLine(Styles.specularGlossMapLabels[glossSource], specularGlossMapProp, specularColorProp);
RangeProperty(shininessProp, Styles.shininessLabel);
m_MaterialEditor.RangeProperty(shininessProp, Styles.shininessLabel);
}
}

EditorGUILayout.Space();
ReflectionSource reflectionSource = (ReflectionSource) reflectionSourceProp.floatValue;
if (reflectionSource == ReflectionSource.Cubemap)
TexturePropertySingleLine(Styles.reflectionMapLabel, reflectionMapProp, reflectionColorProp);
m_MaterialEditor.TexturePropertySingleLine(Styles.reflectionMapLabel, reflectionMapProp, reflectionColorProp);
}
private void UpdateMaterialKeywords(Material material)

private void UpdateMaterialBlendMode(Material material)
{
BlendMode mode = (BlendMode)blendModeProp.floatValue;
Debug.Log("BlendMode");
BlendMode mode = (BlendMode) material.GetFloat("_Mode");
switch (mode)
{
case BlendMode.Opaque:

private void UpdateMaterialSpecularSource(Material material)
{
SpecularSource specSource = (SpecularSource)specularSourceProp.floatValue;
Debug.Log("SpecularSource");
SpecularSource specSource = (SpecularSource) material.GetFloat("_SpecSource");
if (specSource == SpecularSource.NoSpecular)
{
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);

SetKeyword(material, "_SPECULAR_COLOR", true);
}
GlossinessSource glossSource = (GlossinessSource)glossinessSourceProp.floatValue;
GlossinessSource glossSource = (GlossinessSource) material.GetFloat("_GlossinessSource");
if (glossSource == GlossinessSource.BaseAlpha)
SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", true);
else

private void UpdateMaterialReflectionSource(Material material)
{
ReflectionSource reflectionSource = (ReflectionSource) reflectionSourceProp.floatValue;
Debug.Log("ReflectionSource");
ReflectionSource reflectionSource = (ReflectionSource) material.GetFloat("_ReflectionSource");
if (reflectionSource == ReflectionSource.NoReflection)
{
SetKeyword(material, "_CUBEMAP_REFLECTION", false);

2
Assets/ScriptableRenderPipeline/Editor/MaterialUpgrader.cs


material.shaderKeywords = new string[0];
var matEditor = Editor.CreateEditor(material) as MaterialEditor;
matEditor.SetShader(Shader.Find(m_NewShader), false);
matEditor.SetShader(material.shader, false);
matEditor.serializedObject.ApplyModifiedPropertiesWithoutUndo();
}

正在加载...
取消
保存