Animation.js 9.3 KB
Newer Older
1 2 3 4
/**
 * @author mikael emtinger / http://gomo.se/
 */

M
Mikael Emtinger 已提交
5
THREE.Animation = function( root, data, interpolationType, JITCompile ) {
M
Mr.doob 已提交
6 7

	this.root = root;
M
Mikael Emtinger 已提交
8 9 10
	this.data = THREE.AnimationHandler.get( data );
	this.hierarchy = THREE.AnimationHandler.parse( root );
	this.currentTime = 0;
11
	this.isPlaying = false;
M
Mikael Emtinger 已提交
12
	this.isPaused = true;
M
Mr.doob 已提交
13
	this.loop = true;
M
Mikael Emtinger 已提交
14 15
	this.interpolationType = interpolationType !== undefined ? interpolationType : THREE.AnimationHandler.LINEAR;
	this.JITCompile = JITCompile !== undefined ? JITCompile : true;
16 17 18 19 20 21
}

/*
 * Play
 */

M
Mikael Emtinger 已提交
22
THREE.Animation.prototype.play = function( loop, startTimeMS ) {
23 24

	if( !this.isPlaying ) {
M
Mr.doob 已提交
25

26
		this.isPlaying = true;
M
Mikael Emtinger 已提交
27 28 29
		this.loop = loop !== undefined ? loop : true;
		this.currentTime = startTimeMS !== undefined ? startTimeMS : 0;

30 31

		// reset key cache
M
Mr.doob 已提交
32 33 34

		for ( var h = 0; h < this.hierarchy.length; h++ ) {

35
			this.hierarchy[ h ].useQuaternion    = true;
36
			this.hierarchy[ h ].matrixAutoUpdate = true;
M
Mr.doob 已提交
37

M
Mikael Emtinger 已提交
38
			if ( this.hierarchy[ h ].animationCache === undefined ) {
39

M
Mikael Emtinger 已提交
40 41 42 43
				this.hierarchy[ h ].animationCache = {};
				this.hierarchy[ h ].animationCache.prevKey = { pos: 0, rot: 0, scl: 0 };
				this.hierarchy[ h ].animationCache.nextKey = { pos: 0, rot: 0, scl: 0 };
				this.hierarchy[ h ].animationCache.originalMatrix = this.hierarchy[ h ] instanceof THREE.Bone ? this.hierarchy[ h ].skinMatrix : this.hierarchy[ h ].matrix;
44
			}
M
Mr.doob 已提交
45 46


M
Mikael Emtinger 已提交
47 48
			var prevKey = this.hierarchy[ h ].animationCache.prevKey;
			var nextKey = this.hierarchy[ h ].animationCache.nextKey;
M
Mr.doob 已提交
49

M
Mikael Emtinger 已提交
50 51 52
			prevKey.pos = this.data.hierarchy[ h ].keys[ 0 ];
			prevKey.rot = this.data.hierarchy[ h ].keys[ 0 ];
			prevKey.scl = this.data.hierarchy[ h ].keys[ 0 ];
M
Mr.doob 已提交
53

M
Mikael Emtinger 已提交
54 55 56 57
			nextKey.pos = this.getNextKeyWith( "pos", h, 1 );
			nextKey.rot = this.getNextKeyWith( "rot", h, 1 );
			nextKey.scl = this.getNextKeyWith( "scl", h, 1 );
		}
58

M
Mikael Emtinger 已提交
59
		this.update( 0 );
60 61
	}

M
Mikael Emtinger 已提交
62 63 64
	this.isPaused = false;

	THREE.AnimationHandler.addToUpdate( this );
65 66 67 68 69 70 71 72
};


/*
 * Pause
 */

THREE.Animation.prototype.pause = function() {
M
Mr.doob 已提交
73

M
Mikael Emtinger 已提交
74 75 76 77 78 79 80 81 82 83 84
	if( this.isPaused ) {
		
		THREE.AnimationHandler.addToUpdate( this );
		
	} else {
		
		THREE.AnimationHandler.removeFromUpdate( this );
		
	}
	
	this.isPaused = !this.isPaused;
85 86 87 88 89 90 91 92
}


/*
 * Stop
 */

