Browse Source

complete creation of type-specific KeyframeTrack classes.

Ben Houston 10 years ago
parent
commit
73a55e948a

+ 3 - 3
src/animation/AnimationMixer.js

@@ -156,7 +156,7 @@ THREE.AnimationMixer.prototype = {
 		keys.push( { time: this.time, value: 1 } );
 		keys.push( { time: this.time, value: 1 } );
 		keys.push( { time: this.time + duration, value: 0 } );
 		keys.push( { time: this.time + duration, value: 0 } );
 		
 		
-		action.weight = new THREE.KeyframeTrack( "weight", keys );
+		action.weight = new THREE.NumberKeyframeTrack( "weight", keys );
 	},
 	},
 
 
 	fadeIn: function( action, duration ) {
 	fadeIn: function( action, duration ) {
@@ -166,7 +166,7 @@ THREE.AnimationMixer.prototype = {
 		keys.push( { time: this.time, value: 0 } );
 		keys.push( { time: this.time, value: 0 } );
 		keys.push( { time: this.time + duration, value: 1 } );
 		keys.push( { time: this.time + duration, value: 1 } );
 		
 		
-		action.weight = new THREE.KeyframeTrack( "weight", keys );
+		action.weight = new THREE.NumberKeyframeTrack( "weight", keys );
 
 
 	},
 	},
 
 
@@ -177,7 +177,7 @@ THREE.AnimationMixer.prototype = {
 		keys.push( { time: this.time, value: startTimeScale } );
 		keys.push( { time: this.time, value: startTimeScale } );
 		keys.push( { time: this.time + duration, value: endTimeScale } );
 		keys.push( { time: this.time + duration, value: endTimeScale } );
 		
 		
-		action.timeScale = new THREE.KeyframeTrack( "timeScale", keys );
+		action.timeScale = new THREE.NumberKeyframeTrack( "timeScale", keys );
 
 
 	},
 	},
 
 

+ 23 - 28
src/animation/KeyframeTrack.js

@@ -64,43 +64,38 @@ THREE.KeyframeTrack.prototype = {
 		// linear interpolation to start with
 		// linear interpolation to start with
 		var currentKey = this.keys[ this.lastIndex ];
 		var currentKey = this.keys[ this.lastIndex ];
 		var alpha = ( time - prevKey.time ) / ( currentKey.time - prevKey.time );
 		var alpha = ( time - prevKey.time ) / ( currentKey.time - prevKey.time );
-		this.result = this.lerp( this.result, currentKey.value, alpha );
+		this.result = this.lerpValues( this.result, currentKey.value, alpha );
 
 
 		return this.result;
 		return this.result;
 
 
 	},
 	},
 
 
