forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationCurve.ts
256 lines (217 loc) · 7.3 KB
/
AnimationCurve.ts
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
import { PingPong, RepeatSE } from './MathUtil';
import { FrameCache } from './enum/FrameCache';
import { WrapTimeMode } from './enum/WrapTimeMode';
import { Keyframe } from './enum/Keyframe';
/**
* Animation Cureve
* has frame list data
* @group Math
*/
export class AnimationCurve {
private _totalTime: number = 1;
private _cache: FrameCache = new FrameCache();
private _cacheOut: { lhsIndex: number; rhsIndex: number } = {
lhsIndex: 0,
rhsIndex: 0,
};
private _InvalidateCache: boolean = false;
public curve: Keyframe[] = [];
public serializedVersion: number;
public preWarpMode: number;
public postWarpMode: number;
public rotationOrder: number;
constructor(frames?: Keyframe[], preWarpMode: WrapTimeMode = WrapTimeMode.Repeat, postWarpMode: WrapTimeMode = WrapTimeMode.Repeat) {
if (frames) for (let i = 0; i < frames.length; i++) {
const frame = frames[i];
this.addKeyFrame(frame);
}
this.preWarpMode = preWarpMode;
this.postWarpMode = postWarpMode;
}
/**
* return this curve use total time
*/
public get totalTime() {
return this._totalTime;
}
/**
* get curve first keframe time
*/
public get first(): Keyframe {
return this.curve[0];
}
/**
* get curve last keyframe time
*/
public get last(): Keyframe {
return this.curve[this.curve.length - 1];
}
/**
* add keyFrame to curve keyframe last and calcTotalTime
* @param keyFrame {@link Keyframe} sea: one key frame data
*/
public addKeyFrame(keyFrame: Keyframe) {
if (this.curve.indexOf(keyFrame) == -1) {
this.curve.push(keyFrame);
}
this.calcTotalTime();
}
/**
* remove keyframe from this curve
* @param keyFrame {@link Keyframe}
*/
public removeKeyFrame(keyFrame: Keyframe) {
let index = this.curve.indexOf(keyFrame);
if (index != -1) {
this.curve.splice(index, 1);
}
this.calcTotalTime();
}
/**
* calculate keyframe list in to timeline
* @param cache {@link FrameCache}
* @param lhsIndex left frame index
* @param rhsIndex right frame index
* @param timeOffset offset time default 0.0
*/
public calculateCacheData(cache: FrameCache, lhsIndex: number, rhsIndex: number, timeOffset: number = 0) {
let m_Curve = this.curve;
let lhs = m_Curve[lhsIndex];
let rhs = m_Curve[rhsIndex];
// DebugAssertIf (timeOffset < -0.001F || timeOffset - 0.001F > rhs.time - lhs.time);
cache.index = lhsIndex;
cache.time = lhs.time + timeOffset;
cache.timeEnd = rhs.time + timeOffset;
cache.index = lhsIndex;
let dx, length;
let dy;
let m1, m2, d1, d2;
dx = rhs.time - lhs.time;
dx = Math.max(dx, 0.0001);
dy = rhs.value - lhs.value;
length = 1.0 / (dx * dx);
m1 = lhs.outSlope;
m2 = rhs.inSlope;
d1 = m1 * dx;
d2 = m2 * dx;
cache.coeff[0] = ((d1 + d2 - dy - dy) * length) / dx;
cache.coeff[1] = (dy + dy + dy - d1 - d1 - d2) * length;
cache.coeff[2] = m1;
cache.coeff[3] = lhs.value;
this.setupStepped(cache.coeff, lhs, rhs);
}
/**
* get caculate frames value
* @param time
* @returns
*/
public getValue(time: number): number {
time = this.wrapTime(time);
this.findCurve(time, this._cacheOut);
this.calculateCacheData(this._cache, this._cacheOut.lhsIndex, this._cacheOut.rhsIndex, 0);
return this.evaluateCache(this._cache, time);
}
/**
* get has Keyframe list count
* @returns int
*/
public getKeyCount(): number {
return this.curve.length;
}
/**
* Get a Keyframe Data by Index
* @param index must int
* @returns Keyframe {@link Keyframe}
*/
public getKey(index: number): Keyframe {
return this.curve[index];
}
public unSerialized(data: any): this {
this.preWarpMode = data['m_PreInfinity'];
this.postWarpMode = data['m_PostInfinity'];
this.rotationOrder = data['m_RotationOrder'];
let len = data['m_Curve'].length;
for (let i = 0; i < len; i++) {
this.curve[i] = new Keyframe();
this.curve[i].unSerialized(data['m_Curve'][i.toString()]);
}
this.calcTotalTime();
return this;
}
public unSerialized2(data: Object): this {
this.preWarpMode = data['preWrapMode'];
this.postWarpMode = data['postWrapMode'];
let keyFrames = data['keyFrames'] || data['keys'];
let len = keyFrames.length;
for (let i = 0; i < len; i++) {
this.curve[i] = new Keyframe();
this.curve[i].unSerialized2(keyFrames[i.toString()]);
}
this.calcTotalTime();
return this;
}
private wrapTime(curveT: number) {
let m_Curve = this.curve;
let begTime = m_Curve[0].time;
let endTime = m_Curve[m_Curve.length - 1].time;
if (curveT < begTime) {
if (this.preWarpMode == WrapTimeMode.Clamp) curveT = begTime;
else if (this.preWarpMode == WrapTimeMode.PingPong) curveT = PingPong(curveT, begTime, endTime);
else curveT = RepeatSE(curveT, begTime, endTime);
} else if (curveT > endTime) {
if (this.postWarpMode == WrapTimeMode.Clamp) curveT = endTime;
else if (this.postWarpMode == WrapTimeMode.PingPong) curveT = PingPong(curveT, begTime, endTime);
else curveT = RepeatSE(curveT, begTime, endTime);
}
return curveT;
}
private evaluateCache(cache: FrameCache, curveT: number): number {
let t = curveT - cache.time;
let output = t * (t * (t * cache.coeff[0] + cache.coeff[1]) + cache.coeff[2]) + cache.coeff[3];
return output;
}
private findCurve(time: number, out: { lhsIndex: number; rhsIndex: number }) {
let frames = this.curve;
for (let i = 1; i < frames.length; i++) {
let left = frames[i - 1];
let right = frames[i];
if (left.time <= time && right.time > time) {
out.lhsIndex = i - 1;
out.rhsIndex = i;
}
}
}
private setupStepped(coeff: number[], lhs: Keyframe, rhs: Keyframe) {
if (isNaN(lhs.outSlope) || isNaN(rhs.inSlope)) {
coeff[0] = 0.0;
coeff[1] = 0.0;
coeff[2] = 0.0;
coeff[3] = lhs.value;
}
}
private invalidateCache() {
this._InvalidateCache = true;
}
private calcTotalTime() {
let maxTime = 0;
for (let curve of this.curve) {
if (curve) {
maxTime = Math.max(maxTime, curve.time);
} else {
console.error(curve);
}
}
this._totalTime = maxTime;
}
public static scaleCurveValue(curve: AnimationCurve, scale: number) {
if (!curve._InvalidateCache) {
for (let i = 0; i < curve.curve.length; i++) {
let c = curve.curve[i];
c.value *= scale;
c.inSlope *= scale;
c.outSlope *= scale;
}
}
curve.invalidateCache();
}
}