Skip to content
Merged
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
77 changes: 77 additions & 0 deletions Editor/Helper/LwguiSettingsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Jason Ma
// LWGUI - Project Settings Provider
// Provides a toggle in Project Settings > LWGUI to enable/disable ASE integration.

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace LWGUI
{
public static class LwguiSettingsProvider
{
private const string Symbol = "LWGUI_ASE_INTEGRATION";
private const string SettingsPath = "Project/LWGUI";

[SettingsProvider]
static SettingsProvider CreateLwguiSettingsProvider()
{
var provider = new SettingsProvider( SettingsPath, SettingsScope.Project )
{
label = "LWGUI",
keywords = new HashSet<string> { "LWGUI", "ASE", "Amplify", "Integration" },
guiHandler = DrawGUI
};
return provider;
}

private static void DrawGUI( string searchContext )
{
var target = EditorUserBuildSettings.selectedBuildTargetGroup;
PlayerSettings.GetScriptingDefineSymbolsForGroup( target, out string[] defines );
var definesList = defines.ToList();

bool isEnabled = definesList.Contains( Symbol );

EditorGUILayout.Space();
EditorGUILayout.LabelField( "Third-Party Integration", EditorStyles.boldLabel );

if ( isEnabled )
{
EditorGUILayout.HelpBox(
"ASE Integration is enabled. LWGUI Drawer/Decorator attributes " +
"are available in Amplify Shader Editor property nodes.\n" +
"Requires Amplify Shader Editor v1.9.9.10+.",
MessageType.Info );
}
else
{
EditorGUILayout.HelpBox(
"ASE Integration is disabled. Enable it to add LWGUI Drawer/Decorator " +
"selection to Amplify Shader Editor property nodes.\n" +
"Requires Amplify Shader Editor v1.9.9.10+.",
MessageType.None );
}

EditorGUI.BeginChangeCheck();
bool newValue = EditorGUILayout.Toggle(
new GUIContent( "ASE Integration",
"Enable Amplify Shader Editor integration.\n" +
"Adds LWGUI Drawer/Decorator selection to ASE property nodes.\n" +
"Requires Amplify Shader Editor v1.9.9.10+ to be installed.\n" +
"Triggers a domain recompile when changed." ),
isEnabled );

if ( EditorGUI.EndChangeCheck() && newValue != isEnabled )
{
if ( newValue )
definesList.Add( Symbol );
else
definesList.Remove( Symbol );

PlayerSettings.SetScriptingDefineSymbolsForGroup( target, definesList.ToArray() );
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/Helper/LwguiSettingsProvider.cs.meta

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

21 changes: 5 additions & 16 deletions Editor/ScriptableObject/LwguiRampAtlas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public partial class LwguiRampAtlas : ScriptableObject
private string _rampAtlasSOPath = string.Empty;
private string _rampAtlasTexturePath = string.Empty;

[SerializeField] private bool _saveTextureToggle;
[SerializeField] private List<Ramp> _ramps = new List<Ramp>();

[NonSerialized] public Texture2D rampAtlasTexture = null;
Expand Down Expand Up @@ -346,30 +345,20 @@ private void OnEnable()
LoadTexture();
}

private void UserSaveTexture()
/// <summary>
/// Explicitly save texture with P4 checkout. Only call from user-initiated actions (e.g. Save button).
/// </summary>
public void SaveTextureWithCheckout()
{
// It is only called when the user manually saves it, avoiding some strange bugs
if (!_saveTextureToggle)
return;

InitData();

if (!LoadTexture())
return;

UpdateTexturePixels();
SaveTexture(checkoutAndForceWrite:_saveTextureToggle);
_saveTextureToggle = false;
SaveTexture(checkoutAndForceWrite: true);
}

private void OnValidate()
{
// Avoid calling when opening a material, otherwise it will cause P4 communication problems
EditorApplication.delayCall -= UserSaveTexture;
EditorApplication.delayCall += UserSaveTexture;
}


#region Static

public static Texture LoadRampAtlasTexture(LwguiRampAtlas rampAtlasSO)
Expand Down
55 changes: 55 additions & 0 deletions Editor/ScriptableObject/LwguiRampAtlasEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Jason Ma

using UnityEditor;
using UnityEngine;

namespace LWGUI
{
[CustomEditor(typeof(LwguiRampAtlas), true)]
public class LwguiRampAtlasEditor : Editor
{
public override void OnInspectorGUI()
{
ForceExpandAllListElements();

DrawDefaultInspector();

EditorGUILayout.Space();

var rampAtlas = (LwguiRampAtlas)target;

if (rampAtlas.TotalRowCount > rampAtlas.rampAtlasHeight)
{
EditorGUILayout.HelpBox(
$"Atlas height ({rampAtlas.rampAtlasHeight}) is less than total row count ({rampAtlas.TotalRowCount}). " +
"Please increase the height before saving.",
MessageType.Warning);
}

using (new EditorGUI.DisabledScope(!AssetDatabase.Contains(rampAtlas)))
{
if (GUILayout.Button("Save Ramp Atlas Texture", GUILayout.Height(30)))
{
rampAtlas.SaveTextureWithCheckout();
}
}
}

private void ForceExpandAllListElements()
{
var iter = serializedObject.GetIterator();
bool enterChildren = true;
while (iter.NextVisible(enterChildren))
{
enterChildren = true;
if (!iter.isArray || iter.propertyType != SerializedPropertyType.Generic || !iter.isExpanded)
continue;

for (int i = 0; i < iter.arraySize; i++)
iter.GetArrayElementAtIndex(i).isExpanded = true;

enterChildren = false;
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/ScriptableObject/LwguiRampAtlasEditor.cs.meta

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

4 changes: 3 additions & 1 deletion Editor/ShaderDrawers/BasicDrawers/SubDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public SubDrawer(string group)
protected virtual float GetVisibleHeight(MaterialProperty prop)
{
var height = MaterialEditor.GetDefaultPropertyHeight(prop);
return prop.GetPropertyType() == ShaderPropertyType.Vector ? EditorGUIUtility.singleLineHeight : height;
if (prop.GetPropertyType() == ShaderPropertyType.Vector)
return string.IsNullOrEmpty(prop.displayName) ? EditorGUIUtility.singleLineHeight : EditorGUIUtility.singleLineHeight * 2;
return height;
}

public virtual void BuildStaticMetaData(Shader inShader, MaterialProperty inProp, MaterialProperty[] inProps, PropertyStaticData inoutPropertyStaticData)
Expand Down
15 changes: 10 additions & 5 deletions Editor/ShaderDrawers/ExtraDrawers/Numeric/BitMaskDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,25 @@ public BitMaskDrawer(string group, List<string> bitDescriptions)
public override bool IsMatchPropType(ShaderPropertyType propType)
=> propType is ShaderPropertyType.Float or ShaderPropertyType.Int;

protected override float GetVisibleHeight(MaterialProperty prop) { return maxHeight; }
protected override float GetVisibleHeight(MaterialProperty prop)
{
return string.IsNullOrEmpty(prop.displayName) ? EditorGUIUtility.singleLineHeight : maxHeight;
}

public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
label.tooltip += $"\nCurrent Value: { prop.GetNumericValue() }";
bool hasLabel = !string.IsNullOrEmpty(label.text);
if (hasLabel)
label.tooltip += $"\nCurrent Value: { prop.GetNumericValue() }";

int controlId = GUIUtility.GetControlID(_hint, FocusType.Keyboard, position);
var fieldRect = EditorGUI.PrefixLabel(position, controlId, label);

var needSecondRow = totalButtonWidth > fieldRect.width;
var scale = needSecondRow ? position.width / totalButtonWidth : fieldRect.width / totalButtonWidth;
var needSecondRow = hasLabel && totalButtonWidth > fieldRect.width;
var scale = needSecondRow ? (position.width - ReflectionHelper.EditorGUI_indent) / totalButtonWidth : fieldRect.width / totalButtonWidth;

if (needSecondRow)
fieldRect = new Rect(position.x + ReflectionHelper.EditorGUI_indent, position.y + EditorGUIUtility.singleLineHeight, position.width, maxHeight - EditorGUIUtility.singleLineHeight);
fieldRect = new Rect(position.x + ReflectionHelper.EditorGUI_indent, position.y + EditorGUIUtility.singleLineHeight, position.width - ReflectionHelper.EditorGUI_indent, maxHeight - EditorGUIUtility.singleLineHeight);

fieldRect.xMin = fieldRect.xMax;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace LWGUI
/// Target Property Type: Float
/// </summary>
[LwguiDrawerCategory("Ramp")]
[LwguiDrawerParameterString("group", "")]
[LwguiDrawerParameterString("rampAtlasPropName", "")]
[LwguiDrawerParameterString("defaultRampName", "Ramp")]
[LwguiDrawerParameterEnum("colorSpace", 0, "sRGB", "Linear")]
[LwguiDrawerParameterEnum("viewChannelMask", 0, "RGBA", "RGB", "R", "G", "B", "A")]
[LwguiDrawerParameterEnum("timeRange", 0, "1", "24", "2400")]
public class RampAtlasIndexerDrawer : RampDrawer
{
public string rampAtlasPropName = string.Empty;
Expand Down
2 changes: 2 additions & 0 deletions Editor/ShaderDrawers/ExtraDrawers/Texture/TexDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public override void BuildStaticMetaData(Shader inShader, MaterialProperty inPro
{
base.BuildStaticMetaData(inShader, inProp, inProps, inoutPropertyStaticData);
inoutPropertyStaticData.AddExtraProperty(_extraPropName);
inoutPropertyStaticData.labelWidth = Mathf.Max(inoutPropertyStaticData.labelWidth,
EditorStyles.label.CalcSize(new GUIContent(inoutPropertyStaticData.displayName)).x + 32f);
}

public override void GetDefaultValueDescription(Shader inShader, MaterialProperty inProp, MaterialProperty inDefaultProp, PerShaderData inPerShaderData, PerMaterialData inoutPerMaterialData)
Expand Down
2 changes: 2 additions & 0 deletions Editor/ShaderDrawers/ExtraDrawers/Vector/ChannelDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public ChannelDrawer(string group)

public override bool IsMatchPropType(ShaderPropertyType propType) { return propType == ShaderPropertyType.Vector; }

protected override float GetVisibleHeight(MaterialProperty prop) => EditorGUIUtility.singleLineHeight;

private static int GetChannelIndex(MaterialProperty prop)
{
int index = -1;
Expand Down
45 changes: 40 additions & 5 deletions Editor/ShaderDrawers/ExtraDrawers/Vector/ColorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ namespace LWGUI
{
/// <summary>
/// Display up to 4 colors in a single line
///
///
/// group: parent group name (Default: none)
/// color2-4: extra color property name
/// Target Property Type: Color
/// </summary>
[LwguiDrawerCategory("Vector")]
public class ColorDrawer : SubDrawer
{
private string[] _colorStrings = new string[3];
private string[] _colorStrings = new string[3];
private static readonly float _minColorWidth = 50f;

public ColorDrawer(string group, string color2) : this(group, color2, String.Empty, String.Empty) { }

Expand All @@ -43,6 +44,30 @@ public override void BuildStaticMetaData(Shader inShader, MaterialProperty inPro
}
}

private int GetColorCount(MaterialProperty prop, LWGUIMetaDatas inMetaDatas)
{
if (inMetaDatas == null)
return 1;

int count = 1;
foreach (var colorPropName in _colorStrings)
{
if (string.IsNullOrEmpty(colorPropName))
continue;
var p = inMetaDatas.GetProperty(colorPropName);
if (p != null && IsMatchPropType(p.GetPropertyType()))
count++;
}
return count;
}

public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
{
var lwgui = editor != null ? Helper.GetLWGUI(editor) : null;
var count = GetColorCount(prop, lwgui?.metaDatas);
return count >= 3 ? EditorGUIUtility.singleLineHeight * 2 : EditorGUIUtility.singleLineHeight;
}

public override void DrawProp(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
{
var cProps = new Stack<MaterialProperty>();
Expand All @@ -64,10 +89,20 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l

int controlId = GUIUtility.GetControlID(FocusType.Keyboard, position);
var fieldRect = EditorGUI.PrefixLabel(position, controlId, label);

var spacing = 2f;
// Similar to BitMaskDrawer: when each color is too narrow (< _minColorWidth), move all of them to the second row.
var needSecondRow = count >= 3 && (fieldRect.width - spacing * (count - 1)) / count < _minColorWidth;
if (needSecondRow)
fieldRect = new Rect(position.x + ReflectionHelper.EditorGUI_indent,
position.y + EditorGUIUtility.singleLineHeight,
position.width - ReflectionHelper.EditorGUI_indent,
EditorGUIUtility.singleLineHeight);
else
fieldRect.height = EditorGUIUtility.singleLineHeight;

var colorWidth = (fieldRect.width - spacing * (count - 1)) / count;

var indentLevel = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;

Expand All @@ -88,7 +123,7 @@ public override void DrawProp(Rect position, MaterialProperty prop, GUIContent l
cProp.colorValue = dst;
}
}

EditorGUI.indentLevel = indentLevel;
EditorGUI.showMixedValue = false;
}
Expand Down
8 changes: 8 additions & 0 deletions Editor/ThirdParty/AmplifyShaderEditorBridge.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "LWGUI.AmplifyShaderEditorBridge",
"rootNamespace": "LWGUI",
"references": [
"AmplifyShaderEditor",
"LWGUI"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"LWGUI_ASE_INTEGRATION"
],
"versionDefines": [],
"noEngineReferences": false
}

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

Loading
Loading