Skip to content

Commit db61e4b

Browse files
committed
feat: gradation feature
close #277
1 parent c0d6516 commit db61e4b

8 files changed

+517
-11
lines changed

Packages/src/Editor/UIEffectEditor.cs

+36
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ public class UIEffect2Editor : Editor
6060
private SerializedProperty _shadowColor;
6161
private SerializedProperty _shadowColorGlow;
6262

63+
private SerializedProperty _gradationMode;
64+
private SerializedProperty _gradationColor1;
65+
private SerializedProperty _gradationColor2;
66+
private SerializedProperty _gradationGradient;
67+
private SerializedProperty _gradationOffset;
68+
private SerializedProperty _gradationScale;
69+
6370
private bool _expandOthers;
6471
private SerializedProperty _allowExtendVertex;
6572

@@ -115,6 +122,13 @@ private void OnEnable()
115122
_shadowColor = serializedObject.FindProperty("m_ShadowColor");
116123
_shadowColorGlow = serializedObject.FindProperty("m_ShadowColorGlow");
117124

125+
_gradationMode = serializedObject.FindProperty("m_GradationMode");
126+
_gradationColor1 = serializedObject.FindProperty("m_GradationColor1");
127+
_gradationColor2 = serializedObject.FindProperty("m_GradationColor2");
128+
_gradationGradient = serializedObject.FindProperty("m_GradationGradient");
129+
_gradationOffset = serializedObject.FindProperty("m_GradationOffset");
130+
_gradationScale = serializedObject.FindProperty("m_GradationScale");
131+
118132
_allowExtendVertex = serializedObject.FindProperty("m_AllowExtendVertex");
119133
}
120134

@@ -260,6 +274,28 @@ public void DrawProperties()
260274
EditorGUI.indentLevel--;
261275
}
262276

277+
// Gradient
278+
DrawSeparator();
279+
if (DrawHeaderPopup(_gradationMode))
280+
{
281+
EditorGUI.indentLevel++;
282+
switch ((GradationMode)_gradationMode.intValue)
283+
{
284+
case GradationMode.HorizontalGradient:
285+
case GradationMode.VerticalGradient:
286+
EditorGUILayout.PropertyField(_gradationGradient);
287+
break;
288+
default:
289+
EditorGUILayout.PropertyField(_gradationColor1);
290+
EditorGUILayout.PropertyField(_gradationColor2);
291+
break;
292+
}
293+
294+
EditorGUILayout.PropertyField(_gradationOffset);
295+
EditorGUILayout.PropertyField(_gradationScale);
296+
EditorGUI.indentLevel--;
297+
}
298+
263299
DrawSeparator();
264300
_expandOthers = EditorGUILayout.BeginFoldoutHeaderGroup(_expandOthers, "Others");
265301
if (_expandOthers)

Packages/src/Runtime/Enums.cs

+13
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public enum ShadowMode
7676
Mirror
7777
}
7878

79+
public enum GradationMode
80+
{
81+
None = 0,
82+
Horizontal,
83+
HorizontalGradient,
84+
Vertical,
85+
VerticalGradient,
86+
RadialFast,
87+
RadialDetail,
88+
DiagonalToRightBottom,
89+
DiagonalToLeftBottom
90+
}
91+
7992
public enum PreferSamplingSize
8093
{
8194
None = 0,

Packages/src/Runtime/UIEffect.cs

+101
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ public class UIEffect : UIEffectBase
144144
[SerializeField]
145145
protected bool m_ShadowColorGlow = false;
146146

147+
[SerializeField]
148+
protected GradationMode m_GradationMode = GradationMode.None;
149+
150+
[SerializeField]
151+
protected Color m_GradationColor1 = Color.white;
152+
153+
[SerializeField]
154+
protected Color m_GradationColor2 = Color.white;
155+
156+
[SerializeField]
157+
private Gradient m_GradationGradient = new Gradient();
158+
159+
[Range(-1, 1)]
160+
[SerializeField]
161+
protected float m_GradationOffset = 0;
162+
163+
[PowerRange(0.01f, 10, 10)]
164+
[SerializeField]
165+
protected float m_GradationScale = 1;
166+
147167
[SerializeField]
148168
protected bool m_AllowExtendVertex = true;
149169

@@ -597,6 +617,61 @@ public bool shadowGlow
597617
}
598618
}
599619

