Skip to content

Commit

Permalink
fix: Main() Keywords when switching preset
Browse files Browse the repository at this point in the history
change: Preset() can no longer add itself to the preset
  • Loading branch information
JasonMa0012 committed Oct 22, 2023
1 parent c746928 commit 1bdbb23
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 45 deletions.
3 changes: 3 additions & 0 deletions Editor/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ public static bool DrawFoldout(Rect rect, ref bool isFolding, bool toggleValue,
if (GUI.Button(rect, label, _guiStyle_Foldout))
{
isFolding = !isFolding;
GUI.changed = false;
}
GUI.backgroundColor = guiColor;
GUI.enabled = enabled;
Expand Down Expand Up @@ -858,6 +859,8 @@ public static void DoPropertyContextMenus(Rect rect, MaterialProperty prop, LWGU
menus.AddSeparator("");
foreach (var activePresetData in lwgui.perFrameData.activePresets)
{
if (activePresetData.property == prop) continue;

var activePreset = activePresetData.preset;
var presetAsset = lwgui.perShaderData.propertyDatas[activePresetData.property.name].propertyPresetAsset;
var presetPropDisplayName = lwgui.perShaderData.propertyDatas[activePresetData.property.name].displayName;
Expand Down
2 changes: 1 addition & 1 deletion Editor/MetaDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public void BuildPerFrameData(Shader shader, Material material, MaterialProperty
new Material(shader);

foreach (var activePreset in activePresets)
activePreset.preset.Apply(defaultMaterial);
activePreset.preset.ApplyToDefaultMaterial(defaultMaterial);

var defaultProperties = MaterialEditor.GetMaterialProperties(new[] { defaultMaterial });
Debug.Assert(defaultProperties.Length == props.Length);
Expand Down
2 changes: 1 addition & 1 deletion Editor/ShaderDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
if (Helper.EndChangeCheck(lwgui, prop))
{
prop.floatValue = newIndex;
preset.Apply(prop.targets, (int)prop.floatValue);
preset.ApplyToMaterials(prop.targets, (int)prop.floatValue, lwgui.perFrameData);
}
EditorGUI.showMixedValue = false;
}
Expand Down
73 changes: 53 additions & 20 deletions Editor/ShaderPropertyPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PropertyValue(MaterialProperty prop)

private int propertyNameID = -1;

public void Apply(Material material)
public void Apply(Material material, bool isDefaultMaterial, PerFrameData perFrameData = null)
{
if (propertyNameID == -1 || !material.HasProperty(propertyNameID))
propertyNameID = Shader.PropertyToID(propertyName);
Expand All @@ -52,21 +52,53 @@ public void Apply(Material material)
return;
}
}
switch (propertyType)

var isPropertyOtherMaterials = !isDefaultMaterial && perFrameData == null;
if (isPropertyOtherMaterials || isDefaultMaterial)
{
case PropertyType.Color:
material.SetColor(propertyNameID, colorValue);
break;
case PropertyType.Vector:
material.SetVector(propertyNameID, vectorValue);
break;
case PropertyType.Float:
case PropertyType.Range:
material.SetFloat(propertyNameID, floatValue);
break;
case PropertyType.Texture:
material.SetTexture(propertyNameID, textureValue);
break;
switch (propertyType)
{
case PropertyType.Color:
material.SetColor(propertyNameID, colorValue);
break;
case PropertyType.Vector:
material.SetVector(propertyNameID, vectorValue);
break;
case PropertyType.Float:
case PropertyType.Range:
material.SetFloat(propertyNameID, floatValue);
break;
case PropertyType.Texture:
material.SetTexture(propertyNameID, textureValue);
break;
}

if (isPropertyOtherMaterials)
MaterialEditor.ApplyMaterialPropertyDrawers(material);
}
else
// is Property Primary Material
{
var propDynamicData = perFrameData.propertyDatas[propertyName];
var prop = propDynamicData.property;
switch (propertyType)
{
case PropertyType.Color:
prop.colorValue = colorValue;
break;
case PropertyType.Vector:
prop.vectorValue = vectorValue;
break;
case PropertyType.Float:
case PropertyType.Range:
prop.floatValue = floatValue;
break;
case PropertyType.Texture:
prop.textureValue = textureValue;
break;
}

propDynamicData.revertChanged = true;
}
}

Expand Down Expand Up @@ -113,10 +145,10 @@ public class Preset
public string presetName;
public List<PropertyValue> propertyValues = new List<PropertyValue>();

public void Apply(Material material)
public void ApplyToDefaultMaterial(Material material)
{
foreach (var propertyValue in propertyValues)
propertyValue.Apply(material);
propertyValue.Apply(material, true);
}

public PropertyValue GetPropertyValue(string propName)
Expand Down Expand Up @@ -207,16 +239,17 @@ private void OnEnable()
PresetHelper.AddPreset(this);
}

