Browse Source

KeyframeTrack.trim( start, end ). Return this if possible. Use fps in ObjectLoader. Add KeyframeTrack.scale()

Ben Houston 10 years ago
parent
commit
d69840c682

+ 5 - 1
src/animation/AnimationClip.js

@@ -52,10 +52,12 @@ THREE.AnimationClip.prototype = {
 
 		for( var i = 0; i < this.tracks.length; i ++ ) {
 
-			this.tracks[ i ].trim( this.duration );
+			this.tracks[ i ].trim( 0, this.duration );
 
 		}
 
+		return this;
+
 	},
 
 	optimize: function() {
@@ -66,6 +68,8 @@ THREE.AnimationClip.prototype = {
 
 		}
 
+		return this;
+
 	}
 
 };

+ 18 - 0
src/animation/AnimationMixer.js

@@ -105,6 +105,8 @@ THREE.AnimationMixer.prototype = {
 		this.actions = [];
 		this.propertyBindings = [];
 
+		return this;
+
 	},
 
 	removeAction: function( action ) {
@@ -140,6 +142,8 @@ THREE.AnimationMixer.prototype = {
 
 		this.updatePropertyBindingIndices();
 
+		return this;
+
 	},
 
 	play: function( action, optionalFadeInDuration ) {
@@ -147,6 +151,8 @@ THREE.AnimationMixer.prototype = {
 		action.startTime = this.time;
 		this.addAction( action );
 
+		return this;
+
 	},
 
 	fadeOut: function( action, duration ) {
@@ -157,6 +163,9 @@ THREE.AnimationMixer.prototype = {
 		keys.push( { time: this.time + duration, value: 0 } );
 		
 		action.weight = new THREE.NumberKeyframeTrack( "weight", keys );
+
+		return this;
+
 	},
 
 	fadeIn: function( action, duration ) {
@@ -168,6 +177,8 @@ THREE.AnimationMixer.prototype = {
 		
 		action.weight = new THREE.NumberKeyframeTrack( "weight", keys );
 
+		return this;
+
 	},
 
 	warp: function( action, startTimeScale, endTimeScale, duration ) {
@@ -179,6 +190,8 @@ THREE.AnimationMixer.prototype = {
 		
 		action.timeScale = new THREE.NumberKeyframeTrack( "timeScale", keys );
 
+		return this;
+
 	},
 
 	crossFade: function( fadeOutAction, fadeInAction, duration, warp ) {
@@ -195,6 +208,8 @@ THREE.AnimationMixer.prototype = {
 			this.warp( fadeInAction, endStartRatio, 1.0, duration );
 
 		}
+
+		return this;
 		
 	},
 
@@ -232,6 +247,9 @@ THREE.AnimationMixer.prototype = {
 			this.propertyBindings[ i ].apply();
 
 		}
+
+		return this;
+		
 	}
 
 };

+ 38 - 6
src/animation/KeyframeTrack.js

@@ -73,26 +73,47 @@ THREE.KeyframeTrack.prototype = {
 	// move all keyframes either forwards or backwards in time
 	shift: function( timeOffset ) {
 
-		for( var i = 1; i < this.keys.length; i ++ ) {
-			this.keys[i].time += timeOffset;
+		if( timeOffset !== 0.0 ) {
+
+			for( var i = 1; i < this.keys.length; i ++ ) {
+				this.keys[i].time += timeOffset;
+			}
+
 		}
 
+		return this;
+
 	},
 
-	// removes keyframes before and after animation without changing any values within the range [0,duration].
+	// scale all keyframe times by a factor (useful for frame <-> seconds conversions)
+	scale: function( timeScale ) {
+
+		if( timeTime !== 1.0 ) {
+
+			for( var i = 1; i < this.keys.length; i ++ ) {
+				this.keys[i].time *= timeScale;
+			}
+
+		}
+
+		return this;
+
+	},
+
+	// removes keyframes before and after animation without changing any values within the range [startTime, endTime].
 	// IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
- 	trim: function( duration ) {
+ 	trim: function( startTime, endTime ) {
 		
 		var firstKeysToRemove = 0;
 		for( var i = 1; i < this.keys.length; i ++ ) {
-			if( this.keys[i] <= 0 ) {
+			if( this.keys[i] <= startTime ) {
 				firstKeysToRemove ++;
 			}
 		}
  
 		var lastKeysToRemove = 0;
 		for( var i = this.keys.length - 2; i > 0; i ++ ) {
-			if( this.keys[i] >= duration ) {
+			if( this.keys[i] >= endTime ) {
 				lastKeysToRemove ++;
 			}
 			else {
@@ -104,6 +125,9 @@ THREE.KeyframeTrack.prototype = {
 		if( ( firstKeysToRemove + lastKeysToRemove ) > 0 ) {
 			this.keys = this.keys.splice( firstKeysToRemove, this.keys.length - lastKeysToRemove - firstKeysToRemove );;
 		}
+
+		return this;
+
 	},	
 
 	// sort in ascending order
@@ -116,8 +140,12 @@ THREE.KeyframeTrack.prototype = {
 		return function() {
 
 			this.keys.sort( keyComparator );
+
+			return this;
+
 		}
 
+
 	}(),
 
 	// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
@@ -159,6 +187,8 @@ THREE.KeyframeTrack.prototype = {
 
 		}
 
+		return this;
+
 	},
 
 	// currently only removes equivalent sequential keys (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0), which are common in morph target animations
@@ -199,6 +229,8 @@ THREE.KeyframeTrack.prototype = {
 
 		this.keys = newKeys;
 
+		return this;
+
 	}
 
 };

+ 5 - 3
src/loaders/ObjectLoader.js

@@ -631,21 +631,23 @@ THREE.ObjectLoader.prototype = {
 
 				var dataTracks = data.animations.tracks;
 
+				var fpsToSeconds = ( data.animations.fps !== undefined ) ? ( 1.0 / data.animations.fps ) : 1.0;
+
 				if( dataTracks.position ) {
 
-					tracks.push( THREE.VectorKeyframeTrack.parse( object.uuid + '.position', dataTracks.position ) );
+					tracks.push( THREE.VectorKeyframeTrack.parse( object.uuid + '.position', dataTracks.position ).scale( fpsToSeconds ) );
 
 				}
 
 				if( dataTracks.quaternion ) {
 
-					tracks.push( THREE.QuaternionKeyframeTrack.parse( object.uuid + '.quaternion', dataTracks.quaternion ) );
+					tracks.push( THREE.QuaternionKeyframeTrack.parse( object.uuid + '.quaternion', dataTracks.quaternion ).scale( fpsToSeconds ) );
 
 				}
 
 				if( dataTracks.scale ) {
 
-					tracks.push( THREE.VectorKeyframeTrack.parse( object.uuid + '.scale', dataTracks.scale ) );
+					tracks.push( THREE.VectorKeyframeTrack.parse( object.uuid + '.scale', dataTracks.scale ).scale( fpsToSeconds ) );
 
 				}
 			}