THREE.Animation.prototype.stop = function() {
M
Mr.doob 已提交
93

94
	this.isPlaying = false;
M
Mikael Emtinger 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	THREE.AnimationHandler.removeFromUpdate( this );
	
	
	// reset JIT matrix and remove cache
	
	for ( var h = 0; h < this.hierarchy.length; h++ ) {
		
		if ( this.hierarchy[ h ].animationCache !== undefined ) {
			
			if( this.hierarchy[ h ] instanceof THREE.Bone ) {
			
				this.hierarchy[ h ].skinMatrix = this.hierarchy[ h ].animationCache.originalMatrix;
				
			} else {
				
				this.hierarchy[ h ].matrix = this.hierarchy[ h ].animationCache.originalMatrix;
			}
			
			
			delete this.hierarchy[ h ].animationCache;	
		}
	}
 	
118 119 120 121 122 123 124
}


/*
 * Update
 */

M
Mikael Emtinger 已提交
125
THREE.Animation.prototype.update = function( deltaTimeMS ) {
126 127

	// early out
M
Mr.doob 已提交
128

129 130 131 132
	if( !this.isPlaying ) return;


	// vars
M
Mr.doob 已提交
133

134
	var types = [ "pos", "rot", "scl" ];
M
Mikael Emtinger 已提交
135
	var type;
136 137 138
	var scale;
	var vector;
	var prevXYZ, nextXYZ;
M
Mikael Emtinger 已提交
139
	var prevKey, nextKey;
140
	var object;
M
Mikael Emtinger 已提交
141
	var animationCache;
142 143
	var frame;
	var JIThierarchy = this.data.JIT.hierarchy;
M
Mikael Emtinger 已提交
144 145
	var currentTime, unloopedCurrentTime;
	
M
Mr.doob 已提交
146

147
	// update
M
Mikael Emtinger 已提交
148 149
	
	this.currentTime += deltaTimeMS;
M
Mr.doob 已提交
150

M
Mikael Emtinger 已提交
151 152 153
	unloopedCurrentTime = this.currentTime;
	currentTime         = this.currentTime = this.currentTime % this.data.length;
	frame               = parseInt( Math.min( currentTime * this.data.fps, this.data.length * this.data.fps ), 10 );
M
Mr.doob 已提交
154 155


156
	// update
M
Mr.doob 已提交
157 158

	for ( var h = 0, hl = this.hierarchy.length; h < hl; h++ ) {
A
alteredq 已提交
159

160
		object = this.hierarchy[ h ];
M
Mikael Emtinger 已提交
161 162 163 164
		animationCache = object.animationCache;
	
		// use JIT?
	
M
Mikael Emtinger 已提交
165
		if ( this.JITCompile && JIThierarchy[ h ][ frame ] !== undefined ) {
M
Mr.doob 已提交
166

M
Mikael Emtinger 已提交
167 168 169 170 171
			if( object instanceof THREE.Bone ) {
				
				object.skinMatrix = JIThierarchy[ h ][ frame ];
				
				object.matrixAutoUpdate = false;
172
				object.matrixWorldNeedsUpdate = false;
M
Mikael Emtinger 已提交
173 174 175 176 177 178
			}
			else {
			
				object.matrix = JIThierarchy[ h ][ frame ];
				
				object.matrixAutoUpdate = false;
179
				object.matrixWorldNeedsUpdate = true;
M
Mikael Emtinger 已提交
180 181 182 183 184
			}
			
		// use interpolation
	
		} else {
A
alteredq 已提交
185

M
Mikael Emtinger 已提交
186
			// make sure so original matrix and not JIT matrix is set
M
Mr.doob 已提交
187

M
Mikael Emtinger 已提交
188
			if ( this.JITCompile ) {
M
Mikael Emtinger 已提交
189
				
M
Mikael Emtinger 已提交
190 191 192 193 194 195 196 197 198 199
				if( object instanceof THREE.Bone ) {
					
					object.skinMatrix = object.animationCache.originalMatrix;
					
				} else {
					
					object.matrix = object.animationCache.originalMatrix;
					
				}

M
Mikael Emtinger 已提交
200 201 202 203
			}


			// loop through pos/rot/scl
M
Mr.doob 已提交
204 205 206

			for ( var t = 0; t < 3; t++ ) {

207
				// get keys
M
Mr.doob 已提交
208

M
Mikael Emtinger 已提交
209 210 211
				type    = types[ t ];
				prevKey = animationCache.prevKey[ type ];
				nextKey = animationCache.nextKey[ type ];
M
Mr.doob 已提交
212

213
				// switch keys?
A
alteredq 已提交
214

M
Mikael Emtinger 已提交
215
				if ( nextKey.time <= unloopedCurrentTime ) {
M
Mr.doob 已提交
216

217
					// did we loop?
M
Mr.doob 已提交
218

M
Mikael Emtinger 已提交
219
					if ( currentTime <= unloopedCurrentTime ) {
M
Mr.doob 已提交
220 221 222

						if ( this.loop ) {

223 224
							prevKey = this.data.hierarchy[ h ].keys[ 0 ];
							nextKey = this.getNextKeyWith( type, h, 1 );
225

M
Mikael Emtinger 已提交
226 227 228 229 230 231 232
							while( nextKey.time < currentTime ) {
	
								prevKey = nextKey;
								nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
	
							}

233
						} else {
M
Mr.doob 已提交
234

235 236
							this.stop();
							return;
237

238
						}
239 240

					} else {
M
Mr.doob 已提交
241

242
						do {
M
Mr.doob 已提交
243

244 245
							prevKey = nextKey;
							nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
A
alteredq 已提交
246

M
Mr.doob 已提交
247 248
						} while( nextKey.time < currentTime )

249
					}
M
Mr.doob 已提交
250

M
Mikael Emtinger 已提交
251 252
					animationCache.prevKey[ type ] = prevKey;
					animationCache.nextKey[ type ] = nextKey;
253

254
				}
M
Mr.doob 已提交
255 256


257
				object.matrixAutoUpdate = true;
258
				object.matrixWorldNeedsUpdate = true;
M
Mr.doob 已提交
259 260

				scale = ( currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
261 262
				prevXYZ = prevKey[ type ];
				nextXYZ = nextKey[ type ];
M
Mr.doob 已提交
263

264

M
Mikael Emtinger 已提交
265
				// check scale error
M
Mr.doob 已提交
266

M
Mikael Emtinger 已提交
267
				if ( scale < 0 || scale > 1 ) {
M
Mr.doob 已提交
268

M
Mikael Emtinger 已提交
269
					console.log( "THREE.Animation.update: Warning! Scale out of bounds:" + scale + " on bone " + h ); 
M
Mikael Emtinger 已提交
270
					scale = scale < 0 ? 0 : 1;
M
Mr.doob 已提交
271

M
Mikael Emtinger 已提交
272
				}
M
Mr.doob 已提交
273

M
Mikael Emtinger 已提交
274 275 276
				// interpolate

				if ( type === "pos" ) {
277

M
Mikael Emtinger 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
					if( this.interpolationType === THREE.AnimationHandler.LINEAR ) {
						
						vector   = object.position; 
						vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
						vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
						vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;

					} else {
						
						var points = [];
		
						points[ 0 ] = this.getPrevKeyWith( type, h, prevKey.index - 1 )[ type ];
						points[ 1 ] = prevXYZ;
						points[ 2 ] = nextXYZ;
						points[ 3 ] = this.getNextKeyWith( type, h, nextKey.index + 1 )[ type ];

						scale = scale * 0.33 + 0.33;

						var result = this.interpolateCatmullRom( points, scale );
						
						object.position.x = result[ 0 ];
						object.position.y = result[ 1 ];
						object.position.z = result[ 2 ];
					}
302
				}
M
Mikael Emtinger 已提交
303
				else if ( type === "rot" ) {
M
Mr.doob 已提交
304

M
Mikael Emtinger 已提交
305
					THREE.Quaternion.slerp( prevXYZ, nextXYZ, object.quaternion, scale );
306

M
Mikael Emtinger 已提交
307 308
				}
				else if( type === "scl" ) {
M
Mr.doob 已提交
309

M
Mikael Emtinger 已提交
310
					vector   = object.scale; 
311 312 313
					vector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
					vector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
					vector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
314

315
				}
A
alteredq 已提交
316 317 318

			}

319
		}
A
alteredq 已提交
320

321
	}
M
Mr.doob 已提交
322

323
	// update JIT?
M
Mr.doob 已提交
324

M
Mikael Emtinger 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	if ( this.JITCompile ) {
		
		if ( JIThierarchy[ 0 ][ frame ] === undefined ) {
	
			this.hierarchy[ 0 ].update( undefined, true );
	
			for ( var h = 0; h < this.hierarchy.length; h++ ) {
	
				if( this.hierarchy[ h ] instanceof THREE.Bone ) {
	
					JIThierarchy[ h ][ frame ] = this.hierarchy[ h ].skinMatrix.clone();
					
				} else {
	
					JIThierarchy[ h ][ frame ] = this.hierarchy[ h ].matrix.clone();
				
				}
	
			}
	
M
Mr.doob 已提交
345 346
		}

347 348 349 350 351
	}

};


M
Mikael Emtinger 已提交
352 353 354 355 356 357 358
/**
 * Spline from Tween.js, slightly optimized
 * Modified to fit to THREE.Animation.js
 * 
 * http://sole.github.com/tween.js/examples/05_spline.html
 *
 * @author mrdoob / http://mrdoob.com/
M
Mr.doob 已提交
359
 */
A
alteredq 已提交
360

M
Mikael Emtinger 已提交
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
 
THREE.Animation.prototype.interpolateCatmullRom = function ( points, scale ) {

	var c = [], v3 = [],
	point, intPoint, weight, w2, w3,
	pa, pb, pc, pd;
	
	point = ( points.length - 1 ) * scale;
	intPoint = Math.floor( point );
	weight = point - intPoint;

	c[ 0 ] = intPoint == 0 ? intPoint : intPoint - 1;
	c[ 1 ] = intPoint;
	c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1;
	c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2;

	pa = points[ c[ 0 ] ];
	pb = points[ c[ 1 ] ];
	pc = points[ c[ 2 ] ];
	pd = points[ c[ 3 ] ];

	w2 = weight * weight;
	w3 = weight * w2;
	
	v3[ 0 ] = this.interpolate( pa[ 0 ], pb[ 0 ], pc[ 0 ], pd[ 0 ], weight, w2, w3 );
	v3[ 1 ] = this.interpolate( pa[ 1 ], pb[ 1 ], pc[ 1 ], pd[ 1 ], weight, w2, w3 );
	v3[ 2 ] = this.interpolate( pa[ 2 ], pb[ 2 ], pc[ 2 ], pd[ 2 ], weight, w2, w3 );
	
	return v3;
}
391 392


M
Mikael Emtinger 已提交
393 394 395 396 397 398 399 400
THREE.Animation.prototype.interpolate = function( p0, p1, p2, p3, t, t2, t3 ) {

	var v0 = ( p2 - p0 ) * 0.5,
		v1 = ( p3 - p1 ) * 0.5;

	return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
}

401

M
Mikael Emtinger 已提交
402 403 404 405 406 407
/*
 * Get next key with
 */

THREE.Animation.prototype.getNextKeyWith = function( type, h, key ) {
	
M
Mr.doob 已提交
408
	var keys = this.data.hierarchy[ h ].keys;
M
Mikael Emtinger 已提交
409
	key      = key % keys.length;
410

M
Mr.doob 已提交
411
	for ( ; key < keys.length; key++ ) {
412

M
Mr.doob 已提交
413
		if ( keys[ key ][ type ] !== undefined ) {
414

M
Mr.doob 已提交
415
			return keys[ key ];
416

M
Mr.doob 已提交
417
		}
418

M
Mr.doob 已提交
419
	}
420

M
Mr.doob 已提交
421
	return this.data.hierarchy[ h ].keys[ 0 ];
422

M
Mr.doob 已提交
423
}
M
Mikael Emtinger 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

THREE.Animation.prototype.getPrevKeyWith = function( type, h, key ) {
	
	var keys = this.data.hierarchy[ h ].keys;
	key      = key >= 0 ? key : key + keys.length;

	for ( ; key >= 0; key-- ) {

		if ( keys[ key ][ type ] !== undefined ) {

			return keys[ key ];

		}

	}

	return this.data.hierarchy[ h ].keys[ keys.length - 1 ];
}