public void Apply(UnityEngine.Object[] materials, int presetIndex)
public void ApplyToMaterials(UnityEngine.Object[] materials, int presetIndex, PerFrameData perFrameData)
{
if (this.presets.Count == 0) return;

int index = (int)Mathf.Min(this.presets.Count - 1, Mathf.Max(0, presetIndex));
foreach (var propertyValue in this.presets[index].propertyValues)
{
foreach (Material material in materials)
for (int i = 0; i < materials.Length; i++)
{
propertyValue.Apply(material);
var material = materials[i] as Material;
propertyValue.Apply(material, false, i == 0 ? perFrameData : null);
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions Editor/Test/LWGUI_ShaderPropertyPreset2.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 28fbcbca3fb14507af6ed5c104c40b84, type: 3}
m_Name: LWGUI_ShaderPropertyPreset1
m_EditorClassIdentifier:
presets:
- presetName: Preset X
propertyValues: []
- presetName: Preset Y
propertyValues:
- propertyName: _vector1
propertyType: 1
floatValue: 0
colorValue: {r: 0, g: 0, b: 0, a: 0}
vectorValue: {x: 1.32, y: 1, z: 1, w: 1}
textureValue: {fileID: 0}
3 changes: 3 additions & 0 deletions Editor/Test/LWGUI_ShaderPropertyPreset2.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 20 additions & 22 deletions Editor/Test/SampleKeyword.shader
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ Shader "Hidden"
{
Properties
{
[Enum(Name1, 2, Name2, 1)]
_enum ("Enum", float) = 1
[Preset(_, LWGUI_ShaderPropertyPreset2)] _preset2 ("Preset", float) = 0

[KeywordEnum(key1, key2)]
_keywordEnum ("KeywordEnum", float) = 0
Expand All @@ -12,16 +11,14 @@ Shader "Hidden"
_kwenum ("KWEnum", float) = 0

[Toggle(_TOGGLE_KEYWORD)] _toggle1 ("Toggle", float) = 0


[Main(g0, _GROUP_TOGGLE_KEYWORD)] _group_toggle1 ("Group Toggle", float) = 0

[Main(g1, _, on, on)]
[PassSwitch(Always)]
_group ("Pass Switch Group", float) = 1
[SubEnum(g1, Off, 0, On, 1)] _ZWrite ("ZWrite Mode", Float) = 1
[SubToggle(g1, _SUBTOGGLE_KEYWORD)] _toggle ("Sub Toggle", float) = 0

[SubEnum(g1, Name1, 1, Name2, 0.5)]
_subEnum ("SubEnum", float) = 0.5

[SubKeywordEnum(g1, key1, key2)]
_subKeywordEnum ("SubKeywordEnum", float) = 0
}
Expand All @@ -42,6 +39,7 @@ Shader "Hidden"
#pragma multi_compile _SUBKEYWORDENUM_KEY1 _SUBKEYWORDENUM_KEY2
#pragma multi_compile _ _SUBTOGGLE_KEYWORD
#pragma multi_compile _ _TOGGLE_KEYWORD
#pragma multi_compile _ _GROUP_TOGGLE_KEYWORD

#include "UnityCG.cginc"

Expand All @@ -60,9 +58,7 @@ Shader "Hidden"

sampler2D _MainTex;
float4 _MainTex_ST;

float _enum;
float _subEnum;


v2f vert(appdata v)
{
Expand All @@ -78,35 +74,37 @@ Shader "Hidden"
fixed4 col = 0;

#if _KWENUM_KEY1
col.x = 0;
col.x += 0;
#elif _KWENUM_KEY2
col.x = 1;
col.x += 0.25;
#endif

#if _KEYWORDENUM_KEY1
col.y = 0;
col.y += 0;
#elif _KEYWORDENUM_KEY2
col.y = 1;
col.y += 0.25;
#endif

#if _SUBKEYWORDENUM_KEY1
col.z = 0;
col.y += 0.0;
#elif _SUBKEYWORDENUM_KEY2
col.z = 0.5;
col.y += 0.25;
#endif

#if _SUBTOGGLE_KEYWORD
col.z = 0.75;
col.z += 0.25;
#endif

#if _TOGGLE_KEYWORD
col.z = 1;
col.z += 0.25;
#endif

col.x *= _enum;
col.y *= _subEnum;

return col;
#if _GROUP_TOGGLE_KEYWORD
col.z += 0.25;
#endif


return frac(col);
}
ENDCG
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.jasonma.lwgui",
"version": "1.12.1",
"version": "1.12.2",
"displayName": "LWGUI",
"description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.",
"keywords": [
Expand Down

0 comments on commit 1bdbb23

Please sign in to comment.