源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 line
13 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class MainRoleProHologramEditor : ShaderGUI
  6. {
  7. Material target;
  8. MaterialEditor editor;
  9. MaterialProperty[] properties;
  10. static GUIContent staticLabel = new GUIContent();
  11. // 折叠参数
  12. bool expand_color_glitch;
  13. bool expand_fresnel;
  14. bool expand_line1;
  15. bool expand_line2;
  16. bool expand_ver_gliter1;
  17. bool expand_ver_gliter2;
  18. bool expand_grain;
  19. bool expand_mask;
  20. bool expand_dissolve;
  21. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  22. {
  23. // base.OnGUI(materialEditor, properties);
  24. this.target = materialEditor.target as Material;
  25. this.editor = materialEditor;
  26. this.properties = properties;
  27. DoBase();
  28. DoColorGlitch();
  29. DoFresnel();
  30. DoScanLine();
  31. DoVertexGliter();
  32. DoGrain();
  33. DoHgMask();
  34. DoDissolve();
  35. }
  36. // 基础参数
  37. void DoBase()
  38. {
  39. // 全息颜色
  40. // GUILayout.Label("全息基础参数", EditorStyles.helpBox);
  41. MaterialProperty hg_color = FindProperty("_HologramColor");
  42. editor.ShaderProperty(hg_color, MakeLabel("全息整体颜色"));
  43. MaterialProperty hg_alpha = FindProperty("_HologramAlpha");
  44. editor.ShaderProperty(hg_alpha, MakeLabel("全息整体透明度"));
  45. MaterialProperty glitch_map = FindProperty("_HologramGlitchMap");
  46. editor.TextureProperty(glitch_map, "噪声纹理", false);
  47. GUILayout.Space(10);
  48. }
  49. // 颜色故障效果
  50. void DoColorGlitch()
  51. {
  52. // GUILayout.Label("全息颜色故障效果", EditorStyles.helpBox);
  53. expand_color_glitch = EditorGUILayout.Foldout(expand_color_glitch, "全息颜色故障效果");
  54. if(expand_color_glitch)
  55. {
  56. EditorGUI.indentLevel += 1;
  57. MaterialProperty c_g_tog = FindProperty("_HologramColorGlitchTog");
  58. editor.ShaderProperty(c_g_tog, "使用颜色故障");
  59. if(GetToggleEnabled(c_g_tog))
  60. {
  61. MaterialProperty glitch_val = FindProperty("_HologramColorGlitch");
  62. MaterialProperty glitch_data = FindProperty("_HologramColorGlitchData");
  63. MaterialProperty glitch_min = FindProperty("_HologramColorGlitchMin");
  64. editor.ShaderProperty(glitch_val, "颜色故障强度");
  65. editor.VectorProperty(glitch_data, "噪声速度(使用XY分量)");
  66. editor.ShaderProperty(glitch_min, "颜色故障最小值");
  67. }
  68. EditorGUI.indentLevel -= 1;
  69. }
  70. }
  71. // 菲涅尔反射
  72. void DoFresnel()
  73. {
  74. // GUILayout.Label("全息Fresnel效果", EditorStyles.helpBox);
  75. expand_fresnel = EditorGUILayout.Foldout(expand_fresnel, "全息Fresnel效果");
  76. if(expand_fresnel)
  77. {
  78. EditorGUI.indentLevel += 1;
  79. MaterialProperty fresnel_scale = FindProperty("_FresnelScale");
  80. MaterialProperty fresnel_power = FindProperty("_FresnelPower");
  81. MaterialProperty fresnel_scale_a = FindProperty("_FresnelAlphaScale");
  82. MaterialProperty fresnel_power_a = FindProperty("_FresnelAlphaPower");
  83. editor.ShaderProperty(fresnel_scale, "Fresnel大小");
  84. editor.ShaderProperty(fresnel_power, "Fresnel指数");
  85. editor.ShaderProperty(fresnel_scale_a, "Fresnel Alpha大小");
  86. editor.ShaderProperty(fresnel_power_a, "Fresnel Alpha指数");
  87. EditorGUI.indentLevel -= 1;
  88. }
  89. }
  90. // 扫描线属性
  91. void DoScanLine()
  92. {
  93. // GUILayout.Label("扫描线效果", EditorStyles.helpBox);
  94. expand_line1 = EditorGUILayout.Foldout(expand_line1, "扫描线效果1");
  95. if(expand_line1)
  96. {
  97. EditorGUI.indentLevel += 1;
  98. MaterialProperty line1_map = FindProperty("_HologramLine1");
  99. MaterialProperty line1_speed = FindProperty("_HologramLine1Speed");
  100. MaterialProperty line1_tilling = FindProperty("_HologramLine1Frequency");
  101. MaterialProperty line1_alpha = FindProperty("_HologramLine1Alpha");
  102. editor.TextureProperty(line1_map, "扫描线1纹理", false);
  103. editor.ShaderProperty(line1_speed, "扫描线1速度");
  104. editor.ShaderProperty(line1_tilling, "扫描线1tilling");
  105. editor.ShaderProperty(line1_alpha, "扫描线1透明度");
  106. EditorGUI.indentLevel -= 1;
  107. }
  108. expand_line2 = EditorGUILayout.Foldout(expand_line2, "扫描线效果2");
  109. if(expand_line2)
  110. {
  111. EditorGUI.indentLevel += 1;
  112. EditorGUI.BeginChangeCheck();
  113. MaterialProperty line2_tog = FindProperty("_HologramLine2Tog");
  114. editor.ShaderProperty(line2_tog, "使用扫描线2");
  115. bool line2_enabled = GetToggleEnabled(line2_tog);
  116. if(line2_enabled)
  117. {
  118. MaterialProperty line2_map = FindProperty("_HologramLine2");
  119. MaterialProperty line2_speed = FindProperty("_HologramLine2Speed");
  120. MaterialProperty line2_tilling = FindProperty("_HologramLine2Frequency");
  121. MaterialProperty line2_alpha = FindProperty("_HologramLine2Alpha");
  122. editor.TextureProperty(line2_map, "扫描线2纹理", false);
  123. editor.ShaderProperty(line2_speed, "扫描线2速度");
  124. editor.ShaderProperty(line2_tilling, "扫描线2tilling");
  125. editor.ShaderProperty(line2_alpha, "扫描线2透明度");
  126. }
  127. EditorGUI.indentLevel -= 1;
  128. if(EditorGUI.EndChangeCheck())
  129. {
  130. SetKeyword("_USE_SCANLINE2", line2_enabled);
  131. }
  132. }
  133. }
  134. // 顶点故障效果
  135. void DoVertexGliter()
  136. {
  137. // GUILayout.Label("模型顶点故障效果", EditorStyles.helpBox);
  138. expand_ver_gliter1 = EditorGUILayout.Foldout(expand_ver_gliter1, "顶点故障1");
  139. if(expand_ver_gliter1)
  140. {
  141. EditorGUI.indentLevel += 1;
  142. MaterialProperty gliter1 = FindProperty("_HologramGliterData1");
  143. GUILayout.Label("x:速度,y:抖动范围,z:抖动偏移量(正负区分左右抖动),w代表频率(0~0.99)", EditorStyles.centeredGreyMiniLabel);
  144. editor.VectorProperty(gliter1, "故障参数1");
  145. EditorGUI.indentLevel -= 1;
  146. }
  147. expand_ver_gliter2 = EditorGUILayout.Foldout(expand_ver_gliter2, "顶点故障2");
  148. if(expand_ver_gliter2)
  149. {
  150. EditorGUI.indentLevel += 1;
  151. MaterialProperty gliter2 = FindProperty("_HologramGliterData2");
  152. GUILayout.Label("x:速度,y:抖动范围,z:抖动偏移量(正负区分左右抖动),w代表频率(0~0.99)", EditorStyles.centeredGreyMiniLabel);
  153. editor.VectorProperty(gliter2, "故障参数2");
  154. EditorGUI.indentLevel -= 1;
  155. }
  156. }
  157. // 颗粒效果
  158. void DoGrain()
  159. {
  160. // GUILayout.Label("整体颗粒效果", EditorStyles.helpBox);
  161. expand_grain = EditorGUILayout.Foldout(expand_grain, "颗粒效果");
  162. if(expand_grain)
  163. {
  164. EditorGUI.indentLevel += 1;
  165. MaterialProperty grain_data = FindProperty("_HologramGrainData");
  166. MaterialProperty grain_speed = FindProperty("_HologramGrainSpeed");
  167. MaterialProperty grain_affect = FindProperty("_HologramGrainAffect");
  168. GUILayout.Label("xy:噪声采样tilling(需要噪声图),zw:噪声颜色区间(0~1)", EditorStyles.centeredGreyMiniLabel);
  169. editor.VectorProperty(grain_data, "颗粒参数");
  170. editor.ShaderProperty(grain_speed, "颗粒效果速度");
  171. editor.ShaderProperty(grain_affect, "颗粒效果强度");
  172. EditorGUI.indentLevel -= 1;
  173. }
  174. }
  175. // 蒙版
  176. void DoHgMask()
  177. {
  178. // GUILayout.Label("整体颜色蒙版", EditorStyles.helpBox);
  179. expand_mask = EditorGUILayout.Foldout(expand_mask, "整体蒙版");
  180. if(expand_mask)
  181. {
  182. EditorGUI.indentLevel += 1;
  183. MaterialProperty mask = FindProperty("_HologramMaskMap");
  184. MaterialProperty mask_affect = FindProperty("_HologramMaskAffect");
  185. MaterialProperty mask_use_axis = FindProperty("_HologramMaskUseAxis");
  186. MaterialProperty mask_y = FindProperty("_HologramYMask");
  187. editor.TextureProperty(mask, "蒙版");
  188. editor.ShaderProperty(mask_affect, "蒙版强度");
  189. GUILayout.Label("项目中有的模型会带有不同方向的旋转,用这个字段筛选旋转后的正确的y轴", EditorStyles.helpBox);
  190. editor.ShaderProperty(mask_use_axis, "目标渐变的坐标轴");
  191. editor.ShaderProperty(mask_y, "垂直方向显示蒙版阈值");
  192. EditorGUI.indentLevel -= 1;
  193. }
  194. }
  195. // 溶解效果
  196. void DoDissolve()
  197. {
  198. // GUILayout.Label("溶解效果", EditorStyles.helpBox);
  199. expand_dissolve = EditorGUILayout.Foldout(expand_dissolve, "溶解效果");
  200. if(expand_dissolve)
  201. {
  202. EditorGUI.indentLevel += 1;
  203. EditorGUI.BeginChangeCheck();
  204. MaterialProperty dissolve_enabled_tog = FindProperty("_DissolveToggle");
  205. editor.ShaderProperty(dissolve_enabled_tog, "使用溶解效果");
  206. bool tog1_enabled = GetToggleEnabled(dissolve_enabled_tog);
  207. MaterialProperty dissolve_map_tog = FindProperty("_DissolveMapToggle");
  208. bool tog2_enabled = GetToggleEnabled(dissolve_map_tog);
  209. if(tog1_enabled)
  210. {
  211. editor.ShaderProperty(dissolve_map_tog, "使用溶解纹理");
  212. if(tog2_enabled) // 使用溶解纹理
  213. {
  214. MaterialProperty dissolve_map = FindProperty("_DissolveMap");
  215. editor.TextureProperty(dissolve_map, "溶解纹理");
  216. }
  217. MaterialProperty dissolve_threshold = FindProperty("_DissolveThreshold");
  218. MaterialProperty dissolve_fadein_color = FindProperty("_DissolveFadeInColor");
  219. MaterialProperty dissolve_fadein_threshold = FindProperty("_DissolveFadeInThreshold");
  220. MaterialProperty dissolve_mid_color = FindProperty("_DissolveFadeMidColor");
  221. MaterialProperty dissolve_mid_threshold = FindProperty("_DissolveFadeMidThreshold");
  222. MaterialProperty dissolve_out_color = FindProperty("_DissolveFadeOutColor");
  223. MaterialProperty dissolve_out_threshold = FindProperty("_DissolveFadeOutThreshold");
  224. editor.ShaderProperty(dissolve_threshold, "溶解进度");
  225. editor.ColorProperty(dissolve_fadein_color, "开始溶解的颜色");
  226. editor.ShaderProperty(dissolve_fadein_threshold, "开始溶解阈值");
  227. editor.ColorProperty(dissolve_mid_color, "进入到溶解时的颜色");
  228. editor.ShaderProperty(dissolve_mid_threshold, "进入到溶解时的颜色");
  229. editor.ColorProperty(dissolve_out_color, "溶解至淡出时颜色");
  230. editor.ShaderProperty(dissolve_out_threshold, "溶解淡出阈值");
  231. }
  232. EditorGUI.indentLevel -= 1;
  233. if(EditorGUI.EndChangeCheck())
  234. {
  235. SetKeyword("_USE_DISSOLVE_EFFECT", tog1_enabled);
  236. SetKeyword("_USE_DISSOLVE_MAP", tog2_enabled);
  237. }
  238. }
  239. }
  240. // 利用方法优化GUI代码结构,使得代码更容易看
  241. MaterialProperty FindProperty(string name)
  242. {
  243. return FindProperty(name, properties);
  244. }
  245. static GUIContent MakeLabel(string text, string tooltip = null)
  246. {
  247. staticLabel.text = text;
  248. staticLabel.tooltip = tooltip;
  249. return staticLabel;
  250. }
  251. static GUIContent MakeLabel(MaterialProperty property, string tooltip = null)
  252. {
  253. staticLabel.text = property.displayName;
  254. staticLabel.tooltip = tooltip;
  255. return staticLabel;
  256. }
  257. // 设置shader关键字
  258. void SetKeyword(string keyword, bool state)
  259. {
  260. if(state)
  261. {
  262. foreach(Material m in editor.targets)
  263. {
  264. m.EnableKeyword(keyword);
  265. }
  266. }
  267. else{
  268. foreach(Material m in editor.targets)
  269. {
  270. m.DisableKeyword(keyword);
  271. }
  272. }
  273. }
  274. bool IsKeywordEnabled(string keyword)
  275. {
  276. return target.IsKeywordEnabled(keyword);
  277. }
  278. void RecordAction(string label)
  279. {
  280. editor.RegisterPropertyChangeUndo(label);
  281. }
  282. bool GetToggleEnabled(MaterialProperty property)
  283. {
  284. return (int)property.floatValue == 1;
  285. }
  286. }