Skip to content

Commit

Permalink
feat: add GradationMode.Angle and GradationMode.AngleGradient
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 29, 2024
1 parent 5a3dce2 commit 6b5eec2
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 21 deletions.
12 changes: 10 additions & 2 deletions Packages/src/Editor/UIEffectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class UIEffect2Editor : Editor
private SerializedProperty _gradationGradient;
private SerializedProperty _gradationOffset;
private SerializedProperty _gradationScale;
private SerializedProperty _gradationRotation;

private bool _expandOthers;
private SerializedProperty _allowToModifyMeshShape;
Expand Down Expand Up @@ -128,6 +129,7 @@ private void OnEnable()
_gradationGradient = serializedObject.FindProperty("m_GradationGradient");
_gradationOffset = serializedObject.FindProperty("m_GradationOffset");
_gradationScale = serializedObject.FindProperty("m_GradationScale");
_gradationRotation = serializedObject.FindProperty("m_GradationRotation");

_allowToModifyMeshShape = serializedObject.FindProperty("m_AllowToModifyMeshShape");
}
Expand Down Expand Up @@ -283,10 +285,10 @@ public void DrawProperties()
{
case GradationMode.HorizontalGradient:
case GradationMode.VerticalGradient:
case GradationMode.AngleGradient:
EditorGUILayout.PropertyField(_gradationGradient);
break;
default:
{
EditorGUILayout.PropertyField(_gradationColor1);
var r = EditorGUILayout.GetControlRect();
r.width -= 20;
Expand All @@ -302,11 +304,17 @@ public void DrawProperties()
}

break;
}
}

EditorGUILayout.PropertyField(_gradationOffset);
EditorGUILayout.PropertyField(_gradationScale);

if ((GradationMode)_gradationMode.intValue == GradationMode.Angle
|| (GradationMode)_gradationMode.intValue == GradationMode.AngleGradient)
{
EditorGUILayout.PropertyField(_gradationRotation);
}

EditorGUI.indentLevel--;
}

Expand Down
3 changes: 2 additions & 1 deletion Packages/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,13 @@ The `UIEffect` component applies visual effects to UI elements, allowing various

#### Gradation Mode

- **Gradation Mode**: `None`, `Horizontal Gradient`, `Horizontal`, `Vertical Gradient`, `Vertical`, `Radial Fast`, `Radial Detail`, `Diagonal To Right Bottom`, `Diagonal To Right Top`, `Diagonal To Left Bottom`
- **Gradation Mode**: `None`, `Horizontal Gradient`, `Horizontal`, `Vertical Gradient`, `Vertical`, `Radial Fast`, `Radial Detail`, `Diagonal To Right Bottom`, `Diagonal To Right Top`, `Diagonal To Left Bottom`, `Angle`, `Angle Gradient`
- **Gradation Gradient**: The gradient of the gradation.
- **Gradation Color 1**: The first color of the gradation.
- **Gradation Color 2**: The second color of the gradation.
- **Gradation Offset**: The offset of the gradation range.
- **Gradation Scale**: The scale of the gradation range.
- **Gradation Rotation**: The rotation of the gradation range.

#### Others

Expand Down
4 changes: 3 additions & 1 deletion Packages/src/Runtime/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public enum GradationMode
RadialFast,
RadialDetail,
DiagonalToRightBottom,
DiagonalToLeftBottom
DiagonalToLeftBottom,
Angle,
AngleGradient
}

public enum PreferSamplingSize
Expand Down
28 changes: 27 additions & 1 deletion Packages/src/Runtime/UIEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ public class UIEffect : UIEffectBase
protected GradationMode m_GradationMode = GradationMode.None;

[SerializeField]
[ColorUsage(true, true)]
protected Color m_GradationColor1 = Color.white;

[SerializeField]
[ColorUsage(true, true)]
protected Color m_GradationColor2 = Color.white;

