|
|
- using UnityEngine;
- using UnityEditor;
- public class CombineModelEffectMid : ShaderGUI
- {
- Material target;
- MaterialEditor editor;
- MaterialProperty[] properties;
- static GUIContent staticLabel = new GUIContent();
- bool checkFresnel = false;
- bool checkOutline = false;
- public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
- {
- // base.OnGUI(materialEditor, properties);
- this.target = materialEditor.target as Material;
- this.editor = materialEditor;
- this.properties = properties;
- GUILayout.Label("本特效材质至多支持两个UVRoll效果(同UVRoll纹理)+两个Scan效果", EditorStyles.boldLabel);
- GUILayout.Space(20);
- DoEffectMask();
- DoUVRoll();
- DoScan();
- DoFresnel();
- DoOutline();
- }
- // uvroll
- void DoUVRoll()
- {
- GUILayout.Label("UVRoll 效果参数", EditorStyles.boldLabel);
- // 检查参数,优化editor视图
- EditorGUI.BeginChangeCheck();
- MaterialProperty uvRollTex = FindProperty("_UVRollTex");
-
- editor.TexturePropertySingleLine(
- MakeLabel(uvRollTex, "(RGBA)"), uvRollTex);
- editor.TextureScaleOffsetProperty(uvRollTex);
-
- if(uvRollTex.textureValue != null)
- {
- MaterialProperty uvrollColor1 = FindProperty("_UVRollColor1");
- MaterialProperty uvrollColor2 = FindProperty("_UVRollColor2");
- MaterialProperty uvrollSpeed = FindProperty("_UVRollSpeed");
- MaterialProperty uvrollIntensity1 = FindProperty("_UVRollIntensity1");
- MaterialProperty uvrollIntensity2 = FindProperty("_UVRollIntensity2");
- EditorGUI.indentLevel += 1;
- editor.ShaderProperty(uvrollColor1, MakeLabel(uvrollColor1));
- editor.ShaderProperty(uvrollColor2, MakeLabel(uvrollColor2));
- GUILayout.Label("整合UVRoll速度,x y代表特效1的u v速度,z w同理", EditorStyles.boldLabel);
- editor.ShaderProperty(uvrollSpeed, MakeLabel(uvrollSpeed));
- GUILayout.Space(-10);
- editor.ShaderProperty(uvrollIntensity1, MakeLabel(uvrollIntensity1));
- editor.ShaderProperty(uvrollIntensity2, MakeLabel(uvrollIntensity2));
- EditorGUI.indentLevel -= 1;
- }
- if (EditorGUI.EndChangeCheck()) {
- // SetKeyword("_ROLLADD_MAP", uvRollTex.textureValue);
- }
- GUILayout.Space(20);
- DoUVRollNoise();
- }
-
- void DoUVRollNoise()
- {
- GUILayout.Label("UVRoll效果扰动纹理", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- MaterialProperty noise = FindProperty("_UVRollNoiseTex");
- editor.TexturePropertySingleLine(MakeLabel(noise), noise);
- if(noise.textureValue != null)
- {
- MaterialProperty uvrollNoiseSpeed = FindProperty("_UVRollNoiseSpeed");
- MaterialProperty uvrollNoiseIntensity = FindProperty("_UVRollNoiseIntensity");
- EditorGUI.indentLevel += 1;
- GUILayout.Label("整合Noise速度,x y代表特效1的u v速度,z w同理", EditorStyles.boldLabel);
- editor.ShaderProperty(uvrollNoiseSpeed, MakeLabel(uvrollNoiseSpeed));
- GUILayout.Space(-10);
- editor.ShaderProperty(uvrollNoiseIntensity, MakeLabel(uvrollNoiseIntensity));
- EditorGUI.indentLevel -= 1;
- }
- if (EditorGUI.EndChangeCheck()) {
- SetKeyword("_ROLLADD_NOISE_MASK_MAP", noise.textureValue);
- }
- GUILayout.Space(20);
- }
-
- void DoEffectMask()
- {
- GUILayout.Label("特效遮罩(每个特效至多两种,RG通道为UVRoll遮罩,BA通道为扫描图遮罩)", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- MaterialProperty mask = FindProperty("_EffectMaskTex");
- editor.TexturePropertySingleLine(MakeLabel(mask), mask);
- if(mask.textureValue != null)
- {
- MaterialProperty uvrollMaskSpeed = FindProperty("_UVRollMaskSpeed");
- EditorGUI.indentLevel += 1;
- GUILayout.Label("整合UVRollMask速度,x y代表特效1的u v速度,z w同理", EditorStyles.boldLabel);
- editor.ShaderProperty(uvrollMaskSpeed, MakeLabel(uvrollMaskSpeed));
- EditorGUI.indentLevel -= 1;
- }
- if (EditorGUI.EndChangeCheck()) {
- // SetKeyword("_EFFECT_MASK_MAP", mask.textureValue);
- }
- }
- // 扫描效果
- void DoScan()
- {
- GUILayout.Label("Scan扫描效果(网格纹理/扫描纹理 第一效果占用RG通道,第二效果占用BA通道)", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- MaterialProperty scan = FindProperty("_ScanTex");
- editor.TexturePropertySingleLine(
- MakeLabel(scan, "(GR通道为效果1,AB效果2)"), scan,
- FindProperty("_ScanColor"));
- if(scan.textureValue != null)
- {
- MaterialProperty scan_gridline_st = FindProperty("_GridlineST");
- MaterialProperty scan_intensity = FindProperty("_ScanIntensity");
- MaterialProperty scan_speed = FindProperty("_ScanSpeed");
- EditorGUI.indentLevel += 1;
- GUILayout.Label("用这个网格图原先的纹理Tilling和Offset", EditorStyles.boldLabel);
- editor.ShaderProperty(scan_gridline_st, MakeLabel(scan_gridline_st));
- GUILayout.Space(-10);
- editor.ShaderProperty(scan_intensity, MakeLabel(scan_intensity));
- editor.ShaderProperty(scan_speed, MakeLabel(scan_speed));
- EditorGUI.indentLevel -= 1;
- }
- if (EditorGUI.EndChangeCheck()) {
- // SetKeyword("_SCAN_TEX", scan.textureValue);
- }
- GUILayout.Space(20);
- }
- // fresnel
- void DoFresnel()
- {
- GUILayout.Label("Fresnel外发光", EditorStyles.boldLabel);
- checkFresnel = IsKeywordEnabled("_MB_FRESNEL");
- EditorGUI.BeginChangeCheck();
- checkFresnel = EditorGUILayout.Toggle("Enable Fresnel", checkFresnel);
- if(checkFresnel)
- {
- MaterialProperty fresnelColor = FindProperty("_FresnelColor");
- MaterialProperty fresnelIntensity = FindProperty("_FresnelIntensity");
- EditorGUI.indentLevel += 1;
- editor.ShaderProperty(fresnelColor, MakeLabel(fresnelColor));
- editor.ShaderProperty(fresnelIntensity, MakeLabel(fresnelIntensity));
- EditorGUI.indentLevel -= 1;
- }
- if (EditorGUI.EndChangeCheck()) {
- SetKeyword("_MB_FRESNEL", checkFresnel);
- }
- GUILayout.Space(20);
- }
-
- void DoOutline()
- {
- GUILayout.Label("受击描边颜色", EditorStyles.boldLabel);
- MaterialProperty outlineColor = FindProperty("_OutlineColor");
- MaterialProperty outlineIntensity = FindProperty("_OutlineIntensity");
- editor.ShaderProperty(outlineColor, MakeLabel(outlineColor));
- editor.ShaderProperty(outlineIntensity, MakeLabel(outlineIntensity));
- GUILayout.Space(20);
- }
-
- // 利用方法优化GUI代码结构,使得代码更容易看
- MaterialProperty FindProperty(string name)
- {
- return FindProperty(name, properties);
- }
-
- static GUIContent MakeLabel(string text, string tooltip = null)
- {
- staticLabel.text = text;
- staticLabel.tooltip = tooltip;
- return staticLabel;
- }
-
- static GUIContent MakeLabel(MaterialProperty property, string tooltip = null)
- {
- staticLabel.text = property.displayName;
- staticLabel.tooltip = tooltip;
- return staticLabel;
- }
- // 设置shader关键字
- void SetKeyword(string keyword, bool state)
- {
- if(state)
- {
- foreach(Material m in editor.targets)
- {
- m.EnableKeyword(keyword);
- }
- }
- else{
- foreach(Material m in editor.targets)
- {
- m.DisableKeyword(keyword);
- }
- }
- }
- bool IsKeywordEnabled(string keyword)
- {
- return target.IsKeywordEnabled(keyword);
- }
-
- void RecordAction(string label)
- {
- editor.RegisterPropertyChangeUndo(label);
- }
- }
|