summaryrefslogtreecommitdiff
path: root/Assets/Scripts/UIGradient.cs
blob: b1841829fd1af8cb3b7cafabbbd5064a9f444604 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using UnityEngine;
using UnityEngine.UI;

namespace Coffee.UIEffects
{
    /// <summary>
    /// UIGradient.
    /// </summary>
    [DisallowMultipleComponent]
    [AddComponentMenu("UI/UIEffects/UIGradient", 101)]
    public class UIGradient : BaseMeshEffect
    {
        static readonly Vector2[] s_SplitedCharacterPosition = {Vector2.up, Vector2.one, Vector2.right, Vector2.zero};

        /// <summary>
        /// Gradient direction.
        /// </summary>
        public enum Direction
        {
            Horizontal,
            Vertical,
            Angle,
            Diagonal,
        }

        /// <summary>
        /// Gradient space for Text.
        /// </summary>
        public enum GradientStyle
        {
            Rect,
            Fit,
            Split,
        }


        [Tooltip("Gradient Direction.")] [SerializeField]
        Direction m_Direction;

        [Tooltip("Color1: Top or Left.")] [SerializeField]
        Color m_Color1 = Color.white;

        [Tooltip("Color2: Bottom or Right.")] [SerializeField]
        Color m_Color2 = Color.white;

        [Tooltip("Color3: For diagonal.")] [SerializeField]
        Color m_Color3 = Color.white;

        [Tooltip("Color4: For diagonal.")] [SerializeField]
        Color m_Color4 = Color.white;

        [Tooltip("Gradient rotation.")] [SerializeField] [Range(-180, 180)]
        float m_Rotation;

        [Tooltip("Gradient offset for Horizontal, Vertical or Angle.")] [SerializeField] [Range(-1, 1)]
        float m_Offset1;

        [Tooltip("Gradient offset for Diagonal.")] [SerializeField] [Range(-1, 1)]
        float m_Offset2;

        [Tooltip("Gradient style for Text.")] [SerializeField]
        GradientStyle m_GradientStyle;

        [Tooltip("Color space to correct color.")] [SerializeField]
        ColorSpace m_ColorSpace = ColorSpace.Uninitialized;

        [Tooltip("Ignore aspect ratio.")] [SerializeField]
        bool m_IgnoreAspectRatio = true;

