forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskeleton.js
More file actions
384 lines (340 loc) · 13.7 KB
/
Copy pathskeleton.js
File metadata and controls
384 lines (340 loc) · 13.7 KB
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
Object.assign(pc, function () {
function InterpolatedKey() {
this._written = false;
this._name = "";
this._keyFrames = [];
// Result of interpolation
this._quat = new pc.Quat();
this._pos = new pc.Vec3();
this._scale = new pc.Vec3();
// Optional destination for interpolated keyframe
this._targetNode = null;
}
Object.assign(InterpolatedKey.prototype, {
getTarget: function () {
return this._targetNode;
},
setTarget: function (node) {
this._targetNode = node;
}
});
/**
* @constructor
* @name pc.Skeleton
* @classdesc Represents a skeleton used to play animations.
* @param {pc.GraphNode} graph The root pc.GraphNode of the skeleton.
* @property {Boolean} looping Determines whether skeleton is looping its animation.
*/
var Skeleton = function Skeleton(graph) {
this._animation = null;
this._time = 0;
this.looping = true;
this._interpolatedKeys = [];
this._interpolatedKeyDict = {};
this._currKeyIndices = {};
this.graph = null;
var self = this;
function addInterpolatedKeys(node) {
var interpKey = new InterpolatedKey();
interpKey._name = node.name;
self._interpolatedKeys.push(interpKey);
self._interpolatedKeyDict[node.name] = interpKey;
self._currKeyIndices[node.name] = 0;
for (var i = 0; i < node._children.length; i++)
addInterpolatedKeys(node._children[i]);
}
addInterpolatedKeys(graph);
};
/**
* @function
* @name pc.Skeleton#addTime
* @description Progresses the animation assigned to the specified skeleton by the
* supplied time delta. If the delta takes the animation passed its end point, if
* the skeleton is set to loop, the animation will continue from the beginning.
* Otherwise, the animation's current time will remain at its duration (i.e. the
* end).
* @param {Number} delta The time in seconds to progress the skeleton's animation.
*/
Skeleton.prototype.addTime = function (delta) {
if (this._animation !== null) {
var i;
var node, nodeName;
var keys, interpKey;
var k1, k2, alpha;
var nodes = this._animation._nodes;
var duration = this._animation.duration;
// Check if we can early out
if ((this._time === duration) && !this.looping) {
return;
}
// Step the current time and work out if we need to jump ahead, clamp or wrap around
this._time += delta;
if (this._time > duration) {
this._time = this.looping ? 0.0 : duration;
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
nodeName = node._name;
this._currKeyIndices[nodeName] = 0;
}
} else if (this._time < 0) {
this._time = this.looping ? duration : 0.0;
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
nodeName = node._name;
this._currKeyIndices[nodeName] = node._keys.length - 2;
}
}
// For each animated node...
// keys index offset
var offset = (delta >= 0 ? 1 : -1);
var foundKey;
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
nodeName = node._name;
keys = node._keys;
// Determine the interpolated keyframe for this animated node
interpKey = this._interpolatedKeyDict[nodeName];
// If there's only a single key, just copy the key to the interpolated key...
foundKey = false;
if (keys.length !== 1) {
// Otherwise, find the keyframe pair for this node
for (var currKeyIndex = this._currKeyIndices[nodeName]; currKeyIndex < keys.length - 1 && currKeyIndex >= 0; currKeyIndex += offset) {
k1 = keys[currKeyIndex];
k2 = keys[currKeyIndex + 1];
if ((k1.time <= this._time) && (k2.time >= this._time)) {
alpha = (this._time - k1.time) / (k2.time - k1.time);
interpKey._pos.lerp(k1.position, k2.position, alpha);
interpKey._quat.slerp(k1.rotation, k2.rotation, alpha);
interpKey._scale.lerp(k1.scale, k2.scale, alpha);
interpKey._written = true;
this._currKeyIndices[nodeName] = currKeyIndex;
foundKey = true;
break;
}
}
}
if (keys.length === 1 || (!foundKey && this._time === 0.0 && this.looping)) {
interpKey._pos.copy(keys[0].position);
interpKey._quat.copy(keys[0].rotation);
interpKey._scale.copy(keys[0].scale);
interpKey._written = true;
}
}
}
};
/**
* @function
* @name pc.Skeleton#blend
* @description Blends two skeletons together.
* @param {pc.Skeleton} skel1 Skeleton holding the first pose to be blended.
* @param {pc.Skeleton} skel2 Skeleton holding the second pose to be blended.
* @param {Number} alpha The value controlling the interpolation in relation to the two input
* skeletons. The value is in the range 0 to 1, 0 generating skel1, 1 generating skel2 and anything
* in between generating a spherical interpolation between the two.
*/
Skeleton.prototype.blend = function (skel1, skel2, alpha) {
var numNodes = this._interpolatedKeys.length;
for (var i = 0; i < numNodes; i++) {
var key1 = skel1._interpolatedKeys[i];
var key2 = skel2._interpolatedKeys[i];
var dstKey = this._interpolatedKeys[i];
if (key1._written && key2._written) {
dstKey._quat.slerp(key1._quat, skel2._interpolatedKeys[i]._quat, alpha);
dstKey._pos.lerp(key1._pos, skel2._interpolatedKeys[i]._pos, alpha);
dstKey._scale.lerp(key1._scale, key2._scale, alpha);
dstKey._written = true;
} else if (key1._written) {
dstKey._quat.copy(key1._quat);
dstKey._pos.copy(key1._pos);
dstKey._scale.copy(key1._scale);
dstKey._written = true;
} else if (key2._written) {
dstKey._quat.copy(key2._quat);
dstKey._pos.copy(key2._pos);
dstKey._scale.copy(key2._scale);
dstKey._written = true;
}
}
};
/**
* @name pc.Skeleton#animation
* @type pc.Animation
* @description Animation currently assigned to skeleton.
*/
Object.defineProperty(Skeleton.prototype, 'animation', {
get: function () {
return this._animation;
},
set: function (value) {
this._animation = value;
this.currentTime = 0;
}
});
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#getAnimation
* @description Returns the animation currently assigned to the specified skeleton.
* @returns {pc.Animation} The animation set on the skeleton.
*/
Skeleton.prototype.getAnimation = function () {
return this._animation;
};
/**
* @name pc.Skeleton#currentTime
* @type Number
* @description Current time of currently active animation in seconds.
* This value is between zero and the duration of the animation.
*/
Object.defineProperty(Skeleton.prototype, 'currentTime', {
get: function () {
return this._time;
},
set: function (value) {
this._time = value;
var numNodes = this._interpolatedKeys.length;
for (var i = 0; i < numNodes; i++) {
var node = this._interpolatedKeys[i];
var nodeName = node._name;
this._currKeyIndices[nodeName] = 0;
}
this.addTime(0);
this.updateGraph();
}
});
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#getCurrentTime
* @description Returns the current time of the currently active animation as set on
* the specified skeleton. This value will be between zero and the duration of the
* animation.
* @returns {Number} The current time of the animation set on the skeleton.
*/
Skeleton.prototype.getCurrentTime = function () {
return this._time;
};
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#setCurrentTime
* @description Sets the current time of the currently active animation as set on
* the specified skeleton. This value must be between zero and the duration of the
* animation.
* @param {Number} time The current time of the animation set on the skeleton.
*/
Skeleton.prototype.setCurrentTime = function (time) {
this.currentTime = time;
};
/**
* @readonly
* @name pc.Skeleton#numNodes
* @type Number
* @description Read-only property that returns number of nodes of a skeleton.
*/
Object.defineProperty(Skeleton.prototype, 'numNodes', {
get: function () {
return this._interpolatedKeys.length;
}
});
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#getNumNodes
* @description Returns the number of nodes held by the specified skeleton.
* @returns {Number} The number of nodes held by the specified skeleton.
*/
Skeleton.prototype.getNumNodes = function () {
return this._interpolatedKeys.length;
};
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#setAnimation
* @description Sets an animation on the specified skeleton.
* @param {pc.Animation} animation The animation to set on the skeleton.
*/
Skeleton.prototype.setAnimation = function (animation) {
this.animation = animation;
};
/**
* @function
* @name pc.Skeleton#setGraph
* @description Links a skeleton to a node hierarchy. The nodes animated skeleton are
* then subsequently used to drive the local transformation matrices of the node
* hierarchy.
* @param {pc.GraphNode} graph The root node of the graph that the skeleton is to drive.
*/
Skeleton.prototype.setGraph = function (graph) {
var i;
this.graph = graph;
if (graph) {
for (i = 0; i < this._interpolatedKeys.length; i++) {
var interpKey = this._interpolatedKeys[i];
var graphNode = graph.findByName(interpKey._name);
this._interpolatedKeys[i].setTarget(graphNode);
}
} else {
for (i = 0; i < this._interpolatedKeys.length; i++) {
this._interpolatedKeys[i].setTarget(null);
}
}
};
/**
* @function
* @name pc.Skeleton#updateGraph
* @description Synchronizes the currently linked node hierarchy with the current state of the
* skeleton. Internally, this function converts the interpolated keyframe at each node in the
* skeleton into the local transformation matrix at each corresponding node in the linked node
* hierarchy.
*/
Skeleton.prototype.updateGraph = function () {
if (this.graph) {
for (var i = 0; i < this._interpolatedKeys.length; i++) {
var interpKey = this._interpolatedKeys[i];
if (interpKey._written) {
var transform = interpKey.getTarget();
transform.localPosition.copy(interpKey._pos);
transform.localRotation.copy(interpKey._quat);
transform.localScale.copy(interpKey._scale);
if (!transform._dirtyLocal)
transform._dirtify(true);
interpKey._written = false;
}
}
}
};
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#setLooping
* @description Specified whether a skeleton should loop its animation or not. If the animation
* loops, it will wrap back to the start when adding time to the skeleton beyond the duration
* of the animation. Otherwise, the animation stops at its end after a single play through.
* @param {Boolean} looping True to cause the animation to loop back to the start on completion
* and false otherwise.
*/
Skeleton.prototype.setLooping = function (looping) {
this.looping = looping;
};
/**
* @private
* @deprecated
* @function
* @name pc.Skeleton#getLooping
* @description Queries the specified skeleton to determine whether it is looping its animation.
* @returns {Boolean} True if the skeleton is looping the animation, false otherwise.
*/
Skeleton.prototype.getLooping = function () {
return this.looping;
};
return {
Skeleton: Skeleton
};
}());