Tanoda
MaterialInterpolators.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10
11namespace Leap.Unity.Animation {
12 using Internal;
13
14 public partial struct Tween {
15 public MaterialSelector Target(Material material) {
16 return new MaterialSelector(material, this);
17 }
18 }
19}
20
22
23 public struct MaterialSelector {
24 private Material _target;
25 private Tween _tween;
26
27 public MaterialSelector(Material target, Tween tween) {
28 _target = target;
29 _tween = tween;
30 }
31
32 #region COLOR
33 public Tween Color(Color a, Color b, int propertyId) {
34 return _tween.AddInterpolator(Pool<MaterialColorInterpolator>.Spawn().Init(a, b, new MaterialPropertyKey(_target, propertyId)));
35 }
36
37 public Tween Color(Color a, Color b, string propertyName = "_Color") {
38 return Color(a, b, Shader.PropertyToID(propertyName));
39 }
40
41 public Tween Color(Gradient gradient, int propertyId) {
42 return _tween.AddInterpolator(Pool<MaterialGradientInterpolator>.Spawn().Init(gradient, new MaterialPropertyKey(_target, propertyId)));
43 }
44
45 public Tween Color(Gradient gradient, string propertyName = "_Color") {
46 return Color(gradient, Shader.PropertyToID(propertyName));
47 }
48
49 public Tween ToColor(Color b, int propertyId) {
50 return _tween.AddInterpolator(Pool<MaterialColorInterpolator>.Spawn().Init(_target.GetColor(propertyId), b, new MaterialPropertyKey(_target, propertyId)));
51 }
52
53 public Tween ToColor(Color b, string propertyName = "_Color") {
54 return ToColor(b, Shader.PropertyToID(propertyName));
55 }
56
57 public Tween RGB(Color a, Color b, int propertyId) {
58 return _tween.AddInterpolator(Pool<MaterialRGBInterpolator>.Spawn().Init((Vector4)a, (Vector4)b, new MaterialPropertyKey(_target, propertyId)));
59 }
60
61 public Tween RGB(Color a, Color b, string propertyName = "_Color") {
62 return RGB(a, b, Shader.PropertyToID(propertyName));
63 }
64
65 public Tween ToRGB(Color b, int propertyId) {
66 return _tween.AddInterpolator(Pool<MaterialRGBInterpolator>.Spawn().Init((Vector4)_target.GetColor(propertyId), (Vector4)b, new MaterialPropertyKey(_target, propertyId)));
67 }
68
69 public Tween ToRGB(Color b, string propertyName = "_Color") {
70 return ToRGB(b, Shader.PropertyToID(propertyName));
71 }
72
73 public Tween Alpha(float a, float b, int propertyId) {
74 return _tween.AddInterpolator(Pool<MaterialAlphaInterpolator>.Spawn().Init(a, b, new MaterialPropertyKey(_target, propertyId)));
75 }
76
77 public Tween Alpha(float a, float b, string propertyName = "_Color") {
78 return Alpha(a, b, Shader.PropertyToID(propertyName));
79 }
80
81 public Tween ToAlpha(float b, int propertyId) {
82 return _tween.AddInterpolator(Pool<MaterialAlphaInterpolator>.Spawn().Init(_target.GetColor(propertyId).a, b, new MaterialPropertyKey(_target, propertyId)));
83 }
84
85 public Tween ToAlpha(float b, string propertyName = "_Color") {
86 return ToAlpha(b, Shader.PropertyToID(propertyName));
87 }
88
89 private class MaterialColorInterpolator : ColorInterpolatorBase<MaterialPropertyKey> {
90
91 public override void Interpolate(float percent) {
92 _target.material.SetColor(_target.propertyId, _a + _b * percent);
93 }
94
95 public override void Dispose() {
96 _target.material = null;
97 Pool<MaterialColorInterpolator>.Recycle(this);
98 }
99
100 public override bool isValid { get { return _target.material != null; } }
101 }
102
103 private class MaterialGradientInterpolator : GradientInterpolatorBase {
104
105 private MaterialPropertyKey _matPropKey;
106
107 public MaterialGradientInterpolator Init(Gradient gradient, MaterialPropertyKey matPropKey) {
108 this._matPropKey = matPropKey;
109 Init(gradient);
110 return this;
111 }
112
113 public override void Interpolate(float percent) {
114 _matPropKey.material.SetColor(_matPropKey.propertyId, _gradient.Evaluate(percent));
115 }
116
117 public override bool isValid { get { return _matPropKey.material != null; } }
118 }
119
120 private class MaterialRGBInterpolator : Vector3InterpolatorBase<MaterialPropertyKey> {
121
122 public override void Interpolate(float percent) {
123 float currAlpha = _target.material.GetColor(_target.propertyId).a;
124 Color color = (Vector4)(_a + _b * percent);
125 color.a = currAlpha;
126 _target.material.SetColor(_target.propertyId, color);
127 }
128
129 public override void Dispose() {
130 _target.material = null;
131 Pool<MaterialRGBInterpolator>.Recycle(this);
132 }
133
134 public override bool isValid { get { return _target.material != null; } }
135 }
136
137 private class MaterialAlphaInterpolator : FloatInterpolatorBase<MaterialPropertyKey> {
138
139 public override void Interpolate(float percent) {
140 Color color = _target.material.GetColor(_target.propertyId);
141 color.a = Mathf.Lerp(_a, _b, percent);
142 _target.material.SetColor(_target.propertyId, color);
143 }
144
145 public override void Dispose() {
146 _target.material = null;
147 Pool<MaterialAlphaInterpolator>.Recycle(this);
148 }
149
150 public override bool isValid { get { return _target.material != null; } }
151 }
152 #endregion
153
154 #region FLOAT
155 public Tween Float(float a, float b, int propertyId) {
156 return _tween.AddInterpolator(Pool<MaterialFloatInterpolator>.Spawn().Init(a, b, new MaterialPropertyKey(_target, propertyId)));
157 }
158
159 public Tween Float(float a, float b, string propertyName) {
160 return Float(a, b, Shader.PropertyToID(propertyName));
161 }
162
163 public Tween ToFloat(float b, int propertyId) {
164 return _tween.AddInterpolator(Pool<MaterialFloatInterpolator>.Spawn().Init(_target.GetFloat(propertyId), b, new MaterialPropertyKey(_target, propertyId)));
165 }
166
167 public Tween ToFloat(float b, string propertyName) {
168 return ToFloat(b, Shader.PropertyToID(propertyName));
169 }
170
171 private class MaterialFloatInterpolator : FloatInterpolatorBase<MaterialPropertyKey> {
172 public override void Interpolate(float percent) {
173 _target.material.SetFloat(_target.propertyId, _a + _b * percent);
174 }
175
176 public override void Dispose() {
177 _target.material = null;
178 Pool<MaterialFloatInterpolator>.Recycle(this);
179 }
180
181 public override bool isValid { get { return _target.material != null; } }
182 }
183 #endregion
184
185 #region VECTOR
186 public Tween Vector(Vector4 a, Vector4 b, int propertyId) {
187 return _tween.AddInterpolator(Pool<MaterialVectorInterpolator>.Spawn().Init(a, b, new MaterialPropertyKey(_target, propertyId)));
188 }
189
190 public Tween Vector(Vector4 a, Vector4 b, string propertyName) {
191 return Vector(a, b, Shader.PropertyToID(propertyName));
192 }
193
194 public Tween Vector(Vector3 a, Vector3 b, int propertyId) {
195 return _tween.AddInterpolator(Pool<MaterialVectorInterpolator>.Spawn().Init(a, b, new MaterialPropertyKey(_target, propertyId)));
196 }
197
198 public Tween Vector(Vector3 a, Vector3 b, string propertyName) {
199 return Vector(a, b, Shader.PropertyToID(propertyName));
200 }
201
202 public Tween ToVector(Vector4 b, int propertyId) {
203 return _tween.AddInterpolator(Pool<MaterialVectorInterpolator>.Spawn().Init(_target.GetVector(propertyId), b, new MaterialPropertyKey(_target, propertyId)));
204 }
205
206 public Tween ToVector(Vector4 b, string propertyName) {
207 return ToVector(b, Shader.PropertyToID(propertyName));
208 }
209
210 public Tween ToVector(Vector3 b, int propertyId) {
211 return _tween.AddInterpolator(Pool<MaterialVectorInterpolator>.Spawn().Init(_target.GetVector(propertyId), b, new MaterialPropertyKey(_target, propertyId)));
212
213 }
214 public Tween ToVector(Vector3 b, string propertyName) {
215 return ToVector(b, Shader.PropertyToID(propertyName));
216 }
217
218 private class MaterialVectorInterpolator : Vector4InterpolatorBase<MaterialPropertyKey> {
219 public override void Interpolate(float percent) {
220 _target.material.SetVector(_target.propertyId, _a + _b * percent);
221 }
222
223 public override void Dispose() {
224 _target.material = null;
225 Pool<MaterialVectorInterpolator>.Recycle(this);
226 }
227
228 public override bool isValid { get { return _target.material != null; } }
229 }
230 #endregion
231
232 #region MATERIAL
234 return _tween.AddInterpolator(Pool<MaterialInterpolator>.Spawn().Init(a, b, _target));
235 }
236
237 private class MaterialInterpolator : InterpolatorBase<Material, Material> {
238 public override float length {
239 get {
240 return 1;
241 }
242 }
243
244 public override void Interpolate(float percent) {
245 _target.Lerp(_a, _b, percent);
246 }
247
248 public override void Dispose() {
249 _target = null;
250 _a = null;
251 _b = null;
252 Pool<MaterialInterpolator>.Recycle(this);
253 }
254
255 public override bool isValid { get { return _target != null; } }
256 }
257 #endregion
258
259 private struct MaterialPropertyKey {
260 public Material material;
261 public int propertyId;
262
263 public MaterialPropertyKey(Material material, int propertyId) {
264 this.material = material;
265 this.propertyId = propertyId;
266 }
267 }
268 }
269}
UnityEngine.Color Color
Definition: TestScript.cs:32
GradientInterpolatorBase Init(Gradient gradient)
Tween ToColor(Color b, string propertyName="_Color")
Tween ToVector(Vector4 b, string propertyName)
Tween Float(float a, float b, string propertyName)
Tween ToRGB(Color b, string propertyName="_Color")
Tween ToVector(Vector3 b, string propertyName)
Tween ToAlpha(float b, string propertyName="_Color")
Tween Color(Gradient gradient, int propertyId)
Tween Alpha(float a, float b, string propertyName="_Color")
Tween Alpha(float a, float b, int propertyId)
Tween Vector(Vector4 a, Vector4 b, string propertyName)
Tween Float(float a, float b, int propertyId)
Tween Vector(Vector3 a, Vector3 b, string propertyName)
Tween Vector(Vector3 a, Vector3 b, int propertyId)
Tween Color(Color a, Color b, string propertyName="_Color")
Tween Color(Color a, Color b, int propertyId)
Tween Vector(Vector4 a, Vector4 b, int propertyId)
Tween RGB(Color a, Color b, int propertyId)
Tween RGB(Color a, Color b, string propertyName="_Color")
Tween Color(Gradient gradient, string propertyName="_Color")
Tween ToFloat(float b, string propertyName)
Tween AddInterpolator(IInterpolator interpolator)
Adds a new Interpolator to this Tween. This Interpolator will have it's Interpolate method called eve...
Definition: Tween.cs:151
MaterialSelector Target(Material material)