        /// <summary>
        /// Gradient Direction.
        /// </summary>
        public Direction direction
        {
            get { return m_Direction; }
            set
            {
                if (m_Direction == value) return;
                m_Direction = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Color1: Top or Left.
        /// </summary>
        public Color color1
        {
            get { return m_Color1; }
            set
            {
                if (m_Color1 == value) return;
                m_Color1 = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Color2: Bottom or Right.
        /// </summary>
        public Color color2
        {
            get { return m_Color2; }
            set
            {
                if (m_Color2 == value) return;
                m_Color2 = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Color3: For diagonal.
        /// </summary>
        public Color color3
        {
            get { return m_Color3; }
            set
            {
                if (m_Color3 == value) return;
                m_Color3 = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Color4: For diagonal.
        /// </summary>
        public Color color4
        {
            get { return m_Color4; }
            set
            {
                if (m_Color4 == value) return;
                m_Color4 = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Gradient rotation.
        /// </summary>
        public float rotation
        {
            get
            {
                return m_Direction == Direction.Horizontal ? -90
                    : m_Direction == Direction.Vertical ? 0
                    : m_Rotation;
            }
            set
            {
                if (Mathf.Approximately(m_Rotation, value)) return;
                m_Rotation = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Gradient offset for Horizontal, Vertical or Angle.
        /// </summary>
        public float offset
        {
            get { return m_Offset1; }
            set
            {
                if (Mathf.Approximately(m_Offset1, value)) return;
                m_Offset1 = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Gradient offset for Diagonal.
        /// </summary>
        public Vector2 offset2
        {
            get { return new Vector2(m_Offset2, m_Offset1); }
            set
            {
                if (Mathf.Approximately(m_Offset1, value.y) && Mathf.Approximately(m_Offset2, value.x)) return;
                m_Offset1 = value.y;
                m_Offset2 = value.x;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Gradient style for Text.
        /// </summary>
        public GradientStyle gradientStyle
        {
            get { return m_GradientStyle; }
            set
            {
                if (m_GradientStyle == value) return;
                m_GradientStyle = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Color space to correct color.
        /// </summary>
        public ColorSpace colorSpace
        {
            get { return m_ColorSpace; }
            set
            {
                if (m_ColorSpace == value) return;
                m_ColorSpace = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Ignore aspect ratio.
        /// </summary>
        public bool ignoreAspectRatio
        {
            get { return m_IgnoreAspectRatio; }
            set
            {
                if (m_IgnoreAspectRatio == value) return;
                m_IgnoreAspectRatio = value;
                SetVerticesDirty();
            }
        }

        /// <summary>
        /// Call used to modify mesh.
        /// </summary>
        public override void ModifyMesh(VertexHelper vh, Graphic graphic)
        {
            if (!isActiveAndEnabled)
                return;

            // Gradient space.
            var rect = default(Rect);
            var vertex = default(UIVertex);
            switch (m_GradientStyle)
            {
                case GradientStyle.Rect:
                    // RectTransform.
                    rect = graphic.rectTransform.rect;
                    break;
                case GradientStyle.Split:
                    // Each characters.
                    rect.Set(0, 0, 1, 1);
                    break;
                case GradientStyle.Fit:
                {
                    // Fit to contents.
                    rect.xMin = rect.yMin = float.MaxValue;
                    rect.xMax = rect.yMax = float.MinValue;
                    for (var i = 0; i < vh.currentVertCount; i++)
                    {
                        vh.PopulateUIVertex(ref vertex, i);
                        rect.xMin = Mathf.Min(rect.xMin, vertex.position.x);
                        rect.yMin = Mathf.Min(rect.yMin, vertex.position.y);
                        rect.xMax = Mathf.Max(rect.xMax, vertex.position.x);
                        rect.yMax = Mathf.Max(rect.yMax, vertex.position.y);
                    }

                    break;
                }
            }

            // Gradient rotation.
            var rad = rotation * Mathf.Deg2Rad;
            var dir = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad));
            if (!m_IgnoreAspectRatio && Direction.Angle <= m_Direction)
            {
                dir.x *= rect.height / rect.width;
                dir = dir.normalized;
            }

            // Calculate vertex color.
            var localMatrix = new Matrix2x3(rect, dir.x, dir.y); // Get local matrix.
            for (var i = 0; i < vh.currentVertCount; i++)
            {
                vh.PopulateUIVertex(ref vertex, i);

                // Normalize vertex position by local matrix.
                Vector2 normalizedPos;
                if (m_GradientStyle == GradientStyle.Split)
                {
                    // Each characters.
                    normalizedPos = localMatrix * s_SplitedCharacterPosition[i % 4] + offset2;
                }
                else
                {
                    normalizedPos = localMatrix * vertex.position + offset2;
                }

                // Interpolate vertex color.
                Color color;
                if (direction == Direction.Diagonal)
                {
                    color = Color.LerpUnclamped(
                        Color.LerpUnclamped(m_Color1, m_Color2, normalizedPos.x),
                        Color.LerpUnclamped(m_Color3, m_Color4, normalizedPos.x),
                        normalizedPos.y);
                }
                else
                {
                    color = Color.LerpUnclamped(m_Color2, m_Color1, normalizedPos.y);
                }

                // Correct color.
                vertex.color *= (m_ColorSpace == ColorSpace.Gamma) ? color.gamma
                    : (m_ColorSpace == ColorSpace.Linear) ? color.linear
                    : color;

                vh.SetUIVertex(vertex, i);
            }
        }
    }
}