您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
105 行
4.7 KiB
105 行
4.7 KiB
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public abstract class LightweightShaderGUI : ShaderGUI
|
|
{
|
|
public enum BlendMode
|
|
{
|
|
Opaque,
|
|
Cutout,
|
|
Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency
|
|
Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
|
|
}
|
|
|
|
public abstract void FindProperties(MaterialProperty[] props);
|
|
public abstract void ShaderPropertiesGUI(Material material);
|
|
public abstract void MaterialChanged(Material material);
|
|
|
|
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
|
|
{
|
|
FindProperties(properties); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly
|
|
m_MaterialEditor = materialEditor;
|
|
Material material = materialEditor.target as Material;
|
|
|
|
// Make sure that needed setup (ie keywords/renderqueue) are set up if we're switching some existing
|
|
// material to a lightweight shader.
|
|
if (m_FirstTimeApply)
|
|
{
|
|
MaterialChanged(material);
|
|
m_FirstTimeApply = false;
|
|
}
|
|
|
|
ShaderPropertiesGUI(material);
|
|
}
|
|
|
|
protected void DoPopup(string label, MaterialProperty property, string[] options)
|
|
{
|
|
EditorGUI.showMixedValue = property.hasMixedValue;
|
|
|
|
var mode = property.floatValue;
|
|
EditorGUI.BeginChangeCheck();
|
|
mode = EditorGUILayout.Popup(label, (int)mode, options);
|
|
if (EditorGUI.EndChangeCheck())
|
|
{
|
|
m_MaterialEditor.RegisterPropertyChangeUndo(label);
|
|
property.floatValue = (float)mode;
|
|
}
|
|
|
|
EditorGUI.showMixedValue = false;
|
|
}
|
|
|
|
public static void SetupMaterialBlendMode(Material material)
|
|
{
|
|
BlendMode blendMode = (BlendMode)material.GetFloat("_Mode");
|
|
switch (blendMode)
|
|
{
|
|
case BlendMode.Opaque:
|
|
material.SetOverrideTag("RenderType", "");
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
|
|
material.SetInt("_ZWrite", 1);
|
|
material.DisableKeyword("_ALPHATEST_ON");
|
|
material.DisableKeyword("_ALPHABLEND_ON");
|
|
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
|
|
material.renderQueue = -1;
|
|
material.SetShaderPassEnabled("ShadowCaster", true);
|
|
break;
|
|
case BlendMode.Cutout:
|
|
material.SetOverrideTag("RenderType", "TransparentCutout");
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
|
|
material.SetInt("_ZWrite", 1);
|
|
material.EnableKeyword("_ALPHATEST_ON");
|
|
material.DisableKeyword("_ALPHABLEND_ON");
|
|
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
|
|
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
|
|
material.SetShaderPassEnabled("ShadowCaster", true);
|
|
break;
|
|
case BlendMode.Fade:
|
|
material.SetOverrideTag("RenderType", "Transparent");
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
|
|
material.SetInt("_ZWrite", 0);
|
|
material.DisableKeyword("_ALPHATEST_ON");
|
|
material.EnableKeyword("_ALPHABLEND_ON");
|
|
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
|
|
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
|
|
material.SetShaderPassEnabled("ShadowCaster", false);
|
|
break;
|
|
case BlendMode.Transparent:
|
|
material.SetOverrideTag("RenderType", "Transparent");
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
|
|
material.SetInt("_ZWrite", 0);
|
|
material.DisableKeyword("_ALPHATEST_ON");
|
|
material.DisableKeyword("_ALPHABLEND_ON");
|
|
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
|
|
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
|
|
material.SetShaderPassEnabled("ShadowCaster", false);
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected MaterialEditor m_MaterialEditor;
|
|
private bool m_FirstTimeApply = true;
|
|
}
|