您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
316 行
12 KiB
316 行
12 KiB
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace UnityEditor.Experimental.ScriptableRenderLoop
|
|
{
|
|
internal class UnlitGUI : ShaderGUI
|
|
{
|
|
public enum SurfaceType
|
|
{
|
|
Opaque,
|
|
Transparent
|
|
}
|
|
public enum BlendMode
|
|
{
|
|
Lerp,
|
|
Add,
|
|
SoftAdd,
|
|
Multiply,
|
|
Premultiply
|
|
}
|
|
|
|
public enum DoubleSidedMode
|
|
{
|
|
None,
|
|
DoubleSided
|
|
}
|
|
|
|
private static class Styles
|
|
{
|
|
public static string OptionText = "Options";
|
|
public static string SurfaceTypeText = "Surface Type";
|
|
public static string BlendModeText = "Blend Mode";
|
|
|
|
public static GUIContent alphaCutoffEnableText = new GUIContent("Alpha Cutoff Enable", "Threshold for alpha cutoff");
|
|
public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
|
|
public static GUIContent doubleSidedModeText = new GUIContent("Double Sided", "This will render the two face of the objects (disable backface culling)");
|
|
|
|
public static readonly string[] surfaceTypeNames = Enum.GetNames(typeof(SurfaceType));
|
|
public static readonly string[] blendModeNames = Enum.GetNames(typeof(BlendMode));
|
|
|
|
public static string InputsOptionsText = "Inputs options";
|
|
|
|
public static string InputsText = "Inputs";
|
|
|
|
public static string InputsMapText = "";
|
|
|
|
public static GUIContent colorText = new GUIContent("Color + Opacity", "Albedo (RGB) and Opacity (A)");
|
|
|
|
public static GUIContent emissiveText = new GUIContent("Emissive Color", "Emissive");
|
|
public static GUIContent emissiveIntensityText = new GUIContent("Emissive Intensity", "Emissive");
|
|
}
|
|
|
|
MaterialProperty surfaceType = null;
|
|
MaterialProperty blendMode = null;
|
|
MaterialProperty alphaCutoff = null;
|
|
MaterialProperty alphaCutoffEnable = null;
|
|
MaterialProperty doubleSidedMode = null;
|
|
|
|
MaterialProperty color = null;
|
|
MaterialProperty colorMap = null;
|
|
|
|
MaterialProperty emissiveColor = null;
|
|
MaterialProperty emissiveColorMap = null;
|
|
MaterialProperty emissiveIntensity = null;
|
|
|
|
protected MaterialEditor m_MaterialEditor;
|
|
|
|
protected const string kSurfaceType = "_SurfaceType";
|
|
protected const string kBlendMode = "_BlendMode";
|
|
protected const string kAlphaCutoff = "_AlphaCutoff";
|
|
protected const string kAlphaCutoffEnabled = "_AlphaCutoffEnable";
|
|
protected const string kDoubleSidedMode = "_DoubleSidedMode";
|
|
|
|
protected const string kEmissiveColorMap = "_EmissiveColorMap";
|
|
|
|
public void FindOptionProperties(MaterialProperty[] props)
|
|
{
|
|
surfaceType = FindProperty(kSurfaceType, props);
|
|
blendMode = FindProperty(kBlendMode, props);
|
|
alphaCutoff = FindProperty(kAlphaCutoff, props);
|
|
alphaCutoffEnable = FindProperty(kAlphaCutoffEnabled, props);
|
|
doubleSidedMode = FindProperty(kDoubleSidedMode, props);
|
|
}
|
|
|
|
public void FindInputProperties(MaterialProperty[] props)
|
|
{
|
|
color = FindProperty("_Color", props);
|
|
colorMap = FindProperty("_ColorMap", props);
|
|
emissiveColor = FindProperty("_EmissiveColor", props);
|
|
emissiveColorMap = FindProperty(kEmissiveColorMap, props);
|
|
emissiveIntensity = FindProperty("_EmissiveIntensity", props);
|
|
}
|
|
|
|
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
|
|
{
|
|
FindOptionProperties(props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly
|
|
FindInputProperties(props);
|
|
|
|
m_MaterialEditor = materialEditor;
|
|
Material material = materialEditor.target as Material;
|
|
ShaderPropertiesGUI(material);
|
|
}
|
|
|
|
protected void ShaderOptionsGUI()
|
|
{
|
|
EditorGUI.indentLevel++;
|
|
GUILayout.Label(Styles.OptionText, EditorStyles.boldLabel);
|
|
SurfaceTypePopup();
|
|
if ((SurfaceType)surfaceType.floatValue == SurfaceType.Transparent)
|
|
{
|
|
BlendModePopup();
|
|
}
|
|
m_MaterialEditor.ShaderProperty(alphaCutoffEnable, Styles.alphaCutoffEnableText.text);
|
|
if (alphaCutoffEnable.floatValue == 1.0)
|
|
{
|
|
m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText.text);
|
|
}
|
|
m_MaterialEditor.ShaderProperty(doubleSidedMode, Styles.doubleSidedModeText.text);
|
|
|
|
EditorGUI.indentLevel--;
|
|
}
|
|
|
|
protected void ShaderInputOptionsGUI()
|
|
{
|
|
EditorGUI.indentLevel++;
|
|
EditorGUI.indentLevel--;
|
|
}
|
|
|
|
protected void ShaderInputGUI()
|
|
{
|
|
EditorGUI.indentLevel++;
|
|
|
|
GUILayout.Label(Styles.InputsText, EditorStyles.boldLabel);
|
|
|
|
m_MaterialEditor.TexturePropertySingleLine(Styles.colorText, colorMap, color);
|
|
|
|
m_MaterialEditor.TexturePropertySingleLine(Styles.emissiveText, emissiveColorMap, emissiveColor);
|
|
m_MaterialEditor.ShaderProperty(emissiveIntensity, Styles.emissiveIntensityText);
|
|
m_MaterialEditor.LightmapEmissionProperty(MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1);
|
|
|
|
EditorGUI.indentLevel--;
|
|
}
|
|
|
|
public void ShaderPropertiesGUI(Material material)
|
|
{
|
|
// Use default labelWidth
|
|
EditorGUIUtility.labelWidth = 0f;
|
|
|
|
// Detect any changes to the material
|
|
EditorGUI.BeginChangeCheck();
|
|
{
|
|
ShaderOptionsGUI();
|
|
EditorGUILayout.Space();
|
|
|
|
ShaderInputOptionsGUI();
|
|
|
|
EditorGUILayout.Space();
|
|
ShaderInputGUI();
|
|
}
|
|
|
|
if (EditorGUI.EndChangeCheck())
|
|
{
|
|
foreach (var obj in m_MaterialEditor.targets)
|
|
SetupMaterial((Material)obj);
|
|
}
|
|
}
|
|
|
|
// TODO: try to setup minimun value to fall back to standard shaders and reverse
|
|
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
|
|
{
|
|
base.AssignNewShaderToMaterial(material, oldShader, newShader);
|
|
}
|
|
|
|
void SurfaceTypePopup()
|
|
{
|
|
EditorGUI.showMixedValue = surfaceType.hasMixedValue;
|
|
var mode = (SurfaceType)surfaceType.floatValue;
|
|
|
|
EditorGUI.BeginChangeCheck();
|
|
mode = (SurfaceType)EditorGUILayout.Popup(Styles.SurfaceTypeText, (int)mode, Styles.surfaceTypeNames);
|
|
if (EditorGUI.EndChangeCheck())
|
|
{
|
|
m_MaterialEditor.RegisterPropertyChangeUndo("Surface Type");
|
|
surfaceType.floatValue = (float)mode;
|
|
}
|
|
|
|
EditorGUI.showMixedValue = false;
|
|
}
|
|
|
|
void BlendModePopup()
|
|
{
|
|
EditorGUI.showMixedValue = blendMode.hasMixedValue;
|
|
var mode = (BlendMode)blendMode.floatValue;
|
|
|
|
EditorGUI.BeginChangeCheck();
|
|
mode = (BlendMode)EditorGUILayout.Popup(Styles.BlendModeText, (int)mode, Styles.blendModeNames);
|
|
if (EditorGUI.EndChangeCheck())
|
|
{
|
|
m_MaterialEditor.RegisterPropertyChangeUndo("Blend Mode");
|
|
blendMode.floatValue = (float)mode;
|
|
}
|
|
|
|
EditorGUI.showMixedValue = false;
|
|
}
|
|
|
|
protected virtual void SetupKeywordsForInputMaps(Material material)
|
|
{
|
|
SetKeyword(material, "_EMISSIVE_COLOR_MAP", material.GetTexture(kEmissiveColorMap));
|
|
}
|
|
|
|
protected void SetupMaterial(Material material)
|
|
{
|
|
// Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation
|
|
// (MaterialProperty value might come from renderer material property block)
|
|
|
|
bool alphaTestEnable = material.GetFloat(kAlphaCutoffEnabled) == 1.0;
|
|
SurfaceType surfaceType = (SurfaceType)material.GetFloat(kSurfaceType);
|
|
BlendMode blendMode = (BlendMode)material.GetFloat(kBlendMode);
|
|
DoubleSidedMode doubleSidedMode = (DoubleSidedMode)material.GetFloat(kDoubleSidedMode);
|
|
|
|
if (surfaceType == SurfaceType.Opaque)
|
|
{
|
|
material.SetOverrideTag("RenderType", alphaTestEnable ? "TransparentCutout" : "");
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
|
|
material.SetInt("_ZWrite", 1);
|
|
material.renderQueue = alphaTestEnable ? (int)UnityEngine.Rendering.RenderQueue.AlphaTest : -1;
|
|
}
|
|
else
|
|
{
|
|
material.SetOverrideTag("RenderType", "Transparent");
|
|
material.SetInt("_ZWrite", 0);
|
|
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
|
|
|
|
switch (blendMode)
|
|
{
|
|
case BlendMode.Lerp:
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
|
|
break;
|
|
|
|
case BlendMode.Add:
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
break;
|
|
|
|
case BlendMode.SoftAdd:
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusDstColor);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
break;
|
|
|
|
case BlendMode.Multiply:
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.DstColor);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
|
|
break;
|
|
|
|
case BlendMode.Premultiply:
|
|
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
|
|
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (doubleSidedMode == DoubleSidedMode.None)
|
|
{
|
|
material.SetInt("_CullMode", (int)UnityEngine.Rendering.CullMode.Back);
|
|
}
|
|
else
|
|
{
|
|
material.SetInt("_CullMode", (int)UnityEngine.Rendering.CullMode.Off);
|
|
}
|
|
|
|
SetKeyword(material, "_ALPHATEST_ON", alphaTestEnable);
|
|
|
|
SetupKeywordsForInputMaps(material);
|
|
|
|
// Setup lightmap emissive flags
|
|
|
|
MaterialGlobalIlluminationFlags flags = material.globalIlluminationFlags;
|
|
if ((flags & (MaterialGlobalIlluminationFlags.BakedEmissive | MaterialGlobalIlluminationFlags.RealtimeEmissive)) != 0)
|
|
{
|
|
if (LitGUI.ShouldEmissionBeEnabled(material))
|
|
flags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;
|
|
else
|
|
flags |= MaterialGlobalIlluminationFlags.EmissiveIsBlack;
|
|
|
|
material.globalIlluminationFlags = flags;
|
|
}
|
|
}
|
|
|
|
bool HasValidEmissiveKeyword(Material material)
|
|
{
|
|
/*
|
|
// Material animation might be out of sync with the material keyword.
|
|
// So if the emission support is disabled on the material, but the property blocks have a value that requires it, then we need to show a warning.
|
|
// (note: (Renderer MaterialPropertyBlock applies its values to emissionColorForRendering))
|
|
bool hasEmissionKeyword = material.IsKeywordEnabled ("_EMISSION");
|
|
if (!hasEmissionKeyword && ShouldEmissionBeEnabled (material, emissionColorForRendering.colorValue))
|
|
return false;
|
|
else
|
|
return true;
|
|
*/
|
|
|
|
return true;
|
|
}
|
|
|
|
protected void SetKeyword(Material m, string keyword, bool state)
|
|
{
|
|
if (state)
|
|
m.EnableKeyword(keyword);
|
|
else
|
|
m.DisableKeyword(keyword);
|
|
}
|
|
}
|
|
} // namespace UnityEditor
|