Skip to content

Commit

Permalink
Merge pull request #56 from JasonMa0012/2.x
Browse files Browse the repository at this point in the history
Separate LwguiGradient Alpha channel preview
  • Loading branch information
JasonMa0012 authored Aug 7, 2024
2 parents cd7dcf0 + 422ad9e commit 22b2166
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Runtime/LwguiGradient/LwguiGradient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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.18.2",
"version": "1.18.3",
"displayName": "LWGUI",
"description": "A Lightweight, Flexible, Powerful Shader GUI System for Unity.",
"keywords": [
Expand Down

0 comments on commit 22b2166

Please sign in to comment.