-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropShadow.cs
136 lines (115 loc) · 3.5 KB
/
DropShadow.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
using System.Collections.Generic;
using UnityEditor;
namespace UnityEngine.UI
{
[AddComponentMenu("UI/Effects/DropShadow", 14)]
public class DropShadow : BaseMeshEffect
{
[SerializeField]
private Color shadowColor = new Color(0f, 0f, 0f, 0.5f);
[SerializeField]
private Vector2 shadowDistance = new Vector2(1f, -1f);
[SerializeField]
private bool m_UseGraphicAlpha = true;
public int iterations = 5;
public Vector2 shadowSpread = Vector2.one;
protected DropShadow()
{}
#if UNITY_EDITOR
protected override void OnValidate()
{
EffectDistance = shadowDistance;
base.OnValidate();
}
#endif
public Color effectColor
{
get { return shadowColor; }
set
{
shadowColor = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
public Vector2 ShadowSpread
{
get { return shadowSpread; }
set
{
shadowSpread = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
public int Iterations
{
get { return iterations; }
set
{
iterations = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
public Vector2 EffectDistance
{
get { return shadowDistance; }
set
{
shadowDistance = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
public bool useGraphicAlpha
{
get { return m_UseGraphicAlpha; }
set
{
m_UseGraphicAlpha = value;
if (graphic != null)
graphic.SetVerticesDirty();
}
}
void DropShadowEffect(List<UIVertex> verts)
{
UIVertex vt;
int count = verts.Count;
List<UIVertex> vertsCopy = new List<UIVertex>(verts);
verts.Clear();
for(int i=0; i<iterations; i++)
{
for(int v=0; v<count; v++)
{
vt = vertsCopy[v];
Vector3 position = vt.position;
float fac = (float)i/(float)iterations;
position.x *= (1 + shadowSpread.x*fac*0.01f);
position.y *= (1 + shadowSpread.y*fac*0.01f);
position.x += shadowDistance.x * fac;
position.y += shadowDistance.y * fac;
vt.position = position;
Color32 color = shadowColor;
color.a = (byte)((float)color.a /(float)iterations);
vt.color = color;
verts.Add(vt);
}
}
for(int i=0; i<vertsCopy.Count; i++)
{
verts.Add(vertsCopy[i]);
}
}
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
return;
List<UIVertex> output = new List<UIVertex>();
vh.GetUIVertexStream(output);
DropShadowEffect(output);
vh.Clear();
vh.AddUIVertexTriangleStream(output);
}
}
}