Skip to content

Commit

Permalink
Add: Presets now support enable/disable Passes
Browse files Browse the repository at this point in the history
Add: Main() and SubToggle() now support switch preset by value.
Change: Preset() filtered out by ShowIf() will not set default values
  • Loading branch information
JasonMa0012 committed Dec 12, 2024
1 parent 92338d4 commit bcc01b0
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 161 deletions.
9 changes: 7 additions & 2 deletions Editor/Helper/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,16 @@ public static void DoPropertyContextMenus(Rect rect, MaterialProperty prop, LWGU
menus.AddSeparator("");
foreach (var activePresetData in perMaterialData.activePresetDatas)
{
// Cull self
if (activePresetData.property == prop) continue;

var activePreset = activePresetData.preset;
var presetAsset = perShaderData.propStaticDatas[activePresetData.property.name].propertyPresetAsset;
var presetPropDisplayName = perShaderData.propStaticDatas[activePresetData.property.name].displayName;
var (presetPropStaticData, presetPropDynamicData) = metaDatas.GetPropDatas(activePresetData.property);
var presetAsset = presetPropStaticData.propertyPresetAsset;
var presetPropDisplayName = presetPropStaticData.displayName;

// Cull invisible presets
if (!presetPropDynamicData.isShowing) continue;

if (activePreset.GetPropertyValue(prop.name) != null)
{
Expand Down
3 changes: 3 additions & 0 deletions Editor/Helper/PresetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static void AddPreset(ShaderPropertyPreset preset)

public static ShaderPropertyPreset GetPresetFile(string presetFileName)
{
if (string.IsNullOrEmpty(presetFileName))
return null;

if (!_loadedPresets.ContainsKey(presetFileName) || !_loadedPresets[presetFileName])
ForceInit();

Expand Down
23 changes: 9 additions & 14 deletions Editor/MetaData/PerMaterialData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ public void Init(Shader shader, Material material, MaterialProperty[] props, Per
// Get active presets
foreach (var prop in props)
{
var activePreset = perShaderData.propStaticDatas[prop.name].presetDrawer
?.GetActivePreset(prop, perShaderData.propStaticDatas[prop.name].propertyPresetAsset);
if (activePreset != null)
var propStaticData = perShaderData.propStaticDatas[prop.name];
var activePreset = propStaticData.presetDrawer?.GetActivePreset(prop, propStaticData.propertyPresetAsset);
if (activePreset != null
// Filter invisible preset properties
&& (propStaticData.showIfDatas.Count == 0
|| ShowIfDecorator.GetShowIfResultFromMaterial(propStaticData.showIfDatas, this.material)))
{
activePresetDatas.Add(new PersetDynamicData(activePreset, prop));
}
}

{
Expand All @@ -85,16 +90,6 @@ public void Init(Shader shader, Material material, MaterialProperty[] props, Per
var defaultProperties = MaterialEditor.GetMaterialProperties(new[] { defaultMaterial });
Debug.Assert(defaultProperties.Length == props.Length);

// Override default value
for (int i = 0; i < props.Length; i++)
{
Debug.Assert(props[i].name == defaultProperties[i].name);
Debug.Assert(!propDynamicDatas.ContainsKey(props[i].name));

perShaderData.propStaticDatas[props[i].name].baseDrawers
?.ForEach(baseDrawer => baseDrawer.OverrideDefaultValue(shader, props[i], defaultProperties[i], perShaderData));
}

// Init propDynamicDatas
for (int i = 0; i < props.Length; i++)
{
Expand Down Expand Up @@ -132,7 +127,7 @@ public void Init(Shader shader, Material material, MaterialProperty[] props, Per
}
}

// Store Show Modified Props Only Cache
// Store "Show Modified Props Only" Caches
{
if (perShaderData.displayModeData.showOnlyModifiedGroups || perShaderData.displayModeData.showOnlyModifiedProperties)
{
Expand Down
18 changes: 16 additions & 2 deletions Editor/ScriptableObject/ShaderPropertyPreset.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Jason Ma
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -145,6 +146,8 @@ public class Preset
public List<PropertyValue> propertyValues = new List<PropertyValue>();
public List<string> enabledKeywords = new List<string>();
public List<string> disabledKeywords = new List<string>();
public List<string> enabledPasses = new List<string>();
public List<string> disabledPasses = new List<string>();
public int renderQueue = -1;


Expand All @@ -156,11 +159,15 @@ public void ApplyToDefaultMaterial(Material material)
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);

Helper.SetShaderPassEnabled(new Object[] { material }, enabledPasses.Select(s => s.ToUpper()).ToArray(), true);
Helper.SetShaderPassEnabled(new Object[] { material }, disabledPasses.Select(s => s.ToUpper()).ToArray(), false);

if (renderQueue >= 0)
material.renderQueue = renderQueue;
}

public void ApplyToEditingMaterial(UnityEngine.Object[] materials, PerMaterialData perMaterialData)
public void ApplyToEditingMaterial(Object[] materials, PerMaterialData perMaterialData)
{
for (int i = 0; i < materials.Length; i++)
{
Expand All @@ -171,12 +178,16 @@ public void ApplyToEditingMaterial(UnityEngine.Object[] materials, PerMaterialDa
material.EnableKeyword(enabledKeyword);
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);

if (renderQueue >= 0)
material.renderQueue = renderQueue;
}

Helper.SetShaderPassEnabled(materials, enabledPasses.Select(s => s.ToUpper()).ToArray(), true);
Helper.SetShaderPassEnabled(materials, disabledPasses.Select(s => s.ToUpper()).ToArray(), false);
}

public void ApplyKeywordsToMaterials(UnityEngine.Object[] materials)
public void ApplyKeywordsAndPassesToMaterials(Object[] materials)
{
for (int i = 0; i < materials.Length; i++)
{
Expand All @@ -186,6 +197,9 @@ public void ApplyKeywordsToMaterials(UnityEngine.Object[] materials)
foreach (var disabledKeyword in disabledKeywords)
material.DisableKeyword(disabledKeyword);
}

Helper.SetShaderPassEnabled(materials, enabledPasses.Select(s => s.ToUpper()).ToArray(), true);
Helper.SetShaderPassEnabled(materials, disabledPasses.Select(s => s.ToUpper()).ToArray(), false);
}

public PropertyValue GetPropertyValue(string propName)
Expand Down
Loading

0 comments on commit bcc01b0

Please sign in to comment.