protected override bool OnInitProperties(MaterialProperty[] props) { if (m_args != null && props.Length > 0) { var material = this.m_MaterialEditor.target as Material; if (ShaderGUIHelper.IsModeMatched(this, m_args) && GetBoolTestResult(material, props)) { var values = m_args.GetField("values"); if (values != null && values.type == JSONObject.Type.OBJECT) { for (int i = 0; i < values.keys.Count; ++i) { var key = values.keys[i]; var value = values.GetField(key); var prop = ShaderGUI.FindProperty(key, props); if (prop != null && !String.IsNullOrEmpty(key) && value != null) { switch (prop.type) { case MaterialProperty.PropType.Float: { float f; if (ShaderGUIHelper.ParseValue(this, values, key, out f)) { prop.floatValue = f; } } break; case MaterialProperty.PropType.Range: { float f; if (ShaderGUIHelper.ParseValue(this, values, key, out f)) { prop.floatValue = Mathf.Clamp(f, prop.rangeLimits.x, prop.rangeLimits.y); } } break; case MaterialProperty.PropType.Color: { Vector4 c = prop.colorValue; int comp; if (ShaderGUIHelper.TryParseVector4(this, values, key, ref c, out comp)) { if ((prop.flags & MaterialProperty.PropFlags.HDR) != 0) { c.x = Mathf.Clamp01(c.x); c.y = Mathf.Clamp01(c.y); c.z = Mathf.Clamp01(c.z); c.w = Mathf.Clamp01(c.w); } prop.colorValue = new Color(c.x, c.y, c.z, c.w); } } break; case MaterialProperty.PropType.Vector: { Vector4 v = prop.colorValue; int comp; if (ShaderGUIHelper.TryParseVector4(this, values, key, ref v, out comp)) { prop.vectorValue = v; } } break; } } } } } var features = m_args.GetField("shader_feature_list"); if (features != null && features.type == JSONObject.Type.ARRAY) { var features_list = new List <String>(features.list.Count); for (int i = 0; i < features.list.Count; ++i) { var o = features.list[i]; if (o != null && o.type == JSONObject.Type.STRING && !String.IsNullOrEmpty(o.str)) { features_list.Add(o.str); } } for (int i = 0; i < BlendModeNames.Length; ++i) { features_list.Add("_MODE_" + BlendModeNames[i].ToUpper()); } features_list.Add("_BLEND_ADDITIVE_SERIES"); features_list.Add("_ALPHAPREMULTIPLY_ON"); m_featureList = features_list.Distinct().ToList(); } } return(true); }
protected override bool OnInitProperties(MaterialProperty[] props) { m_prop = FindCachedProperty(m_propName, props); m_keep = null; if (m_prop != null && m_args != null) { if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_PropGUIType, out m_guiType)) { m_guiType = m_guiType.ToLower(); } ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_GUILabel, out m_label); ParsePrecision(); if (!ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_AutoResetValue, out m_autoReset)) { m_autoReset = true; } m_label = String.IsNullOrEmpty(m_label) ? null : m_label; while (m_args.HasField(Cfg.Key_FixedValue)) { var material = this.materialEditor.target as Material; var enable_if = TryGetBoolTestResult(material, props, Cfg.Key_FixedIfValue); if (enable_if != null && enable_if.Value == false) { break; } switch (m_prop.type) { case MaterialProperty.PropType.Float: case MaterialProperty.PropType.Range: { float val; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out val)) { var _defalueVal = GetDefaultRange(material.shader, m_prop.name); if (_defalueVal != null) { m_prop.floatValue = _RoundFloat(val, _defalueVal.Value); Debug.AssertFormat(m_prop.floatValue == val, "{0} = {1}/[{2}]", m_prop.name, m_prop.floatValue, val); } m_keep = m_prop.floatValue; } } break; case MaterialProperty.PropType.Color: { Vector4 val; int mask; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out val, out mask)) { var _val = m_prop.colorValue; for (int i = 0; i < 4; ++i) { if ((mask & (1 << i)) != 0) { _val[i] = _RoundColorFixed(val[i], m_precision); } } m_prop.colorValue = _val; m_keep = _val; } } break; case MaterialProperty.PropType.Vector: { Vector4 val; int mask; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out val, out mask)) { var _val = m_prop.vectorValue; for (int i = 0; i < 4; ++i) { if ((mask & (1 << i)) != 0) { _val[i] = _RoundFloat(val[i]); } } m_prop.vectorValue = _val; m_keep = _val; } } break; case MaterialProperty.PropType.Texture: { String val; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out val)) { var tex = AssetDatabase.LoadAssetAtPath <Texture>(val); if (tex != null) { m_prop.textureValue = tex; m_keep = tex; } else { m_prop.textureValue = null; } } } break; } break; } } if (m_prop == null) { var mat = this.materialEditor.target as Material; if (mat != null && mat.shader != null) { Debug.LogErrorFormat("Find Shader Property failed: {0}.{1}. \n{2}", mat.shader.name, m_propName, AssetDatabase.GetAssetPath(mat)); } } return(m_prop != null); }
protected void ParseOptions() { var enames = Enum.GetNames(typeof(RenderMode)); var evals = Enum.GetValues(typeof(RenderMode)); var options = new List <KeyValuePair <String, int> >(); if (m_args != null) { var _options = m_args.GetField(Cfg.ArgsKey_Options); if (_options != null && _options.IsString && !String.IsNullOrEmpty(_options.str)) { var seps = _options.str.Split(',', ';', '|'); for (int i = 0; i < seps.Length; ++i) { var key = seps[i].Trim(); var index = Array.IndexOf(enames, key); if (index >= 0) { options.Add(new KeyValuePair <String, int>(enames[index], ( int )evals.GetValue(index))); } } } } if (options.Count > 0) { m_optionNames = new string[options.Count]; m_optionValues = new int[options.Count]; for (int i = 0; i < options.Count; ++i) { m_optionNames[i] = options[i].Key; m_optionValues[i] = options[i].Value; } } else { m_optionNames = enames; m_optionValues = new int[m_optionNames.Length]; for (int i = 0; i < m_optionNames.Length; ++i) { m_optionValues[i] = ( int )evals.GetValue(i); } } for (; ;) { if (m_args != null) { String keepOption; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out keepOption)) { var index = Array.IndexOf(m_optionNames, keepOption); if (index >= 0) { m_keep = ( RenderMode )m_optionValues[index]; _Mode = m_keep.Value; SetupMaterialWithRenderingMode(this.m_MaterialEditor.target as Material, true, false); break; } } } if (Array.IndexOf(m_optionValues, ( int )_Mode) < 0) { // 修正选项 _Mode = ( RenderMode )m_optionValues[0]; } // 修正渲染模式参数 SetupMaterialWithRenderingMode(this.m_MaterialEditor.target as Material, true, false); break; } }
public static int ExcuteLogicOp(UnitMaterialEditor gui, MaterialProperty prop, MaterialProperty[] props, JSONObject args) { var _op = String.Empty; args.GetField(out _op, UnitMaterialEditor.Cfg.ArgsKey_OP, "=="); if (prop == null) { var ret = -1; String _id0, _id1; if (args.GetField(out _id0, UnitMaterialEditor.Cfg.ArgsKey_OP_Arg0, String.Empty)) { String ret0; var _0 = EvalLogicOpArg(gui, _id0, props, out ret0); var argIndex = 1; while (args.GetField(out _id1, UnitMaterialEditor.Cfg.ArgsKey_OP_ArgPrefix + argIndex.ToString(), String.Empty)) { String ret1; var _1 = EvalLogicOpArg(gui, _id1, props, out ret1); if (_0 != -1 && _1 != -1) { ret = Compare(_op, _0, _1) ? 1 : 0; } else { if (ret0 != null && ret1 != null) { ret = Compare(_op, ret0, ret1) ? 1 : 0; } } if (_op != "&&" && _op != "||") { break; } else { if (_op == "&&") { if (ret != 1) { break; } } else if (_op == "||") { if (ret == 1) { break; } } } ++argIndex; _0 = _1; ret0 = ret1; } } return(ret); } switch (prop.type) { case MaterialProperty.PropType.Texture: { var lh = prop.textureValue != null ? 1 : 0; int rh; if (!ShaderGUIHelper.ParseValue(gui, args, UnitMaterialEditor.Cfg.ArgsKey_OP_Ref, out rh)) { rh = 1; } return(ShaderGUIHelper.Compare(_op, lh, rh) ? 1 : 0); } case MaterialProperty.PropType.Vector: { Vector4 rh; int mask; if (ShaderGUIHelper.ParseValue(gui, args, UnitMaterialEditor.Cfg.ArgsKey_OP_Ref, out rh, out mask)) { return(ShaderGUIHelper.Compare(_op, prop.vectorValue, rh, mask) ? 1 : 0); } } break; case MaterialProperty.PropType.Color: { Vector4 rh; int mask; if (ShaderGUIHelper.ParseValue(gui, args, UnitMaterialEditor.Cfg.ArgsKey_OP_Ref, out rh, out mask)) { return(ShaderGUIHelper.Compare(_op, prop.colorValue, rh, mask) ? 1 : 0); } } break; case MaterialProperty.PropType.Range: case MaterialProperty.PropType.Float: { float rh = 0; ShaderGUIHelper.ParseValue(gui, args, UnitMaterialEditor.Cfg.ArgsKey_OP_Ref, out rh); return(ShaderGUIHelper.Compare(_op, prop.floatValue, rh) ? 1 : 0); } } return(-1); }
public static int ExcuteLogicOp(UnitMaterialEditor gui, MaterialProperty prop, JSONObject args) { var _op = String.Empty; args.GetField(out _op, "op", "=="); if (prop == null) { String _id0, _id1; if (args.GetField(out _id0, "arg0", String.Empty) && args.GetField(out _id1, "arg1", String.Empty)) { String ret0; String ret1; var _0 = EvalLogicOpArg(gui, _id0, out ret0); var _1 = EvalLogicOpArg(gui, _id1, out ret1); if (_0 != -1 && _1 != -1) { return(Compare(_op, _0, _1) ? 1 : 0); } else { if (ret0 != null && ret1 != null) { return(Compare(_op, ret0, ret1) ? 1 : 0); } } } return(-1); } switch (prop.type) { case MaterialProperty.PropType.Texture: { var lh = prop.textureValue != null ? 1 : 0; int rh; if (!ShaderGUIHelper.ParseValue(gui, args, "ref", out rh)) { rh = 1; } return(ShaderGUIHelper.Compare(_op, lh, rh) ? 1 : 0); } case MaterialProperty.PropType.Vector: { Vector4 rh; int comp; if (ShaderGUIHelper.ParseValue(gui, args, "ref", out rh, out comp)) { return(ShaderGUIHelper.Compare(_op, prop.vectorValue, rh, comp) ? 1 : 0); } } break; case MaterialProperty.PropType.Color: { Vector4 rh; int comp; if (ShaderGUIHelper.ParseValue(gui, args, "ref", out rh, out comp)) { var c = prop.colorValue; return(ShaderGUIHelper.Compare(_op, new Vector4(c.r, c.g, c.b, c.r), rh, comp) ? 1 : 0); } } break; case MaterialProperty.PropType.Range: case MaterialProperty.PropType.Float: { float rh = 0; ShaderGUIHelper.ParseValue(gui, args, "ref", out rh); return(ShaderGUIHelper.Compare(_op, prop.floatValue, rh) ? 1 : 0); } } return(-1); }
protected override bool OnInitProperties(MaterialProperty[] props) { m_prop = FindCachedProperty(m_propName, props); m_keep = null; if (m_prop != null && m_args != null && m_prop.type == MaterialProperty.PropType.Float) { ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_GUILabel, out m_label); m_label = String.IsNullOrEmpty(m_label) ? null : m_label; if (m_args.HasField(Cfg.Key_FixedValue)) { switch (m_prop.type) { case MaterialProperty.PropType.Float: { float val; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out val)) { int ival = ( int )val; switch (ival) { case 0: m_keep = CullMode.Off; break; case 1: m_keep = CullMode.Front; break; case 2: m_keep = CullMode.Back; break; default: m_keep = CullMode.Back; break; } m_prop.floatValue = ( float )( CullMode )m_keep; } else { String s; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Key_FixedValue, out s)) { s = s.ToLower(); switch (s) { case "none": case Cfg.Value_CullMode_Off: m_keep = CullMode.Off; break; case Cfg.Value_CullMode_Front: m_keep = CullMode.Front; break; case Cfg.Value_CullMode_Back: m_keep = CullMode.Back; break; default: m_keep = CullMode.Back; break; } m_prop.floatValue = ( float )( CullMode )m_keep; } } } break; } } else if (m_args.HasField(Cfg.Command_Value_Invert)) { String val; if (ShaderGUIHelper.ParseValue(this, m_args, Cfg.Command_Value_Invert, out val) && !String.IsNullOrEmpty(val) && val != m_propName) { var invertProp = FindCachedProperty(val, props, false); if (invertProp != null && invertProp.type == MaterialProperty.PropType.Float) { m_invertProp = invertProp; } } } } if (m_prop == null) { var mat = this.materialEditor.target as Material; if (mat != null && mat.shader != null) { Debug.LogErrorFormat("Find Shader Property failed: {0}.{1}. \n{2}", mat.shader.name, m_propName, AssetDatabase.GetAssetPath(mat)); } } return(m_prop != null); }