Skip to content

Commit

Permalink
feat: Angle Gradient gradation supports fixed mode gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Jan 7, 2025
1 parent eafb9ce commit 7cb8325
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion Packages/src/Runtime/Utilities/GradientUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ static Color Evaluate(Gradient gradient, Rect rect, float t, float offset, float
}
}

public static void DoHorizontalGradientFixed(List<UIVertex> verts, Gradient gradient, float offset, float scale,
Rect rect, Matrix4x4 m)
{
var count = verts.Count;
for (var i = 0; i < count; i += 3)
{
var vt0 = verts[i + 0];
var vt1 = verts[i + 1];
var vt2 = verts[i + 2];
var center = m.MultiplyPoint3x4((vt0.position + vt1.position + vt2.position) / 3).x;
vt0.color *= Evaluate(gradient, rect, center, offset, scale);
vt1.color *= Evaluate(gradient, rect, center, offset, scale);
vt2.color *= Evaluate(gradient, rect, center, offset, scale);
verts[i + 0] = vt0;
verts[i + 1] = vt1;
verts[i + 2] = vt2;
}

return;

static Color Evaluate(Gradient gradient, Rect rect, float t, float offset, float scale)
{
t = Mathf.InverseLerp(rect.xMin, rect.xMax, t); // 0-1
t = Mathf.Repeat((t - offset * scale - (1 - scale) / 2) / scale, 1); // Revert
return gradient.Evaluate(t);
}
}

public static void DoHorizontalGradient(List<UIVertex> verts, Gradient gradient, float offset, float scale,
Rect rect, Matrix4x4 m)
{
Expand Down Expand Up @@ -219,7 +247,14 @@ public static void DoAngleGradient(List<UIVertex> verts, Gradient gradient, List
float offset, float scale, Rect rect, Matrix4x4 m)
{
UIVertexUtil.SplitAngle(verts, splitTimes, rect, m);
DoHorizontalGradient(verts, gradient, offset, scale, rect, m);
if (gradient.mode == GradientMode.Fixed)
{
DoHorizontalGradientFixed(verts, gradient, offset, scale, rect, m);
}
else
{
DoHorizontalGradient(verts, gradient, offset, scale, rect, m);
}
}

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

0 comments on commit 7cb8325

Please sign in to comment.