620+
public GradationMode gradationMode
621+
{
622+
get => m_GradationMode;
623+
set
624+
{
625+
if (m_GradationMode == value) return;
626+
context.gradationMode = m_GradationMode = value;
627+
SetVerticesDirty();
628+
}
629+
}
630+
631+
public Color gradationColor1
632+
{
633+
get => m_GradationColor1;
634+
set
635+
{
636+
if (m_GradationColor1 == value) return;
637+
context.gradationColor1 = m_GradationColor1 = value;
638+
SetVerticesDirty();
639+
}
640+
}
641+
642+
public Color gradationColor2
643+
{
644+
get => m_GradationColor2;
645+
set
646+
{
647+
if (m_GradationColor2 == value) return;
648+
context.gradationColor2 = m_GradationColor2 = value;
649+
SetVerticesDirty();
650+
}
651+
}
652+
653+
public float gradationOffset
654+
{
655+
get => m_GradationOffset;
656+
set
657+
{
658+
if (Mathf.Approximately(m_GradationOffset, value)) return;
659+
context.gradationOffset = m_GradationOffset = value;
660+
SetVerticesDirty();
661+
}
662+
}
663+
664+
public float gradationScale
665+
{
666+
get => m_GradationScale;
667+
set
668+
{
669+
if (Mathf.Approximately(m_GradationScale, value)) return;
670+
context.gradationScale = m_GradationScale = value;
671+
SetVerticesDirty();
672+
}
673+
}
674+
600675
public bool allowExtendVertex
601676
{
602677
get => m_AllowExtendVertex;
@@ -633,6 +708,7 @@ protected override void OnDestroy()
633708
protected override void OnValidate()
634709
{
635710
(m_SrcBlendMode, m_DstBlendMode) = (m_BlendType, m_SrcBlendMode, m_DstBlendMode).Convert();
711+
context?.SetGradationDirty();
636712
base.OnValidate();
637713
}
638714
#endif
@@ -657,6 +733,13 @@ public override void SetMaterialDirty()
657733
});
658734
}
659735

736+
public void SetGradientKeys(GradientColorKey[] colorKeys, GradientAlphaKey[] alphaKeys)
737+
{
738+
m_GradationGradient ??= new Gradient();
739+
m_GradationGradient.SetKeys(colorKeys, alphaKeys);
740+
context?.SetGradationDirty();
741+
}
742+
660743
protected override void UpdateContext(UIEffectContext c)
661744
{
662745
c.toneFilter = m_ToneFilter;
@@ -696,6 +779,12 @@ protected override void UpdateContext(UIEffectContext c)
696779
c.shadowColorFilter = m_ShadowColorFilter;
697780
c.shadowColor = m_ShadowColor;
698781
c.shadowColorGlow = m_ShadowColorGlow;
782+
c.gradationMode = m_GradationMode;
783+
c.gradationColor1 = m_GradationColor1;
784+
c.gradationColor2 = m_GradationColor2;
785+
c.gradationGradient = m_GradationGradient;
786+
c.gradationOffset = m_GradationOffset;
787+
c.gradationScale = m_GradationScale;
699788
c.allowExtendVertex = m_AllowExtendVertex;
700789
}
701790

@@ -730,6 +819,11 @@ public override void SetRate(float rate, UIEffectTweener.CullingMask mask)
730819
{
731820
transitionRate = rate;
732821
}
822+
823+
if (gradationMode != GradationMode.None && 0 < (mask & UIEffectTweener.CullingMask.Gradiation))
824+
{
825+
gradationOffset = Mathf.Lerp(-1f, 1f, rate);
826+
}
733827
}
734828

735829
public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
@@ -852,6 +946,13 @@ internal void CopyFrom(UIEffectContext c)
852946
m_ShadowColor = c.shadowColor;
853947
m_ShadowColorGlow = c.shadowColorGlow;
854948

949+
m_GradationMode = c.gradationMode;
950+
m_GradationColor1 = c.gradationColor1;
951+
m_GradationColor2 = c.gradationColor2;
952+
m_GradationGradient = c.gradationGradient;
953+
m_GradationOffset = c.gradationOffset;
954+
m_GradationScale = c.gradationScale;
955+
855956
m_AllowExtendVertex = c.allowExtendVertex;
856957

857958
UpdateContext(context);

Packages/src/Runtime/UIEffectContext.cs

+87-2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public class UIEffectContext
175175
public Color shadowColor;
176176
public bool shadowColorGlow;
177177

178+
public GradationMode gradationMode;
179+
public Color gradationColor1;
180+
public Color gradationColor2;
181+
public Gradient gradationGradient;
182+
public float gradationOffset;
183+
public float gradationScale;
184+
private List<float> _keyTimes;
185+
private List<float> _splitTimes;
186+
178187
public bool allowExtendVertex;
179188

180189

@@ -188,6 +197,7 @@ public class UIEffectContext
188197

189198
public void Reset()
190199
{
200+
InternalListPool<float>.Return(ref _keyTimes);
191201
CopyFrom(s_DefaultContext);
192202
}
193203

@@ -237,9 +247,21 @@ public void CopyFrom(UIEffectContext preset)
237247
shadowColor = preset.shadowColor;
238248
shadowColorGlow = preset.shadowColorGlow;
239249

250+
gradationMode = preset.gradationMode;
251+
gradationColor1 = preset.gradationColor1;
252+
gradationColor2 = preset.gradationColor2;
253+
gradationGradient = preset.gradationGradient;
254+
gradationOffset = preset.gradationOffset;
255+
gradationScale = preset.gradationScale;
256+
240257
allowExtendVertex = preset.allowExtendVertex;
241258
}
242259

