/
animation-clip.ts
414 lines (351 loc) · 11.3 KB
/
animation-clip.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* @packageDocumentation
* @module animation
*/
import { EDITOR } from 'internal:constants';
import { Asset } from '../assets/asset';
import { SpriteFrame } from '../assets/sprite-frame';
import { ccclass, serializable } from 'cc.decorator';
import { CompactValueTypeArray } from '../data/utils/compact-value-type-array';
import { errorID } from '../platform/debug';
import { DataPoolManager } from '../renderer/data-pool-manager';
import binarySearchEpsilon from '../utils/binary-search';
import { murmurhash2_32_gc } from '../utils/murmurhash2_gc';
import { AnimCurve, IPropertyCurveData, RatioSampler } from './animation-curve';
import { SkelAnimDataHub } from './skeletal-animation-data-hub';
import { ComponentPath, HierarchyPath, TargetPath } from './target-path';
import { WrapMode as AnimationWrapMode } from './types';
import { IValueProxyFactory } from './value-proxy';
import { legacyCC } from '../global-exports';
export interface IObjectCurveData {
[propertyName: string]: IPropertyCurveData;
}
export interface IComponentsCurveData {
[componentName: string]: IObjectCurveData;
}
export interface INodeCurveData {
props?: IObjectCurveData;
comps?: IComponentsCurveData;
}
export type IRuntimeCurve = Pick<AnimationClip.ICurve, 'modifiers' | 'valueAdapter' | 'commonTarget'> & {
/**
* 属性曲线。
*/
curve: AnimCurve;
/**
* 曲线采样器。
*/
sampler: RatioSampler | null;
};
export interface IAnimationEvent {
functionName: string;
parameters: string[];
}
export interface IAnimationEventGroup {
events: IAnimationEvent[];
}
export declare namespace AnimationClip {
export type PropertyCurveData = IPropertyCurveData;
export interface ICurve {
commonTarget?: number;
modifiers: TargetPath[];
valueAdapter?: IValueProxyFactory;
data: PropertyCurveData;
}
export interface ICommonTarget {
modifiers: TargetPath[];
valueAdapter?: IValueProxyFactory;
}
export interface IEvent {
frame: number;
func: string;
params: string[];
}
export namespace _impl {
type MaybeCompactCurve = Omit<AnimationClip.ICurve, 'data'> & {
data: Omit<AnimationClip.PropertyCurveData, 'values'> & {
values: any[] | CompactValueTypeArray;
};
};
type MaybeCompactKeys = Array<number[] | CompactValueTypeArray>;
}
}
/**
* @zh 动画剪辑表示一段使用动画编辑器编辑的关键帧动画或是外部美术工具生产的骨骼动画。
* 它的数据主要被分为几层:轨道、关键帧和曲线。
* @en The animation clip represents a sequence of key frame animation created with the animation editor or skeletal animation other DCC tools.
* The data is divided in different levels: tracks, key frames, curves.
*/
@ccclass('cc.AnimationClip')
export class AnimationClip extends Asset {
public static preventDeferredLoadDependents = true;
public static WrapMode = AnimationWrapMode;
/**
* @en Crate clip with a set of sprite frames
* @zh 使用一组序列帧图片来创建动画剪辑
* @example
* ```
* import { AnimationClip } from 'cc';
* const clip = AnimationClip.createWithSpriteFrames(spriteFrames, 10);
* ```
*/
public static createWithSpriteFrames (spriteFrames: SpriteFrame[], sample: number) {
if (!Array.isArray(spriteFrames)) {
errorID(3905);
return null;
}
const clip = new AnimationClip();
clip.sample = sample || clip.sample;
clip.duration = spriteFrames.length / clip.sample;
const step = 1 / clip.sample;
const keys = new Array<number>(spriteFrames.length);
const values = new Array<SpriteFrame>(keys.length);
for (let i = 0; i < spriteFrames.length; i++) {
keys[i] = i * step;
values[i] = spriteFrames[i];
}
clip.keys = [keys];
clip.curves = [{
modifiers: [
new ComponentPath('cc.Sprite'),
'spriteFrame',
],
data: {
keys: 0,
values,
},
}];
return clip;
}
/**
* @zh 动画帧率,单位为帧/秒。注意此属性仅用于编辑器动画编辑。
* @en Animation frame rate: frames per second.
* Note this property is only used for animation editing in Editor.
*/
@serializable
public sample = 60;
/**
* @zh 动画的播放速度。
* @en Animation playback speed.
*/
@serializable
public speed = 1;
/**
* @zh 动画的循环模式。
* @en Animation loop mode.
*/
@serializable
public wrapMode = AnimationWrapMode.Normal;
/**
* @zh 动画包含的事件数据。
* @en Associated event data.
*/
@serializable
public events: AnimationClip.IEvent[] = [];
@serializable
private _duration = 0;
@serializable
private _keys: number[][] = [];
@serializable
private _stepness = 0;
@serializable
private _curves: AnimationClip.ICurve[] = [];
@serializable
private _commonTargets: AnimationClip.ICommonTarget[] = [];
@serializable
private _hash = 0;
private frameRate = 0;
private _ratioSamplers: RatioSampler[] = [];
private _runtimeCurves?: IRuntimeCurve[];
private _runtimeEvents?: {
ratios: number[];
eventGroups: IAnimationEventGroup[];
};
private _data: Uint8Array | null = null;
/**
* @zh 动画的周期。
* @en Animation duration.
*/
get duration () {
return this._duration;
}
set duration (value) {
this._duration = value;
}
/**
* @zh 曲线可引用的所有时间轴。
* @en Frame keys referenced by curves.
*/
get keys () {
return this._keys;
}
set keys (value) {
this._keys = value;
}
/**
* @protected
*/
get eventGroups (): readonly IAnimationEventGroup[] {
if (!this._runtimeEvents) {
this._createRuntimeEvents();
}
return this._runtimeEvents!.eventGroups;
}
/**
* @protected
*/
get stepness () {
return this._stepness;
}
/**
* @protected
*/
set stepness (value) {
this._stepness = value;
this._applyStepness();
}
get hash () {
// hashes should already be computed offline, but if not, make one
if (this._hash) { return this._hash; }
const data = this._nativeAsset;
const buffer = new Uint8Array(ArrayBuffer.isView(data) ? data.buffer : data);
return this._hash = murmurhash2_32_gc(buffer, 666);
}
get curves () {
return this._curves;
}
set curves (value) {
this._curves = value;
delete this._runtimeCurves;
}
/**
* 此动画的数据。
*/
get data () {
return this._data;
}
get commonTargets () {
return this._commonTargets;
}
set commonTargets (value) {
this._commonTargets = value;
}
public onLoaded () {
this.frameRate = this.sample;
this._decodeCVTAs();
}
public getPropertyCurves (): readonly IRuntimeCurve[] {
if (!this._runtimeCurves) {
this._createPropertyCurves();
}
return this._runtimeCurves!;
}
/**
* @zh 提交事件数据的修改。
* 当你修改了 `this.events` 时,必须调用 `this.updateEventDatas()` 使修改生效。
* @en
* Commit event data update.
* You should call this function after you changed the `events` data to take effect.
* @internal
*/
public updateEventDatas () {
delete this._runtimeEvents;
}
/**
* @en Gets the event group shall be processed at specified ratio.
* @zh 获取事件组应按指定比例处理。
* @param ratio The ratio.
* @internal
*/
public getEventGroupIndexAtRatio (ratio: number): number {
if (!this._runtimeEvents) {
this._createRuntimeEvents();
}
const result = binarySearchEpsilon(this._runtimeEvents!.ratios, ratio);
return result;
}
/**
* @zh 返回本动画是否包含事件数据。
* @en Returns if this animation contains event data.
* @protected
*/
public hasEvents () {
return this.events.length !== 0;
}
public destroy () {
(legacyCC.director.root.dataPoolManager as DataPoolManager).releaseAnimationClip(this);
SkelAnimDataHub.destroy(this);
return super.destroy();
}
protected _createPropertyCurves () {
this._ratioSamplers = this._keys.map(
(keys) => new RatioSampler(
keys.map(
(key) => key / this._duration)));
this._runtimeCurves = this._curves.map((targetCurve): IRuntimeCurve => {
return {
curve: new AnimCurve(targetCurve.data, this._duration),
modifiers: targetCurve.modifiers,
valueAdapter: targetCurve.valueAdapter,
sampler: this._ratioSamplers[targetCurve.data.keys],
commonTarget: targetCurve.commonTarget,
};
});
this._applyStepness();
}
protected _createRuntimeEvents () {
if (EDITOR) {
return;
}
const ratios: number[] = [];
const eventGroups: IAnimationEventGroup[] = [];
const events = this.events.sort((a, b) => a.frame - b.frame);
for (const eventData of events) {
const ratio = eventData.frame / this._duration;
let i = ratios.findIndex((r) => r === ratio);
if (i < 0) {
i = ratios.length;
ratios.push(ratio);
eventGroups.push({
events: [],
});
}
eventGroups[i].events.push({
functionName: eventData.func,
parameters: eventData.params,
});
}
this._runtimeEvents = {
ratios,
eventGroups,
};
}
protected _applyStepness () {
if (!this._runtimeCurves) {
return;
}
// for (const propertyCurve of this._propertyCurves) {
// propertyCurve.curve.stepfy(this._stepness);
// }
}
private _decodeCVTAs () {
const binaryBuffer: ArrayBuffer = ArrayBuffer.isView(this._nativeAsset) ? this._nativeAsset.buffer : this._nativeAsset;
if (!binaryBuffer) {
return;
}
const maybeCompressedKeys = this._keys as AnimationClip._impl.MaybeCompactKeys;
for (let iKey = 0; iKey < maybeCompressedKeys.length; ++iKey) {
const keys = maybeCompressedKeys[iKey];
if (keys instanceof CompactValueTypeArray) {
maybeCompressedKeys[iKey] = keys.decompress(binaryBuffer);
}
}
for (let iCurve = 0; iCurve < this._curves.length; ++iCurve) {
const curve = this._curves[iCurve] as AnimationClip._impl.MaybeCompactCurve;
if (curve.data.values instanceof CompactValueTypeArray) {
curve.data.values = curve.data.values.decompress(binaryBuffer);
}
}
}
}
legacyCC.AnimationClip = AnimationClip;