BabylonShaderInterface.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor
  4. {
  5. public class BabylonShaderInterface : ShaderGUI
  6. {
  7. private enum WorkflowMode
  8. {
  9. Specular,
  10. Metallic,
  11. Dielectric
  12. }
  13. public enum BlendMode
  14. {
  15. Opaque,
  16. Cutout,
  17. Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency
  18. Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
  19. }
  20. public enum SmoothnessMapChannel
  21. {
  22. SpecularMetallicAlpha,
  23. AlbedoAlpha,
  24. }
  25. private static class Styles
  26. {
  27. public static GUIStyle optionsButton = "PaneOptions";
  28. public static GUIContent uvSetLabel = new GUIContent("UV Set");
  29. public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") };
  30. public static string emptyTootip = "";
  31. public static GUIContent albedoText = new GUIContent("Albedo", "Albedo (RGB) and Transparency (A)");
  32. public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
  33. public static GUIContent specularMapText = new GUIContent("Specular", "Specular (RGB) and Smoothness (A)");
  34. public static GUIContent metallicMapText = new GUIContent("Metallic", "Metallic (R) and Smoothness (A)");
  35. public static GUIContent smoothnessText = new GUIContent("Smoothness", "Smoothness value");
  36. public static GUIContent smoothnessScaleText = new GUIContent("Smoothness", "Smoothness scale factor");
  37. public static GUIContent smoothnessMapChannelText = new GUIContent("Source", "Smoothness texture and channel");
  38. public static GUIContent highlightsText = new GUIContent("Specular Highlights", "Specular Highlights");
  39. public static GUIContent reflectionsText = new GUIContent("Reflections", "Glossy Reflections");
  40. public static GUIContent alphaModeText = new GUIContent("Alpha Blending Mode", "Alpha Blending Mode");
  41. public static GUIContent disableLightingText = new GUIContent("Disable Surface Lighting", "Disable Surface Lighting");
  42. public static GUIContent useEmissiveAsIlluminationText = new GUIContent("Use Emissive Illumination", "Use Emissive Illumination");
  43. public static GUIContent roughnessText = new GUIContent("Roughness", "Roughness");
  44. public static GUIContent useRoughnessAlphaText = new GUIContent("From Alpha", "From Alpha");
  45. public static GUIContent useRoughnessGreenText = new GUIContent("From Green", "From Green");
  46. public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map");
  47. public static GUIContent heightMapText = new GUIContent("Height Map", "Height Map (G)");
  48. public static GUIContent occlusionText = new GUIContent("Occlusion", "Occlusion (G)");
  49. public static GUIContent emissionText = new GUIContent("Emission", "Emission (RGB)");
  50. public static GUIContent detailMaskText = new GUIContent("Detail Mask", "Mask for Secondary Maps (A)");
  51. public static GUIContent detailAlbedoText = new GUIContent("Detail Albedo x2", "Albedo (RGB) multiplied by 2");
  52. public static GUIContent detailNormalMapText = new GUIContent("Normal Map", "Normal Map");
  53. public static string whiteSpaceString = " ";
  54. public static string primaryMapsText = "Main Maps";
  55. public static string secondaryMapsText = "Secondary Maps";
  56. public static string babylonText = "Babylon Rendering Options";
  57. public static string forwardText = "Forward Rendering Options";
  58. public static string renderingMode = "Rendering Mode";
  59. public static GUIContent emissiveWarning = new GUIContent ("Emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive.");
  60. public static GUIContent emissiveColorWarning = new GUIContent ("Ensure emissive color is non-black for emission to have effect.");
  61. public static readonly string[] blendNames = Enum.GetNames (typeof (BlendMode));
  62. }
  63. MaterialProperty blendMode = null;
  64. MaterialProperty albedoMap = null;
  65. MaterialProperty albedoColor = null;
  66. MaterialProperty alphaCutoff = null;
  67. MaterialProperty specularMap = null;
  68. MaterialProperty specularColor = null;
  69. MaterialProperty metallicMap = null;
  70. MaterialProperty metallic = null;
  71. MaterialProperty smoothness = null;
  72. MaterialProperty smoothnessScale = null;
  73. MaterialProperty smoothnessMapChannel = null;
  74. MaterialProperty highlights = null;
  75. MaterialProperty reflections = null;
  76. MaterialProperty alphaMode = null;
  77. MaterialProperty disableLighting = null;
  78. MaterialProperty useEmissiveAsIllumination = null;
  79. MaterialProperty roughness = null;
  80. MaterialProperty useRoughnessAlpha = null;
  81. MaterialProperty useRoughnessGreen = null;
  82. MaterialProperty bumpScale = null;
  83. MaterialProperty bumpMap = null;
  84. MaterialProperty occlusionStrength = null;
  85. MaterialProperty occlusionMap = null;
  86. MaterialProperty heigtMapScale = null;
  87. MaterialProperty heightMap = null;
  88. MaterialProperty emissionColorForRendering = null;
  89. MaterialProperty emissionMap = null;
  90. MaterialProperty detailMask = null;
  91. MaterialProperty detailAlbedoMap = null;
  92. MaterialProperty detailNormalMapScale = null;
  93. MaterialProperty detailNormalMap = null;
  94. MaterialProperty uvSetSecondary = null;
  95. MaterialEditor m_MaterialEditor;
  96. WorkflowMode m_WorkflowMode = WorkflowMode.Specular;
  97. ColorPickerHDRConfig m_ColorPickerHDRConfig = new ColorPickerHDRConfig(0f, 99f, 1/99f, 3f);
  98. bool m_FirstTimeApply = true;
  99. public void FindProperties (MaterialProperty[] props)
  100. {
  101. blendMode = FindProperty ("_Mode", props);
  102. albedoMap = FindProperty ("_MainTex", props);
  103. albedoColor = FindProperty ("_Color", props);
  104. alphaCutoff = FindProperty ("_Cutoff", props);
  105. specularMap = FindProperty ("_SpecGlossMap", props, false);
  106. specularColor = FindProperty ("_SpecColor", props, false);
  107. metallicMap = FindProperty ("_MetallicGlossMap", props, false);
  108. metallic = FindProperty ("_Metallic", props, false);
  109. if (specularMap != null && specularColor != null)
  110. m_WorkflowMode = WorkflowMode.Specular;
  111. else if (metallicMap != null && metallic != null)
  112. m_WorkflowMode = WorkflowMode.Metallic;
  113. else
  114. m_WorkflowMode = WorkflowMode.Dielectric;
  115. smoothness = FindProperty ("_Glossiness", props);
  116. smoothnessScale = FindProperty ("_GlossMapScale", props, false);
  117. smoothnessMapChannel = FindProperty ("_SmoothnessTextureChannel", props, false);
  118. highlights = FindProperty ("_SpecularHighlights", props, false);
  119. reflections = FindProperty ("_GlossyReflections", props, false);
  120. disableLighting = FindProperty ("_DisableLighting", props, false);
  121. useEmissiveAsIllumination = FindProperty ("_UseEmissiveAsIllumination", props, false);
  122. roughness = FindProperty ("_Roughness", props, false);
  123. useRoughnessAlpha = FindProperty ("_UseRoughnessFromMetallicTextureAlpha", props, false);
  124. useRoughnessGreen = FindProperty ("_UseRoughnessFromMetallicTextureGreen", props, false);
  125. bumpScale = FindProperty ("_BumpScale", props);
  126. bumpMap = FindProperty ("_BumpMap", props);
  127. heigtMapScale = FindProperty ("_Parallax", props);
  128. heightMap = FindProperty("_ParallaxMap", props);
  129. occlusionStrength = FindProperty ("_OcclusionStrength", props);
  130. occlusionMap = FindProperty ("_OcclusionMap", props);
  131. emissionColorForRendering = FindProperty ("_EmissionColor", props);
  132. emissionMap = FindProperty ("_EmissionMap", props);
  133. detailMask = FindProperty ("_DetailMask", props);
  134. detailAlbedoMap = FindProperty ("_DetailAlbedoMap", props);
  135. detailNormalMapScale = FindProperty ("_DetailNormalMapScale", props);
  136. detailNormalMap = FindProperty ("_DetailNormalMap", props);
  137. uvSetSecondary = FindProperty ("_UVSec", props);
  138. alphaMode = FindProperty ("_AlphaMode", props);
  139. }
  140. public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] props)
  141. {
  142. FindProperties (props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly
  143. m_MaterialEditor = materialEditor;
  144. Material material = materialEditor.target as Material;
  145. // Make sure that needed setup (ie keywords/renderqueue) are set up if we're switching some existing
  146. // material to a standard shader.
  147. // Do this before any GUI code has been issued to prevent layout issues in subsequent GUILayout statements (case 780071)
  148. if (m_FirstTimeApply)
  149. {
  150. MaterialChanged(material, m_WorkflowMode);
  151. m_FirstTimeApply = false;
  152. }
  153. ShaderPropertiesGUI (material);
  154. }
  155. public void ShaderPropertiesGUI (Material material)
  156. {
  157. // Use default labelWidth
  158. EditorGUIUtility.labelWidth = 0f;
  159. // Detect any changes to the material
  160. EditorGUI.BeginChangeCheck();
  161. {
  162. BlendModePopup();
  163. // Primary properties
  164. GUILayout.Label (Styles.primaryMapsText, EditorStyles.boldLabel);
  165. DoAlbedoArea(material);
  166. DoSpecularMetallicArea();
  167. m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, bumpMap, bumpMap.textureValue != null ? bumpScale : null);
  168. m_MaterialEditor.TexturePropertySingleLine(Styles.heightMapText, heightMap, heightMap.textureValue != null ? heigtMapScale : null);
  169. m_MaterialEditor.TexturePropertySingleLine(Styles.occlusionText, occlusionMap, occlusionMap.textureValue != null ? occlusionStrength : null);
  170. DoEmissionArea(material);
  171. m_MaterialEditor.TexturePropertySingleLine(Styles.detailMaskText, detailMask);
  172. EditorGUI.BeginChangeCheck();
  173. m_MaterialEditor.TextureScaleOffsetProperty(albedoMap);
  174. if (EditorGUI.EndChangeCheck())
  175. emissionMap.textureScaleAndOffset = albedoMap.textureScaleAndOffset; // Apply the main texture scale and offset to the emission texture as well, for Enlighten's sake
  176. EditorGUILayout.Space();
  177. // Secondary properties
  178. GUILayout.Label(Styles.secondaryMapsText, EditorStyles.boldLabel);
  179. m_MaterialEditor.TexturePropertySingleLine(Styles.detailAlbedoText, detailAlbedoMap);
  180. m_MaterialEditor.TexturePropertySingleLine(Styles.detailNormalMapText, detailNormalMap, detailNormalMapScale);
  181. m_MaterialEditor.TextureScaleOffsetProperty(detailAlbedoMap);
  182. m_MaterialEditor.ShaderProperty(uvSetSecondary, Styles.uvSetLabel.text);
  183. // Third properties
  184. GUILayout.Label(Styles.babylonText, EditorStyles.boldLabel);
  185. if (alphaMode != null)
  186. m_MaterialEditor.ShaderProperty(alphaMode, Styles.alphaModeText);
  187. if (disableLighting != null)
  188. m_MaterialEditor.ShaderProperty(disableLighting, Styles.disableLightingText);
  189. if (useEmissiveAsIllumination != null)
  190. m_MaterialEditor.ShaderProperty(useEmissiveAsIllumination, Styles.useEmissiveAsIlluminationText);
  191. // Forth properties
  192. GUILayout.Label(Styles.forwardText, EditorStyles.boldLabel);
  193. if (highlights != null)
  194. m_MaterialEditor.ShaderProperty(highlights, Styles.highlightsText);
  195. if (reflections != null)
  196. m_MaterialEditor.ShaderProperty(reflections, Styles.reflectionsText);
  197. }
  198. if (EditorGUI.EndChangeCheck())
  199. {
  200. foreach (var obj in blendMode.targets)
  201. MaterialChanged((Material)obj, m_WorkflowMode);
  202. }
  203. }
  204. internal void DetermineWorkflow(MaterialProperty[] props)
  205. {
  206. if (FindProperty("_SpecGlossMap", props, false) != null && FindProperty("_SpecColor", props, false) != null)
  207. m_WorkflowMode = WorkflowMode.Specular;
  208. else if (FindProperty("_MetallicGlossMap", props, false) != null && FindProperty("_Metallic", props, false) != null)
  209. m_WorkflowMode = WorkflowMode.Metallic;
  210. else
  211. m_WorkflowMode = WorkflowMode.Dielectric;
  212. }
  213. public override void AssignNewShaderToMaterial (Material material, Shader oldShader, Shader newShader)
  214. {
  215. // _Emission property is lost after assigning Standard shader to the material
  216. // thus transfer it before assigning the new shader
  217. if (material.HasProperty("_Emission"))
  218. {
  219. material.SetColor("_EmissionColor", material.GetColor("_Emission"));
  220. }
  221. base.AssignNewShaderToMaterial(material, oldShader, newShader);
  222. if (oldShader == null || !oldShader.name.Contains("Legacy Shaders/"))
  223. {
  224. SetupMaterialWithBlendMode(material, (BlendMode)material.GetFloat("_Mode"));
  225. return;
  226. }
  227. BlendMode blendMode = BlendMode.Opaque;
  228. if (oldShader.name.Contains("/Transparent/Cutout/"))
  229. {
  230. blendMode = BlendMode.Cutout;
  231. }
  232. else if (oldShader.name.Contains("/Transparent/"))
  233. {
  234. // NOTE: legacy shaders did not provide physically based transparency
  235. // therefore Fade mode
  236. blendMode = BlendMode.Fade;
  237. }
  238. material.SetFloat("_Mode", (float)blendMode);
  239. DetermineWorkflow( MaterialEditor.GetMaterialProperties (new Material[] { material }) );
  240. MaterialChanged(material, m_WorkflowMode);
  241. }
  242. void BlendModePopup()
  243. {
  244. EditorGUI.showMixedValue = blendMode.hasMixedValue;
  245. var mode = (BlendMode)blendMode.floatValue;
  246. EditorGUI.BeginChangeCheck();
  247. mode = (BlendMode)EditorGUILayout.Popup(Styles.renderingMode, (int)mode, Styles.blendNames);
  248. if (EditorGUI.EndChangeCheck())
  249. {
  250. m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode");
  251. blendMode.floatValue = (float)mode;
  252. }
  253. EditorGUI.showMixedValue = false;
  254. }
  255. void DoAlbedoArea(Material material)
  256. {
  257. m_MaterialEditor.TexturePropertySingleLine(Styles.albedoText, albedoMap, albedoColor);
  258. if (((BlendMode)material.GetFloat("_Mode") == BlendMode.Cutout))
  259. {
  260. m_MaterialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoffText.text, MaterialEditor.kMiniTextureFieldLabelIndentLevel+1);
  261. }
  262. }
  263. void DoEmissionArea(Material material)
  264. {
  265. bool showHelpBox = !HasValidEmissiveKeyword(material);
  266. bool hadEmissionTexture = emissionMap.textureValue != null;
  267. // Texture and HDR color controls
  268. m_MaterialEditor.TexturePropertyWithHDRColor(Styles.emissionText, emissionMap, emissionColorForRendering, m_ColorPickerHDRConfig, false);
  269. // If texture was assigned and color was black set color to white
  270. float brightness = emissionColorForRendering.colorValue.maxColorComponent;
  271. if (emissionMap.textureValue != null && !hadEmissionTexture && brightness <= 0f)
  272. emissionColorForRendering.colorValue = Color.white;
  273. // Emission for GI?
  274. m_MaterialEditor.LightmapEmissionProperty (MaterialEditor.kMiniTextureFieldLabelIndentLevel + 1);
  275. if (showHelpBox)
  276. {
  277. EditorGUILayout.HelpBox(Styles.emissiveWarning.text, MessageType.Warning);
  278. }
  279. }
  280. void DoSpecularMetallicArea()
  281. {
  282. bool hasGlossMap = false;
  283. if (m_WorkflowMode == WorkflowMode.Specular)
  284. {
  285. hasGlossMap = specularMap.textureValue != null;
  286. m_MaterialEditor.TexturePropertySingleLine(Styles.specularMapText, specularMap, hasGlossMap ? null : specularColor);
  287. }
  288. else if (m_WorkflowMode == WorkflowMode.Metallic)
  289. {
  290. hasGlossMap = metallicMap.textureValue != null;
  291. m_MaterialEditor.TexturePropertySingleLine(Styles.metallicMapText, metallicMap, hasGlossMap ? null : metallic);
  292. }
  293. bool showSmoothnessScale = hasGlossMap;
  294. if (smoothnessMapChannel != null)
  295. {
  296. int smoothnessChannel = (int) smoothnessMapChannel.floatValue;
  297. if (smoothnessChannel == (int) SmoothnessMapChannel.AlbedoAlpha)
  298. showSmoothnessScale = true;
  299. }
  300. int indentation = 2; // align with labels of texture properties
  301. // Roughness properties
  302. if (roughness != null)
  303. m_MaterialEditor.ShaderProperty(roughness, Styles.roughnessText, indentation);
  304. if (useRoughnessAlpha != null)
  305. m_MaterialEditor.ShaderProperty(useRoughnessAlpha, Styles.useRoughnessAlphaText, indentation);
  306. if (useRoughnessGreen != null)
  307. m_MaterialEditor.ShaderProperty(useRoughnessGreen, Styles.useRoughnessGreenText, indentation);
  308. if (roughness != null || useRoughnessAlpha != null || useRoughnessGreen != null)
  309. EditorGUILayout.Space();
  310. m_MaterialEditor.ShaderProperty(showSmoothnessScale ? smoothnessScale : smoothness, showSmoothnessScale ? Styles.smoothnessScaleText : Styles.smoothnessText, indentation);
  311. ++indentation;
  312. if (smoothnessMapChannel != null)
  313. m_MaterialEditor.ShaderProperty(smoothnessMapChannel, Styles.smoothnessMapChannelText, indentation);
  314. }
  315. public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode)
  316. {
  317. switch (blendMode)
  318. {
  319. case BlendMode.Opaque:
  320. material.SetOverrideTag("RenderType", "");
  321. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  322. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  323. material.SetInt("_ZWrite", 1);
  324. material.DisableKeyword("_ALPHATEST_ON");
  325. material.DisableKeyword("_ALPHABLEND_ON");
  326. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  327. material.renderQueue = -1;
  328. break;
  329. case BlendMode.Cutout:
  330. material.SetOverrideTag("RenderType", "TransparentCutout");
  331. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  332. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
  333. material.SetInt("_ZWrite", 1);
  334. material.EnableKeyword("_ALPHATEST_ON");
  335. material.DisableKeyword("_ALPHABLEND_ON");
  336. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  337. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
  338. break;
  339. case BlendMode.Fade:
  340. material.SetOverrideTag("RenderType", "Transparent");
  341. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  342. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  343. material.SetInt("_ZWrite", 0);
  344. material.DisableKeyword("_ALPHATEST_ON");
  345. material.EnableKeyword("_ALPHABLEND_ON");
  346. material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  347. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  348. break;
  349. case BlendMode.Transparent:
  350. material.SetOverrideTag("RenderType", "Transparent");
  351. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
  352. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  353. material.SetInt("_ZWrite", 0);
  354. material.DisableKeyword("_ALPHATEST_ON");
  355. material.DisableKeyword("_ALPHABLEND_ON");
  356. material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
  357. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  358. break;
  359. }
  360. }
  361. static SmoothnessMapChannel GetSmoothnessMapChannel(Material material)
  362. {
  363. int ch = (int) material.GetFloat("_SmoothnessTextureChannel");
  364. if (ch == (int) SmoothnessMapChannel.AlbedoAlpha)
  365. return SmoothnessMapChannel.AlbedoAlpha;
  366. else
  367. return SmoothnessMapChannel.SpecularMetallicAlpha;
  368. }
  369. static bool ShouldEmissionBeEnabled(Material mat, Color color)
  370. {
  371. var realtimeEmission = (mat.globalIlluminationFlags & MaterialGlobalIlluminationFlags.RealtimeEmissive) > 0;
  372. return color.maxColorComponent > 0.1f / 255.0f || realtimeEmission;
  373. }
  374. static void SetMaterialKeywords(Material material, WorkflowMode workflowMode)
  375. {
  376. // Note: keywords must be based on Material value not on MaterialProperty due to multi-edit & material animation
  377. // (MaterialProperty value might come from renderer material property block)
  378. SetKeyword (material, "_NORMALMAP", material.GetTexture ("_BumpMap") || material.GetTexture ("_DetailNormalMap"));
  379. if (workflowMode == WorkflowMode.Specular)
  380. SetKeyword (material, "_SPECGLOSSMAP", material.GetTexture ("_SpecGlossMap"));
  381. else if (workflowMode == WorkflowMode.Metallic)
  382. SetKeyword (material, "_METALLICGLOSSMAP", material.GetTexture ("_MetallicGlossMap"));
  383. SetKeyword (material, "_PARALLAXMAP", material.GetTexture ("_ParallaxMap"));
  384. SetKeyword (material, "_DETAIL_MULX2", material.GetTexture ("_DetailAlbedoMap") || material.GetTexture ("_DetailNormalMap"));
  385. bool shouldEmissionBeEnabled = ShouldEmissionBeEnabled (material, material.GetColor("_EmissionColor"));
  386. SetKeyword (material, "_EMISSION", shouldEmissionBeEnabled);
  387. if (material.HasProperty("_SmoothnessTextureChannel"))
  388. {
  389. SetKeyword (material, "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A", GetSmoothnessMapChannel(material) == SmoothnessMapChannel.AlbedoAlpha);
  390. }
  391. // Setup lightmap emissive flags
  392. MaterialGlobalIlluminationFlags flags = material.globalIlluminationFlags;
  393. if ((flags & (MaterialGlobalIlluminationFlags.BakedEmissive | MaterialGlobalIlluminationFlags.RealtimeEmissive)) != 0)
  394. {
  395. flags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  396. if (!shouldEmissionBeEnabled)
  397. flags |= MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  398. material.globalIlluminationFlags = flags;
  399. }
  400. }
  401. bool HasValidEmissiveKeyword (Material material)
  402. {
  403. // Material animation might be out of sync with the material keyword.
  404. // 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.
  405. // (note: (Renderer MaterialPropertyBlock applies its values to emissionColorForRendering))
  406. bool hasEmissionKeyword = material.IsKeywordEnabled ("_EMISSION");
  407. if (!hasEmissionKeyword && ShouldEmissionBeEnabled (material, emissionColorForRendering.colorValue))
  408. return false;
  409. else
  410. return true;
  411. }
  412. static void MaterialChanged(Material material, WorkflowMode workflowMode)
  413. {
  414. SetupMaterialWithBlendMode(material, (BlendMode)material.GetFloat("_Mode"));
  415. SetMaterialKeywords(material, workflowMode);
  416. }
  417. static void SetKeyword(Material m, string keyword, bool state)
  418. {
  419. if (state)
  420. m.EnableKeyword (keyword);
  421. else
  422. m.DisableKeyword (keyword);
  423. }
  424. }
  425. } // namespace UnityEditor