Browse Source

improve keyframe track parsing to be more flexible.

Ben Houston 10 years ago
parent
commit
84a942334b

+ 31 - 23
src/animation/KeyframeTrack.js

@@ -8,6 +8,7 @@
 
 
 THREE.KeyframeTrack = function ( name, keys ) {
 THREE.KeyframeTrack = function ( name, keys ) {
 
 
+	if( name === undefined ) throw new Error( "track name is undefined" );
 	if( keys === undefined || keys.length === 0 ) throw new Error( "no keys in track named " + name );
 	if( keys === undefined || keys.length === 0 ) throw new Error( "no keys in track named " + name );
 
 
 	this.name = name;
 	this.name = name;
@@ -16,7 +17,6 @@ THREE.KeyframeTrack = function ( name, keys ) {
 	// the index of the last result, used as a starting point for local search.
 	// the index of the last result, used as a starting point for local search.
 	this.lastIndex = 0;
 	this.lastIndex = 0;
 
 
-	this.sort();
 	this.validate();
 	this.validate();
 	this.optimize();
 	this.optimize();
 };
 };
@@ -133,7 +133,8 @@ THREE.KeyframeTrack.prototype = {
 
 
 	},	
 	},	
 
 
-	// sort in ascending order
+	/* NOTE: This is commented out because we really shouldn't have to handle unsorted key lists
+	         Tracks with out of order keys should be considered to be invalid.  - bhouston
 	sort: function() {
 	sort: function() {
 
 
 		function keyComparator(key0, key1) {
 		function keyComparator(key0, key1) {
@@ -148,8 +149,7 @@ THREE.KeyframeTrack.prototype = {
 
 
 		}
 		}
 
 
-
-	}(),
+	}(),*/
 
 
 	// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
 	// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
 	// One could eventually ensure that all key.values in a track are all of the same type (otherwise interpolation makes no sense.)
 	// One could eventually ensure that all key.values in a track are all of the same type (otherwise interpolation makes no sense.)
@@ -238,33 +238,41 @@ THREE.KeyframeTrack.prototype = {
 
 
 };
 };
 
 
+THREE.KeyframeTrack.parse( json ) {
 
 
-THREE.KeyframeTrack.GetTrackTypeForValue = function( value ) {
-	switch( typeof value ) {
-	 	case "object": {
-
-			if( value.lerp ) {
+	if( json.type === undefined ) throw new Error( "track type undefined, can not parse" );
 
 
-				return THREE.VectorKeyframeTrack;
+	var trackType = THREE.KeyframeTrack.GetTrackTypeForTypeName( json.type );
 
 
-			}
-			if( value.slerp ) {
+	return trackType.parse( json );
 
 
-				return THREE.QuaternionKeyframeTrack;
+};
 
 
-			}
-			break;
-		}
-	 	case "number": {
+THREE.KeyframeTrack.GetTrackTypeForTypeName = function( typeName ) {
+	switch( typeName.toLower() ) {
+	 	case "vector":
+	 	case "vector2":
+	 	case "vector3":
+	 	case "vector4":
+			return THREE.VectorKeyframeTrack;
+
+	 	case "quaternion":
+			return THREE.QuaternionKeyframeTrack;
+
+	 	case "integer":
+	 	case "scalar":
+	 	case "double":
+	 	case "float":
+	 	case "number":
 			return THREE.NumberKeyframeTrack;
 			return THREE.NumberKeyframeTrack;
-	 	}	
-	 	case "boolean": {
+
+	 	case "bool":
+	 	case "boolean":
 			return THREE.BooleanKeyframeTrack;
 			return THREE.BooleanKeyframeTrack;
-	 	}
-	 	case "string": {
+
+	 	case "string":
 	 		return THREE.StringKeyframeTrack;
 	 		return THREE.StringKeyframeTrack;
-	 	}
 	};
 	};
 
 
-	throw new Error( "Unsupported value type" );
+	throw new Error( "Unsupported typeName: " + typeName );
 };
 };

+ 2 - 2
src/animation/tracks/BooleanKeyframeTrack.js

@@ -56,9 +56,9 @@ THREE.BooleanKeyframeTrack.prototype.clone = function() {
 
 
 };
 };
 
 
-THREE.BooleanKeyframeTrack.parse = function( name, jsonKeys ) {
+THREE.BooleanKeyframeTrack.parse = function( json ) {
 
 
-	return new THREE.BooleanKeyframeTrack( name, jsonKeys );
+	return new THREE.BooleanKeyframeTrack( json.name, json.keys );
 
 
 };
 };
  
  

+ 6 - 7
src/animation/tracks/ColorKeyframeTrack.js

@@ -56,20 +56,19 @@ THREE.ColorKeyframeTrack.prototype.clone = function() {
 
 
 };
 };
 
 
-THREE.ColorKeyframeTrack.parse = function( name, jsonKeys ) {
+THREE.ColorKeyframeTrack.parse = function( json ) {
 
 
 	var keys = [];
 	var keys = [];
 
 
-	for( var i = 0; i < jsonKeys.length; i ++ ) {
-		var jsonKey = jsonKeys[i];
-		var key = {
+	for( var i = 0; i < json.keys.length; i ++ ) {
+		var jsonKey = json.keys[i];
+		keys.push( {
 			value: new THREE.Color().fromArray( jsonKey.value ),
 			value: new THREE.Color().fromArray( jsonKey.value ),
 			time: jsonKey.time
 			time: jsonKey.time
-		};
-		keys.push( key );
+		} );
 	}
 	}
 
 
-	return new THREE.ColorKeyframeTrack( name, keys );
+	return new THREE.ColorKeyframeTrack( json.name, keys );
 
 
 };
 };
  
  

+ 2 - 2
src/animation/tracks/NumberKeyframeTrack.js

@@ -56,9 +56,9 @@ THREE.NumberKeyframeTrack.prototype.clone = function() {
 
 
 };
 };
 
 
-THREE.NumberKeyframeTrack.parse = function( name, jsonKeys ) {
+THREE.NumberKeyframeTrack.parse = function( json ) {
 
 
-	return new THREE.NumberKeyframeTrack( name, jsonKeys );
+	return new THREE.NumberKeyframeTrack( json.name, json.keys );
 
 
 };
 };
  
  

+ 6 - 7
src/animation/tracks/QuaternionKeyframeTrack.js

@@ -68,20 +68,19 @@ THREE.QuaternionKeyframeTrack.prototype.clone = function() {
 
 
 };
 };
 
 
-THREE.QuaternionKeyframeTrack.parse = function( name, jsonKeys ) {
+THREE.QuaternionKeyframeTrack.parse = function( json ) {
 
 
 	var keys = [];
 	var keys = [];
 
 
-	for( var i = 0; i < jsonKeys.length; i ++ ) {
-		var jsonKey = jsonKeys[i];
-		var key = {
+	for( var i = 0; i < json.keys.length; i ++ ) {
+		var jsonKey = json.keys[i];
+		keys.push( {
 			value: new THREE.Quaternion().fromArray( jsonKey.value ),
 			value: new THREE.Quaternion().fromArray( jsonKey.value ),
 			time: jsonKey.time
 			time: jsonKey.time
-		};
-		keys.push( key );
+		} );
 	}
 	}
 
 
-	return new THREE.QuaternionKeyframeTrack( name, keys );
+	return new THREE.QuaternionKeyframeTrack( json.name, keys );
 
 
 };
 };
  
  

+ 2 - 2
src/animation/tracks/StringKeyframeTrack.js

@@ -56,9 +56,9 @@ THREE.StringKeyframeTrack.prototype.clone = function() {
 
 
 };
 };
 
 
-THREE.StringKeyframeTrack.parse = function( name, jsonKeys ) {
+THREE.StringKeyframeTrack.parse = function( json ) {
 
 
-	return new THREE.StringKeyframeTrack( name, jsonKeys );
+	return new THREE.StringKeyframeTrack( json.name, json.keys );
 
 
 };
 };
  
  

+ 7 - 8
src/animation/tracks/VectorKeyframeTrack.js

@@ -56,23 +56,22 @@ THREE.VectorKeyframeTrack.prototype.clone = function() {
 
 
 };
 };
 
 
-THREE.VectorKeyframeTrack.parse = function( name, jsonKeys ) {
+THREE.VectorKeyframeTrack.parse = function( json ) {
 
 
-	var elementCount = jsonKeys[0].value.length;
+	var elementCount = json.keys[0].value.length;
 	var valueType = THREE[ 'Vector' + elementCount ];
 	var valueType = THREE[ 'Vector' + elementCount ];
 
 
 	var keys = [];
 	var keys = [];
 
 
-	for( var i = 0; i < jsonKeys.length; i ++ ) {
-		var jsonKey = jsonKeys[i];
-		var key = {
+	for( var i = 0; i < json.keys.length; i ++ ) {
+		var jsonKey = json.keys[i];
+		keys.push( {
 			value: new valueType().fromArray( jsonKey.value ),
 			value: new valueType().fromArray( jsonKey.value ),
 			time: jsonKey.time
 			time: jsonKey.time
-		};
-		keys.push( key );
+		} );
 	}
 	}
 
 
-	return new THREE.VectorKeyframeTrack( name, keys );
+	return new THREE.VectorKeyframeTrack( json.keys, keys );
 
 
 };
 };
  
  

+ 5 - 18
src/loaders/ObjectLoader.js

@@ -633,28 +633,15 @@ THREE.ObjectLoader.prototype = {
 
 
 				var fpsToSeconds = ( data.animations.fps !== undefined ) ? ( 1.0 / data.animations.fps ) : 1.0;
 				var fpsToSeconds = ( data.animations.fps !== undefined ) ? ( 1.0 / data.animations.fps ) : 1.0;
 
 
-				if( dataTracks.position ) {
+				for( var i = 0; i < data.animations.tracks.length; i ++ ) {
 
 
-					tracks.push( THREE.VectorKeyframeTrack.parse( object.uuid + '.position', dataTracks.position ).scale( fpsToSeconds ) );
-
-				}
-
-				if( dataTracks.quaternion ) {
-
-					var trackQuaternion = THREE.QuaternionKeyframeTrack.parse( object.uuid + '.quaternion', dataTracks.quaternion ).scale( fpsToSeconds );
-				
-					//trackQuaternion.multiply( trackQuaternion.keys[0].value.clone().inverse() );
-					//trackQuaternion.multiply( object.quaternion );
+					var track = THREE.KeyframeTrack.parse( data.animations.tracks[i] );
+					track.name = object.uuid + '.' + track.name;
+					track.scale( fpsToSeconds );
+					tracks.push( track );
 					
 					
-					tracks.push( trackQuaternion );
-
 				}
 				}
 
 
-				if( dataTracks.scale ) {
-
-					tracks.push( THREE.VectorKeyframeTrack.parse( object.uuid + '.scale', dataTracks.scale ).scale( fpsToSeconds ) );
-
-				}
 			}
 			}
 
 
 			return object;
 			return object;