源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

211 行
8.3 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. public class CombineModelEffectFull : ShaderGUI
  4. {
  5. Material target;
  6. MaterialEditor editor;
  7. MaterialProperty[] properties;
  8. static GUIContent staticLabel = new GUIContent();
  9. bool checkFresnel = false;
  10. bool checkOutline = false;
  11. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  12. {
  13. // base.OnGUI(materialEditor, properties);
  14. this.target = materialEditor.target as Material;
  15. this.editor = materialEditor;
  16. this.properties = properties;
  17. GUILayout.Label("本特效材质至多支持两个UVRoll效果(两张UVRoll纹理)+两个Scan效果", EditorStyles.boldLabel);
  18. GUILayout.Space(20);
  19. DoEffectMask();
  20. DoUVRoll();
  21. DoScan();
  22. DoFresnel();
  23. DoOutline();
  24. }
  25. // uvroll
  26. void DoUVRoll()
  27. {
  28. GUILayout.Label("UVRoll 效果参数", EditorStyles.boldLabel);
  29. // 检查参数,优化editor视图
  30. EditorGUI.BeginChangeCheck();
  31. MaterialProperty uvRollTex = FindProperty("_UVRollTex");
  32. MaterialProperty uvRollTex2 = FindProperty("_UVRollTex2");
  33. editor.TexturePropertySingleLine(
  34. MakeLabel(uvRollTex, "(RGBA)"), uvRollTex);
  35. editor.TextureScaleOffsetProperty(uvRollTex);
  36. editor.TexturePropertySingleLine(
  37. MakeLabel(uvRollTex2, "(RGBA)"), uvRollTex2);
  38. editor.TextureScaleOffsetProperty(uvRollTex2);
  39. if(uvRollTex.textureValue != null || uvRollTex2.textureValue != null)
  40. {
  41. MaterialProperty uvrollColor1 = FindProperty("_UVRollColor1");
  42. MaterialProperty uvrollColor2 = FindProperty("_UVRollColor2");
  43. MaterialProperty uvrollSpeed = FindProperty("_UVRollSpeed");
  44. MaterialProperty uvrollIntensity1 = FindProperty("_UVRollIntensity1");
  45. MaterialProperty uvrollIntensity2 = FindProperty("_UVRollIntensity2");
  46. EditorGUI.indentLevel += 1;
  47. editor.ShaderProperty(uvrollColor1, MakeLabel(uvrollColor1));
  48. editor.ShaderProperty(uvrollColor2, MakeLabel(uvrollColor2));
  49. GUILayout.Label("整合UVRoll速度,x y代表特效1的u v速度,z w同理", EditorStyles.boldLabel);
  50. editor.ShaderProperty(uvrollSpeed, MakeLabel(uvrollSpeed));
  51. GUILayout.Space(-10);
  52. editor.ShaderProperty(uvrollIntensity1, MakeLabel(uvrollIntensity1));
  53. editor.ShaderProperty(uvrollIntensity2, MakeLabel(uvrollIntensity2));
  54. EditorGUI.indentLevel -= 1;
  55. }
  56. if (EditorGUI.EndChangeCheck()) {
  57. // SetKeyword("_ROLLADD_MAP", uvRollTex.textureValue);
  58. }
  59. GUILayout.Space(20);
  60. DoUVRollNoise();
  61. }
  62. void DoUVRollNoise()
  63. {
  64. GUILayout.Label("UVRoll效果扰动纹理", EditorStyles.boldLabel);
  65. EditorGUI.BeginChangeCheck();
  66. MaterialProperty noise = FindProperty("_UVRollNoiseTex");
  67. editor.TexturePropertySingleLine(MakeLabel(noise), noise);
  68. if(noise.textureValue != null)
  69. {
  70. MaterialProperty uvrollNoiseSpeed = FindProperty("_UVRollNoiseSpeed");
  71. MaterialProperty uvrollNoiseIntensity = FindProperty("_UVRollNoiseIntensity");
  72. EditorGUI.indentLevel += 1;
  73. GUILayout.Label("整合Noise速度,x y代表特效1的u v速度,z w同理", EditorStyles.boldLabel);
  74. editor.ShaderProperty(uvrollNoiseSpeed, MakeLabel(uvrollNoiseSpeed));
  75. GUILayout.Space(-10);
  76. editor.ShaderProperty(uvrollNoiseIntensity, MakeLabel(uvrollNoiseIntensity));
  77. EditorGUI.indentLevel -= 1;
  78. }
  79. if (EditorGUI.EndChangeCheck()) {
  80. SetKeyword("_ROLLADD_NOISE_MASK_MAP", noise.textureValue);
  81. }
  82. GUILayout.Space(20);
  83. }
  84. void DoEffectMask()
  85. {
  86. GUILayout.Label("特效遮罩(每个特效至多两种,RG通道为UVRoll遮罩,BA通道为扫描图遮罩)", EditorStyles.boldLabel);
  87. EditorGUI.BeginChangeCheck();
  88. MaterialProperty mask = FindProperty("_EffectMaskTex");
  89. editor.TexturePropertySingleLine(MakeLabel(mask), mask);
  90. if(mask.textureValue != null)
  91. {
  92. MaterialProperty uvrollMaskSpeed = FindProperty("_UVRollMaskSpeed");
  93. EditorGUI.indentLevel += 1;
  94. GUILayout.Label("整合UVRollMask速度,x y代表特效1的u v速度,z w同理", EditorStyles.boldLabel);
  95. editor.ShaderProperty(uvrollMaskSpeed, MakeLabel(uvrollMaskSpeed));
  96. EditorGUI.indentLevel -= 1;
  97. }
  98. if (EditorGUI.EndChangeCheck()) {
  99. // SetKeyword("_EFFECT_MASK_MAP", mask.textureValue);
  100. }
  101. }
  102. // 扫描效果
  103. void DoScan()
  104. {
  105. GUILayout.Label("Scan扫描效果(网格纹理/扫描纹理 第一效果占用RG通道,第二效果占用BA通道)", EditorStyles.boldLabel);
  106. EditorGUI.BeginChangeCheck();
  107. MaterialProperty scan = FindProperty("_ScanTex");
  108. editor.TexturePropertySingleLine(
  109. MakeLabel(scan, "(GR通道为效果1,AB效果2)"), scan,
  110. FindProperty("_ScanColor"));
  111. if(scan.textureValue != null)
  112. {
  113. MaterialProperty scan_gridline_st = FindProperty("_GridlineST");
  114. MaterialProperty scan_intensity = FindProperty("_ScanIntensity");
  115. MaterialProperty scan_speed = FindProperty("_ScanSpeed");
  116. EditorGUI.indentLevel += 1;
  117. GUILayout.Label("用这个网格图原先的纹理Tilling和Offset", EditorStyles.boldLabel);
  118. editor.ShaderProperty(scan_gridline_st, MakeLabel(scan_gridline_st));
  119. GUILayout.Space(-10);
  120. editor.ShaderProperty(scan_intensity, MakeLabel(scan_intensity));
  121. editor.ShaderProperty(scan_speed, MakeLabel(scan_speed));
  122. EditorGUI.indentLevel -= 1;
  123. }
  124. if (EditorGUI.EndChangeCheck()) {
  125. // SetKeyword("_SCAN_TEX", scan.textureValue);
  126. }
  127. GUILayout.Space(20);
  128. }
  129. // fresnel
  130. void DoFresnel()
  131. {
  132. GUILayout.Label("Fresnel外发光", EditorStyles.boldLabel);
  133. checkFresnel = IsKeywordEnabled("_MB_FRESNEL");
  134. EditorGUI.BeginChangeCheck();
  135. checkFresnel = EditorGUILayout.Toggle("Enable Fresnel", checkFresnel);
  136. if(checkFresnel)
  137. {
  138. MaterialProperty fresnelColor = FindProperty("_FresnelColor");
  139. MaterialProperty fresnelIntensity = FindProperty("_FresnelIntensity");
  140. EditorGUI.indentLevel += 1;
  141. editor.ShaderProperty(fresnelColor, MakeLabel(fresnelColor));
  142. editor.ShaderProperty(fresnelIntensity, MakeLabel(fresnelIntensity));
  143. EditorGUI.indentLevel -= 1;
  144. }
  145. if (EditorGUI.EndChangeCheck()) {
  146. SetKeyword("_MB_FRESNEL", checkFresnel);
  147. }
  148. GUILayout.Space(20);
  149. }
  150. void DoOutline()
  151. {
  152. GUILayout.Label("受击描边颜色", EditorStyles.boldLabel);
  153. MaterialProperty outlineColor = FindProperty("_OutlineColor");
  154. MaterialProperty outlineIntensity = FindProperty("_OutlineIntensity");
  155. editor.ShaderProperty(outlineColor, MakeLabel(outlineColor));
  156. editor.ShaderProperty(outlineIntensity, MakeLabel(outlineIntensity));
  157. GUILayout.Space(20);
  158. }
  159. // 利用方法优化GUI代码结构,使得代码更容易看
  160. MaterialProperty FindProperty(string name)
  161. {
  162. return FindProperty(name, properties);
  163. }
  164. static GUIContent MakeLabel(string text, string tooltip = null)
  165. {
  166. staticLabel.text = text;
  167. staticLabel.tooltip = tooltip;
  168. return staticLabel;
  169. }
  170. static GUIContent MakeLabel(MaterialProperty property, string tooltip = null)
  171. {
  172. staticLabel.text = property.displayName;
  173. staticLabel.tooltip = tooltip;
  174. return staticLabel;
  175. }
  176. // 设置shader关键字
  177. void SetKeyword(string keyword, bool state)
  178. {
  179. if(state)
  180. {
  181. foreach(Material m in editor.targets)
  182. {
  183. m.EnableKeyword(keyword);
  184. }
  185. }
  186. else{
  187. foreach(Material m in editor.targets)
  188. {
  189. m.DisableKeyword(keyword);
  190. }
  191. }
  192. }
  193. bool IsKeywordEnabled(string keyword)
  194. {
  195. return target.IsKeywordEnabled(keyword);
  196. }
  197. void RecordAction(string label)
  198. {
  199. editor.RegisterPropertyChangeUndo(label);
  200. }
  201. }