-	// required implementations:
-	setResult: function( value ) {
-
-		if( this.result.copy ) {
-
-			this.setResult = function( value ) {
-				this.result.copy( value );
+	// removes keyframes before and after animation without changing any values within the range [0,duration].
+	// 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 ) {
+		
+		var firstKeysToRemove = 0;
+		for( var i = 1; i < this.keys.length; i ++ ) {
+			if( this.keys[i] <= 0 ) {
+				firstKeysToRemove ++;
 			}
 			}
-
 		}
 		}
-		else {
-
-			this.setResult = function( value ) {
-				this.result = value;
+ 
+		var lastKeysToRemove = 0;
+		for( var i = this.keys.length - 2; i > 0; i ++ ) {
+			if( this.keys[i] >= duration ) {
+				lastKeysToRemove ++;
+			}
+			else {
+				break;
 			}
 			}
-
 		}
 		}
-
-		this.setResult( value );
-
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerp: function( value0, value1, alpha ) {
-
-		this.lerp = THREE.AnimationUtils.getLerpFunc( value0, false );
-
-		return this.lerp( value0, value1, alpha );
-
-	},
+ 
+		// remove last keys first because it doesn't affect the position of the first keys (the otherway around doesn't work as easily)
+		if( ( firstKeysToRemove + lastKeysToRemove ) > 0 ) {
+			this.keys = this.keys.splice( firstKeysToRemove, this.keys.length - lastKeysToRemove - firstKeysToRemove );;
+		}
+	},	
 
 
 	// sort in ascending order
 	// sort in ascending order
 	sort: function() {
 	sort: function() {

+ 13 - 15
src/animation/tracks/BooleanKeyframeTrack.js

@@ -15,28 +15,26 @@ THREE.BooleanKeyframeTrack = function ( name, keys ) {
 
 
 };
 };
 
 
-THREE.BooleanKeyframeTrack.prototype = {
+THREE.BooleanKeyframeTrack.prototype = Object.create( THREE.KeyframeTrack.prototype );
 
 
-	constructor: THREE.BooleanKeyframeTrack,
+THREE.BooleanKeyframeTrack.prototype.constructor = THREE.BooleanKeyframeTrack;
 
 
-	setResult: function( value ) {
+THREE.BooleanKeyframeTrack.prototype.setResult = function( value ) {
 
 
-		this.result = value;
+	this.result = value;
 
 
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerpValues: function( value0, value1, alpha ) {
+};
 
 
-		return ( alpha < 1.0 ) ? value0 : value1;
+// memoization of the lerp function for speed.
+// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
+THREE.BooleanKeyframeTrack.prototype.lerpValues = function( value0, value1, alpha ) {
 
 
-	},
+	return ( alpha < 1.0 ) ? value0 : value1;
 
 
-	compareValues: function( value0, value1 ) {
+};
 
 
-		return ( value0 === value1 );
+THREE.BooleanKeyframeTrack.prototype.compareValues = function( value0, value1 ) {
 
 
-	}
+	return ( value0 === value1 );
 
 
-};
+};

+ 13 - 15
src/animation/tracks/ColorKeyframeTrack.js

@@ -15,28 +15,26 @@ THREE.ColorKeyframeTrack = function ( name, keys ) {
 
 
 };
 };
 
 
-THREE.ColorKeyframeTrack.prototype = {
+THREE.ColorKeyframeTrack.prototype = Object.create( THREE.KeyframeTrack.prototype );
 
 
-	constructor: THREE.ColorKeyframeTrack,
+THREE.ColorKeyframeTrack.prototype.constructor = THREE.ColorKeyframeTrack;
 
 
-	setResult: function( value ) {
+THREE.ColorKeyframeTrack.prototype.setResult = function( value ) {
 
 
-		this.result.copy( value );
+	this.result.copy( value );
 
 
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerpValues: function( value0, value1, alpha ) {
+};
 
 
-		return value0.slerp( value1, alpha );
+// memoization of the lerp function for speed.
+// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
+THREE.ColorKeyframeTrack.prototype.lerpValues = function( value0, value1, alpha ) {
 
 
-	},
+	return value0.lerp( value1, alpha );
 
 
-	compareValues: function( value0, value1 ) {
+};
 
 
-		return value0.equals( value1 );
+THREE.ColorKeyframeTrack.prototype.compareValues = function( value0, value1 ) {
 
 
-	}
+	return value0.equals( value1 );
 
 
-};
+};

+ 13 - 15
src/animation/tracks/NumberKeyframeTrack.js

@@ -15,28 +15,26 @@ THREE.NumberKeyframeTrack = function ( name, keys ) {
 
 
 };
 };
 
 
-THREE.NumberKeyframeTrack.prototype = {
+THREE.NumberKeyframeTrack.prototype = Object.create( THREE.KeyframeTrack.prototype );
 
 
-	constructor: THREE.NumberKeyframeTrack,
+THREE.NumberKeyframeTrack.prototype.constructor = THREE.NumberKeyframeTrack;
 
 
-	setResult: function( value ) {
+THREE.NumberKeyframeTrack.prototype.setResult = function( value ) {
 
 
-		this.result = value;
+	this.result = value;
 
 
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerpValues: function( value0, value1, alpha ) {
+};
 
 
-		return value0 * ( 1 - alpha ) + value1 * alpha;
+// memoization of the lerp function for speed.
+// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
+THREE.NumberKeyframeTrack.prototype.lerpValues = function( value0, value1, alpha ) {
 
 
-	},
+	return value0 * ( 1 - alpha ) + value1 * alpha;
 
 
-	compareValues: function( value0, value1 ) {
+};
 
 
-		return ( value0 === value1 );
+THREE.NumberKeyframeTrack.prototype.compareValues = function( value0, value1 ) {
 
 
-	}
+	return ( value0 === value1 );
 
 
-};
+};

+ 13 - 15
src/animation/tracks/QuaternionKeyframeTrack.js

@@ -14,29 +14,27 @@ THREE.QuaternionKeyframeTrack = function ( name, keys ) {
 	this.result = this.keys[0].value.clone();
 	this.result = this.keys[0].value.clone();
 
 
 };
 };
+ 
+THREE.QuaternionKeyframeTrack.prototype = Object.create( THREE.KeyframeTrack.prototype );
 
 
-THREE.QuaternionKeyframeTrack.prototype = {
+THREE.QuaternionKeyframeTrack.prototype.constructor = THREE.QuaternionKeyframeTrack;
 
 
-	constructor: THREE.QuaternionKeyframeTrack,
+THREE.QuaternionKeyframeTrack.prototype.setResult = function( value ) {
 
 
-	setResult: function( value ) {
+	this.result.copy( value );
 
 
-		this.result.copy( value );
-
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerpValues: function( value0, value1, alpha ) {
+};
 
 
-		return value0.lerp( value1, alpha );
+// memoization of the lerp function for speed.
+// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
+THREE.QuaternionKeyframeTrack.prototype.lerpValues = function( value0, value1, alpha ) {
 
 
-	},
+	return value0.slerp( value1, alpha );
 
 
-	compareValues: function( value0, value1 ) {
+};
 
 
-		return value0.equals( value1 );
+THREE.QuaternionKeyframeTrack.prototype.compareValues = function( value0, value1 ) {
 
 
-	}
+	return value0.equals( value1 );
 
 
 };
 };

+ 13 - 15
src/animation/tracks/StringKeyframeTrack.js

@@ -15,28 +15,26 @@ THREE.StringKeyframeTrack = function ( name, keys ) {
 
 
 };
 };
 
 
-THREE.StringKeyframeTrack.prototype = {
+THREE.StringKeyframeTrack.prototype = Object.create( THREE.KeyframeTrack.prototype );
 
 
-	constructor: THREE.StringKeyframeTrack,
+THREE.StringKeyframeTrack.prototype.constructor = THREE.StringKeyframeTrack;
 
 
-	setResult: function( value ) {
+THREE.StringKeyframeTrack.prototype.setResult = function( value ) {
 
 
-		this.result = value;
+	this.result = value;
 
 
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerpValues: function( value0, value1, alpha ) {
+};
 
 
-		return ( alpha < 1.0 ) ? value0 : value1;
+// memoization of the lerp function for speed.
+// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
+THREE.StringKeyframeTrack.prototype.lerpValues = function( value0, value1, alpha ) {
 
 
-	},
+	return ( alpha < 1.0 ) ? value0 : value1;
 
 
-	compareValues: function( value0, value1 ) {
+};
 
 
-		return ( value0 === value1 );
+THREE.StringKeyframeTrack.prototype.compareValues = function( value0, value1 ) {
 
 
-	}
+	return ( value0 === value1 );
 
 
-};
+};

+ 13 - 15
src/animation/tracks/VectorKeyframeTrack.js

@@ -15,28 +15,26 @@ THREE.VectorKeyframeTrack = function ( name, keys ) {
 
 
 };
 };
 
 
-THREE.VectorKeyframeTrack.prototype = {
+THREE.VectorKeyframeTrack.prototype = Object.create( THREE.KeyframeTrack.prototype );
 
 
-	constructor: THREE.VectorKeyframeTrack,
+THREE.VectorKeyframeTrack.prototype.constructor = THREE.VectorKeyframeTrack;
 
 
-	setResult: function( value ) {
+THREE.VectorKeyframeTrack.prototype.setResult = function( value ) {
 
 
-		this.result.copy( value );
+	this.result.copy( value );
 
 
-	},
-
-	// memoization of the lerp function for speed.
-	// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
-	lerpValues: function( value0, value1, alpha ) {
+};
 
 
-		return value0.slerp( value1, alpha );
+// memoization of the lerp function for speed.
+// NOTE: Do not optimize as a prototype initialization closure, as value0 will be different on a per class basis.
+THREE.VectorKeyframeTrack.prototype.lerpValues = function( value0, value1, alpha ) {
 
 
-	},
+	return value0.lerp( value1, alpha );
 
 
-	compareValues: function( value0, value1 ) {
+};
 
 
-		return value0.equals( value1 );
+THREE.VectorKeyframeTrack.prototype.compareValues = function( value0, value1 ) {
 
 
-	}
+	return value0.equals( value1 );
 
 
-};
+};

+ 5 - 5
utils/build/includes/common.json

@@ -40,11 +40,11 @@
 	"src/animation/AnimationUtils.js",
 	"src/animation/AnimationUtils.js",
 	"src/animation/KeyframeTrack.js",
 	"src/animation/KeyframeTrack.js",
 	"src/animation/PropertyBinding.js",
 	"src/animation/PropertyBinding.js",
-	"src/animation/track/VectorKeyframeTrack.js",
-	"src/animation/track/QuaternionKeyframeTrack.js",
-	"src/animation/track/StringKeyframeTrack.js",
-	"src/animation/track/BooleanKeyframeTrack.js",
-	"src/animation/track/NumberKeyframeTrack.js",
+	"src/animation/tracks/VectorKeyframeTrack.js",
+	"src/animation/tracks/QuaternionKeyframeTrack.js",
+	"src/animation/tracks/StringKeyframeTrack.js",
+	"src/animation/tracks/BooleanKeyframeTrack.js",
+	"src/animation/tracks/NumberKeyframeTrack.js",
 	"src/cameras/Camera.js",
 	"src/cameras/Camera.js",
 	"src/cameras/CubeCamera.js",
 	"src/cameras/CubeCamera.js",
 	"src/cameras/OrthographicCamera.js",
 	"src/cameras/OrthographicCamera.js",