Skip to content

Commit

Permalink
Add Keywords and RenderQueue in preset
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMa0012 committed Feb 1, 2024
1 parent 61e8b4c commit 5e37b80
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
20 changes: 14 additions & 6 deletions Editor/ShaderDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override float GetPropertyHeight(MaterialProperty prop, string label, Mat
return _height;
}

// Call when creating new material
// Call when creating new material, used to set keywords
public override void Apply(MaterialProperty prop)
{
base.Apply(prop);
Expand Down Expand Up @@ -1097,8 +1097,8 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
var rect = position;

int index = (int)Mathf.Max(0, prop.floatValue);
var preset = PresetHelper.GetPresetFile(presetFileName);
if (preset == null || preset.presets.Count == 0)
var presetFile = PresetHelper.GetPresetFile(presetFileName);
if (presetFile == null || presetFile.presets.Count == 0)
{
var c = GUI.color;
GUI.color = Color.red;
Expand All @@ -1107,17 +1107,25 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
GUI.color = c;
return;
}
var presetNames = preset.presets.Select(((inPreset) => new GUIContent(inPreset.presetName))).ToArray();

var presetNames = presetFile.presets.Select(((inPreset) => new GUIContent(inPreset.presetName))).ToArray();
Helper.AdaptiveFieldWidth(EditorStyles.popup, presetNames[index]);
int newIndex = EditorGUI.Popup(rect, label, index, presetNames);
if (Helper.EndChangeCheck(lwgui, prop))
{
prop.floatValue = newIndex;
preset.ApplyToMaterials(prop.targets, (int)prop.floatValue, lwgui.perFrameData);
presetFile.presets[newIndex].ApplyToEditingMaterial(prop.targets, lwgui.perFrameData);
}
EditorGUI.showMixedValue = false;
}

public override void Apply(MaterialProperty prop)
{
base.Apply(prop);
var presetFile = PresetHelper.GetPresetFile(presetFileName);
if (presetFile != null && prop.floatValue < presetFile.presets.Count)
presetFile.presets[(int)prop.floatValue].ApplyKeywordsToMaterials(prop.targets);
}
}

/// <summary>
Expand Down
83 changes: 45 additions & 38 deletions Editor/ShaderPropertyPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;

namespace LWGUI
{
Expand Down Expand Up @@ -41,6 +42,7 @@ public void Apply(Material material, bool isDefaultMaterial, PerFrameData perFra
propertyNameID = Shader.PropertyToID(propertyName);
if (!material.HasProperty(propertyNameID))
{
// Legacy
var propertyNameLower = propertyName.ToLower();
switch (propertyNameLower)
{
Expand All @@ -53,6 +55,9 @@ public void Apply(Material material, bool isDefaultMaterial, PerFrameData perFra
}
}


// Must be modified MaterialProperty directly for the material editing in ShaderGUI.
// For the Material in background, just use Material.SetXXX().
var isPropertyOtherMaterials = !isDefaultMaterial && perFrameData == null;
if (isPropertyOtherMaterials || isDefaultMaterial)
{
Expand Down Expand Up @@ -143,12 +148,50 @@ public void OnValidate()
public class Preset
{
public string presetName;
public List<PropertyValue> propertyValues = new List<PropertyValue>();
public List<PropertyValue> propertyValues = new List<PropertyValue>();
public List<string> enabledKeywords = new List<string>();
public List<string> disabledKeywords = new List<string>();
public int renderQueue = -1;


public void ApplyToDefaultMaterial(Material material)
{
foreach (var propertyValue in propertyValues)
propertyValue.Apply(material, true);
foreach (var enabledKeyword in enabledKeywords)
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);
if (renderQueue >= 0)
material.renderQueue = renderQueue;
}

public void ApplyToEditingMaterial(UnityEngine.Object[] materials, PerFrameData perFrameData)
{
for (int i = 0; i < materials.Length; i++)
{
var material = materials[i] as Material;
foreach (var propertyValue in propertyValues)
propertyValue.Apply(material, false, i == 0 ? perFrameData : null);
foreach (var enabledKeyword in enabledKeywords)
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);
if (renderQueue >= 0)
material.renderQueue = renderQueue;
}
}

public void ApplyKeywordsToMaterials(UnityEngine.Object[] materials)
{
for (int i = 0; i < materials.Length; i++)
{
var material = materials[i] as Material;
foreach (var enabledKeyword in enabledKeywords)
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);
}
}

public PropertyValue GetPropertyValue(string propName)
Expand Down Expand Up @@ -203,55 +246,19 @@ public void RemoveIncludeExtraProperties(LWGUI lwgui, string propName)
}
}


public List<Preset> presets;

// private void Awake()
// {
// Debug.Log($"{this.name} Awake");
// }
//
// private void OnDestroy()
// {
// Debug.Log($"{this.name} OnDestroy");
// }
//
// private void OnDisable()
// {
// Debug.Log($"{this.name} OnDisable");
// }
//
// private void Reset()
// {
// Debug.Log($"{this.name} Reset");
// }

private void OnValidate()
{
// Debug.Log($"{this.name} OnValidate");
PresetHelper.ForceInit();
}

private void OnEnable()
{
// Debug.Log($"{this.name} OnEnable");
// manually added when a preset is manually created
if (PresetHelper.IsInitComplete)
PresetHelper.AddPreset(this);
}

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)
{
for (int i = 0; i < materials.Length; i++)
{
var material = materials[i] as Material;
propertyValue.Apply(material, false, i == 0 ? perFrameData : null);
}
}
}
}
}
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.15.0",
"version": "1.16.0",
"displayName": "LWGUI",
"description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.",
"keywords": [
Expand Down

0 comments on commit 5e37b80

Please sign in to comment.