From 422ad9e85b2ea9049c73b803067f8b9a80e25d26 Mon Sep 17 00:00:00 2001 From: Jason Ma <1312119064@qq.com> Date: Wed, 7 Aug 2024 16:14:51 +0800 Subject: [PATCH] Separate LwguiGradient Alpha channel preview --- Runtime/LwguiGradient/LwguiGradient.cs | 2 ++ .../LwguiGradientEditorHelper.cs | 25 ++++++++++++++++++- .../LwguiGradientPresetLibrary.cs | 2 +- package.json | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Runtime/LwguiGradient/LwguiGradient.cs b/Runtime/LwguiGradient/LwguiGradient.cs index db81f04..ac019e0 100644 --- a/Runtime/LwguiGradient/LwguiGradient.cs +++ b/Runtime/LwguiGradient/LwguiGradient.cs @@ -39,6 +39,8 @@ public enum GradientTimeRange TwentyFour = 24, TwentyFourHundred = 2400 } + + public static bool HasChannelMask(ChannelMask channelMaskA, ChannelMask channelMaskB) => ((uint)channelMaskA & (uint)channelMaskB) > 0; public static bool IsChannelIndexInMask(int channelIndex, ChannelMask channelMask) => ((uint)channelMask & (uint)(1 << channelIndex)) > 0; diff --git a/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditorHelper.cs b/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditorHelper.cs index 6253426..fc76b00 100644 --- a/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditorHelper.cs +++ b/UnityEditorExtension/LwguiGradientEditor/LwguiGradientEditorHelper.cs @@ -42,6 +42,29 @@ public static void DrawGradientWithBackground(Rect position, LwguiGradient gradi // } } + public static void DrawGradientWithSeparateAlphaChannel(Rect position, LwguiGradient gradient, ColorSpace colorSpace, LwguiGradient.ChannelMask viewChannelMask) + { + if (!LwguiGradient.HasChannelMask(viewChannelMask, LwguiGradient.ChannelMask.Alpha) || viewChannelMask == LwguiGradient.ChannelMask.Alpha) + { + DrawGradientWithBackground(position, gradient, colorSpace, viewChannelMask); + } + else + { + var r2 = new Rect(position.x + 1, position.y + 1, position.width - 2, position.height - 2); + var rgbRect = new Rect(r2.x, r2.y, r2.width, r2.height * 0.8f); + var alphaRect = new Rect(rgbRect.x, rgbRect.yMax, r2.width, r2.height * 0.2f); + + var rgbTexture = gradient.GetPreviewRampTexture(256, 1, colorSpace, viewChannelMask ^ LwguiGradient.ChannelMask.Alpha); + var alphaTexture = gradient.GetPreviewRampTexture(256, 1, colorSpace, LwguiGradient.ChannelMask.Alpha); + + Color oldColor = GUI.color; + GUI.color = Color.white; //Dont want the Playmode tint to be applied to gradient textures. + GUI.DrawTexture(rgbRect, rgbTexture, ScaleMode.StretchToFill, false); + GUI.DrawTexture(alphaRect, alphaTexture, ScaleMode.StretchToFill, false); + GUI.color = oldColor; + } + } + public static void GradientField(Rect position, GUIContent label, LwguiGradient gradient, ColorSpace colorSpace = ColorSpace.Gamma, LwguiGradient.ChannelMask viewChannelMask = LwguiGradient.ChannelMask.All, @@ -85,7 +108,7 @@ public static void GradientField(Rect position, GUIContent label, LwguiGradient break; case EventType.Repaint: - DrawGradientWithBackground(rect, gradient, colorSpace, viewChannelMask); + DrawGradientWithSeparateAlphaChannel(rect, gradient, colorSpace, viewChannelMask); break; case EventType.ExecuteCommand: // When drawing the modifying Gradient Field and it has changed diff --git a/UnityEditorExtension/LwguiGradientEditor/LwguiGradientPresetLibrary.cs b/UnityEditorExtension/LwguiGradientEditor/LwguiGradientPresetLibrary.cs index 6504e8f..d53cfb2 100644 --- a/UnityEditorExtension/LwguiGradientEditor/LwguiGradientPresetLibrary.cs +++ b/UnityEditorExtension/LwguiGradientEditor/LwguiGradientPresetLibrary.cs @@ -72,7 +72,7 @@ public void Draw(Rect rect, LwguiGradient gradient, ColorSpace colorSpace, Lwgui { if (gradient == null) return; - LwguiGradientEditorHelper.DrawGradientWithBackground(rect, gradient, colorSpace, viewChannelMask); + LwguiGradientEditorHelper.DrawGradientWithSeparateAlphaChannel(rect, gradient, colorSpace, viewChannelMask); } public override string GetName(int index) diff --git a/package.json b/package.json index dc84727..6928163 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.jasonma.lwgui", - "version": "1.18.2", + "version": "1.18.3", "displayName": "LWGUI", "description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.", "keywords": [