Initial upload

This commit is contained in:
2025-02-01 23:08:14 +09:00
commit 4723c9833a
31 changed files with 660 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace Misaki.ShaderGUI
{
public class PopupUIDrawer : MaterialPropertyDrawer
{
private static readonly GUIContent[] _options = { new("Disabled"), new("Enabled") };
protected virtual void SetKeyword(MaterialProperty prop, bool on)
{
}
static bool IsPropertyTypeSuitable(MaterialProperty prop)
{
#if UNITY_6000_1_OR_NEWER
return prop.propertyType == ShaderPropertyType.Float || prop.propertyType == ShaderPropertyType.Range || prop.propertyType == ShaderPropertyType.Int;
#else
return prop.type == MaterialProperty.PropType.Float || prop.type == MaterialProperty.PropType.Range || prop.type == MaterialProperty.PropType.Float;
#endif
}
public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
if (!IsPropertyTypeSuitable(prop))
{
var c = EditorGUIUtility.TrTempContent("Toggle used on a non-float property: " + prop.name);
EditorGUI.LabelField(position, c, EditorStyles.helpBox);
return;
}
MaterialEditor.BeginProperty(position, prop);
#if UNITY_6000_1_OR_NEWER
if (prop.propertyType != ShaderPropertyType.Int)
#else
if (prop.type != MaterialProperty.PropType.Int)
#endif
{
EditorGUI.BeginChangeCheck();
var value = (int)Math.Abs(prop.floatValue);
EditorGUI.showMixedValue = prop.hasMixedValue;
value = EditorGUI.Popup(position, label, value, _options);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
{
prop.floatValue = value;
SetKeyword(prop, value != 0);
}
}
else
{
EditorGUI.BeginChangeCheck();
var value = Math.Abs(prop.intValue);
EditorGUI.showMixedValue = prop.hasMixedValue;
value = EditorGUI.Popup(position, label, value, _options);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
{
prop.intValue = value;
SetKeyword(prop, value != 0);
}
}
MaterialEditor.EndProperty();
}
}
public class PopupDrawer : PopupUIDrawer
{
private const string Keyword_Suffix = "_ON";
protected override void SetKeyword(MaterialProperty prop, bool on)
{
var keywordName = prop.name.ToUpper() + Keyword_Suffix;
foreach (var target in prop.targets)
{
if (target is not Material material)
{
continue;
}
material.SetKeyword(new LocalKeyword(material.shader, keywordName), on);
}
}
}
}