Skip to content

Commit 7cb8325

Browse files
committed
feat: Angle Gradient gradation supports fixed mode gradient
1 parent eafb9ce commit 7cb8325

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

Packages/src/Runtime/Utilities/GradientUtil.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ static Color Evaluate(Gradient gradient, Rect rect, float t, float offset, float
4747
}
4848
}
4949

50+
public static void DoHorizontalGradientFixed(List<UIVertex> verts, Gradient gradient, float offset, float scale,
51+
Rect rect, Matrix4x4 m)
52+
{
53+
var count = verts.Count;
54+
for (var i = 0; i < count; i += 3)
55+
{
56+
var vt0 = verts[i + 0];
57+
var vt1 = verts[i + 1];
58+
var vt2 = verts[i + 2];
59+
var center = m.MultiplyPoint3x4((vt0.position + vt1.position + vt2.position) / 3).x;
60+
vt0.color *= Evaluate(gradient, rect, center, offset, scale);
61+
vt1.color *= Evaluate(gradient, rect, center, offset, scale);
62+
vt2.color *= Evaluate(gradient, rect, center, offset, scale);
63+
verts[i + 0] = vt0;
64+
verts[i + 1] = vt1;
65+
verts[i + 2] = vt2;
66+
}
67+
68+
return;
69+
70+
static Color Evaluate(Gradient gradient, Rect rect, float t, float offset, float scale)
71+
{
72+
t = Mathf.InverseLerp(rect.xMin, rect.xMax, t); // 0-1
73+
t = Mathf.Repeat((t - offset * scale - (1 - scale) / 2) / scale, 1); // Revert
74+
return gradient.Evaluate(t);
75+
}
76+
}
77+
5078
public static void DoHorizontalGradient(List<UIVertex> verts, Gradient gradient, float offset, float scale,
5179
Rect rect, Matrix4x4 m)
5280
{
@@ -219,7 +247,14 @@ public static void DoAngleGradient(List<UIVertex> verts, Gradient gradient, List
219247
float offset, float scale, Rect rect, Matrix4x4 m)
220248
{
221249
UIVertexUtil.SplitAngle(verts, splitTimes, rect, m);
222-
DoHorizontalGradient(verts, gradient, offset, scale, rect, m);
250+
if (gradient.mode == GradientMode.Fixed)
251+
{
252+
DoHorizontalGradientFixed(verts, gradient, offset, scale, rect, m);
253+
}
254+
else
255+
{
256+
DoHorizontalGradient(verts, gradient, offset, scale, rect, m);
257+
}
223258
}
224259

225260
public static void GetKeyTimes(Gradient gradient, List<float> results)

0 commit comments

Comments
 (0)