[SerializeField]
Expand All @@ -164,6 +166,10 @@ public class UIEffect : UIEffectBase
[SerializeField]
protected float m_GradationScale = 1;

[SerializeField]
[Range(0, 360)]
private float m_GradationRotation = 0;

[SerializeField]
protected bool m_AllowToModifyMeshShape = true;

Expand Down Expand Up @@ -696,6 +702,18 @@ public float gradationScale
}
}

public float gradationRotation
{
get => m_GradationRotation;
set
{
value = Mathf.Repeat(value, 360);
if (Mathf.Approximately(m_GradationRotation, value)) return;
context.gradationRotation = m_GradationRotation = value;
SetVerticesDirty();
}
}

public bool allowToModifyMeshShape
{
get => m_AllowToModifyMeshShape;
Expand Down Expand Up @@ -819,6 +837,7 @@ protected override void UpdateContext(UIEffectContext c)
c.gradationGradient = m_GradationGradient;
c.gradationOffset = m_GradationOffset;
c.gradationScale = m_GradationScale;
c.gradationRotation = m_GradationRotation;

c.allowToModifyMeshShape = m_AllowToModifyMeshShape;
}
Expand Down Expand Up @@ -855,10 +874,16 @@ public override void SetRate(float rate, UIEffectTweener.CullingMask mask)
transitionRate = rate;
}

if (gradationMode != GradationMode.None && 0 < (mask & UIEffectTweener.CullingMask.Gradiation))
if (gradationMode != GradationMode.None && 0 < (mask & UIEffectTweener.CullingMask.GradiationOffset))
{
gradationOffset = Mathf.Lerp(-1f, 1f, rate);
}

if ((gradationMode == GradationMode.Angle || gradationMode == GradationMode.AngleGradient)
&& 0 < (mask & UIEffectTweener.CullingMask.GradiationRotation))
{
gradationRotation = Mathf.Lerp(0f, 360f, rate);
}
}

public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
Expand Down Expand Up @@ -993,6 +1018,7 @@ internal void CopyFrom(UIEffectContext c)
m_GradationGradient = c.gradationGradient;
m_GradationOffset = c.gradationOffset;
m_GradationScale = c.gradationScale;
m_GradationRotation = c.gradationRotation;

m_AllowToModifyMeshShape = c.allowToModifyMeshShape;

Expand Down
32 changes: 31 additions & 1 deletion Packages/src/Runtime/UIEffectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public class UIEffectContext
public Gradient gradationGradient;
public float gradationOffset;
public float gradationScale;
public float gradationRotation;
private List<float> _keyTimes;
private List<float> _splitTimes;

public bool allowToModifyMeshShape;

Expand Down Expand Up @@ -253,6 +253,7 @@ public void CopyFrom(UIEffectContext preset)
gradationGradient = preset.gradationGradient;
gradationOffset = preset.gradationOffset;
gradationScale = preset.gradationScale;
gradationRotation = preset.gradationRotation;

