Browse Source

implement some of the TODOs. add lots of verbose debug info.

Ben Houston 10 years ago
parent
commit
0962f9044f

+ 12 - 11
src/animation/Action.js

@@ -10,6 +10,7 @@ THREE.Action = function ( clip, startTime, timeScale, weight, loop ) {
 	this.timeScale = timeScale || 1;
 	this.timeScale = timeScale || 1;
 	this.weight = weight || 1;
 	this.weight = weight || 1;
 	this.loop = loop || false;
 	this.loop = loop || false;
+	this.enabled = true;	// allow for easy disabling of the action.
 
 
 	this.cache = {}; // track name, track, last evaluated time, last evaluated value (with weight included), keyIndex.
 	this.cache = {}; // track name, track, last evaluated time, last evaluated value (with weight included), keyIndex.
 };
 };
@@ -20,6 +21,8 @@ THREE.Action.prototype = {
 
 
 	toClipTime: function( time ) {
 	toClipTime: function( time ) {
 
 
+		console.log( 'Action[' + this.clip.name + '].toClipTime( ' + time + ' )' );
+
 		var clipTime = time - this.startTime;
 		var clipTime = time - this.startTime;
 
 
 		clipTime *= this.timeScale;
 		clipTime *= this.timeScale;
@@ -42,25 +45,23 @@ THREE.Action.prototype = {
 
 
 	   	}
 	   	}
 
 
+		console.log( '   clipTime: ' + clipTime );
+
    		return clipTime;
    		return clipTime;
-	}
+	},
 
 
 	getAt: function( time ) {
 	getAt: function( time ) {
 
 
-		var clipTime = this.toClipTime( time );
+		console.log( 'Action[' + this.clip.name + '].getAt( ' + time + ' )' );
 
 
-		var results = {};
-
-		foreach( var name in clip.tracks ) {
-
-			var track = clip.tracks[name];
+		var clipTime = this.toClipTime( time );
 
 
-			var value = track.getAt( time );
+		var clipResults = this.clip.getAt( clipTime );
 
 
-			results[name] = value;
-		}
+		console.log( "  clipResults: ", clipResults );
 
 
-		return results;
+		return clipResults;
+		
 	}
 	}
 
 
 };
 };

+ 16 - 3
src/animation/AnimationMath.js → src/animation/AnimationUtils.js

@@ -29,8 +29,11 @@
 	// TODO: Cache this at some point
 	// TODO: Cache this at some point
 	findNode: function( root, nodeName ) {
 	findNode: function( root, nodeName ) {
 
 
+		console.log( 'AnimationUtils.findNode( ' + root.name + ', nodeName: ' + nodeName + ')');
+		
 		if( ! nodeName || nodeName === "" || nodeName === "root" || nodeName === "." || nodeName === -1 ) {
 		if( ! nodeName || nodeName === "" || nodeName === "root" || nodeName === "." || nodeName === -1 ) {
 
 
+			console.log( '  root.' );
 			return root;
 			return root;
 
 
 		}
 		}
@@ -57,7 +60,12 @@
 
 
 			var boneNode = searchSkeleton( root.skeleton );
 			var boneNode = searchSkeleton( root.skeleton );
 
 
-			if( boneNode ) return boneNode;
+			if( boneNode ) {
+
+				console.log( '  bone: ' + bone.name + '.' );
+				return boneNode;
+
+			}
 		}
 		}
 
 
 		// (3) search into node subtree.
 		// (3) search into node subtree.
@@ -87,11 +95,16 @@
 
 
 			var subTreeNode = searchNodeSubtree( root.children );
 			var subTreeNode = searchNodeSubtree( root.children );
 
 
-			if( subTreeNode ) return subTreeNode;
+			if( subTreeNode ) {
+
+				console.log( '  node: ' + subTreeNode.name + '.' );
+				return subTreeNode;
+
+			}
 
 
 		}
 		}
 
 
-		console.log( "no node found for name: " + name );
+		console.log( "   <null>.  No node found for name: " + name );
 
 
 		return null;
 		return null;
 	}
 	}

