-
Notifications
You must be signed in to change notification settings - Fork 304
/
animation-mixer.js
154 lines (134 loc) · 4.57 KB
/
animation-mixer.js
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
const LoopMode = {
once: THREE.LoopOnce,
repeat: THREE.LoopRepeat,
pingpong: THREE.LoopPingPong
};
/**
* animation-mixer
*
* Player for animation clips. Intended to be compatible with any model format that supports
* skeletal or morph animations through THREE.AnimationMixer.
* See: https://threejs.org/docs/?q=animation#Reference/Animation/AnimationMixer
*/
module.exports = AFRAME.registerComponent('animation-mixer', {
schema: {
clip: { default: '*' },
useRegExp: {default: false},
duration: { default: 0 },
clampWhenFinished: { default: false, type: 'boolean' },
crossFadeDuration: { default: 0 },
loop: { default: 'repeat', oneOf: Object.keys(LoopMode) },
repetitions: { default: Infinity, min: 0 },
timeScale: { default: 1 },
startAt: { default: 0 }
},
init: function () {
/** @type {THREE.Mesh} */
this.model = null;
/** @type {THREE.AnimationMixer} */
this.mixer = null;
/** @type {Array<THREE.AnimationAction>} */
this.activeActions = [];
const model = this.el.getObject3D('mesh');
if (model) {
this.load(model);
} else {
this.el.addEventListener('model-loaded', (e) => {
this.load(e.detail.model);
});
}
},
load: function (model) {
const el = this.el;
this.model = model;
this.mixer = new THREE.AnimationMixer(model);
this.mixer.addEventListener('loop', (e) => {
el.emit('animation-loop', { action: e.action, loopDelta: e.loopDelta });
});
this.mixer.addEventListener('finished', (e) => {
el.emit('animation-finished', { action: e.action, direction: e.direction });
});
if (this.data.clip) this.update({});
},
remove: function () {
if (this.mixer) this.mixer.stopAllAction();
},
update: function (prevData) {
if (!prevData) return;
const data = this.data;
const changes = AFRAME.utils.diff(data, prevData);
// If selected clips have changed, restart animation.
if ('clip' in changes) {
this.stopAction();
if (data.clip) this.playAction();
return;
}
// Otherwise, modify running actions.
this.activeActions.forEach((action) => {
if ('duration' in changes && data.duration) {
action.setDuration(data.duration);
}
if ('clampWhenFinished' in changes) {
action.clampWhenFinished = data.clampWhenFinished;
}
if ('loop' in changes || 'repetitions' in changes) {
action.setLoop(LoopMode[data.loop], data.repetitions);
}
if ('timeScale' in changes) {
action.setEffectiveTimeScale(data.timeScale);
}
});
},
stopAction: function () {
const data = this.data;
for (let i = 0; i < this.activeActions.length; i++) {
data.crossFadeDuration
? this.activeActions[i].fadeOut(data.crossFadeDuration)
: this.activeActions[i].stop();
}
this.activeActions.length = 0;
},
playAction: function () {
if (!this.mixer) return;
const model = this.model,
data = this.data,
clips = model.animations || (model.geometry || {}).animations || [];
if (!clips.length) return;
const re = data.useRegExp ? data.clip : wildcardToRegExp(data.clip);
for (let clip, i = 0; (clip = clips[i]); i++) {
if (clip.name.match(re)) {
const action = this.mixer.clipAction(clip, model);
action.enabled = true;
action.clampWhenFinished = data.clampWhenFinished;
if (data.duration) action.setDuration(data.duration);
if (data.timeScale !== 1) action.setEffectiveTimeScale(data.timeScale);
// animation-mixer.startAt and AnimationAction.startAt have very different meanings.
// animation-mixer.startAt indicates which frame in the animation to start at, in msecs.
// AnimationAction.startAt indicates when to start the animation (from the 1st frame),
// measured in global mixer time, in seconds.
action.startAt(this.mixer.time - data.startAt / 1000);
action
.setLoop(LoopMode[data.loop], data.repetitions)
.fadeIn(data.crossFadeDuration)
.play();
this.activeActions.push(action);
}
}
},
tick: function (t, dt) {
if (this.mixer && !isNaN(dt)) this.mixer.update(dt / 1000);
}
});
/**
* Creates a RegExp from the given string, converting asterisks to .* expressions,
* and escaping all other characters.
*/
function wildcardToRegExp(s) {
return new RegExp('^' + s.split(/\*+/).map(regExpEscape).join('.*') + '$');
}
/**
* RegExp-escapes all characters in the given string.
*/
function regExpEscape(s) {
return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}