AllIn1SpriteShaderStencil.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.SceneManagement;
  4. using System.Linq;
  5. [CanEditMultipleObjects]
  6. public class AllIn1SpriteShaderStencil : ShaderGUI
  7. {
  8. Material targetMat;
  9. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  10. {
  11. targetMat = materialEditor.target as Material;
  12. string[] oldKeyWords = targetMat.shaderKeywords;
  13. GUIStyle style = EditorStyles.helpBox;
  14. style.margin = new RectOffset(0, 0, 0, 0);
  15. materialEditor.ShaderProperty(properties[0], properties[0].displayName);
  16. materialEditor.ShaderProperty(properties[1], properties[1].displayName);
  17. materialEditor.ShaderProperty(properties[2], properties[2].displayName);
  18. //Not needed since Unity batches sprites on its own
  19. //EditorGUILayout.Separator();
  20. //materialEditor.EnableInstancingField();
  21. //Debug.Log(materialEditor.IsInstancingEnabled() + " " + Application.isBatchMode);
  22. EditorGUILayout.Separator();
  23. SpriteAtlas(materialEditor, style, oldKeyWords.Contains("ATLAS_ON"), "Sprite inside an atlas?", "ATLAS_ON");
  24. EditorGUILayout.Separator();
  25. GUILayout.Label("___Color Effects___", EditorStyles.boldLabel);
  26. Glow(materialEditor, properties, style, oldKeyWords.Contains("GLOW_ON"));
  27. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("FADE_ON"), "2.Fade", "FADE_ON", 7, 13);
  28. Outline(materialEditor, properties, style, oldKeyWords.Contains("OUTBASE_ON"));
  29. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("GRADIENT_ON"), "4.Gradient", "GRADIENT_ON", 31, 35);
  30. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("COLORSWAP_ON"), "5.Color Swap", "COLORSWAP_ON", 36, 42);
  31. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("HSV_ON"), "6.Hue Shift", "HSV_ON", 43, 45);
  32. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("CHANGECOLOR_ON"), "7.Change 1 Color", "CHANGECOLOR_ON", 123, 126);
  33. ColorRamp(materialEditor, properties, style, oldKeyWords.Contains("COLORRAMP_ON"));
  34. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("HITEFFECT_ON"), "9.Hit Effect", "HITEFFECT_ON", 46, 48);
  35. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("NEGATIVE_ON"), "10.Negative", "NEGATIVE_ON", 49, 49);
  36. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("PIXELATE_ON"), "11.Pixelate", "PIXELATE_ON", 50, 50);
  37. GreyScale(materialEditor, properties, style, oldKeyWords.Contains("GREYSCALE_ON"));
  38. Posterize(materialEditor, properties, style, oldKeyWords.Contains("POSTERIZE_ON"));
  39. Blur(materialEditor, properties, style, oldKeyWords.Contains("BLUR_ON"));
  40. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("MOTIONBLUR_ON"), "15.Motion Blur", "MOTIONBLUR_ON", 62, 63);
  41. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("GHOST_ON"), "16.Ghost", "GHOST_ON", 64, 65);
  42. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("INNEROUTLINE_ON"), "17.Inner Outline", "INNEROUTLINE_ON", 66, 69);
  43. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("HOLOGRAM_ON"), "18.Hologram", "HOLOGRAM_ON", 73, 77);
  44. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("CHROMABERR_ON"), "19.Chromatic Aberration", "CHROMABERR_ON", 78, 79);
  45. Glitch(materialEditor, properties, style, oldKeyWords.Contains("GLITCH_ON"), "20.Glitch", "GLITCH_ON");
  46. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("FLICKER_ON"), "21.Flicker", "FLICKER_ON", 81, 83);
  47. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("SHADOW_ON"), "22.Shadow", "SHADOW_ON", 84, 87);
  48. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("SHINE_ON"), "23.Shine", "SHINE_ON", 130, 134);
  49. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("ALPHACUTOFF_ON"), "24.Alpha Cutoff", "ALPHACUTOFF_ON", 70, 70);
  50. EditorGUILayout.Separator();
  51. GUILayout.Label("___UV Effects___", EditorStyles.boldLabel);
  52. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("DOODLE_ON"), "25.Hand Drawn", "DOODLE_ON", 88, 89);
  53. Grass(materialEditor, properties, style, oldKeyWords.Contains("WIND_ON"));
  54. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("WAVEUV_ON"), "27.Wave", "WAVEUV_ON", 94, 98);
  55. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("ROUNDWAVEUV_ON"), "28.Round Wave", "ROUNDWAVEUV_ON", 127, 128);
  56. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("RECTSIZE_ON"), "29.Rect Size (Enable wireframe to see result)", "RECTSIZE_ON", 99, 99);
  57. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("OFFSETUV_ON"), "30.Offset", "OFFSETUV_ON", 100, 101);
  58. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("CLIPPING_ON"), "31.Clipping / Fill Amount", "CLIPPING_ON", 102, 105);
  59. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("TEXTURESCROLL_ON"), "32.Texture Scroll", "TEXTURESCROLL_ON", 106, 107);
  60. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("ZOOMUV_ON"), "33.Zoom", "ZOOMUV_ON", 108, 108);
  61. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("DISTORT_ON"), "34.Distortion", "DISTORT_ON", 109, 112);
  62. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("TWISTUV_ON"), "35.Twist", "TWISTUV_ON", 113, 116);
  63. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("ROTATEUV_ON"), "36.Rotate", "ROTATEUV_ON", 117, 117);
  64. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("POLARUV_ON"), "37.Polar Coordinates (Tile texture for good results)", "POLARUV_ON", -1, -1);
  65. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("FISHEYE_ON"), "38.Fish Eye", "FISHEYE_ON", 118, 118);
  66. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("PINCH_ON"), "39.Pinch", "PINCH_ON", 119, 119);
  67. GenericEffect(materialEditor, properties, style, oldKeyWords.Contains("SHAKEUV_ON"), "40.Shake", "SHAKEUV_ON", 120, 122);
  68. }
  69. private void SpriteAtlas(MaterialEditor materialEditor, GUIStyle style, bool toggle, string inspector, string flag)
  70. {
  71. bool ini = toggle;
  72. toggle = EditorGUILayout.BeginToggleGroup(inspector, toggle);
  73. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  74. if (toggle)
  75. {
  76. targetMat.EnableKeyword(flag);
  77. EditorGUILayout.BeginVertical(style);
  78. {
  79. GUILayout.Label("Make sure SpriteAtlasUV component is added \n " +
  80. "*Check documentation if unsure what this does or how it works", style);
  81. }
  82. EditorGUILayout.EndVertical();
  83. }
  84. else targetMat.DisableKeyword(flag);
  85. EditorGUILayout.EndToggleGroup();
  86. }
  87. private void Outline(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  88. {
  89. bool ini = toggle;
  90. toggle = EditorGUILayout.BeginToggleGroup("3.Outline", toggle);
  91. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  92. if (toggle)
  93. {
  94. targetMat.EnableKeyword("OUTBASE_ON");
  95. EditorGUILayout.BeginVertical(style);
  96. {
  97. materialEditor.ShaderProperty(properties[14], properties[14].displayName);
  98. materialEditor.ShaderProperty(properties[15], properties[15].displayName);
  99. materialEditor.ShaderProperty(properties[16], properties[16].displayName);
  100. materialEditor.ShaderProperty(properties[17], properties[17].displayName);
  101. MaterialProperty outline8dir = ShaderGUI.FindProperty("_Outline8Directions", properties);
  102. if (outline8dir.floatValue == 1) targetMat.EnableKeyword("OUTBASE8DIR_ON");
  103. else targetMat.DisableKeyword("OUTBASE8DIR_ON");
  104. materialEditor.ShaderProperty(properties[19], properties[19].displayName);
  105. MaterialProperty outlinePixel = ShaderGUI.FindProperty("_OutlineIsPixel", properties);
  106. if (outlinePixel.floatValue == 1)
  107. {
  108. targetMat.EnableKeyword("OUTBASEPIXELPERF_ON");
  109. materialEditor.ShaderProperty(properties[20], properties[20].displayName);
  110. }
  111. else
  112. {
  113. targetMat.DisableKeyword("OUTBASEPIXELPERF_ON");
  114. materialEditor.ShaderProperty(properties[18], properties[18].displayName);
  115. }
  116. materialEditor.ShaderProperty(properties[21], properties[21].displayName);
  117. MaterialProperty outlineTex = ShaderGUI.FindProperty("_OutlineTexToggle", properties);
  118. if (outlineTex.floatValue == 1)
  119. {
  120. targetMat.EnableKeyword("OUTTEX_ON");
  121. materialEditor.ShaderProperty(properties[22], properties[22].displayName);
  122. materialEditor.ShaderProperty(properties[23], properties[23].displayName);
  123. materialEditor.ShaderProperty(properties[24], properties[24].displayName);
  124. materialEditor.ShaderProperty(properties[25], properties[25].displayName);
  125. MaterialProperty outlineTexGrey = ShaderGUI.FindProperty("_OutlineTexGrey", properties);
  126. if (outlineTexGrey.floatValue == 1) targetMat.EnableKeyword("OUTGREYTEXTURE_ON");
  127. else targetMat.DisableKeyword("OUTGREYTEXTURE_ON");
  128. }
  129. else targetMat.DisableKeyword("OUTTEX_ON");
  130. materialEditor.ShaderProperty(properties[26], properties[26].displayName);
  131. MaterialProperty outlineDistort = ShaderGUI.FindProperty("_OutlineDistortToggle", properties);
  132. if (outlineDistort.floatValue == 1)
  133. {
  134. targetMat.EnableKeyword("OUTDIST_ON");
  135. materialEditor.ShaderProperty(properties[27], properties[27].displayName);
  136. materialEditor.ShaderProperty(properties[28], properties[28].displayName);
  137. materialEditor.ShaderProperty(properties[29], properties[29].displayName);
  138. materialEditor.ShaderProperty(properties[30], properties[30].displayName);
  139. }
  140. else targetMat.DisableKeyword("OUTDIST_ON");
  141. }
  142. EditorGUILayout.EndVertical();
  143. }
  144. else targetMat.DisableKeyword("OUTBASE_ON");
  145. EditorGUILayout.EndToggleGroup();
  146. }
  147. private void GenericEffect(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle, string inspector, string flag, int first, int last)
  148. {
  149. bool ini = toggle;
  150. toggle = EditorGUILayout.BeginToggleGroup(inspector, toggle);
  151. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  152. if (toggle)
  153. {
  154. targetMat.EnableKeyword(flag);
  155. if (first > 0)
  156. {
  157. EditorGUILayout.BeginVertical(style);
  158. {
  159. for (int i = first; i <= last; i++) materialEditor.ShaderProperty(properties[i], properties[i].displayName);
  160. }
  161. EditorGUILayout.EndVertical();
  162. }
  163. }
  164. else targetMat.DisableKeyword(flag);
  165. EditorGUILayout.EndToggleGroup();
  166. }
  167. private void Glow(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  168. {
  169. bool ini = toggle;
  170. toggle = EditorGUILayout.BeginToggleGroup("1.Glow", toggle);
  171. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  172. if (toggle)
  173. {
  174. targetMat.EnableKeyword("GLOW_ON");
  175. EditorGUILayout.BeginVertical(style);
  176. {
  177. materialEditor.ShaderProperty(properties[3], properties[3].displayName);
  178. materialEditor.ShaderProperty(properties[4], properties[4].displayName);
  179. materialEditor.ShaderProperty(properties[5], properties[5].displayName);
  180. MaterialProperty useGlowTex = ShaderGUI.FindProperty("_GlowTexUsed", properties);
  181. if (useGlowTex.floatValue == 1)
  182. {
  183. targetMat.EnableKeyword("GLOWTEX_ON");
  184. materialEditor.ShaderProperty(properties[6], properties[6].displayName);
  185. }
  186. else targetMat.DisableKeyword("GLOWTEX_ON");
  187. }
  188. EditorGUILayout.EndVertical();
  189. }
  190. else targetMat.DisableKeyword("GLOW_ON");
  191. EditorGUILayout.EndToggleGroup();
  192. }
  193. private void ColorRamp(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  194. {
  195. bool ini = toggle;
  196. toggle = EditorGUILayout.BeginToggleGroup("8.Color Ramp", toggle);
  197. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  198. if (toggle)
  199. {
  200. targetMat.EnableKeyword("COLORRAMP_ON");
  201. EditorGUILayout.BeginVertical(style);
  202. {
  203. materialEditor.ShaderProperty(properties[51], properties[51].displayName);
  204. materialEditor.ShaderProperty(properties[52], properties[52].displayName);
  205. materialEditor.ShaderProperty(properties[53], properties[53].displayName);
  206. MaterialProperty colorRampOut = ShaderGUI.FindProperty("_ColorRampOutline", properties);
  207. if (colorRampOut.floatValue == 1) targetMat.EnableKeyword("COLORRAMPOUTLINE_ON");
  208. else targetMat.DisableKeyword("COLORRAMPOUTLINE_ON");
  209. }
  210. EditorGUILayout.EndVertical();
  211. }
  212. else targetMat.DisableKeyword("COLORRAMP_ON");
  213. EditorGUILayout.EndToggleGroup();
  214. }
  215. private void GreyScale(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  216. {
  217. bool ini = toggle;
  218. toggle = EditorGUILayout.BeginToggleGroup("12.Greyscale", toggle);
  219. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  220. if (toggle)
  221. {
  222. targetMat.EnableKeyword("GREYSCALE_ON");
  223. EditorGUILayout.BeginVertical(style);
  224. {
  225. materialEditor.ShaderProperty(properties[54], properties[54].displayName);
  226. materialEditor.ShaderProperty(properties[55], properties[55].displayName);
  227. materialEditor.ShaderProperty(properties[56], properties[56].displayName);
  228. MaterialProperty greyScaleOut = ShaderGUI.FindProperty("_GreyscaleOutline", properties);
  229. if (greyScaleOut.floatValue == 1) targetMat.EnableKeyword("GREYSCALEOUTLINE_ON");
  230. else targetMat.DisableKeyword("GREYSCALEOUTLINE_ON");
  231. }
  232. EditorGUILayout.EndVertical();
  233. }
  234. else targetMat.DisableKeyword("GREYSCALE_ON");
  235. EditorGUILayout.EndToggleGroup();
  236. }
  237. private void Posterize(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  238. {
  239. bool ini = toggle;
  240. toggle = EditorGUILayout.BeginToggleGroup("13.Posterize", toggle);
  241. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  242. if (toggle)
  243. {
  244. targetMat.EnableKeyword("POSTERIZE_ON");
  245. EditorGUILayout.BeginVertical(style);
  246. {
  247. materialEditor.ShaderProperty(properties[57], properties[57].displayName);
  248. materialEditor.ShaderProperty(properties[58], properties[58].displayName);
  249. materialEditor.ShaderProperty(properties[59], properties[59].displayName);
  250. MaterialProperty posterizeOut = ShaderGUI.FindProperty("_PosterizeOutline", properties);
  251. if (posterizeOut.floatValue == 1) targetMat.EnableKeyword("POSTERIZEOUTLINE_ON");
  252. else targetMat.DisableKeyword("POSTERIZEOUTLINE_ON");
  253. }
  254. EditorGUILayout.EndVertical();
  255. }
  256. else targetMat.DisableKeyword("POSTERIZE_ON");
  257. EditorGUILayout.EndToggleGroup();
  258. }
  259. private void Blur(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  260. {
  261. bool ini = toggle;
  262. toggle = EditorGUILayout.BeginToggleGroup("14.Blur", toggle);
  263. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  264. if (toggle)
  265. {
  266. targetMat.EnableKeyword("BLUR_ON");
  267. EditorGUILayout.BeginVertical(style);
  268. {
  269. materialEditor.ShaderProperty(properties[60], properties[60].displayName);
  270. materialEditor.ShaderProperty(properties[61], properties[61].displayName);
  271. MaterialProperty blurIsHd = ShaderGUI.FindProperty("_BlurHD", properties);
  272. if (blurIsHd.floatValue == 1) targetMat.EnableKeyword("BLURISHD_ON");
  273. else targetMat.DisableKeyword("BLURISHD_ON");
  274. }
  275. EditorGUILayout.EndVertical();
  276. }
  277. else targetMat.DisableKeyword("BLUR_ON");
  278. EditorGUILayout.EndToggleGroup();
  279. }
  280. private void Grass(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle)
  281. {
  282. bool ini = toggle;
  283. toggle = EditorGUILayout.BeginToggleGroup("26.Grass Movement / Wind", toggle);
  284. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  285. if (toggle)
  286. {
  287. targetMat.EnableKeyword("WIND_ON");
  288. EditorGUILayout.BeginVertical(style);
  289. {
  290. materialEditor.ShaderProperty(properties[90], properties[90].displayName);
  291. materialEditor.ShaderProperty(properties[91], properties[91].displayName);
  292. materialEditor.ShaderProperty(properties[92], properties[92].displayName);
  293. materialEditor.ShaderProperty(properties[93], properties[93].displayName);
  294. MaterialProperty grassManual = ShaderGUI.FindProperty("_GrassManualToggle", properties);
  295. if (grassManual.floatValue == 1) targetMat.EnableKeyword("MANUALWIND_ON");
  296. else targetMat.DisableKeyword("MANUALWIND_ON");
  297. }
  298. EditorGUILayout.EndVertical();
  299. }
  300. else targetMat.DisableKeyword("WIND_ON");
  301. EditorGUILayout.EndToggleGroup();
  302. }
  303. private void Glitch(MaterialEditor materialEditor, MaterialProperty[] properties, GUIStyle style, bool toggle, string inspector, string flag)
  304. {
  305. bool ini = toggle;
  306. toggle = EditorGUILayout.BeginToggleGroup(inspector, toggle);
  307. if (ini != toggle && !Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  308. if (toggle)
  309. {
  310. targetMat.EnableKeyword(flag);
  311. EditorGUILayout.BeginVertical(style);
  312. {
  313. materialEditor.ShaderProperty(properties[80], properties[80].displayName);
  314. materialEditor.ShaderProperty(properties[135], properties[135].displayName);
  315. }
  316. EditorGUILayout.EndVertical();
  317. }
  318. else targetMat.DisableKeyword(flag);
  319. EditorGUILayout.EndToggleGroup();
  320. }
  321. }