| | 1 | | // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace UnityEditor |
| | 7 | | { |
| | 8 | | internal class PbrShaderGUI : ShaderGUI |
| | 9 | | { |
| | 10 | | private enum WorkflowMode |
| | 11 | | { |
| | 12 | | SpecularGlossiness, |
| | 13 | | MetallicRoughness, |
| | 14 | | Unlit |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public enum BlendMode |
| | 18 | | { |
| | 19 | | Opaque, |
| | 20 | | Mask, |
| | 21 | | Blend |
| | 22 | | } |
| | 23 | |
|
| | 24 | | private static class Styles |
| | 25 | | { |
| 0 | 26 | | public static GUIContent albedoText = new GUIContent("Base Color", "Albedo (RGB) and Transparency (A)"); |
| 0 | 27 | | public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff"); |
| 0 | 28 | | public static GUIContent specularMapText = new GUIContent("Spec Gloss", "Specular (RGB) and Glossiness (A)") |
| 0 | 29 | | public static GUIContent metallicMapText = new GUIContent("Metal Rough", "Metallic (B) and Roughness (G)"); |
| 0 | 30 | | public static GUIContent metallicText = new GUIContent("Metallic", "Metallic value"); |
| 0 | 31 | | public static GUIContent metallicScaleText = new GUIContent("Metallic", "Metallic scale factor"); |
| 0 | 32 | | public static GUIContent roughnessText = new GUIContent("Roughness", "Roughness value"); |
| | 33 | |
|
| 0 | 34 | | public static GUIContent roughnessScaleText = new GUIContent("Roughness", "Roughness scale factor"); |
| 0 | 35 | | public static GUIContent glossinessText = new GUIContent("Glossiness", "Glossiness value"); |
| 0 | 36 | | public static GUIContent glossinessScaleText = new GUIContent("Glossiness", "Glossiness scale factor"); |
| 0 | 37 | | public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map"); |
| 0 | 38 | | public static GUIContent occlusionText = new GUIContent("Occlusion", "Occlusion (R)"); |
| 0 | 39 | | public static GUIContent emissionText = new GUIContent("Emissive", "Emissive (RGB)"); |
| | 40 | |
|
| 0 | 41 | | public static string primaryMapsText = "Main Maps"; |
| 0 | 42 | | public static string renderingMode = "Rendering Mode"; |
| 0 | 43 | | public static GUIContent emissiveWarning = new GUIContent("Emissive value is animated but the material has n |
| 0 | 44 | | public static readonly string[] blendNames = Enum.GetNames(typeof(BlendMode)); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | MaterialProperty blendMode = null; |
| | 48 | | MaterialProperty albedoMap = null; |
| | 49 | | MaterialProperty albedoColor = null; |
| | 50 | | MaterialProperty alphaCutoff = null; |
| | 51 | | MaterialProperty specularMap = null; |
| | 52 | | MaterialProperty specularColor = null; |
| | 53 | | MaterialProperty metallicMap = null; |
| | 54 | | MaterialProperty metallic = null; |
| | 55 | | MaterialProperty smoothness = null; |
| | 56 | | MaterialProperty bumpScale = null; |
| | 57 | | MaterialProperty bumpMap = null; |
| | 58 | | MaterialProperty occlusionStrength = null; |
| | 59 | | MaterialProperty occlusionMap = null; |
| | 60 | | MaterialProperty emissionColor = null; |
| | 61 | | MaterialProperty emissionMap = null; |
| | 62 | |
|
| | 63 | | MaterialEditor m_MaterialEditor; |
| 0 | 64 | | WorkflowMode m_WorkflowMode = WorkflowMode.MetallicRoughness; |
| | 65 | |
|
| 0 | 66 | | bool m_FirstTimeApply = true; |
| | 67 | |
|
| | 68 | | public void FindProperties(MaterialProperty[] props) |
| | 69 | | { |
| 0 | 70 | | blendMode = FindProperty("_Mode", props); |
| 0 | 71 | | albedoMap = FindProperty("_MainTex", props); |
| 0 | 72 | | albedoColor = FindProperty("_Color", props); |
| 0 | 73 | | alphaCutoff = FindProperty("_Cutoff", props); |
| 0 | 74 | | specularMap = FindProperty("_SpecGlossMap", props, false); |
| 0 | 75 | | specularColor = FindProperty("_SpecColor", props, false); |
| 0 | 76 | | metallicMap = FindProperty("_MetallicGlossMap", props, false); |
| 0 | 77 | | metallic = FindProperty("_Metallic", props, false); |
| 0 | 78 | | if (specularMap != null && specularColor != null) |
| | 79 | | { |
| 0 | 80 | | m_WorkflowMode = WorkflowMode.SpecularGlossiness; |
| 0 | 81 | | } |
| 0 | 82 | | else if (metallicMap != null && metallic != null) |
| | 83 | | { |
| 0 | 84 | | m_WorkflowMode = WorkflowMode.MetallicRoughness; |
| 0 | 85 | | } |
| | 86 | | else |
| | 87 | | { |
| 0 | 88 | | m_WorkflowMode = WorkflowMode.Unlit; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | smoothness = FindProperty("_Glossiness", props); |
| 0 | 92 | | bumpScale = FindProperty("_BumpScale", props); |
| 0 | 93 | | bumpMap = FindProperty("_BumpMap", props); |
| 0 | 94 | | occlusionStrength = FindProperty("_OcclusionStrength", props); |
| 0 | 95 | | occlusionMap = FindProperty("_OcclusionMap", props); |
| 0 | 96 | | emissionColor = FindProperty("_EmissionColor", props); |
| 0 | 97 | | emissionMap = FindProperty("_EmissionMap", props); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) |
| | 101 | | { |
| 0 | 102 | | FindProperties(props); // MaterialProperties can be animated so we do not cache them but fetch them every ev |
| 0 | 103 | | m_MaterialEditor = materialEditor; |
| 0 | 104 | | Material material = materialEditor.target as Material; |
| | 105 | |
|
| | 106 | | // Make sure that needed setup (ie keywords/renderqueue) are set up if we're switching some existing |
| | 107 | | // material to a standard shader. |
| | 108 | | // Do this before any GUI code has been issued to prevent layout issues in subsequent GUILayout statements ( |
| 0 | 109 | | if (m_FirstTimeApply) |
| | 110 | | { |
| 0 | 111 | | MaterialChanged(material, m_WorkflowMode); |
| 0 | 112 | | m_FirstTimeApply = false; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | ShaderPropertiesGUI(material); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public void ShaderPropertiesGUI(Material material) |
| | 119 | | { |
| | 120 | | // Use default labelWidth |
| 0 | 121 | | EditorGUIUtility.labelWidth = 0f; |
| | 122 | |
|
| | 123 | | // Detect any changes to the material |
| 0 | 124 | | EditorGUI.BeginChangeCheck(); |
| | 125 | | { |
| 0 | 126 | | BlendModePopup(); |
| | 127 | |
|
| | 128 | | // Primary properties |
| 0 | 129 | | GUILayout.Label(Styles.primaryMapsText, EditorStyles.boldLabel); |
| 0 | 130 | | DoAlbedoArea(material); |
| 0 | 131 | | DoSpecularMetallicArea(); |
| 0 | 132 | | m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? |
| 0 | 133 | | m_MaterialEditor.TexturePropertySingleLine(Styles.occlusionText, occlusionMap, occlusionMap.textureValue |
| 0 | 134 | | DoEmissionArea(material); |
| | 135 | |
|
| 0 | 136 | | EditorGUILayout.Space(); |
| | 137 | | } |
| 0 | 138 | | if (EditorGUI.EndChangeCheck()) |
| | 139 | | { |
| 0 | 140 | | foreach (var obj in blendMode.targets) |
| | 141 | | { |
| 0 | 142 | | MaterialChanged((Material)obj, m_WorkflowMode); |
| | 143 | | } |
| | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | internal void DetermineWorkflow(MaterialProperty[] props) |
| | 148 | | { |
| 0 | 149 | | if (FindProperty("_SpecGlossMap", props, false) != null && FindProperty("_SpecColor", props, false) != null) |
| | 150 | | { |
| 0 | 151 | | m_WorkflowMode = WorkflowMode.SpecularGlossiness; |
| 0 | 152 | | } |
| 0 | 153 | | else if (FindProperty("_MetallicGlossMap", props, false) != null && FindProperty("_Metallic", props, false) |
| | 154 | | { |
| 0 | 155 | | m_WorkflowMode = WorkflowMode.MetallicRoughness; |
| 0 | 156 | | } |
| | 157 | | else |
| | 158 | | { |
| 0 | 159 | | m_WorkflowMode = WorkflowMode.Unlit; |
| | 160 | | } |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | void BlendModePopup() |
| | 164 | | { |
| 0 | 165 | | EditorGUI.showMixedValue = blendMode.hasMixedValue; |
| 0 | 166 | | var mode = (BlendMode)blendMode.floatValue; |
| | 167 | |
|
| 0 | 168 | | EditorGUI.BeginChangeCheck(); |
| 0 | 169 | | mode = (BlendMode)EditorGUILayout.Popup(Styles.renderingMode, (int)mode, Styles.blendNames); |
| 0 | 170 | | if (EditorGUI.EndChangeCheck()) |
| | 171 | | { |
| 0 | 172 | | m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode"); |
| 0 | 173 | | blendMode.floatValue = (float)mode; |
| | 174 | | } |
| | 175 | |
|
| 0 | 176 | | EditorGUI.showMixedValue = false; |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | void DoAlbedoArea(Material material) |
| | 180 | | { |
| 0 | 181 | | m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoMap, albedoColor); |
| 0 | 182 | | if (((BlendMode)material.GetFloat("_Mode") == BlendMode.Mask)) |
| | 183 | | { |
| 0 | 184 | | m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText.text, MaterialEditor.kMiniTextureFie |
| | 185 | | } |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | void DoEmissionArea(Material material) |
| | 189 | | { |
| 0 | 190 | | bool hadEmissionTexture = emissionMap.textureValue != null; |
| | 191 | |
|
| | 192 | | // Texture and HDR color controls |
| 0 | 193 | | m_MaterialEditor.TexturePropertySingleLine(Styles.emissionText, emissionMap, emissionColor); |
| | 194 | |
|
| | 195 | | // If texture was assigned and color was black set color to white |
| 0 | 196 | | float brightness = emissionColor.colorValue.maxColorComponent; |
| 0 | 197 | | if (emissionMap.textureValue != null && !hadEmissionTexture && brightness <= 0f) |
| | 198 | | { |
| 0 | 199 | | emissionColor.colorValue = Color.white; |
| | 200 | | } |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | void DoSpecularMetallicArea() |
| | 204 | | { |
| 0 | 205 | | bool hasGlossMap = false; |
| 0 | 206 | | if (m_WorkflowMode == WorkflowMode.SpecularGlossiness) |
| | 207 | | { |
| 0 | 208 | | hasGlossMap = specularMap.textureValue != null; |
| 0 | 209 | | m_MaterialEditor.TexturePropertySingleLine(Styles.specularMapText, specularMap, hasGlossMap ? null : spe |
| 0 | 210 | | m_MaterialEditor.ShaderProperty(smoothness, hasGlossMap ? Styles.glossinessScaleText : Styles.glossiness |
| 0 | 211 | | } |
| 0 | 212 | | else if (m_WorkflowMode == WorkflowMode.MetallicRoughness) |
| | 213 | | { |
| 0 | 214 | | hasGlossMap = metallicMap.textureValue != null; |
| 0 | 215 | | m_MaterialEditor.TexturePropertySingleLine(Styles.metallicMapText, metallicMap); |
| 0 | 216 | | m_MaterialEditor.ShaderProperty(metallic, hasGlossMap ? Styles.metallicScaleText : Styles.metallicText, |
| 0 | 217 | | m_MaterialEditor.ShaderProperty(smoothness, hasGlossMap ? Styles.roughnessScaleText : Styles.roughnessTe |
| | 218 | | } |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode, float alphaCutoff) |
| | 222 | | { |
| | 223 | | switch (blendMode) |
| | 224 | | { |
| | 225 | | case BlendMode.Opaque: |
| 0 | 226 | | material.SetOverrideTag("RenderType", "Opaque"); |
| 0 | 227 | | material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); |
| 0 | 228 | | material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); |
| 0 | 229 | | material.SetInt("_ZWrite", 1); |
| 0 | 230 | | material.DisableKeyword("_ALPHATEST_ON"); |
| 0 | 231 | | material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); |
| 0 | 232 | | material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Geometry; |
| 0 | 233 | | break; |
| | 234 | | case BlendMode.Mask: |
| 0 | 235 | | material.SetOverrideTag("RenderType", "TransparentCutout"); |
| 0 | 236 | | material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); |
| 0 | 237 | | material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); |
| 0 | 238 | | material.SetInt("_ZWrite", 1); |
| 0 | 239 | | material.EnableKeyword("_ALPHATEST_ON"); |
| 0 | 240 | | material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); |
| 0 | 241 | | material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; |
| 0 | 242 | | material.SetFloat("_Cutoff", alphaCutoff); |
| 0 | 243 | | break; |
| | 244 | | case BlendMode.Blend: |
| 0 | 245 | | material.SetOverrideTag("RenderType", "Transparent"); |
| 0 | 246 | | material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); |
| 0 | 247 | | material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); |
| 0 | 248 | | material.SetInt("_ZWrite", 0); |
| 0 | 249 | | material.DisableKeyword("_ALPHATEST_ON"); |
| 0 | 250 | | material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); |
| 0 | 251 | | material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; |
| | 252 | | break; |
| | 253 | | } |
| | 254 | |
|
| 0 | 255 | | material.SetFloat("_Surface", (float)blendMode); |
| 0 | 256 | | } |
| | 257 | |
|
| | 258 | | static void SetMaterialKeywords(Material material, WorkflowMode workflowMode) |
| | 259 | | { |
| | 260 | | // Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material anima |
| | 261 | | // (MaterialProperty value might come from renderer material property block) |
| 0 | 262 | | SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap")); |
| 0 | 263 | | if (workflowMode == WorkflowMode.SpecularGlossiness) |
| | 264 | | { |
| 0 | 265 | | SetKeyword(material, "_SPECGLOSSMAP", material.GetTexture("_SpecGlossMap")); |
| 0 | 266 | | } |
| 0 | 267 | | else if (workflowMode == WorkflowMode.MetallicRoughness) |
| | 268 | | { |
| 0 | 269 | | SetKeyword(material, "_METALLICGLOSSMAP", material.GetTexture("_MetallicGlossMap")); |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | bool shouldEmissionBeEnabled = material.GetColor("_EmissionColor") != Color.black; |
| 0 | 273 | | SetKeyword(material, "_EMISSION", shouldEmissionBeEnabled); |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | static void MaterialChanged(Material material, WorkflowMode workflowMode) |
| | 277 | | { |
| 0 | 278 | | SetupMaterialWithBlendMode(material, (BlendMode)material.GetFloat("_Mode"), material.GetFloat("_Cutoff")); |
| | 279 | |
|
| 0 | 280 | | SetMaterialKeywords(material, workflowMode); |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | static void SetKeyword(Material m, string keyword, bool state) |
| | 284 | | { |
| 0 | 285 | | if (state) |
| | 286 | | { |
| 0 | 287 | | m.EnableKeyword(keyword); |
| 0 | 288 | | } |
| | 289 | | else |
| | 290 | | { |
| 0 | 291 | | m.DisableKeyword(keyword); |
| | 292 | | } |
| 0 | 293 | | } |
| | 294 | | } |
| | 295 | | } // namespace UnityEditor |