Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Editor/LWGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
if (propStaticData.parent.parent != null)
EditorGUI.indentLevel++;
}



// Advanced Header
if (propStaticData.isAdvancedHeader && !propStaticData.isAdvancedHeaderProperty)
Expand All @@ -103,9 +105,17 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
continue;
}
}


if (!MetaDataHelper.GetPropertyAcitveStatus(prop, material, this))
{
GUI.enabled = false;
}
DrawProperty(prop);

if (!MetaDataHelper.GetPropertyAcitveStatus(prop, material, this))
{
GUI.enabled = true;
}

RevertableHelper.SetRevertableGUIWidths();
EditorGUI.indentLevel = indentLevel;
}
Expand Down
66 changes: 64 additions & 2 deletions Editor/MetaDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public class ShowIfData
public CompareFunction compareFunction = CompareFunction.Equal;
public float value = 0;
}

public class ActiveIfData
{
public LogicalOperator logicalOperator = LogicalOperator.And;
public string targetPropertyName = string.Empty;
public CompareFunction compareFunction = CompareFunction.Equal;
public float value = 0;
}


/// <summary>
/// All static metadata for a Property, determined after the Shader is compiled.
Expand All @@ -67,7 +76,8 @@ public class PropertyStaticData
public bool isHidden = false; // [Hidden]
public bool isReadOnly = false; // [ReadOnly]
public List<ShowIfData> showIfDatas = new List<ShowIfData>(); // [ShowIf()]

public List<ActiveIfData> activeIfDatas = new List<ActiveIfData>(); // [ActiveIf()]

// Metadata
public List<string> extraPropNames = new List<string>(); // Other Props that have been associated
public string helpboxMessages = string.Empty;
Expand Down Expand Up @@ -346,7 +356,7 @@ public class PropertyDynamicData
public bool hasChildrenModified = false; // Are Children properties modified in the material?
public bool hasRevertChanged = false; // Used to call property EndChangeCheck()
public bool isShowing = true; // ShowIf() result

public bool isActive = true; // ActiveIf() result
}

public class PersetDynamicData
Expand Down Expand Up @@ -505,6 +515,44 @@ public void BuildPerFrameData(Shader shader, Material material, MaterialProperty
break;
}
}

foreach (var activeIfData in propStaticData.activeIfDatas)
{
var propCurrentValue = propertyDatas[activeIfData.targetPropertyName].property.floatValue;
bool compareResult;

switch (activeIfData.compareFunction)
{
case CompareFunction.Less:
compareResult = propCurrentValue < activeIfData.value;
break;
case CompareFunction.LessEqual:
compareResult = propCurrentValue <= activeIfData.value;
break;
case CompareFunction.Greater:
compareResult = propCurrentValue > activeIfData.value;
break;
case CompareFunction.NotEqual:
compareResult = propCurrentValue != activeIfData.value;
break;
case CompareFunction.GreaterEqual:
compareResult = propCurrentValue >= activeIfData.value;
break;
default:
compareResult = propCurrentValue == activeIfData.value;
break;
}

switch (activeIfData.logicalOperator)
{
case LogicalOperator.And:
propDynamicData.isActive &= compareResult;
break;
case LogicalOperator.Or:
propDynamicData.isActive |= compareResult;
break;
}
}
}
}

Expand Down Expand Up @@ -591,6 +639,20 @@ public static string GetPropertyDisplayName(Shader shader, MaterialProperty prop
else
return prop.displayName.Substring(0, minIndex);
}

public static bool GetPropertyAcitveStatus(MaterialProperty prop, Material material, LWGUI lwgui)
{
bool result = true;
var propertyStaticData = lwgui.perShaderData.propertyDatas[prop.name];
var propertyDynamicData = lwgui.perFrameData.propertyDatas[prop.name];
var displayModeData = lwgui.perShaderData.displayModeData;
if (!propertyDynamicData.isActive)
{
result = false;
}

return result;
}

public static bool GetPropertyVisibility(MaterialProperty prop, Material material, LWGUI lwgui)
{
Expand Down
44 changes: 44 additions & 0 deletions Editor/ShaderDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,4 +1418,48 @@ public override void BuildStaticMetaData(Shader inShader, MaterialProperty inPro

public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { }
}

public class ActiveIfDecorator : SubDrawer
{
private ActiveIfData _activeIfData = new ActiveIfData();
private readonly Dictionary<string, string> _compareFunctionLUT = new Dictionary<string, string>()
{
{ "Less", "Less" },
{ "L", "Less" },
{ "Equal", "Equal" },
{ "E", "Equal" },
{ "LessEqual", "LessEqual" },
{ "LEqual", "LessEqual" },
{ "LE", "LessEqual" },
{ "Greater", "Greater" },
{ "G", "Greater" },
{ "NotEqual", "NotEqual" },
{ "NEqual", "NotEqual" },
{ "NE", "NotEqual" },
{ "GreaterEqual", "GreaterEqual" },
{ "GEqual", "GreaterEqual" },
{ "GE", "GreaterEqual" },
};

public ActiveIfDecorator(string propName, string comparisonMethod, float value) : this("And", propName, comparisonMethod, value) { }
public ActiveIfDecorator(string logicalOperator, string propName, string compareFunction, float value)
{
_activeIfData.logicalOperator = logicalOperator.ToLower() == "or" ? LogicalOperator.Or : LogicalOperator.And;
_activeIfData.targetPropertyName = propName;
if (!_compareFunctionLUT.ContainsKey(compareFunction) || !Enum.IsDefined(typeof(CompareFunction), _compareFunctionLUT[compareFunction]))
Debug.LogError("Invalid compareFunction: '" + compareFunction + "', Must be one of the following: Less (L) | Equal (E) | LessEqual (LEqual / LE) | Greater (G) | NotEqual (NEqual / NE) | GreaterEqual (GEqual / GE).");
else
_activeIfData.compareFunction = (CompareFunction)Enum.Parse(typeof(CompareFunction), _compareFunctionLUT[compareFunction]);
_activeIfData.value = value;
}

protected override float GetVisibleHeight(MaterialProperty prop) { return 0; }

public override void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData)
{
inoutPropertyStaticData.activeIfDatas.Add(_activeIfData);
}

public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor) { }
}
} //namespace LWGUI