260+
public void SetGradationDirty()
261+
{
262+
InternalListPool<float>.Return(ref _keyTimes);
263+
}
264+
243265
public void ApplyToMaterial(Material material, float actualSamplingScale = 1f)
244266
{
245267
if (!material) return;
@@ -353,6 +375,7 @@ public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelp
353375
// Get the rectangle to calculate the normalized position.
354376
vh.GetUIVertexStream(verts);
355377
var bundleSize = isText ? 6 : count;
378+
var rectMatrix = Matrix4x4.identity;
356379
var rot = Matrix4x4.Rotate(Quaternion.Euler(0, 0, transitionRotation));
357380
var v1 = rot.MultiplyPoint3x4(new Vector3(1, 1, 0));
358381
var multiplier = Mathf.Max(Mathf.Abs(v1.x), Mathf.Abs(v1.y));
@@ -361,9 +384,10 @@ public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelp
361384
if (transitionRoot)
362385
{
363386
rect = transitionRoot.rect;
387+
rectMatrix = transitionRoot.worldToLocalMatrix
388+
* graphic.transform.localToWorldMatrix;
364389
rot *= Matrix4x4.Scale(new Vector3(1 / multiplier, 1 / multiplier, 1))
365-
* transitionRoot.worldToLocalMatrix
366-
* graphic.rectTransform.localToWorldMatrix;
390+
* rectMatrix;
367391
}
368392
else
369393
{
@@ -401,13 +425,74 @@ public void ModifyMesh(Graphic graphic, RectTransform transitionRoot, VertexHelp
401425
}
402426
}
403427

428+
// Apply gradation.
429+
ApplyGradation(verts, transitionRoot.rect, rectMatrix);
430+
404431
// Apply shadow.
405432
ApplyShadow(transitionRoot, verts);
406433

407434
vh.Clear();
408435
vh.AddUIVertexTriangleStream(verts);
409436
}
410437

438+
private void ApplyGradation(List<UIVertex> verts, Rect rect, Matrix4x4 m)
439+
{
440+
var a = gradationColor1;
441+
var b = gradationColor2;
442+
var offset = gradationOffset;
443+
var scale = gradationScale;
444+
var grad = gradationGradient;
445+
switch (gradationMode)
446+
{
447+
case GradationMode.Horizontal:
448+
GradientUtil.DoHorizontalGradient(verts, a, b, offset, scale, rect, m);
449+
break;
450+
case GradationMode.Vertical:
451+
GradientUtil.DoVerticalGradient(verts, a, b, offset, scale, rect, m);
452+
break;
453+
case GradationMode.DiagonalToRightBottom:
454+
GradientUtil.DoDiagonalGradientToRightBottom(verts, a, b, offset, scale, rect, m);
455+
break;
456+
case GradationMode.DiagonalToLeftBottom:
457+
GradientUtil.DoDiagonalGradientToLeftBottom(verts, a, b, offset, scale, rect, m);
458+
break;
459+
case GradationMode.RadialFast:
460+
GradientUtil.DoRadialGradient(verts, a, b, offset, scale, rect, m, 4);
461+
break;
462+
case GradationMode.RadialDetail:
463+
GradientUtil.DoRadialGradient(verts, a, b, offset, scale, rect, m, 12);
464+
break;
465+
case GradationMode.HorizontalGradient:
466+
{
467+
if (_keyTimes == null)
468+
{
469+
_keyTimes = InternalListPool<float>.Rent();
470+
GradientUtil.GetKeyTimes(grad, _keyTimes);
471+
}
472+
473+
var splitTimes = InternalListPool<float>.Rent();
474+
GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale);
475+
GradientUtil.DoHorizontalGradient(verts, grad, splitTimes, offset, scale, rect, m);
476+
InternalListPool<float>.Return(ref splitTimes);
477+
break;
478+
}
479+
case GradationMode.VerticalGradient:
480+
{
481+
if (_keyTimes == null)
482+
{
483+
_keyTimes = InternalListPool<float>.Rent();
484+
GradientUtil.GetKeyTimes(grad, _keyTimes);
485+
}
486+
487+
var splitTimes = InternalListPool<float>.Rent();
488+
GradientUtil.SplitKeyTimes(_keyTimes, splitTimes, offset, scale);
489+
GradientUtil.DoVerticalGradient(verts, grad, splitTimes, offset, scale, rect, m);
490+
InternalListPool<float>.Return(ref splitTimes);
491+
break;
492+
}
493+
}
494+
}
495+
411496
private void ApplyShadow(RectTransform transitionRoot, List<UIVertex> verts)
412497
{
413498
switch (shadowMode)

Packages/src/Runtime/UIEffectTweener.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public enum CullingMask
1414
Tone = 1 << 0,
1515
Color = 1 << 1,
1616
Sampling = 1 << 2,
17-
Transition = 1 << 3
17+
Transition = 1 << 3,
18+
Gradiation = 1 << 5
1819
}
1920

2021
public enum UpdateMode

0 commit comments

Comments
 (0)