+ 18 - 0
src/animation/Clip.js

@@ -1,4 +1,7 @@
 /**
 /**
+ *
+ * Reusable set of Tracks that represent an animation.
+ * 
  * @author Ben Houston / http://clara.io/
  * @author Ben Houston / http://clara.io/
  * @author David Sarno / http://lighthaus.us/
  * @author David Sarno / http://lighthaus.us/
  */
  */
@@ -15,6 +18,21 @@ THREE.Clip.prototype = {
 
 
 	constructor: THREE.Clip,
 	constructor: THREE.Clip,
 
 
+	getAt: function( clipTime ) {
+
+		var results = [];
+
+		for( var i = 0; i < this.tracks.length; i ++ ) {
+
+			var track = this.tracks[i];
+
+			results.push( { name: track.name, value: this.getAt( time ) } );
+
+		}
+
+		return results;
+	},
+
 	importFromData: function( data ) {
 	importFromData: function( data ) {
 
 
 		init: function ( data ) {
 		init: function ( data ) {

+ 16 - 1
src/animation/KeyframeTrack.js

@@ -16,12 +16,14 @@ THREE.KeyframeTrack.prototype = {
 	constructor: THREE.Track,
 	constructor: THREE.Track,
 
 
 	getAt: function( time ) {
 	getAt: function( time ) {
+		console.log( 'KeyframeTrack[' + this.name + '].getAt( ' + time + ' )' );
 
 
 		if( this.keys.length == 0 ) throw new Error( "no keys in track named " + this.name );
 		if( this.keys.length == 0 ) throw new Error( "no keys in track named " + this.name );
 		
 		
 		// before the start of the track, return the first key value
 		// before the start of the track, return the first key value
 		if( this.keys[0].time >= time ) {
 		if( this.keys[0].time >= time ) {
 
 
+			console.log( '   before: ' + this.keys[0].value );
 			return this.keys[0].value;
 			return this.keys[0].value;
 
 
 		}
 		}
@@ -29,6 +31,7 @@ THREE.KeyframeTrack.prototype = {
 		// past the end of the track, return the last key value
 		// past the end of the track, return the last key value
 		if( this.keys[ this.keys.length - 1 ].time <= time ) {
 		if( this.keys[ this.keys.length - 1 ].time <= time ) {
 
 
+			console.log( '   after: ' + this.keys[ this.keys.length - 1 ].value );
 			return this.keys[ this.keys.length - 1 ].value;
 			return this.keys[ this.keys.length - 1 ].value;
 
 
 		}
 		}
@@ -41,7 +44,19 @@ THREE.KeyframeTrack.prototype = {
 				// linear interpolation to start with
 				// linear interpolation to start with
 				var alpha = ( time - this.keys[ i - 1 ].time ) / ( this.keys[ i ].time - this.keys[ i - 1 ].time );
 				var alpha = ( time - this.keys[ i - 1 ].time ) / ( this.keys[ i ].time - this.keys[ i - 1 ].time );
 
 
-				return THREE.AnimationUtils.lerp( this.keys[ i - 1 ].value, this.keys[ i ].value, alpha );
+
+				var interpolatedValue = THREE.AnimationUtils.lerp( this.keys[ i - 1 ].value, this.keys[ i ].value, alpha );
+
+				console.log( '   interpolated: ', {
+					value: interpolatedValue, 
+					alpha: alpha,
+					time0: this.keys[ i - 1 ].time,
+					time1: this.keys[ i ].time,
+					value0: this.keys[ i - 1 ].value,
+					value1: this.keys[ i ].value
+				} );
+
+				return interpolatedValue;
 
 
 			}
 			}
 		}
 		}

+ 58 - 13
src/animation/Mixer.js

@@ -16,6 +16,7 @@ THREE.Mixer.prototype = {
 	constructor: THREE.Mixer,
 	constructor: THREE.Mixer,
 
 
 	addAction: function( action ) {
 	addAction: function( action ) {
+		console.log( root.name + ".Mixer.addAction( " + action.name + " )" );
 
 
 		this.actions.push( action );
 		this.actions.push( action );
 
 
@@ -30,7 +31,20 @@ THREE.Mixer.prototype = {
 
 
 	},
 	},
 
 
+	removeAction: function( action ) {
+		console.log( root.name + ".Mixer.addRemove( " + action.name + " )" );
+
+		var index = this.actions.indexOf( action );
+
+		if ( index !== - 1 ) {
+
+			this.actions.splice( index, 1 );
+
+		}
+	},
+
 	update: function( time ) {
 	update: function( time ) {
+		console.log( root.name + ".Mixer.update( " + time + " )" );
 
 
 		var mixerResults = {};
 		var mixerResults = {};
 
 
@@ -38,40 +52,71 @@ THREE.Mixer.prototype = {
 
 
 			var action = this.actions[i];
 			var action = this.actions[i];
 
 
-			var actionResults = action.update( time );
+			if( action.weight <= 0 || ! action.enabled ) continue;
+
+			var actionResults = action.getAt( time );
 
 
-			foreach( var result in actionResults ) {
+			for( var j = 0; j < actionResults.length; j ++ ) {
+
+				var actionResult = actionResults[j];
 
 
 				// TODO: do iterative linear interpolator based on cumulative weight ratios
 				// TODO: do iterative linear interpolator based on cumulative weight ratios
-				mixerResults[result.name].value = result.value;
+				if( ! mixerResults[ actionResult.name ] ) {
+
+					mixerResults[actionResult.name] = {
+						name: actionResult.name
+					};
+				}
+
+				mixerResults[actionResult.name].value = result.value;
 
 
 			}
 			}
 
 
 		}
 		}
+	    console.log( "  mixerResults: ", mixerResults );
+	
 
 
 		// apply to nodes
 		// apply to nodes
-		foreach( var mixerResult in mixerResults ) {
+		for ( var name in mixerResults ) {
+
+			var mixerResult = mixerResults[ name ];
+
+			var trackInfo = this.trackInfos[ name ];
 
 
-			var trackInfo = this.trackInfos[mixerResult.name];
+			var node = trackInfo.node;
+
+			if( ! node ) {
+				console.log( "  trying to update node for track: " + name + " but it wasn't found." );
+				continue;
+			}
+
+			var propertyName = trackInfo.propertyName;
+
+			if( ! node[ propertyName ] ) {
+				console.log( "  trying to update property for track: " + name + '.' + propertyName + " but it wasn't found." );				
+				continue;
+			}
 
 
 			// must use copy for Object3D.Euler/Quaternion
 			// must use copy for Object3D.Euler/Quaternion
-			if( trackInfo.node[trackInfo.propertyName].copy ) {
-				trackInfo.node[trackInfo.propertyName].copy( mixerResult.value );
+			if( node[ propertyName ].copy ) {
+				console.log( '  update property ' + name + '.' + propertyName + ' via a set() function.' );				
+				node[ propertyName ].copy( mixerResult.value );
 			}
 			}
 			// otherwise just copy across value
 			// otherwise just copy across value
 			else {
 			else {
-				trackInfo.node[trackInfo.propertyName] = mixerResult.value;	
+				console.log( '  update property ' + name + '.' + propertyName + ' via assignment.' );				
+				node[ propertyName ] = mixerResult.value;	
 			}
 			}
 
 
 
 
 			// trigger node dirty			
 			// trigger node dirty			
-			if( trackInfo.node.needsUpdate ) { // material
-				trackInfo.node.needsUpdate = true;
+			if( node.needsUpdate ) { // material
+				node.needsUpdate = true;
 			}			
 			}			
-			if( trackInfo.node.matrixWorldNeedsUpdate && ! this.matrixAutoUpdate ) { // node transform
-				trackInfo.node.matrixWorldNeedsUpdate = true;
+			if( node.matrixWorldNeedsUpdate && ! this.matrixAutoUpdate ) { // node transform
+				node.matrixWorldNeedsUpdate = true;
 			}
 			}
-			
+
 		}
 		}
 	}
 	}