allowToModifyMeshShape = preset.allowToModifyMeshShape;
}
Expand Down Expand Up @@ -442,6 +443,7 @@ private void ApplyGradation(List<UIVertex> verts, Rect rect, Matrix4x4 m)
var offset = gradationOffset;
var scale = gradationScale;
var grad = gradationGradient;
var rot = gradationRotation;
switch (gradationMode)
{
case GradationMode.Horizontal:
Expand All @@ -462,6 +464,10 @@ private void ApplyGradation(List<UIVertex> verts, Rect rect, Matrix4x4 m)
case GradationMode.RadialDetail:
GradientUtil.DoRadialGradient(verts, a, b, offset, scale, rect, m, 12);
break;
case GradationMode.Angle:
m = Matrix4x4.Rotate(Quaternion.Euler(0, 0, rot)) * m;
GradientUtil.DoHorizontalGradient(verts, a, b, offset, scale, rect, m);
break;
case GradationMode.HorizontalGradient:
{
if (_keyTimes == null)
Expand Down Expand Up @@ -503,6 +509,30 @@ private void ApplyGradation(List<UIVertex> verts, Rect rect, Matrix4x4 m)
{
GradientUtil.DoVerticalGradient(verts, grad, offset, scale, rect, m);
}

break;
}
case GradationMode.AngleGradient:
{
if (_keyTimes == null)
{
_keyTimes = InternalListPool<float>.Rent();
GradientUtil.GetKeyTimes(grad, _keyTimes);
}

m = Matrix4x4.Rotate(Quaternion.Euler(0, 0, rot)) * m;
if (allowToModifyMeshShape)
{
var splitTimes = InternalListPool<float>.Rent();
GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale);
GradientUtil.DoAngleGradient(verts, grad, splitTimes, offset, scale, rect, m);
InternalListPool<float>.Return(ref splitTimes);
}
else
{
GradientUtil.DoHorizontalGradient(verts, grad, offset, scale, rect, m);
}

break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Packages/src/Runtime/UIEffectTweener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum CullingMask
Color = 1 << 1,
Sampling = 1 << 2,
Transition = 1 << 3,
Gradiation = 1 << 5
GradiationOffset = 1 << 5,
GradiationRotation = 1 << 6
}

public enum UpdateMode
Expand Down
15 changes: 11 additions & 4 deletions Packages/src/Runtime/Utilities/GradientUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void DoHorizontalGradient(List<UIVertex> verts, Gradient gradient,
UIVertexUtil.GetQuad(verts, i, out var lb, out var lt, out var rt, out var rb);
if (i + 5 < count)
{
UIVertexUtil.SplitHorizontal(verts, splitTimes, lb, lt, ref rb, ref rt, rect, m);
UIVertexUtil.SplitHorizontalFast(verts, splitTimes, lb, lt, ref rb, ref rt, rect, m);
}

lb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lb.position).x + 0.0005f, offset, scale);
Expand All @@ -51,7 +51,7 @@ public static void DoHorizontalGradient(List<UIVertex> verts, Gradient gradient,
Rect rect, Matrix4x4 m)
{
var count = verts.Count;
for (var i = 0; i < count; i ++)
for (var i = 0; i < count; i++)
{
var vt = verts[i];
vt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(vt.position).x, offset, scale);
Expand Down Expand Up @@ -90,7 +90,7 @@ public static void DoVerticalGradient(List<UIVertex> verts, Gradient gradient, L
UIVertexUtil.GetQuad(verts, i, out var lb, out var lt, out var rt, out var rb);
if (i + 5 < count)
{
UIVertexUtil.SplitVertical(verts, splitTimes, lb, ref lt, rb, ref rt, rect, m);
UIVertexUtil.SplitVerticalFast(verts, splitTimes, lb, ref lt, rb, ref rt, rect, m);
}

lb.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(lb.position).y + 0.0005f, offset, scale);
Expand All @@ -114,7 +114,7 @@ public static void DoVerticalGradient(List<UIVertex> verts, Gradient gradient, f
Rect rect, Matrix4x4 m)
{
var count = verts.Count;
for (var i = 0; i < count; i ++)
for (var i = 0; i < count; i++)
{
var vt = verts[i];
vt.color *= Evaluate(gradient, rect, m.MultiplyPoint3x4(vt.position).y, offset, scale);
Expand Down Expand Up @@ -215,6 +215,13 @@ static Color DoGradient(UIVertex vt, Color a, Color b, Rect rect, Matrix4x4 m, f
}
}

public static void DoAngleGradient(List<UIVertex> verts, Gradient gradient, List<float> splitTimes,
float offset, float scale, Rect rect, Matrix4x4 m)
{
UIVertexUtil.SplitAngle(verts, splitTimes, rect, m);
DoHorizontalGradient(verts, gradient, offset, scale, rect, m);
}

public static void GetKeyTimes(Gradient gradient, List<float> results)
{
results.Clear();
Expand Down
Loading

0 comments on commit 6b5eec2

Please sign in to comment.