-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlendCharacter.js
executable file
·259 lines (150 loc) · 4.72 KB
/
BlendCharacter.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
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
/**
* @author Michael Guerrero / http://realitymeltdown.com
*/
THREE.BlendCharacter = function () {
this.animations = {};
this.weightSchedule = [];
this.warpSchedule = [];
this.load = function ( url, onLoad ) {
var scope = this;
var loader = new THREE.JSONLoader();
loader.load( url, function( geometry, materials ) {
var originalMaterial = materials[ 0 ];
originalMaterial.skinning = true;
THREE.SkinnedMesh.call( scope, geometry, originalMaterial );
// Create the animations
for ( var i = 0; i < geometry.animations.length; ++ i ) {
var animName = geometry.animations[ i ].name;
scope.animations[ animName ] = new THREE.Animation( scope, geometry.animations[ i ] );
}
// Loading is complete, fire the callback
if ( onLoad !== undefined ) onLoad();
} );
};
this.update = function( dt ) {
for ( var i = this.weightSchedule.length - 1; i >= 0; -- i ) {
var data = this.weightSchedule[ i ];
data.timeElapsed += dt;
// If the transition is complete, remove it from the schedule
if ( data.timeElapsed > data.duration ) {
data.anim.weight = data.endWeight;
this.weightSchedule.splice( i, 1 );
// If we've faded out completely, stop the animation
if ( data.anim.weight == 0 ) {
data.anim.stop( 0 );
}
} else {
// interpolate the weight for the current time
data.anim.weight = data.startWeight + ( data.endWeight - data.startWeight ) * data.timeElapsed / data.duration;
}
}
this.updateWarps( dt );
};
this.updateWarps = function( dt ) {
// Warping modifies the time scale over time to make 2 animations of different
// lengths match. This is useful for smoothing out transitions that get out of
// phase such as between a walk and run cycle
for ( var i = this.warpSchedule.length - 1; i >= 0; -- i ) {
var data = this.warpSchedule[ i ];
data.timeElapsed += dt;
if ( data.timeElapsed > data.duration ) {
data.to.weight = 1;
data.to.timeScale = 1;
data.from.weight = 0;
data.from.timeScale = 1;
data.from.stop( 0 );
this.warpSchedule.splice( i, 1 );
} else {
var alpha = data.timeElapsed / data.duration;
var fromLength = data.from.data.length;
var toLength = data.to.data.length;
var fromToRatio = fromLength / toLength;
var toFromRatio = toLength / fromLength;
// scale from each time proportionally to the other animation
data.from.timeScale = ( 1 - alpha ) + fromToRatio * alpha;
data.to.timeScale = alpha + toFromRatio * ( 1 - alpha );
data.from.weight = 1 - alpha;
data.to.weight = alpha;
}
}
};
this.play = function( animName, weight ) {
this.animations[ animName ].play( 0, weight );
};
this.crossfade = function( fromAnimName, toAnimName, duration ) {
var fromAnim = this.animations[ fromAnimName ];
var toAnim = this.animations[ toAnimName ];
fromAnim.play( 0, 1 );
toAnim.play( 0, 0 );
this.weightSchedule.push( {
anim: fromAnim,
startWeight: 1,
endWeight: 0,
timeElapsed: 0,
duration: duration
} );
this.weightSchedule.push( {
anim: toAnim,
startWeight: 0,
endWeight: 1,
timeElapsed: 0,
duration: duration
} );
};
this.warp = function( fromAnimName, toAnimName, duration ) {
var fromAnim = this.animations[ fromAnimName ];
var toAnim = this.animations[ toAnimName ];
fromAnim.play( 0, 1 );
toAnim.play( 0, 0 );
this.warpSchedule.push( {
from: fromAnim,
to: toAnim,
timeElapsed: 0,
duration: duration
} );
};
this.applyWeight = function( animName, weight ) {
this.animations[ animName ].weight = weight;
};
this.pauseAll = function() {
for ( var a in this.animations ) {
if ( this.animations[ a ].isPlaying ) {
this.animations[ a ].stop();
}
}
};
this.unPauseAll = function() {
for ( var a in this.animations ) {
if ( this.animations[ a ].isPlaying && this.animations[ a ].isPaused ) {
this.animations[ a ].pause();
}
}
};
this.stopAll = function() {
for ( a in this.animations ) {
if ( this.animations[ a ].isPlaying ) {
this.animations[ a ].stop( 0 );
}
this.animations[ a ].weight = 0;
}
this.weightSchedule.length = 0;
this.warpSchedule.length = 0;
};
this.showModel = function( boolean ) {
this.visible = boolean;
}
};
THREE.BlendCharacter.prototype = Object.create( THREE.SkinnedMesh.prototype );
THREE.BlendCharacter.prototype.constructor = THREE.BlendCharacter;
THREE.BlendCharacter.prototype.getForward = function() {
var forward = new THREE.Vector3();
return function() {
// pull the character's forward basis vector out of the matrix
forward.set(
- this.matrix.elements[ 8 ],
- this.matrix.elements[ 9 ],
- this.matrix.elements[ 10 ]
);
return forward;
}
};