Advertisement
Guest User

Untitled

a guest
Dec 29th, 2020
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.52 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using UnityEditor.Rendering;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.Rendering.Universal;
  7. using System.IO;
  8.  
  9. public class UnlitMasterShaderGUI : ShaderGUI
  10. {
  11. GrainMode grainMode;
  12.  
  13. bool showColorsSection, showLightingSection, showRimSection, showTransitionSection, showOutlineControlSection, showHatchingSection, showVertexExtrusionSection;
  14.  
  15. Material targetMat;
  16.  
  17. public static Gradient colorRampGradient = new Gradient();
  18.  
  19. public delegate void DrawSettingsMethod(MaterialEditor materialEditor, MaterialProperty[] properties);
  20.  
  21. static GUIStyle _centeredGreyMiniLabel;
  22. public static GUIStyle CenteredGreyMiniLabel
  23. {
  24. get
  25. {
  26. if (_centeredGreyMiniLabel == null)
  27. {
  28. _centeredGreyMiniLabel = new GUIStyle(GUI.skin.FindStyle("MiniLabel") ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("MiniLabel"))
  29. {
  30. alignment = TextAnchor.MiddleLeft,
  31. normal = { textColor = Color.gray }
  32. };
  33.  
  34. // _centeredGreyMiniLabel = new GUIStyle(UnityEditor.EditorStyles.label);
  35. // _centeredGreyMiniLabel.normal.textColor = Color.gray;
  36. // _centeredGreyMiniLabel.alignment = TextAnchor.MiddleCenter;
  37. }
  38. return _centeredGreyMiniLabel;
  39. }
  40. }
  41.  
  42.  
  43.  
  44. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  45. {
  46.  
  47. targetMat = materialEditor.target as Material;
  48.  
  49. EditorGUILayout.LabelField("Unlit Master Shader v1.1.0", EditorStyles.boldLabel);
  50. if (GUILayout.Button("@alexanderameye", CenteredGreyMiniLabel)) Application.OpenURL("https://twitter.com/alexanderameye");
  51. EditorGUILayout.Space();
  52. CoreEditorUtils.DrawSplitter();
  53.  
  54. showColorsSection = CoreEditorUtils.DrawHeaderFoldout("Color", showColorsSection, false, (Func<bool>)null, null);
  55. DrawPropertiesInspector(showColorsSection, materialEditor, properties, DrawColorsSettings);
  56.  
  57. showLightingSection = CoreEditorUtils.DrawHeaderFoldout("Lighting", showLightingSection, false, (Func<bool>)null, null);
  58. DrawPropertiesInspector(showLightingSection, materialEditor, properties, DrawLightingSettings);
  59.  
  60. showRimSection = CoreEditorUtils.DrawHeaderFoldout("Rim", showRimSection, false, (Func<bool>)null, null);
  61. DrawPropertiesInspector(showRimSection, materialEditor, properties, DrawRimSettings);
  62.  
  63. showHatchingSection = CoreEditorUtils.DrawHeaderFoldout("Surface Effects", showHatchingSection, false, (Func<bool>)null, null);
  64. DrawPropertiesInspector(showHatchingSection, materialEditor, properties, DrawHatchingSettings);
  65.  
  66. showOutlineControlSection = CoreEditorUtils.DrawHeaderFoldout("Outline", showOutlineControlSection, false, (Func<bool>)null, null);
  67. DrawPropertiesInspector(showOutlineControlSection, materialEditor, properties, DrawOutlineSettings);
  68.  
  69. showVertexExtrusionSection = CoreEditorUtils.DrawHeaderFoldout("Vertex Distortion", showVertexExtrusionSection, false, (Func<bool>)null, null);
  70. DrawPropertiesInspector(showVertexExtrusionSection, materialEditor, properties, DrawVertexExtrusionSettings);
  71. EditorGUILayout.Space();
  72.  
  73.  
  74. }
  75.  
  76.  
  77. void DrawColorsSettings(MaterialEditor editor, MaterialProperty[] properties)
  78. {
  79. MaterialProperty litColor = FindProperty("_LitColor", properties);
  80. MaterialProperty colorTexture = FindProperty("_ColorTexture", properties);
  81. MaterialProperty rampTexture = FindProperty("_RampTexture", properties);
  82. MaterialProperty shadedColor = FindProperty("_ShadedColor", properties);
  83. MaterialProperty colorSteps = FindProperty("_ColorSteps", properties);
  84. MaterialProperty colorSpread = FindProperty("_ColorSpread", properties);
  85. MaterialProperty colorOffset = FindProperty("_ColorOffset", properties);
  86. MaterialProperty hardness = FindProperty("_TransitionHardness", properties);
  87.  
  88. EditorGUILayout.Space();
  89. EditorGUILayout.LabelField("Colors", EditorStyles.boldLabel);
  90. MaterialProperty control = FindProperty("_COLORSOURCE", properties);
  91. editor.ShaderProperty(control, "Source");
  92. if (Array.IndexOf(targetMat.shaderKeywords, "_COLORSOURCE_GRADIENT") != -1)
  93. {
  94. EditorGUILayout.BeginHorizontal();
  95. EditorGUILayout.GradientField("Ramp", colorRampGradient);
  96. if (GUILayout.Button("Apply")) ApplyRampTexture();
  97. EditorGUILayout.EndHorizontal();
  98. }
  99. else if (Array.IndexOf(targetMat.shaderKeywords, "_COLORSOURCE_RAMP") != -1)
  100. {
  101. editor.ShaderProperty(rampTexture, "Gradient");
  102. }
  103. else if(Array.IndexOf(targetMat.shaderKeywords, "_COLORSOURCE_COLOR") != -1)
  104. {
  105. editor.ShaderProperty(litColor, "Lit");
  106. editor.ShaderProperty(shadedColor, "Shaded");
  107. }
  108. else if(Array.IndexOf(targetMat.shaderKeywords, "_COLORSOURCE_TEXTURE") != -1)
  109. {
  110. editor.ShaderProperty(colorTexture, "Texture");
  111.  
  112. }
  113.  
  114.  
  115.  
  116. if (Array.IndexOf(targetMat.shaderKeywords, "_COLORSOURCE_TEXTURE") == -1)
  117. {
  118. EditorGUILayout.Space();
  119. EditorGUILayout.LabelField("Transition", EditorStyles.boldLabel);
  120. editor.ShaderProperty(colorSpread, "Spread");
  121. editor.ShaderProperty(colorOffset, "Offset");
  122. }
  123.  
  124.  
  125. if (Array.IndexOf(targetMat.shaderKeywords, "_COLORSOURCE_COLOR") != -1)
  126. {
  127.  
  128. editor.ShaderProperty(hardness, "Hardness");
  129. editor.ShaderProperty(colorSteps, "Steps");
  130. }
  131. EditorGUILayout.Separator();
  132. }
  133.  
  134. public void ApplyRampTexture()
  135. {
  136. targetMat.SetTexture("_RampTexture", CreateGradientTexture(targetMat, colorRampGradient));
  137. }
  138.  
  139.  
  140. public static int width = 256;
  141. public static int height = 4; // needs to be multiple of 4 for DXT1 format compression
  142.  
  143. public static Texture2D CreateGradientTexture(Material targetMaterial, Gradient gradient)
  144. {
  145. Texture2D gradientTexture = new Texture2D(width, height, TextureFormat.ARGB32, false, false)
  146. {
  147. name = "_LUT",
  148. filterMode = FilterMode.Point,
  149. wrapMode = TextureWrapMode.Clamp,
  150. alphaIsTransparency = true,
  151.  
  152. };
  153.  
  154. for (int j = 0; j < height; j++)
  155. {
  156. for (int i = 0; i < width; i++) gradientTexture.SetPixel(i, j, gradient.Evaluate((float)i / (float)width));
  157. }
  158.  
  159. gradientTexture.Apply(false);
  160. gradientTexture = SaveAndGetTexture(targetMaterial, gradientTexture);
  161. return gradientTexture;
  162. }
  163.  
  164. private static Texture2D SaveAndGetTexture(Material targetMaterial, Texture2D sourceTexture)
  165. {
  166. string targetFolder = AssetDatabase.GetAssetPath(targetMaterial);
  167. targetFolder = targetFolder.Replace(targetMaterial.name + ".mat", string.Empty);
  168.  
  169. targetFolder += "Color Ramp Textures/";
  170.  
  171. if (!Directory.Exists(targetFolder))
  172. {
  173. Directory.CreateDirectory(targetFolder);
  174. AssetDatabase.Refresh();
  175. }
  176.  
  177. string path = targetFolder + targetMaterial.name + sourceTexture.name + ".asset";
  178. // File.WriteAllBytes(path, sourceTexture.EncodeToPNG());
  179.  
  180. AssetDatabase.CreateAsset(sourceTexture, path);
  181. AssetDatabase.SaveAssets();
  182.  
  183. AssetDatabase.Refresh();
  184. // AssetDatabase.ImportAsset(path, ImportAssetOptions.Default);
  185. sourceTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
  186.  
  187. return sourceTexture;
  188. }
  189.  
  190.  
  191. void DrawLightingSettings(MaterialEditor editor, MaterialProperty[] properties)
  192. {
  193. EditorGUILayout.Space();
  194. EditorGUILayout.LabelField("Highlight", EditorStyles.boldLabel);
  195. editor.ShaderProperty(FindProperty("_HighlightColor", properties), "Color");
  196. editor.ShaderProperty(FindProperty("_HighlightSize", properties), "Size");
  197. editor.ShaderProperty(FindProperty("_HighlightOpacity", properties), "Opacity");
  198.  
  199. EditorGUILayout.Space();
  200. EditorGUILayout.LabelField("Shadows", EditorStyles.boldLabel);
  201. EditorGUILayout.Separator();
  202. }
  203.  
  204. void DrawRimSettings(MaterialEditor editor, MaterialProperty[] properties)
  205. {
  206. MaterialProperty color = FindProperty("_RimColor", properties);
  207. MaterialProperty amount = FindProperty("_RimAmount", properties);
  208. MaterialProperty opacity = FindProperty("_RimOpacity", properties);
  209. MaterialProperty hardness = FindProperty("_RimHardness", properties);
  210.  
  211. EditorGUILayout.Space();
  212. editor.ShaderProperty(color, "Color");
  213. editor.ShaderProperty(amount, "Amount");
  214. editor.ShaderProperty(opacity, "Opacity");
  215. editor.ShaderProperty(hardness, "Hardness");
  216. EditorGUILayout.Separator();
  217. }
  218.  
  219. void DrawOutlineSettings(MaterialEditor editor, MaterialProperty[] properties)
  220. {
  221. MaterialProperty outlineSource = FindProperty("_OUTLINESOURCE", properties);
  222. MaterialProperty showVertexColors = FindProperty("SHOWVERTEXCOLORS", properties);
  223. MaterialProperty outlineColor = FindProperty("_OutlineColor", properties);
  224. MaterialProperty texture = FindProperty("_OutlineTexture", properties);
  225.  
  226. EditorGUILayout.Space();
  227. editor.ShaderProperty(outlineSource, "Source");
  228.  
  229. if (Array.IndexOf(targetMat.shaderKeywords, "_OUTLINESOURCE_TEXTURE") != -1)
  230. {
  231. editor.ShaderProperty(texture, "Texture");
  232. EditorGUILayout.LabelField("Using a custom texture for sectioning.", CenteredGreyMiniLabel);
  233. }
  234.  
  235. else if (Array.IndexOf(targetMat.shaderKeywords, "_OUTLINESOURCE_COLOR") != -1)
  236. {
  237. EditorGUILayout.LabelField("Using the color output for sectioning.", CenteredGreyMiniLabel);
  238.  
  239. }
  240.  
  241. if (Array.IndexOf(targetMat.shaderKeywords, "_OUTLINESOURCE_NONE") == -1)
  242. {
  243.  
  244. editor.ShaderProperty(outlineColor, "Color Control");
  245. EditorGUILayout.LabelField("The special value of 0 is used for shadowing!", CenteredGreyMiniLabel);
  246. }
  247.  
  248. if (Array.IndexOf(targetMat.shaderKeywords, "_OUTLINESOURCE_NONE") != -1)
  249. {
  250. EditorGUILayout.LabelField("The shader now just writes black to the sectioning texture.", CenteredGreyMiniLabel);
  251. EditorGUILayout.LabelField("You can fake an outline using a rim effect.", CenteredGreyMiniLabel);
  252. }
  253. editor.ShaderProperty(showVertexColors, "Show Vertex Colors");
  254. EditorGUILayout.LabelField("Using the color output for sectioning.", CenteredGreyMiniLabel);
  255.  
  256.  
  257. EditorGUILayout.Separator();
  258. }
  259.  
  260. void DrawHatchingSettings(MaterialEditor editor, MaterialProperty[] properties)
  261. {
  262. MaterialProperty scale1 = FindProperty("_MossScale1", properties);
  263. MaterialProperty scale2 = FindProperty("_MossScale2", properties);
  264. MaterialProperty color = FindProperty("_MossColor", properties);
  265. MaterialProperty coverage = FindProperty("_MossCoverage", properties);
  266. MaterialProperty height = FindProperty("_MossHeight", properties);
  267.  
  268. EditorGUILayout.Space();
  269. EditorGUILayout.LabelField("Moss", EditorStyles.boldLabel);
  270. editor.ShaderProperty(color, "Color");
  271. editor.ShaderProperty(FindProperty("_MossOpacity", properties), "Opacity");
  272. editor.ShaderProperty(coverage, "Coverage");
  273. editor.ShaderProperty(height, "Height");
  274. editor.ShaderProperty(scale1, "Scale 1");
  275. editor.ShaderProperty(scale2, "Scale 2");
  276.  
  277. EditorGUILayout.Space();
  278. EditorGUILayout.LabelField("Hatching", EditorStyles.boldLabel);
  279. editor.ShaderProperty(FindProperty("_HatchingOpacity", properties), "Opacity");
  280. EditorGUILayout.Separator();
  281. }
  282.  
  283.  
  284. void DrawVertexExtrusionSettings(MaterialEditor editor, MaterialProperty[] properties)
  285. {
  286. //// MaterialProperty enable = ShaderGUI.FindProperty("_VERTEXDISTORTION", properties);
  287. MaterialProperty strength = FindProperty("_DistortionStrength", properties);
  288. MaterialProperty scale = FindProperty("_DistortionScale", properties);
  289.  
  290.  
  291. editor.ShaderProperty(strength, "Strength");
  292. editor.ShaderProperty(scale, "Scale");
  293.  
  294. EditorGUILayout.Separator();
  295. }
  296.  
  297.  
  298. private void DrawPropertiesInspector(bool active, MaterialEditor editor, MaterialProperty[] properties, string[] props)
  299. {
  300. if (active)
  301. {
  302. EditorGUI.indentLevel++;
  303. foreach (string prop in props)
  304. {
  305. MaterialProperty reference = FindProperty(prop, properties);
  306. editor.ShaderProperty(reference, reference.displayName);
  307. }
  308. EditorGUILayout.Space();
  309. EditorGUI.indentLevel--;
  310. }
  311. CoreEditorUtils.DrawSplitter();
  312. }
  313.  
  314. private static void DrawPropertiesInspector(bool active, MaterialEditor editor, MaterialProperty[] properties, DrawSettingsMethod Drawer)
  315. {
  316. if (active)
  317. {
  318. EditorGUI.indentLevel++;
  319. Drawer(editor, properties);
  320. EditorGUILayout.Space();
  321. EditorGUI.indentLevel--;
  322. }
  323. CoreEditorUtils.DrawSplitter();
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement