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 System;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.Serialization;
using System.Text;
using System.Linq;
using System.IO;
namespace Coffee.UIEffects
{
/// <summary>
/// Dissolve effect for uGUI.
/// </summary>
[AddComponentMenu("UI/UIEffects/UIDissolve", 3)]
public class UIDissolve : BaseMaterialEffect, IMaterialModifier
{
private const uint k_ShaderId = 0 << 3;
private static readonly ParameterTexture s_ParamTex = new ParameterTexture(8, 128, "_ParamTex");
private static readonly int k_TransitionTexId = Shader.PropertyToID("_TransitionTex");
private bool _lastKeepAspectRatio;
private EffectArea _lastEffectArea;
private static Texture _defaultTransitionTexture;
[Tooltip("Current location[0-1] for dissolve effect. 0 is not dissolved, 1 is completely dissolved.")]
[FormerlySerializedAs("m_Location")]
[SerializeField]
[Range(0, 1)]
float m_EffectFactor = 0.5f;
[Tooltip("Edge width.")] [SerializeField] [Range(0, 1)]
float m_Width = 0.5f;
[Tooltip("Edge softness.")] [SerializeField] [Range(0, 1)]
float m_Softness = 0.5f;
[Tooltip("Edge color.")] [SerializeField] [ColorUsage(false)]
Color m_Color = new Color(0.0f, 0.25f, 1.0f);
[Tooltip("Edge color effect mode.")] [SerializeField]
ColorMode m_ColorMode = ColorMode.Add;
[Tooltip("Noise texture for dissolving (single channel texture).")]
[SerializeField]
[FormerlySerializedAs("m_NoiseTexture")]
Texture m_TransitionTexture;
[Header("Advanced Option")] [Tooltip("The area for effect.")] [SerializeField]
protected EffectArea m_EffectArea;
[Tooltip("Keep effect aspect ratio.")] [SerializeField]
bool m_KeepAspectRatio;
[Header("Effect Player")] [SerializeField]
EffectPlayer m_Player;
[Tooltip("Reverse the dissolve effect.")] [FormerlySerializedAs("m_ReverseAnimation")] [SerializeField]
bool m_Reverse = false;
/// <summary>
/// Effect factor between 0(start) and 1(end).
/// </summary>
public float effectFactor
{
get { return m_EffectFactor; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_EffectFactor, value)) return;
m_EffectFactor = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Edge width.
/// </summary>
public float width
{
get { return m_Width; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_Width, value)) return;
m_Width = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Edge softness.
/// </summary>
public float softness
{
get { return m_Softness; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_Softness, value)) return;
m_Softness = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Edge color.
/// </summary>
public Color color
{
get { return m_Color; }
set
{
if (m_Color == value) return;
m_Color = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Noise texture.
/// </summary>
public Texture transitionTexture
{
get
{
return m_TransitionTexture
? m_TransitionTexture
: defaultTransitionTexture;
}
set
{
if (m_TransitionTexture == value) return;
m_TransitionTexture = value;
SetMaterialDirty();
}
}
private static Texture defaultTransitionTexture
{
get
{
return _defaultTransitionTexture
? _defaultTransitionTexture
: (_defaultTransitionTexture = Resources.Load<Texture>("Default-Transition"));
}
}
/// <summary>
/// The area for effect.
/// </summary>
public EffectArea effectArea
{
get { return m_EffectArea; }
set
{
if (m_EffectArea == value) return;
m_EffectArea = value;
SetVerticesDirty();
}
}
/// <summary>
/// Keep aspect ratio.
/// </summary>
public bool keepAspectRatio
{
get { return m_KeepAspectRatio; }
set
{
if (m_KeepAspectRatio == value) return;
m_KeepAspectRatio = value;
SetVerticesDirty();
}
}
/// <summary>
/// Color effect mode.
/// </summary>
public ColorMode colorMode
{
get { return m_ColorMode; }
set
{
if (m_ColorMode == value) return;
m_ColorMode = value;
SetMaterialDirty();
}
}
/// <summary>
/// Gets the parameter texture.
/// </summary>
public override ParameterTexture paramTex
{
get { return s_ParamTex; }
}
public EffectPlayer effectPlayer
{
get { return m_Player ?? (m_Player = new EffectPlayer()); }
}
public override Hash128 GetMaterialHash(Material material)
{
if (!isActiveAndEnabled || !material || !material.shader)
return k_InvalidHash;
var shaderVariantId = (uint) ((int) m_ColorMode << 6);
var resourceId = (uint) transitionTexture.GetInstanceID();
return new Hash128(
(uint) material.GetInstanceID(),
k_ShaderId + shaderVariantId,
resourceId,
0
);
}
public override void ModifyMaterial(Material newMaterial, Graphic graphic)
{
var connector = GraphicConnector.FindConnector(graphic);
newMaterial.shader = Shader.Find(string.Format("Hidden/{0} (UIDissolve)", newMaterial.shader.name));
SetShaderVariants(newMaterial, m_ColorMode);
newMaterial.SetTexture(k_TransitionTexId, transitionTexture);
paramTex.RegisterMaterial(newMaterial);
}
/// <summary>
/// Modifies the mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
// bool isText = isTMPro || graphic is Text;
var normalizedIndex = paramTex.GetNormalizedIndex(this);
// rect.
var tex = transitionTexture;
var aspectRatio = m_KeepAspectRatio && tex ? ((float) tex.width) / tex.height : -1;
var rect = m_EffectArea.GetEffectArea(vh, rectTransform.rect, aspectRatio);
// Calculate vertex position.
var vertex = default(UIVertex);
var count = vh.currentVertCount;
for (var i = 0; i < count; i++)
{
vh.PopulateUIVertex(ref vertex, i);
float x;
float y;
connector.GetPositionFactor(m_EffectArea, i, rect, vertex.position, out x, out y);
vertex.uv0 = new Vector2(
Packer.ToFloat(vertex.uv0.x, vertex.uv0.y),
Packer.ToFloat(x, y, normalizedIndex)
);
vh.SetUIVertex(vertex, i);
}
}
protected override void SetEffectParamsDirty()
{
paramTex.SetData(this, 0, m_EffectFactor); // param1.x : location
paramTex.SetData(this, 1, m_Width); // param1.y : width
paramTex.SetData(this, 2, m_Softness); // param1.z : softness
paramTex.SetData(this, 4, m_Color.r); // param2.x : red
paramTex.SetData(this, 5, m_Color.g); // param2.y : green
paramTex.SetData(this, 6, m_Color.b); // param2.z : blue
}
protected override void SetVerticesDirty()
{
base.SetVerticesDirty();
_lastKeepAspectRatio = m_KeepAspectRatio;
_lastEffectArea = m_EffectArea;
}
protected override void OnDidApplyAnimationProperties()
{
base.OnDidApplyAnimationProperties();
if (_lastKeepAspectRatio != m_KeepAspectRatio
|| _lastEffectArea != m_EffectArea)
SetVerticesDirty();
}
/// <summary>
/// Play effect.
/// </summary>
public void Play(bool reset = true)
{
effectPlayer.Play(reset);
}
/// <summary>
/// Stop effect.
/// </summary>
public void Stop(bool reset = true)
{
effectPlayer.Stop(reset);
}
protected override void OnEnable()
{
base.OnEnable();
effectPlayer.OnEnable((f) => effectFactor = m_Reverse ? 1f - f : f);
}
protected override void OnDisable()
{
base.OnDisable();
effectPlayer.OnDisable();
}
}
}
|