-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGradientRect.cs
179 lines (154 loc) · 6.16 KB
/
GradientRect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Gilzoide.GradientRect
{
[RequireComponent(typeof(CanvasRenderer))]
public class GradientRect : MaskableGraphic
{
public enum GradientDirection
{
LeftToRight,
RightToLeft,
BottomToTop,
TopToBottom,
}
[Header("Gradient")]
[SerializeField] protected Gradient _gradient;
[SerializeField] protected GradientDirection _direction;
public Gradient Gradient
{
get => _gradient;
set
{
if (_gradient == value)
{
return;
}
_gradient = value;
SetVerticesDirty();
}
}
public GradientDirection Direction
{
get => _direction;
set
{
if (_direction == value)
{
return;
}
_direction = value;
SetVerticesDirty();
}
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
using (var enumerator = EnumerateGradient(_gradient).GetEnumerator())
{
if (!enumerator.MoveNext())
{
return;
}
Rect rect = GetPixelAdjustedRect();
Color tint = color;
(float time1, Color color1) = enumerator.Current;
while (enumerator.MoveNext())
{
(float time2, Color color2) = enumerator.Current;
Vector2 v1, v2, v3, v4;
Color c1, c2, c3, c4;
switch (_direction)
{
case GradientDirection.LeftToRight:
v1 = new Vector2(time1, 0);
v2 = new Vector2(time1, 1);
v3 = new Vector2(time2, 1);
v4 = new Vector2(time2, 0);
c1 = c2 = color1 * tint;
c3 = c4 = color2 * tint;
break;
case GradientDirection.RightToLeft:
v1 = new Vector2(1 - time2, 0);
v2 = new Vector2(1 - time2, 1);
v3 = new Vector2(1 - time1, 1);
v4 = new Vector2(1 - time1, 0);
c1 = c2 = color2 * tint;
c3 = c4 = color1 * tint;
break;
case GradientDirection.BottomToTop:
v1 = new Vector2(0, time1);
v2 = new Vector2(0, time2);
v3 = new Vector2(1, time2);
v4 = new Vector2(1, time1);
c1 = c4 = color1 * tint;
c2 = c3 = color2 * tint;
break;
case GradientDirection.TopToBottom:
v1 = new Vector2(0, 1 - time2);
v2 = new Vector2(0, 1 - time1);
v3 = new Vector2(1, 1 - time1);
v4 = new Vector2(1, 1 - time2);
c1 = c4 = color2 * tint;
c2 = c3 = color1 * tint;
break;
default: throw new ArgumentOutOfRangeException(nameof(_direction));
}
int vertexCount = vh.currentVertCount;
vh.AddVert(Rect.NormalizedToPoint(rect, v1), c1, GetUVForNormalizedPosition(v1));
vh.AddVert(Rect.NormalizedToPoint(rect, v2), c2, GetUVForNormalizedPosition(v2));
vh.AddVert(Rect.NormalizedToPoint(rect, v3), c3, GetUVForNormalizedPosition(v3));
vh.AddVert(Rect.NormalizedToPoint(rect, v4), c4, GetUVForNormalizedPosition(v4));
vh.AddTriangle(vertexCount, vertexCount + 1, vertexCount + 2);
vh.AddTriangle(vertexCount + 2, vertexCount + 3, vertexCount);
(time1, color1) = (time2, color2);
}
}
}
protected virtual Vector2 GetUVForNormalizedPosition(Vector2 position)
{
return position;
}
public static IEnumerable<(float t, Color c)> EnumerateGradient(Gradient gradient)
{
if (gradient == null)
{
yield break;
}
float colorTime = 0;
float alphaTime = 0;
yield return (0, gradient.Evaluate(0));
GradientColorKey[] colorKeys = gradient.colorKeys;
GradientAlphaKey[] alphaKeys = gradient.alphaKeys;
int colorIndex = colorKeys[0].time == 0 ? 1 : 0;
int alphaIndex = alphaKeys[0].time == 0 ? 1 : 0;
while (colorIndex < colorKeys.Length && alphaIndex < alphaKeys.Length)
{
colorTime = colorIndex < colorKeys.Length ? colorKeys[colorIndex].time : float.MaxValue;
alphaTime = alphaIndex < alphaKeys.Length ? alphaKeys[alphaIndex].time : float.MaxValue;
if (alphaTime == colorTime)
{
alphaIndex++;
colorIndex++;
yield return (colorTime, gradient.Evaluate(colorTime));
}
else if (alphaTime < colorTime)
{
alphaIndex++;
yield return (alphaTime, gradient.Evaluate(alphaTime));
}
else
{
colorIndex++;
yield return (colorTime, gradient.Evaluate(colorTime));
}
}
if (colorTime < 1 || alphaTime < 1)
{
yield return (1, gradient.Evaluate(1));
}
}
}
}