-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInterpolator.cs
160 lines (139 loc) · 4.53 KB
/
Interpolator.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
//MIT License
//
//Copyright (c) 2018 Tom Blind
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System;
using UnityEngine;
namespace AsyncTweens
{
public interface IInterpolator<T>
{
T Interpolate(T startValue, T endValue, float t);
}
public struct FloatInterpolator : IInterpolator<float>
{
private Func<float, float> easing;
public FloatInterpolator(Func<float, float> easing)
{
this.easing = easing;
}
public float Interpolate(float startValue, float endValue, float t)
{
return Mathf.LerpUnclamped(startValue, endValue, easing(t));
}
}
public struct Vector2Interpolator : IInterpolator<Vector2>
{
private Func<float, float> easingX;
private Func<float, float> easingY;
public Vector2Interpolator(Func<float, float> easingX, Func<float, float> easingY)
{
this.easingX = easingX;
this.easingY = easingY;
}
public Vector2Interpolator(Func<float, float> easing)
{
this.easingX = easing;
this.easingY = easing;
}
public Vector2 Interpolate(Vector2 startValue, Vector2 endValue, float t)
{
return new Vector2(
Mathf.LerpUnclamped(startValue.x, endValue.x, easingX(t)),
Mathf.LerpUnclamped(startValue.y, endValue.y, easingY(t))
);
}
}
public struct Vector3Interpolator : IInterpolator<Vector3>
{
private Func<float, float> easingX;
private Func<float, float> easingY;
private Func<float, float> easingZ;
public Vector3Interpolator(Func<float, float> easingX, Func<float, float> easingY, Func<float, float> easingZ)
{
this.easingX = easingX;
this.easingY = easingY;
this.easingZ = easingZ;
}
public Vector3Interpolator(Func<float, float> easing)
{
this.easingX = easing;
this.easingY = easing;
this.easingZ = easing;
}
public Vector3 Interpolate(Vector3 startValue, Vector3 endValue, float t)
{
return new Vector3(
Mathf.LerpUnclamped(startValue.x, endValue.x, easingX(t)),
Mathf.LerpUnclamped(startValue.y, endValue.y, easingY(t)),
Mathf.LerpUnclamped(startValue.z, endValue.z, easingZ(t))
);
}
}
public struct QuaternionInterpolator : IInterpolator<Quaternion>
{
private Func<float, float> easing;
public QuaternionInterpolator(Func<float, float> easing)
{
this.easing = easing;
}
public Quaternion Interpolate(Quaternion startValue, Quaternion endValue, float t)
{
return Quaternion.SlerpUnclamped(startValue, endValue, easing(t));
}
}
public struct ColorInterpolator : IInterpolator<Color>
{
private Func<float, float> easingR;
private Func<float, float> easingG;
private Func<float, float> easingB;
private Func<float, float> easingA;
public ColorInterpolator(Func<float, float> easingR, Func<float, float> easingG, Func<float, float> easingB, Func<float, float> easingA)
{
this.easingR = easingR;
this.easingG = easingG;
this.easingB = easingB;
this.easingA = easingA;
}
public ColorInterpolator(Func<float, float> easingRGB, Func<float, float> easingA)
{
this.easingR = easingRGB;
this.easingG = easingRGB;
this.easingB = easingRGB;
this.easingA = easingA;
}
public ColorInterpolator(Func<float, float> easing)
{
this.easingR = easing;
this.easingG = easing;
this.easingB = easing;
this.easingA = easing;
}
public Color Interpolate(Color startValue, Color endValue, float t)
{
return new Color(
Mathf.LerpUnclamped(startValue.r, endValue.r, easingR(t)),
Mathf.LerpUnclamped(startValue.g, endValue.g, easingG(t)),
Mathf.LerpUnclamped(startValue.b, endValue.b, easingB(t)),
Mathf.LerpUnclamped(startValue.a, endValue.a, easingA(t))
);
}
}
}